Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add defaultIgnore config option #1138

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
timdeschryver marked this conversation as resolved.
Show resolved Hide resolved
showOriginalStackTrace: boolean
throwSuggestions: boolean
getElementError: (message: string | null, container: Element) => Error
Expand Down