Skip to content

Commit 7ffb941

Browse files
eps1lonKent C. Dodds
authored andcommitted
feat: make default of hidden in ByRole configurable (#398)
1 parent bcb1b8c commit 7ffb941

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/__tests__/role.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {configure, getConfig} from '../config'
12
import {render} from './helpers/test-utils'
23

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

182183
expect(getByRole('list', {hidden: true})).not.toBeNull()
183184
})
185+
186+
describe('configuration', () => {
187+
let originalConfig
188+
beforeEach(() => {
189+
originalConfig = getConfig()
190+
})
191+
192+
afterEach(() => {
193+
configure(originalConfig)
194+
})
195+
196+
test('the default value for `hidden` can be configured', () => {
197+
configure({defaultHidden: true})
198+
199+
const {getByRole} = render('<div hidden><ul /></div>')
200+
expect(getByRole('list')).not.toBeNull()
201+
})
202+
})

src/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ let config = {
1212
// react-testing-library to use. For that reason, this feature will remain
1313
// undocumented.
1414
asyncWrapper: cb => cb(),
15+
// default value for the `hidden` option in `ByRole` queries
16+
defaultHidden: false,
1517
}
1618

1719
export function configure(newConfig) {

src/queries/role.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@ import {
44
isInaccessible,
55
isSubtreeInaccessible,
66
} from '../role-helpers'
7-
import {buildQueries, fuzzyMatches, makeNormalizer, matches} from './all-utils'
7+
import {
8+
buildQueries,
9+
fuzzyMatches,
10+
getConfig,
11+
makeNormalizer,
12+
matches,
13+
} from './all-utils'
814

915
function queryAllByRole(
1016
container,
1117
role,
12-
{exact = true, collapseWhitespace, hidden = false, trim, normalizer} = {},
18+
{
19+
exact = true,
20+
collapseWhitespace,
21+
hidden = getConfig().defaultHidden,
22+
trim,
23+
normalizer,
24+
} = {},
1325
) {
1426
const matcher = exact ? matches : fuzzyMatches
1527
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
@@ -49,7 +61,11 @@ function queryAllByRole(
4961
const getMultipleError = (c, role) =>
5062
`Found multiple elements with the role "${role}"`
5163

52-
const getMissingError = (container, role, {hidden = false} = {}) => {
64+
const getMissingError = (
65+
container,
66+
role,
67+
{hidden = getConfig().defaultHidden} = {},
68+
) => {
5369
const roles = prettyRoles(container, {hidden})
5470
let roleMessage
5571

0 commit comments

Comments
 (0)