Skip to content

Commit a7aa387

Browse files
committed
Tests matching elements in multiple found error
1 parent 418d4fa commit a7aa387

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/__tests__/role.js

Lines changed: 45 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,48 @@ Here are the accessible roles:
378380
`)
379381
})
380382

383+
test('matching elements in error for multiple found', () => {
384+
const {getByRole} = render(
385+
`<button>Increment value</button
386+
><button>Different label</button
387+
><p>Wrong role</p
388+
><button>Reset value</button
389+
>`,
390+
)
391+
392+
expect(() => getByRole('button', {name: /value/i}))
393+
.toThrowErrorMatchingInlineSnapshot(`
394+
"Found multiple elements with the role "button"
395+
396+
Here are the matching elements:
397+
398+
<button>
399+
Increment value
400+
</button>
401+
402+
<button>
403+
Reset value
404+
</button>
405+
406+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
407+
408+
<div>
409+
<button>
410+
Increment value
411+
</button>
412+
<button>
413+
Different label
414+
</button>
415+
<p>
416+
Wrong role
417+
</p>
418+
<button>
419+
Reset value
420+
</button>
421+
</div>"
422+
`)
423+
})
424+
381425
describe('configuration', () => {
382426
let originalConfig
383427
beforeEach(() => {

0 commit comments

Comments
 (0)