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 3 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
46 changes: 45 additions & 1 deletion src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ Here are the accessible roles:
test('has no useful error message in findBy', async () => {
const {findByRole} = render(`<li />`)

await expect(findByRole('option', {timeout: 1})).rejects.toThrow('Unable to find role="option"')
await expect(findByRole('option', {timeout: 1})).rejects.toThrow(
'Unable to find role="option"',
)
})

test('explicit role is most specific', () => {
Expand Down Expand Up @@ -378,6 +380,48 @@ Here are the accessible roles:
`)
})

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"

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
5 changes: 3 additions & 2 deletions src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ function queryAllByRole(
})
}

const getMultipleError = (c, role) =>
`Found multiple elements with the role "${role}"`
const getMultipleError = (c, role) => {
return `Found multiple elements with the role "${role}"`
}

const getMissingError = (
container,
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