Skip to content

Commit bec190b

Browse files
authored
fix: handle ignores for ByText in suggestions (#597)
* fix: don't suggest for script,style tags * respect custom ignores * Update src/__tests__/suggestions.js * don't suggest on nested ignore tags * remove comment * move default tags to ignore to config * remove eslint disable
1 parent 95bb7f2 commit bec190b

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/__tests__/suggestions.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ afterAll(() => {
1010
configure({throwSuggestions: false})
1111
})
1212

13+
test('does not suggest for nested inline style', () => {
14+
renderIntoDocument(
15+
`<div data-testid="style"><style>.hsuHs{margin:auto}.wFncld{margin-top:3px;color:#9AA0A6;height:20px;width:20px}</style></div>`,
16+
)
17+
18+
expect(() => screen.getByTestId('style')).not.toThrow()
19+
})
20+
21+
test('does not suggest for inline script, style', () => {
22+
renderIntoDocument(
23+
`<script data-testid="script">alert('hello')</script><style data-testid="style">.hsuHs{margin:auto}.wFncld{margin-top:3px;color:#9AA0A6;height:20px;width:20px}</style>`,
24+
)
25+
26+
expect(() => screen.getByTestId('script')).not.toThrow()
27+
expect(() => screen.getByTestId('style')).not.toThrow()
28+
})
29+
30+
test('respects ignores', () => {
31+
renderIntoDocument(`<my-thing>foo</my-thing>`)
32+
33+
expect(() =>
34+
screen.queryByText('foo', {ignore: 'my-thing'}),
35+
).not.toThrowError()
36+
})
37+
1338
test('does not suggest when using getByRole', () => {
1439
renderIntoDocument(`<button data-testid="foo">submit</button>`)
1540

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let config = {
3333
_disableExpensiveErrorDiagnostics: false,
3434
}
3535

36+
export const DEFAULT_IGNORE_TAGS = 'script, style'
3637
export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
3738
try {
3839
config._disableExpensiveErrorDiagnostics = true

src/queries/text.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
2+
import {DEFAULT_IGNORE_TAGS} from '../config'
13
import {
24
fuzzyMatches,
35
matches,
46
makeNormalizer,
57
getNodeText,
68
buildQueries,
79
} from './all-utils'
8-
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
910

1011
function queryAllByText(
1112
container,
@@ -15,7 +16,7 @@ function queryAllByText(
1516
exact = true,
1617
collapseWhitespace,
1718
trim,
18-
ignore = 'script, style',
19+
ignore = DEFAULT_IGNORE_TAGS,
1920
normalizer,
2021
} = {},
2122
) {

src/query-helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => (
9494
) => {
9595
const element = query(container, ...args)
9696
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
97-
if (suggest) {
97+
if (element && suggest) {
9898
const suggestion = getSuggestedQuery(element, variant)
9999
if (suggestion && !queryAllByName.endsWith(suggestion.queryName)) {
100100
throw getSuggestionError(suggestion.toString(), container)
@@ -111,7 +111,7 @@ const wrapAllByQueryWithSuggestion = (query, queryAllByName, variant) => (
111111
const els = query(container, ...args)
112112

113113
const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1)
114-
if (suggest) {
114+
if (els.length && suggest) {
115115
// get a unique list of all suggestion messages. We are only going to make a suggestion if
116116
// all the suggestions are the same
117117
const uniqueSuggestionMessages = [

src/suggestions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {computeAccessibleName} from 'dom-accessibility-api'
22
import {getRoles} from './role-helpers'
33
import {getDefaultNormalizer} from './matches'
4+
import {getNodeText} from './get-node-text'
5+
import {DEFAULT_IGNORE_TAGS} from './config'
46

57
const normalize = getDefaultNormalizer()
68

@@ -57,8 +59,8 @@ export function getSuggestedQuery(element, variant) {
5759
return makeSuggestion('PlaceholderText', placeholderText, {variant})
5860
}
5961

60-
const textContent = normalize(element.textContent)
61-
if (textContent) {
62+
const textContent = normalize(getNodeText(element))
63+
if (textContent && !element.matches(DEFAULT_IGNORE_TAGS)) {
6264
return makeSuggestion('Text', textContent, {variant})
6365
}
6466

0 commit comments

Comments
 (0)