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
19 changes: 19 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {configure, getConfig} from '../config'
import {render} from './helpers/test-utils'

test('by default logs accessible roles when it fails', () => {
Expand Down Expand Up @@ -181,3 +182,21 @@ test('can include inaccessible roles', () => {

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

describe('configuration', () => {
let originalConfig
beforeEach(() => {
originalConfig = getConfig()
})

afterEach(() => {
configure(originalConfig)
})

test('the default value for `hidden` can be configured', () => {
configure({defaultHidden: true})

const {getByRole} = render('<div hidden><ul /></div>')
expect(getByRole('list')).not.toBeNull()
})
})
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(),
// default value for the `hidden` option in `ByRole` queries
defaultHidden: false,
}

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 @@ -4,12 +4,24 @@ import {
isInaccessible,
isSubtreeInaccessible,
} 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 Down Expand Up @@ -49,7 +61,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