Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ test('get throws a useful error message', () => {
</div>"
`)
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "LucyRicardo"
"Unable to find an element with the role "LucyRicardo"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
There are no available roles.

<div>
<div />
Expand Down
60 changes: 30 additions & 30 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {render} from './helpers/test-utils'

test('by default logs accessible roles when it fails', () => {
test('by default logs available roles when it fails', () => {
const {getByRole} = render(`<h1>Hi</h1>`)
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "article"
"Unable to find an element with the role "article"

Here are the accessible roles:
Here are the available roles:

heading:

Expand All @@ -21,13 +21,13 @@ Here are the accessible roles:
`)
})

test('when hidden: true logs available roles when it fails', () => {
const {getByRole} = render(`<div hidden><h1>Hi</h1></div>`)
expect(() => getByRole('article', {hidden: true}))
test('when hidden: false logs only accessible roles when it fails', () => {
const {getByRole} = render(`<h1>Hi</h1>`)
expect(() => getByRole('article', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with the role "article"
"Unable to find an accessible element with the role "article"

Here are the available roles:
Here are the accessible roles:

heading:

Expand All @@ -36,20 +36,17 @@ Here are the available roles:
--------------------------------------------------

<div>
<div
hidden=""
>
<h1>
Hi
</h1>
</div>
<h1>
Hi
</h1>
</div>"
`)
})

test('logs error when there are no accessible roles', () => {
test('logs a different error if inaccessible roles should not be included', () => {
const {getByRole} = render('<div />')
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('article', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "article"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Expand All @@ -60,10 +57,9 @@ There are no accessible roles. But there might be some inaccessible roles. If yo
`)
})

test('logs a different error if inaccessible roles should be included', () => {
test('logs error when there are no available roles', () => {
const {getByRole} = render('<div />')
expect(() => getByRole('article', {hidden: true}))
.toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with the role "article"

There are no available roles.
Expand All @@ -74,10 +70,11 @@ There are no available roles.
`)
})

test('by default excludes elements that have the html hidden attribute or any of their parents', () => {
test('can exclude elements that have the html hidden attribute or any of their parents', () => {
const {getByRole} = render('<div hidden><ul /></div>')

expect(() => getByRole('list')).toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('list', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "list"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Expand All @@ -92,12 +89,13 @@ There are no accessible roles. But there might be some inaccessible roles. If yo
`)
})

test('by default excludes elements which have display: none or any of their parents', () => {
test('can exclude elements which have display: none or any of their parents', () => {
const {getByRole} = render(
'<div style="display: none;"><ul style="display: block;" /></div>',
)

expect(() => getByRole('list')).toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('list', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "list"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Expand All @@ -114,10 +112,11 @@ There are no accessible roles. But there might be some inaccessible roles. If yo
`)
})

test('by default excludes elements which have visibility hidden', () => {
test('can exclude elements which have visibility hidden', () => {
const {getByRole} = render('<div style="visibility: hidden;"><ul /></div>')

expect(() => getByRole('list')).toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('list', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "list"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Expand All @@ -141,7 +140,8 @@ test('by default excludes elements which have aria-hidden="true" or any of their
'<div aria-hidden="true"><ul aria-hidden="false" /></div>',
)

expect(() => getByRole('list')).toThrowErrorMatchingInlineSnapshot(`
expect(() => getByRole('list', {hidden: false}))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "list"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
Expand All @@ -158,7 +158,7 @@ There are no accessible roles. But there might be some inaccessible roles. If yo
`)
})

test('considers the computed visibility style not the parent', () => {
test('when hidden: false considers the computed visibility style not the parent', () => {
// this behavior deviates from the spec which includes "any descendant"
// if visibility is hidden. However, chrome a11y tree and nvda will include
// the following markup. This behavior might change depending on how
Expand All @@ -167,10 +167,10 @@ test('considers the computed visibility style not the parent', () => {
'<div style="visibility: hidden;"><main style="visibility: visible;"><ul /></main></div>',
)

expect(getByRole('list')).not.toBeNull()
expect(getByRole('list', {hidden: false})).not.toBeNull()
})

test('can include inaccessible roles', () => {
test('includes inaccessible roles by default', () => {
// this behavior deviates from the spec which includes "any descendant"
// if visibility is hidden. However, chrome a11y tree and nvda will include
// the following markup. This behavior might change depending on how
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ let config = {
// react-testing-library to use. For that reason, this feature will remain
// undocumented.
asyncWrapper: cb => cb(),
// don't check if the element is part of the a11y tree by default
defaultHidden: true,
}

export function configure(newConfig) {
Expand Down
22 changes: 19 additions & 3 deletions src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ import {
prettyRoles,
isInaccessible,
} from '../role-helpers'
import {buildQueries, fuzzyMatches, makeNormalizer, matches} from './all-utils'
import {
buildQueries,
fuzzyMatches,
getConfig,
makeNormalizer,
matches,
} from './all-utils'

function queryAllByRole(
container,
role,
{exact = true, collapseWhitespace, hidden = false, trim, normalizer} = {},
{
exact = true,
collapseWhitespace,
hidden = getConfig().defaultHidden,
trim,
normalizer,
} = {},
) {
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
Expand All @@ -35,7 +47,11 @@ function queryAllByRole(
const getMultipleError = (c, role) =>
`Found multiple elements with the role "${role}"`

const getMissingError = (container, role, {hidden = false} = {}) => {
const getMissingError = (
container,
role,
{hidden = getConfig().defaultHidden} = {},
) => {
const roles = prettyRoles(container, {hidden})
let roleMessage

Expand Down