Skip to content

Commit 4e9484a

Browse files
feat: add defaultIgnore config option (#1138)
1 parent 11fc773 commit 4e9484a

File tree

7 files changed

+22
-8
lines changed

7 files changed

+22
-8
lines changed

Diff for: src/__tests__/element-queries.js

+12
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,18 @@ test('getByText ignores script tags by default', () => {
10801080
expect(getAllByText(/hello/i, {ignore: false})).toHaveLength(3)
10811081
})
10821082

1083+
test('the default value for `ignore` can be configured', () => {
1084+
configure({defaultIgnore: 'style'})
1085+
1086+
const {getAllByText} = render(
1087+
'<script>Hello</script><div>Hello</div><style>.Hello{}</style>',
1088+
)
1089+
const noStyle = getAllByText(/hello/i)
1090+
expect(noStyle).toHaveLength(2)
1091+
expect(noStyle[0].tagName).toBe('SCRIPT')
1092+
expect(noStyle[1].tagName).toBe('DIV')
1093+
})
1094+
10831095
test('get/query input element by current value', () => {
10841096
const {getByDisplayValue, queryByDisplayValue, getByTestId} =
10851097
renderIntoDocument(`

Diff for: src/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ let config: InternalConfig = {
2424
eventWrapper: cb => cb(),
2525
// default value for the `hidden` option in `ByRole` queries
2626
defaultHidden: false,
27+
// default value for the `ignore` option in `ByText` queries
28+
defaultIgnore: 'script, style',
2729
// showOriginalStackTrace flag to show the full error stack traces for async errors
2830
showOriginalStackTrace: false,
2931

Diff for: src/pretty-dom.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as prettyFormat from 'pretty-format'
22
import createDOMElementFilter from './DOMElementFilter'
33
import {getUserCodeFrame} from './get-user-code-frame'
44
import {getDocument} from './helpers'
5-
import {DEFAULT_IGNORE_TAGS} from './shared'
5+
import {getConfig} from './config'
66

77
const inNode = () =>
88
typeof process !== 'undefined' &&
@@ -19,8 +19,8 @@ const COMMENT_NODE = 8
1919
function filterCommentsAndDefaultIgnoreTagsTags(value) {
2020
return (
2121
value.nodeType !== COMMENT_NODE &&
22-
// value.nodeType === ELEMENT_NODE => !value.matches(DEFAULT_IGNORE_TAGS)
23-
(value.nodeType !== ELEMENT_NODE || !value.matches(DEFAULT_IGNORE_TAGS))
22+
(value.nodeType !== ELEMENT_NODE ||
23+
!value.matches(getConfig().defaultIgnore))
2424
)
2525
}
2626

Diff for: src/queries/text.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {DEFAULT_IGNORE_TAGS} from '../shared'
43
import {
54
AllByText,
65
GetErrorFunction,
@@ -13,6 +12,7 @@ import {
1312
makeNormalizer,
1413
getNodeText,
1514
buildQueries,
15+
getConfig,
1616
} from './all-utils'
1717

1818
const queryAllByText: AllByText = (
@@ -23,7 +23,7 @@ const queryAllByText: AllByText = (
2323
exact = true,
2424
collapseWhitespace,
2525
trim,
26-
ignore = DEFAULT_IGNORE_TAGS,
26+
ignore = getConfig().defaultIgnore,
2727
normalizer,
2828
} = {},
2929
) => {

Diff for: src/shared.ts

-1
This file was deleted.

Diff for: src/suggestions.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {getNodeText} from './get-node-text'
44
import {getConfig} from './config'
55
import {getImplicitAriaRoles, isInaccessible} from './role-helpers'
66
import {getLabels} from './label-helpers'
7-
import {DEFAULT_IGNORE_TAGS} from './shared'
87

98
const normalize = getDefaultNormalizer()
109

@@ -76,7 +75,7 @@ function canSuggest(currentMethod, requestedMethod, data) {
7675

7776
export function getSuggestedQuery(element, variant = 'get', method) {
7877
// don't create suggestions for script and style elements
79-
if (element.matches(DEFAULT_IGNORE_TAGS)) {
78+
if (element.matches(getConfig().defaultIgnore)) {
8079
return undefined
8180
}
8281

Diff for: types/config.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface Config {
1212
asyncUtilTimeout: number
1313
computedStyleSupportsPseudoElements: boolean
1414
defaultHidden: boolean
15+
/** default value for the `ignore` option in `ByText` queries */
16+
defaultIgnore: string
1517
showOriginalStackTrace: boolean
1618
throwSuggestions: boolean
1719
getElementError: (message: string | null, container: Element) => Error

0 commit comments

Comments
 (0)