Skip to content
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

feat: add name hint to missing error in find by queries #1143

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/__tests__/role.js
Expand Up @@ -363,6 +363,14 @@ test('has no useful error message in findBy', async () => {
)
})

test('findBy error message for missing elements contains a name hint', async () => {
const {findByRole} = render(`<button>Click me</button>`)

await expect(findByRole('button', {name: 'Submit'})).rejects.toThrow(
'Unable to find role="button" and name "Submit"',
)
})

test('explicit role is most specific', () => {
const {getByRole} = renderIntoDocument(
`<button role="tab" aria-label="my-tab" />`,
Expand Down
10 changes: 7 additions & 3 deletions src/queries/role.js
Expand Up @@ -220,7 +220,7 @@ function makeRoleSelector(role, exact, customNormalizer) {
.join(',')
}

const getMultipleError = (c, role, {name} = {}) => {
const getNameHint = name => {
let nameHint = ''
if (name === undefined) {
nameHint = ''
Expand All @@ -230,7 +230,11 @@ const getMultipleError = (c, role, {name} = {}) => {
nameHint = ` and name \`${name}\``
}

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

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

const getMissingError = (
Expand All @@ -239,7 +243,7 @@ const getMissingError = (
{hidden = getConfig().defaultHidden, name, description} = {},
) => {
if (getConfig()._disableExpensiveErrorDiagnostics) {
return `Unable to find role="${role}"`
return `Unable to find role="${role}"${getNameHint(name)}`
}

let roles = ''
Expand Down