Skip to content
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
60 changes: 59 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,62 @@ Here are the accessible roles:
`)
})

test('accessible regex name in error message for multiple found', () => {
const {getByRole} = render(
`<button>Increment value</button
><button>Decrement value</button
><button>Reset value</button
>`,
)

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

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

<div>
<button>
Increment value
</button>
<button>
Decrement value
</button>
<button>
Reset value
</button>
</div>"
`)
})

test('accessible string name in error message for multiple found', () => {
const {getByRole} = render(
`<button>Submit</button
><button>Submit</button
><button>Submit</button
>`,
)

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

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

<div>
<button>
Submit
</button>
<button>
Submit
</button>
<button>
Submit
</button>
</div>"
`)
})

describe('configuration', () => {
let originalConfig
beforeEach(() => {
Expand Down
14 changes: 12 additions & 2 deletions src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ function queryAllByRole(
})
}

const getMultipleError = (c, role) =>
`Found multiple elements with the role "${role}"`
const getMultipleError = (c, role, {name} = {}) => {
let nameHint = ''
if (name === undefined) {
nameHint = ''
} else if (typeof name === 'string') {
nameHint = ` and name "${name}"`
} else {
nameHint = ` and name \`${name}\``
}

return `Found multiple elements with the role "${role}"${nameHint}`
}

const getMissingError = (
container,
Expand Down