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
2 changes: 1 addition & 1 deletion src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test('get throws a useful error message', () => {
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with the role "LucyRicardo"

Here are the available roles:
There are no available roles.

<div>
<div />
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ Here are the available roles:
</div>"
`)
})

test('logs error when there are no available roles', () => {
const {getByRole} = render('<div />')
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with the role "article"

There are no available roles.

<div>
<div />
</div>"
`)
})
22 changes: 16 additions & 6 deletions src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ function queryAllByRole(

const getMultipleError = (c, role) =>
`Found multiple elements with the role "${role}"`
const getMissingError = (container, role) =>
`
Unable to find an element with the role "${role}"

const getMissingError = (container, role) => {
const roles = prettyRoles(container)
let roleMessage

if (roles.length === 0) {
roleMessage = 'There are no available roles.'
} else {
roleMessage = `
Here are the available roles:

${prettyRoles(container)
.replace(/\n/g, '\n ')
.replace(/\n\s\s\n/g, '\n\n')}
${roles.replace(/\n/g, '\n ').replace(/\n\s\s\n/g, '\n\n')}
`.trim()
}

return `
Unable to find an element with the role "${role}"

${roleMessage}`.trim()
}

const [
queryByRole,
Expand Down