Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add defaultIgnore config option (#1138)
  • Loading branch information
robin-drexler committed Jun 20, 2022
1 parent 11fc773 commit 4e9484a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/element-queries.js
Expand Up @@ -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(
'<script>Hello</script><div>Hello</div><style>.Hello{}</style>',
)
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(`
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Expand Up @@ -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,

Expand Down
6 changes: 3 additions & 3 deletions src/pretty-dom.js
Expand Up @@ -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' &&
Expand All @@ -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))
)
}

Expand Down
4 changes: 2 additions & 2 deletions 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,
Expand All @@ -13,6 +12,7 @@ import {
makeNormalizer,
getNodeText,
buildQueries,
getConfig,
} from './all-utils'

const queryAllByText: AllByText = (
Expand All @@ -23,7 +23,7 @@ const queryAllByText: AllByText = (
exact = true,
collapseWhitespace,
trim,
ignore = DEFAULT_IGNORE_TAGS,
ignore = getConfig().defaultIgnore,
normalizer,
} = {},
) => {
Expand Down
1 change: 0 additions & 1 deletion src/shared.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/suggestions.js
Expand Up @@ -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()

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions types/config.d.ts
Expand Up @@ -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
Expand Down

0 comments on commit 4e9484a

Please sign in to comment.