diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index 05724c15..0739ea37 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -1080,6 +1080,18 @@ test('getByText ignores script tags by default', () => { expect(getAllByText(/hello/i, {ignore: false})).toHaveLength(3) }) +test('the default value for `ignore` can be configured', () => { + configure({defaultIgnore: 'style'}) + + const {getAllByText} = render( + '
Hello
', + ) + const noStyle = getAllByText(/hello/i) + expect(noStyle).toHaveLength(2) + expect(noStyle[0].tagName).toBe('SCRIPT') + expect(noStyle[1].tagName).toBe('DIV') +}) + test('get/query input element by current value', () => { const {getByDisplayValue, queryByDisplayValue, getByTestId} = renderIntoDocument(` diff --git a/src/config.ts b/src/config.ts index ed321155..161e58f4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -24,6 +24,8 @@ let config: InternalConfig = { eventWrapper: cb => cb(), // default value for the `hidden` option in `ByRole` queries defaultHidden: false, + // default value for the `ignore` option in `ByText` queries + defaultIgnore: 'script, style', // showOriginalStackTrace flag to show the full error stack traces for async errors showOriginalStackTrace: false, diff --git a/src/pretty-dom.js b/src/pretty-dom.js index 115db5d0..2a207cef 100644 --- a/src/pretty-dom.js +++ b/src/pretty-dom.js @@ -2,7 +2,7 @@ import * as prettyFormat from 'pretty-format' import createDOMElementFilter from './DOMElementFilter' import {getUserCodeFrame} from './get-user-code-frame' import {getDocument} from './helpers' -import {DEFAULT_IGNORE_TAGS} from './shared' +import {getConfig} from './config' const inNode = () => typeof process !== 'undefined' && @@ -19,8 +19,8 @@ const COMMENT_NODE = 8 function filterCommentsAndDefaultIgnoreTagsTags(value) { return ( value.nodeType !== COMMENT_NODE && - // value.nodeType === ELEMENT_NODE => !value.matches(DEFAULT_IGNORE_TAGS) - (value.nodeType !== ELEMENT_NODE || !value.matches(DEFAULT_IGNORE_TAGS)) + (value.nodeType !== ELEMENT_NODE || + !value.matches(getConfig().defaultIgnore)) ) } diff --git a/src/queries/text.ts b/src/queries/text.ts index bbda3b3d..e0282844 100644 --- a/src/queries/text.ts +++ b/src/queries/text.ts @@ -1,6 +1,5 @@ import {wrapAllByQueryWithSuggestion} from '../query-helpers' import {checkContainerType} from '../helpers' -import {DEFAULT_IGNORE_TAGS} from '../shared' import { AllByText, GetErrorFunction, @@ -13,6 +12,7 @@ import { makeNormalizer, getNodeText, buildQueries, + getConfig, } from './all-utils' const queryAllByText: AllByText = ( @@ -23,7 +23,7 @@ const queryAllByText: AllByText = ( exact = true, collapseWhitespace, trim, - ignore = DEFAULT_IGNORE_TAGS, + ignore = getConfig().defaultIgnore, normalizer, } = {}, ) => { diff --git a/src/shared.ts b/src/shared.ts deleted file mode 100644 index cca0a1e1..00000000 --- a/src/shared.ts +++ /dev/null @@ -1 +0,0 @@ -export const DEFAULT_IGNORE_TAGS = 'script, style' diff --git a/src/suggestions.js b/src/suggestions.js index 88601823..ccec24b4 100644 --- a/src/suggestions.js +++ b/src/suggestions.js @@ -4,7 +4,6 @@ import {getNodeText} from './get-node-text' import {getConfig} from './config' import {getImplicitAriaRoles, isInaccessible} from './role-helpers' import {getLabels} from './label-helpers' -import {DEFAULT_IGNORE_TAGS} from './shared' const normalize = getDefaultNormalizer() @@ -76,7 +75,7 @@ function canSuggest(currentMethod, requestedMethod, data) { export function getSuggestedQuery(element, variant = 'get', method) { // don't create suggestions for script and style elements - if (element.matches(DEFAULT_IGNORE_TAGS)) { + if (element.matches(getConfig().defaultIgnore)) { return undefined } diff --git a/types/config.d.ts b/types/config.d.ts index 6a3d1247..c3617ef0 100644 --- a/types/config.d.ts +++ b/types/config.d.ts @@ -12,6 +12,8 @@ export interface Config { asyncUtilTimeout: number computedStyleSupportsPseudoElements: boolean defaultHidden: boolean + /** default value for the `ignore` option in `ByText` queries */ + defaultIgnore: string showOriginalStackTrace: boolean throwSuggestions: boolean getElementError: (message: string | null, container: Element) => Error