1
- import { matches , matchesExact } from './matches'
1
+ import { fuzzyMatches , matches } from './matches'
2
2
import { getNodeText } from './get-node-text'
3
3
import { prettyDOM } from './pretty-dom'
4
4
@@ -16,14 +16,20 @@ function firstResultOrNull(queryFunction, ...args) {
16
16
return result [ 0 ]
17
17
}
18
18
19
- function queryAllLabelsByText ( container , text ) {
19
+ function queryAllLabelsByText ( container , text , { exact = true } = { } ) {
20
+ const matcher = exact ? matches : fuzzyMatches
21
+ const COLLAPSE_WHITESPACE = true // a little more fuzzy than other queries
20
22
return Array . from ( container . querySelectorAll ( 'label' ) ) . filter ( label =>
21
- matches ( label . textContent , label , text ) ,
23
+ matcher ( label . textContent , label , text , COLLAPSE_WHITESPACE ) ,
22
24
)
23
25
}
24
26
25
- function queryAllByLabelText ( container , text , { selector = '*' } = { } ) {
26
- const labels = queryAllLabelsByText ( container , text )
27
+ function queryAllByLabelText (
28
+ container ,
29
+ text ,
30
+ { selector = '*' , exact = true } = { } ,
31
+ ) {
32
+ const labels = queryAllLabelsByText ( container , text , { exact} )
27
33
const labelledElements = labels
28
34
. map ( label => {
29
35
/* istanbul ignore if */
@@ -49,29 +55,31 @@ function queryAllByLabelText(container, text, {selector = '*'} = {}) {
49
55
}
50
56
} )
51
57
. filter ( label => label !== null )
52
- . concat ( queryAllByAttribute ( 'aria-label' , container , text ) )
58
+ . concat ( queryAllByAttribute ( 'aria-label' , container , text , { exact } ) )
53
59
54
60
return labelledElements
55
61
}
56
62
57
- function queryByLabelText ( container , text , opts ) {
58
- return firstResultOrNull ( queryAllByLabelText , container , text , opts )
63
+ function queryByLabelText ( ... args ) {
64
+ return firstResultOrNull ( queryAllByLabelText , ... args )
59
65
}
60
66
61
- function queryAllByText ( container , text , { selector = '*' } = { } ) {
67
+ function queryAllByText ( container , text , { selector = '*' , exact = true } = { } ) {
68
+ const matcher = exact ? matches : fuzzyMatches
69
+ const COLLAPSE_WHITESPACE = true // a little more fuzzy than other queries
62
70
return Array . from ( container . querySelectorAll ( selector ) ) . filter ( node =>
63
- matches ( getNodeText ( node ) , node , text ) ,
71
+ matcher ( getNodeText ( node ) , node , text , COLLAPSE_WHITESPACE ) ,
64
72
)
65
73
}
66
74
67
- function queryByText ( container , text , opts ) {
68
- return firstResultOrNull ( queryAllByText , container , text , opts )
75
+ function queryByText ( ... args ) {
76
+ return firstResultOrNull ( queryAllByText , ... args )
69
77
}
70
78
71
79
// this is just a utility and not an exposed query.
72
80
// There are no plans to expose this.
73
- function queryAllByAttribute ( attribute , container , text , { exact = false } = { } ) {
74
- const matcher = exact ? matchesExact : matches
81
+ function queryAllByAttribute ( attribute , container , text , { exact = true } = { } ) {
82
+ const matcher = exact ? matches : fuzzyMatches
75
83
return Array . from ( container . querySelectorAll ( `[${ attribute } ]` ) ) . filter ( node =>
76
84
matcher ( node . getAttribute ( attribute ) , node , text ) ,
77
85
)
@@ -85,19 +93,18 @@ function queryByAttribute(...args) {
85
93
86
94
const queryByPlaceholderText = queryByAttribute . bind ( null , 'placeholder' )
87
95
const queryAllByPlaceholderText = queryAllByAttribute . bind ( null , 'placeholder' )
88
- const queryByTestId = ( ...args ) =>
89
- queryByAttribute ( 'data-testid' , ...args , { exact : true } )
90
- const queryAllByTestId = ( ...args ) =>
91
- queryAllByAttribute ( 'data-testid' , ...args , { exact : true } )
96
+ const queryByTestId = queryByAttribute . bind ( null , 'data-testid' )
97
+ const queryAllByTestId = queryAllByAttribute . bind ( null , 'data-testid' )
92
98
93
- function queryAllByAltText ( container , alt ) {
99
+ function queryAllByAltText ( container , alt , { exact = true } = { } ) {
100
+ const matcher = exact ? matches : fuzzyMatches
94
101
return Array . from ( container . querySelectorAll ( 'img,input,area' ) ) . filter ( node =>
95
- matches ( node . getAttribute ( 'alt' ) , node , alt ) ,
102
+ matcher ( node . getAttribute ( 'alt' ) , node , alt ) ,
96
103
)
97
104
}
98
105
99
- function queryByAltText ( container , alt ) {
100
- return firstResultOrNull ( queryAllByAltText , container , alt )
106
+ function queryByAltText ( ... args ) {
107
+ return firstResultOrNull ( queryAllByAltText , ... args )
101
108
}
102
109
103
110
// getters
@@ -140,7 +147,7 @@ function getByPlaceholderText(...args) {
140
147
function getAllByLabelText ( container , text , ...rest ) {
141
148
const els = queryAllByLabelText ( container , text , ...rest )
142
149
if ( ! els . length ) {
143
- const labels = queryAllLabelsByText ( container , text )
150
+ const labels = queryAllLabelsByText ( container , text , ... rest )
144
151
if ( labels . length ) {
145
152
throw new Error (
146
153
`Found a label with the text of: ${ text } , however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly. \n\n${ debugDOM (
@@ -178,8 +185,8 @@ function getByText(...args) {
178
185
return firstResultOrNull ( getAllByText , ...args )
179
186
}
180
187
181
- function getAllByAltText ( container , alt ) {
182
- const els = queryAllByAltText ( container , alt )
188
+ function getAllByAltText ( container , alt , ... rest ) {
189
+ const els = queryAllByAltText ( container , alt , ... rest )
183
190
if ( ! els . length ) {
184
191
throw new Error (
185
192
`Unable to find an element with the alt text: ${ alt } \n\n${ debugDOM (
0 commit comments