diff --git a/src/__tests__/role.js b/src/__tests__/role.js index a372a1c9..565fe605 100644 --- a/src/__tests__/role.js +++ b/src/__tests__/role.js @@ -1,3 +1,4 @@ +import {configure, getConfig} from '../config' import {render} from './helpers/test-utils' test('by default logs accessible roles when it fails', () => { @@ -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('') + expect(getByRole('list')).not.toBeNull() + }) +}) diff --git a/src/config.js b/src/config.js index c09c5085..f8109352 100644 --- a/src/config.js +++ b/src/config.js @@ -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) { diff --git a/src/queries/role.js b/src/queries/role.js index b84c4993..dfc8a331 100644 --- a/src/queries/role.js +++ b/src/queries/role.js @@ -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}) @@ -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