@@ -93,8 +93,8 @@ test('get can get form controls by placeholder', () => {
93
93
94
94
test ( 'label with no form control' , ( ) => {
95
95
const { getByLabelText, queryByLabelText} = render ( `<label>All alone</label>` )
96
- expect ( queryByLabelText ( ' alone' ) ) . toBeNull ( )
97
- expect ( ( ) => getByLabelText ( ' alone' ) ) . toThrowErrorMatchingSnapshot ( )
96
+ expect ( queryByLabelText ( / a l o n e / ) ) . toBeNull ( )
97
+ expect ( ( ) => getByLabelText ( / a l o n e / ) ) . toThrowErrorMatchingSnapshot ( )
98
98
} )
99
99
100
100
test ( 'totally empty label' , ( ) => {
@@ -106,7 +106,7 @@ test('totally empty label', () => {
106
106
test ( 'getByLabelText with aria-label' , ( ) => {
107
107
// not recommended normally, but supported for completeness
108
108
const { queryByLabelText} = render ( `<input aria-label="batman" />` )
109
- expect ( queryByLabelText ( ' bat' ) ) . toBeInTheDOM ( )
109
+ expect ( queryByLabelText ( / b a t / ) ) . toBeInTheDOM ( )
110
110
} )
111
111
112
112
test ( 'get element by its alt text' , ( ) => {
@@ -144,6 +144,114 @@ test('can get elements by data-testid attribute', () => {
144
144
expect ( queryByTestId ( 'first-name' ) ) . not . toBeInTheDOM ( )
145
145
} )
146
146
147
+ test ( 'queries passed strings match case-sensitive exact strings' , ( ) => {
148
+ const {
149
+ queryAllByTestId,
150
+ queryAllByAltText,
151
+ queryAllByText,
152
+ queryAllByLabelText,
153
+ queryAllByPlaceholderText,
154
+ } = render ( `
155
+ <div>
156
+ <img
157
+ data-testid="poster"
158
+ alt="Finding Nemo poster"
159
+ src="/finding-nemo.png" />
160
+ <img
161
+ data-testid="poster"
162
+ alt="Finding Dory poster"
163
+ src="/finding-dory.png" />
164
+ <img
165
+ data-testid="poster"
166
+ alt="jumanji poster"
167
+ src="/jumanji.png" />
168
+ <p>Where to next?</p>
169
+ <p>
170
+ content
171
+ with
172
+ linebreaks
173
+ is
174
+ ok
175
+ </p>
176
+ <label for="username">User Name</label>
177
+ <input id="username" placeholder="Dwayne 'The Rock' Johnson" />
178
+ </div>,
179
+ ` )
180
+ expect ( queryAllByAltText ( 'Finding Nemo poster' ) ) . toHaveLength ( 1 )
181
+ expect ( queryAllByAltText ( 'Finding' ) ) . toHaveLength ( 0 )
182
+ expect ( queryAllByAltText ( 'finding nemo poster' ) ) . toHaveLength ( 0 )
183
+ expect ( queryAllByTestId ( 'poster' ) ) . toHaveLength ( 3 )
184
+ expect ( queryAllByTestId ( 'Poster' ) ) . toHaveLength ( 0 )
185
+ expect ( queryAllByTestId ( 'post' ) ) . toHaveLength ( 0 )
186
+ expect ( queryAllByPlaceholderText ( "Dwayne 'The Rock' Johnson" ) ) . toHaveLength ( 1 )
187
+ expect ( queryAllByPlaceholderText ( 'The Rock' ) ) . toHaveLength ( 0 )
188
+ expect ( queryAllByPlaceholderText ( "dwayne 'the rock' johnson" ) ) . toHaveLength ( 0 )
189
+ expect ( queryAllByLabelText ( 'User Name' ) ) . toHaveLength ( 1 )
190
+ expect ( queryAllByLabelText ( 'user name' ) ) . toHaveLength ( 0 )
191
+ expect ( queryAllByLabelText ( 'User' ) ) . toHaveLength ( 0 )
192
+ expect ( queryAllByText ( 'Where to next?' ) ) . toHaveLength ( 1 )
193
+ expect ( queryAllByText ( 'Where to next' ) ) . toHaveLength ( 0 )
194
+ expect ( queryAllByText ( 'Where' ) ) . toHaveLength ( 0 )
195
+ expect ( queryAllByText ( 'where to next?' ) ) . toHaveLength ( 0 )
196
+ expect ( queryAllByText ( 'content with linebreaks is ok' ) ) . toHaveLength ( 1 )
197
+ } )
198
+
199
+ test ( 'passing {exact: false} uses fuzzy matches' , ( ) => {
200
+ const fuzzy = Object . freeze ( { exact : false } )
201
+ const {
202
+ queryAllByTestId,
203
+ queryAllByAltText,
204
+ queryAllByText,
205
+ queryAllByLabelText,
206
+ queryAllByPlaceholderText,
207
+ } = render ( `
208
+ <div>
209
+ <img
210
+ data-testid="poster"
211
+ alt="Finding Nemo poster"
212
+ src="/finding-nemo.png" />
213
+ <img
214
+ data-testid="poster"
215
+ alt="Finding Dory poster"
216
+ src="/finding-dory.png" />
217
+ <img
218
+ data-testid="poster"
219
+ alt="jumanji poster"
220
+ src="/jumanji.png" />
221
+ <p>Where to next?</p>
222
+ <p>
223
+ content
224
+ with
225
+ linebreaks
226
+ </p>
227
+ <label for="username">User Name</label>
228
+ <input id="username" placeholder="Dwayne 'The Rock' Johnson" />
229
+ </div>,
230
+ ` )
231
+ expect ( queryAllByAltText ( 'Finding Nemo poster' , fuzzy ) ) . toHaveLength ( 1 )
232
+ expect ( queryAllByAltText ( 'Finding' , fuzzy ) ) . toHaveLength ( 2 )
233
+ expect ( queryAllByAltText ( 'finding nemo poster' , fuzzy ) ) . toHaveLength ( 1 )
234
+ expect ( queryAllByTestId ( 'poster' , fuzzy ) ) . toHaveLength ( 3 )
235
+ expect ( queryAllByTestId ( 'Poster' , fuzzy ) ) . toHaveLength ( 3 )
236
+ expect ( queryAllByTestId ( 'post' , fuzzy ) ) . toHaveLength ( 3 )
237
+ expect (
238
+ queryAllByPlaceholderText ( "Dwayne 'The Rock' Johnson" , fuzzy ) ,
239
+ ) . toHaveLength ( 1 )
240
+ expect ( queryAllByPlaceholderText ( 'The Rock' , fuzzy ) ) . toHaveLength ( 1 )
241
+ expect (
242
+ queryAllByPlaceholderText ( "dwayne 'the rock' johnson" , fuzzy ) ,
243
+ ) . toHaveLength ( 1 )
244
+ expect ( queryAllByLabelText ( 'User Name' , fuzzy ) ) . toHaveLength ( 1 )
245
+ expect ( queryAllByLabelText ( 'user name' , fuzzy ) ) . toHaveLength ( 1 )
246
+ expect ( queryAllByLabelText ( 'user' , fuzzy ) ) . toHaveLength ( 1 )
247
+ expect ( queryAllByLabelText ( 'User' , fuzzy ) ) . toHaveLength ( 1 )
248
+ expect ( queryAllByText ( 'Where to next?' , fuzzy ) ) . toHaveLength ( 1 )
249
+ expect ( queryAllByText ( 'Where to next' , fuzzy ) ) . toHaveLength ( 1 )
250
+ expect ( queryAllByText ( 'Where' , fuzzy ) ) . toHaveLength ( 1 )
251
+ expect ( queryAllByText ( 'where to next?' , fuzzy ) ) . toHaveLength ( 1 )
252
+ expect ( queryAllByText ( 'content with linebreaks' , fuzzy ) ) . toHaveLength ( 1 )
253
+ } )
254
+
147
255
test ( 'getAll* matchers return an array' , ( ) => {
148
256
const {
149
257
getAllByAltText,
@@ -171,11 +279,11 @@ test('getAll* matchers return an array', () => {
171
279
</div>,
172
280
` )
173
281
expect ( getAllByAltText ( / f i n d i n g .* p o s t e r $ / i) ) . toHaveLength ( 2 )
174
- expect ( getAllByAltText ( ' jumanji' ) ) . toHaveLength ( 1 )
282
+ expect ( getAllByAltText ( / j u m a n j i / ) ) . toHaveLength ( 1 )
175
283
expect ( getAllByTestId ( 'poster' ) ) . toHaveLength ( 3 )
176
284
expect ( getAllByPlaceholderText ( / T h e R o c k / ) ) . toHaveLength ( 1 )
177
285
expect ( getAllByLabelText ( 'User Name' ) ) . toHaveLength ( 1 )
178
- expect ( getAllByText ( ' where' ) ) . toHaveLength ( 1 )
286
+ expect ( getAllByText ( / ^ w h e r e / i ) ) . toHaveLength ( 1 )
179
287
} )
180
288
181
289
test ( 'getAll* matchers throw for 0 matches' , ( ) => {
@@ -188,8 +296,6 @@ test('getAll* matchers throw for 0 matches', () => {
188
296
} = render ( `
189
297
<div>
190
298
<label>No Matches Please</label>
191
- <div data-testid="ABC"></div>
192
- <div data-testid="a-b-c"></div>
193
299
</div>,
194
300
` )
195
301
expect ( ( ) => getAllByTestId ( 'nope' ) ) . toThrow ( )
0 commit comments