Skip to content

Commit b339616

Browse files
authored
Merge branch 'master' into Lists-elements-that-matched-multiple-error
2 parents a7aa387 + 3c262fd commit b339616

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,16 @@
10871087
"code",
10881088
"test"
10891089
]
1090+
},
1091+
{
1092+
"login": "JacobParis",
1093+
"name": "Jacob Paris",
1094+
"avatar_url": "https://avatars2.githubusercontent.com/u/5633704?v=4",
1095+
"profile": "https://www.jacobparis.com/",
1096+
"contributions": [
1097+
"code",
1098+
"test"
1099+
]
10901100
}
10911101
],
10921102
"repoHost": "https://github.com"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ Thanks goes to these people ([emoji key][emojis]):
273273
<td align="center"><a href="https://commonlit.org"><img src="https://avatars3.githubusercontent.com/u/319471?v=4" width="100px;" alt=""/><br /><sub><b>Geoff Harcourt</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=geoffharcourt" title="Code">💻</a></td>
274274
<td align="center"><a href="http://www.joshuakgoldberg.com"><img src="https://avatars1.githubusercontent.com/u/3335181?v=4" width="100px;" alt=""/><br /><sub><b>Josh Goldberg</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=JoshuaKGoldberg" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=JoshuaKGoldberg" title="Tests">⚠️</a></td>
275275
<td align="center"><a href="http://kengregory.com"><img src="https://avatars0.githubusercontent.com/u/3155127?v=4" width="100px;" alt=""/><br /><sub><b>Ken Gregory</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=kgregory" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=kgregory" title="Tests">⚠️</a></td>
276+
<td align="center"><a href="https://www.jacobparis.com/"><img src="https://avatars2.githubusercontent.com/u/5633704?v=4" width="100px;" alt=""/><br /><sub><b>Jacob Paris</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=JacobParis" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=JacobParis" title="Tests">⚠️</a></td>
276277
</tr>
277278
</table>
278279

279280
<!-- markdownlint-enable -->
280281
<!-- prettier-ignore-end -->
281-
282282
<!-- ALL-CONTRIBUTORS-LIST:END -->
283283

284284
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/role.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,73 @@ Here are the accessible roles:
380380
`)
381381
})
382382

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"
394+
395+
Here are the matching elements:
396+
397+
<button>
398+
Increment value
399+
</button>
400+
401+
<button>
402+
Reset value
403+
</button>
404+
"Found multiple elements with the role "button" and name \`/value/i\`
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+
Decrement value
414+
</button>
415+
<button>
416+
Reset value
417+
</button>
418+
</div>"
419+
`)
420+
})
421+
422+
test('accessible string name in error message for multiple found', () => {
423+
const {getByRole} = render(
424+
`<button>Submit</button
425+
><button>Submit</button
426+
><button>Submit</button
427+
>`,
428+
)
429+
430+
expect(() => getByRole('button', {name: 'Submit'}))
431+
.toThrowErrorMatchingInlineSnapshot(`
432+
"Found multiple elements with the role "button" and name "Submit"
433+
434+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
435+
436+
<div>
437+
<button>
438+
Submit
439+
</button>
440+
<button>
441+
Submit
442+
</button>
443+
<button>
444+
Submit
445+
</button>
446+
</div>"
447+
`)
448+
})
449+
383450
test('matching elements in error for multiple found', () => {
384451
const {getByRole} = render(
385452
`<button>Increment value</button

src/queries/role.js

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

110-
const getMultipleError = (c, role) => {
111-
return `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}`
112121
}
113122

114123
const getMissingError = (

0 commit comments

Comments
 (0)