Skip to content

Commit aefebfb

Browse files
authored
fix: add matcher name to multiple elements error (#677)
1 parent 373dbc4 commit aefebfb

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/__tests__/role.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ Here are the accessible roles:
346346
test('has no useful error message in findBy', async () => {
347347
const {findByRole} = render(`<li />`)
348348

349-
await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"')
349+
await expect(findByRole('option', {timeout: 1})).rejects.toThrow(
350+
'Unable to find role="option"',
351+
)
350352
})
351353

352354
test('explicit role is most specific', () => {
@@ -378,6 +380,62 @@ Here are the accessible roles:
378380
`)
379381
})
380382

383+
test('accessible regex name in error message for multiple found', () => {
384+
const {getByRole} = render(
385+
`<button>Increment value</button
386+
><button>Decrement value</button
387+
><button>Reset value</button
388+
>`,
389+
)
390+
391+
expect(() => getByRole('button', {name: /value/i}))
392+
.toThrowErrorMatchingInlineSnapshot(`
393+
"Found multiple elements with the role "button" and name \`/value/i\`
394+
395+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
396+
397+
<div>
398+
<button>
399+
Increment value
400+
</button>
401+
<button>
402+
Decrement value
403+
</button>
404+
<button>
405+
Reset value
406+
</button>
407+
</div>"
408+
`)
409+
})
410+
411+
test('accessible string name in error message for multiple found', () => {
412+
const {getByRole} = render(
413+
`<button>Submit</button
414+
><button>Submit</button
415+
><button>Submit</button
416+
>`,
417+
)
418+
419+
expect(() => getByRole('button', {name: 'Submit'}))
420+
.toThrowErrorMatchingInlineSnapshot(`
421+
"Found multiple elements with the role "button" and name "Submit"
422+
423+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
424+
425+
<div>
426+
<button>
427+
Submit
428+
</button>
429+
<button>
430+
Submit
431+
</button>
432+
<button>
433+
Submit
434+
</button>
435+
</div>"
436+
`)
437+
})
438+
381439
describe('configuration', () => {
382440
let originalConfig
383441
beforeEach(() => {

src/queries/role.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,18 @@ function queryAllByRole(
107107
})
108108
}
109109

110-
const getMultipleError = (c, role) =>
111-
`Found multiple elements with the role "${role}"`
110+
const getMultipleError = (c, role, {name} = {}) => {
111+
let nameHint = ''
112+
if (name === undefined) {
113+
nameHint = ''
114+
} else if (typeof name === 'string') {
115+
nameHint = ` and name "${name}"`
116+
} else {
117+
nameHint = ` and name \`${name}\``
118+
}
119+
120+
return `Found multiple elements with the role "${role}"${nameHint}`
121+
}
112122

113123
const getMissingError = (
114124
container,

0 commit comments

Comments
 (0)