Skip to content

Lists elements that matched multiple error #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,20 @@ test('accessible regex name in error message for multiple found', () => {
.toThrowErrorMatchingInlineSnapshot(`
"Found multiple elements with the role "button" and name \`/value/i\`

Here are the matching elements:

<button>
Increment value
</button>

<button>
Decrement value
</button>

<button>
Reset value
</button>

(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).

<div>
Expand Down Expand Up @@ -420,6 +434,20 @@ test('accessible string name in error message for multiple found', () => {
.toThrowErrorMatchingInlineSnapshot(`
"Found multiple elements with the role "button" and name "Submit"

Here are the matching elements:

<button>
Submit
</button>

<button>
Submit
</button>

<button>
Submit
</button>

(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).

<div>
Expand All @@ -436,6 +464,48 @@ test('accessible string name in error message for multiple found', () => {
`)
})

test('matching elements in error for multiple found', () => {
const {getByRole} = render(
`<button>Increment value</button
><button>Different label</button
><p>Wrong role</p
><button>Reset value</button
>`,
)

expect(() => getByRole('button', {name: /value/i}))
.toThrowErrorMatchingInlineSnapshot(`
"Found multiple elements with the role "button" and name \`/value/i\`

Here are the matching elements:

<button>
Increment value
</button>

<button>
Reset value
</button>

(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).

<div>
<button>
Increment value
</button>
<button>
Different label
</button>
<p>
Wrong role
</p>
<button>
Reset value
</button>
</div>"
`)
})

describe('configuration', () => {
let originalConfig
beforeEach(() => {
Expand Down
9 changes: 8 additions & 1 deletion src/query-helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {prettyDOM} from './pretty-dom'
import {getSuggestedQuery} from './suggestions'
import {fuzzyMatches, matches, makeNormalizer} from './matches'
import {waitFor} from './wait-for'
Expand Down Expand Up @@ -45,8 +46,14 @@ function makeSingleQuery(allQuery, getMultipleError) {
return (container, ...args) => {
const els = allQuery(container, ...args)
if (els.length > 1) {
const elementStrings = els.map(element => prettyDOM(element)).join('\n\n')

throw getMultipleElementsFoundError(
getMultipleError(container, ...args),
`${getMultipleError(container, ...args)}

Here are the matching elements:

${elementStrings}`,
container,
)
}
Expand Down