@@ -2,6 +2,7 @@ import {fuzzyMatches, matches, makeNormalizer} from './matches'
2
2
import { getNodeText } from './get-node-text'
3
3
import {
4
4
getElementError ,
5
+ getGetByElementError ,
5
6
firstResultOrNull ,
6
7
queryAllByAttribute ,
7
8
queryByAttribute ,
@@ -136,7 +137,6 @@ function queryAllByTitle(
136
137
function queryByTitle ( ...args ) {
137
138
return firstResultOrNull ( queryAllByTitle , ...args )
138
139
}
139
-
140
140
function getTestIdAttribute ( ) {
141
141
return getConfig ( ) . testIdAttribute
142
142
}
@@ -209,8 +209,15 @@ function getAllByTestId(container, id, ...rest) {
209
209
return els
210
210
}
211
211
212
- function getByTestId ( ...args ) {
213
- return firstResultOrNull ( getAllByTestId , ...args )
212
+ function getByTestId ( container , id , ...args ) {
213
+ const els = getAllByTestId ( container , id , ...args )
214
+ if ( els . length > 1 ) {
215
+ throw getGetByElementError (
216
+ `Found multiple elements by: [${ getTestIdAttribute ( ) } ="${ id } "]` ,
217
+ container ,
218
+ )
219
+ }
220
+ return els [ 0 ]
214
221
}
215
222
216
223
function getAllByTitle ( container , title , ...rest ) {
@@ -224,8 +231,15 @@ function getAllByTitle(container, title, ...rest) {
224
231
return els
225
232
}
226
233
227
- function getByTitle ( ...args ) {
228
- return firstResultOrNull ( getAllByTitle , ...args )
234
+ function getByTitle ( container , title , ...args ) {
235
+ const els = getAllByTitle ( container , title , ...args )
236
+ if ( els . length > 1 ) {
237
+ throw getGetByElementError (
238
+ `Found multiple elements with the title: ${ title } .` ,
239
+ container ,
240
+ )
241
+ }
242
+ return els [ 0 ]
229
243
}
230
244
231
245
function getAllByPlaceholderText ( container , text , ...rest ) {
@@ -239,8 +253,15 @@ function getAllByPlaceholderText(container, text, ...rest) {
239
253
return els
240
254
}
241
255
242
- function getByPlaceholderText ( ...args ) {
243
- return firstResultOrNull ( getAllByPlaceholderText , ...args )
256
+ function getByPlaceholderText ( container , text , ...args ) {
257
+ const els = getAllByPlaceholderText ( container , text , ...args )
258
+ if ( els . length > 1 ) {
259
+ throw getGetByElementError (
260
+ `Found multiple elements with the placeholder text of: ${ text } ` ,
261
+ container ,
262
+ )
263
+ }
264
+ return els [ 0 ]
244
265
}
245
266
246
267
function getAllByLabelText ( container , text , ...rest ) {
@@ -262,8 +283,15 @@ function getAllByLabelText(container, text, ...rest) {
262
283
return els
263
284
}
264
285
265
- function getByLabelText ( ...args ) {
266
- return firstResultOrNull ( getAllByLabelText , ...args )
286
+ function getByLabelText ( container , text , ...args ) {
287
+ const els = getAllByLabelText ( container , text , ...args )
288
+ if ( els . length > 1 ) {
289
+ throw getGetByElementError (
290
+ `Found multiple elements with the text of: ${ text } ` ,
291
+ container ,
292
+ )
293
+ }
294
+ return els [ 0 ]
267
295
}
268
296
269
297
function getAllByText ( container , text , ...rest ) {
@@ -277,8 +305,15 @@ function getAllByText(container, text, ...rest) {
277
305
return els
278
306
}
279
307
280
- function getByText ( ...args ) {
281
- return firstResultOrNull ( getAllByText , ...args )
308
+ function getByText ( container , text , ...args ) {
309
+ const els = getAllByText ( container , text , ...args )
310
+ if ( els . length > 1 ) {
311
+ throw getGetByElementError (
312
+ `Found multiple elements with the text: ${ text } ` ,
313
+ container ,
314
+ )
315
+ }
316
+ return els [ 0 ]
282
317
}
283
318
284
319
function getAllByAltText ( container , alt , ...rest ) {
@@ -292,20 +327,37 @@ function getAllByAltText(container, alt, ...rest) {
292
327
return els
293
328
}
294
329
295
- function getByAltText ( ...args ) {
296
- return firstResultOrNull ( getAllByAltText , ...args )
330
+ function getByAltText ( container , alt , ...args ) {
331
+ const els = getAllByAltText ( container , alt , ...args )
332
+ if ( els . length > 1 ) {
333
+ throw getGetByElementError (
334
+ `Found multiple elements with the alt text: ${ alt } ` ,
335
+ container ,
336
+ )
337
+ }
338
+ return els [ 0 ]
297
339
}
298
340
299
341
function getAllByRole ( container , id , ...rest ) {
300
342
const els = queryAllByRole ( container , id , ...rest )
301
343
if ( ! els . length ) {
302
- throw getElementError ( `Unable to find an element by role=${ id } ` , container )
344
+ throw getElementError (
345
+ `Unable to find an element by [role=${ id } ]` ,
346
+ container ,
347
+ )
303
348
}
304
349
return els
305
350
}
306
351
307
- function getByRole ( ...args ) {
308
- return firstResultOrNull ( getAllByRole , ...args )
352
+ function getByRole ( container , id , ...args ) {
353
+ const els = getAllByRole ( container , id , ...args )
354
+ if ( els . length > 1 ) {
355
+ throw getGetByElementError (
356
+ `Found multiple elements by [role=${ id } ]` ,
357
+ container ,
358
+ )
359
+ }
360
+ return els [ 0 ]
309
361
}
310
362
311
363
function getAllByDisplayValue ( container , value , ...rest ) {
@@ -319,8 +371,15 @@ function getAllByDisplayValue(container, value, ...rest) {
319
371
return els
320
372
}
321
373
322
- function getByDisplayValue ( ...args ) {
323
- return firstResultOrNull ( getAllByDisplayValue , ...args )
374
+ function getByDisplayValue ( container , value , ...args ) {
375
+ const els = getAllByDisplayValue ( container , value , ...args )
376
+ if ( els . length > 1 ) {
377
+ throw getGetByElementError (
378
+ `Found multiple elements with the value: ${ value } .` ,
379
+ container ,
380
+ )
381
+ }
382
+ return els [ 0 ]
324
383
}
325
384
326
385
function makeFinder ( getter ) {
0 commit comments