diff --git a/src/__tests__/suggestions.js b/src/__tests__/suggestions.js index 75d6f90e..485299d5 100644 --- a/src/__tests__/suggestions.js +++ b/src/__tests__/suggestions.js @@ -10,6 +10,31 @@ afterAll(() => { configure({throwSuggestions: false}) }) +test('does not suggest for nested inline style', () => { + renderIntoDocument( + `
`, + ) + + expect(() => screen.getByTestId('style')).not.toThrow() +}) + +test('does not suggest for inline script, style', () => { + renderIntoDocument( + ``, + ) + + expect(() => screen.getByTestId('script')).not.toThrow() + expect(() => screen.getByTestId('style')).not.toThrow() +}) + +test('respects ignores', () => { + renderIntoDocument(`foo`) + + expect(() => + screen.queryByText('foo', {ignore: 'my-thing'}), + ).not.toThrowError() +}) + test('does not suggest when using getByRole', () => { renderIntoDocument(``) diff --git a/src/config.js b/src/config.js index bce4d959..37ab986b 100644 --- a/src/config.js +++ b/src/config.js @@ -33,6 +33,7 @@ let config = { _disableExpensiveErrorDiagnostics: false, } +export const DEFAULT_IGNORE_TAGS = 'script, style' export function runWithExpensiveErrorDiagnosticsDisabled(callback) { try { config._disableExpensiveErrorDiagnostics = true diff --git a/src/queries/text.js b/src/queries/text.js index aa7419d9..4d31b6c2 100644 --- a/src/queries/text.js +++ b/src/queries/text.js @@ -1,3 +1,5 @@ +import {wrapAllByQueryWithSuggestion} from '../query-helpers' +import {DEFAULT_IGNORE_TAGS} from '../config' import { fuzzyMatches, matches, @@ -5,7 +7,6 @@ import { getNodeText, buildQueries, } from './all-utils' -import {wrapAllByQueryWithSuggestion} from '../query-helpers' function queryAllByText( container, @@ -15,7 +16,7 @@ function queryAllByText( exact = true, collapseWhitespace, trim, - ignore = 'script, style', + ignore = DEFAULT_IGNORE_TAGS, normalizer, } = {}, ) { diff --git a/src/query-helpers.js b/src/query-helpers.js index 0b7793f0..cd4c3481 100644 --- a/src/query-helpers.js +++ b/src/query-helpers.js @@ -94,7 +94,7 @@ const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => ( ) => { const element = query(container, ...args) const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1) - if (suggest) { + if (element && suggest) { const suggestion = getSuggestedQuery(element, variant) if (suggestion && !queryAllByName.endsWith(suggestion.queryName)) { throw getSuggestionError(suggestion.toString(), container) @@ -111,7 +111,7 @@ const wrapAllByQueryWithSuggestion = (query, queryAllByName, variant) => ( const els = query(container, ...args) const [{suggest = getConfig().throwSuggestions} = {}] = args.slice(-1) - if (suggest) { + if (els.length && suggest) { // get a unique list of all suggestion messages. We are only going to make a suggestion if // all the suggestions are the same const uniqueSuggestionMessages = [ diff --git a/src/suggestions.js b/src/suggestions.js index 7d109bd3..20735889 100644 --- a/src/suggestions.js +++ b/src/suggestions.js @@ -1,6 +1,8 @@ import {computeAccessibleName} from 'dom-accessibility-api' import {getRoles} from './role-helpers' import {getDefaultNormalizer} from './matches' +import {getNodeText} from './get-node-text' +import {DEFAULT_IGNORE_TAGS} from './config' const normalize = getDefaultNormalizer() @@ -57,8 +59,8 @@ export function getSuggestedQuery(element, variant) { return makeSuggestion('PlaceholderText', placeholderText, {variant}) } - const textContent = normalize(element.textContent) - if (textContent) { + const textContent = normalize(getNodeText(element)) + if (textContent && !element.matches(DEFAULT_IGNORE_TAGS)) { return makeSuggestion('Text', textContent, {variant}) }