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

fix: handle ignores for ByText in suggestions #597

Merged
merged 8 commits into from May 31, 2020
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
25 changes: 25 additions & 0 deletions src/__tests__/suggestions.js
Expand Up @@ -10,6 +10,31 @@ afterAll(() => {
configure({throwSuggestions: false})
})

test('does not suggest for nested inline style', () => {
renderIntoDocument(
`<div data-testid="style"><style>.hsuHs{margin:auto}.wFncld{margin-top:3px;color:#9AA0A6;height:20px;width:20px}</style></div>`,
)

expect(() => screen.getByTestId('style')).not.toThrow()
})

test('does not suggest for inline script, style', () => {
renderIntoDocument(
`<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>`,
)

expect(() => screen.getByTestId('script')).not.toThrow()
expect(() => screen.getByTestId('style')).not.toThrow()
})

test('respects ignores', () => {
renderIntoDocument(`<my-thing>foo</my-thing>`)

expect(() =>
screen.queryByText('foo', {ignore: 'my-thing'}),
).not.toThrowError()
})

test('does not suggest when using getByRole', () => {
renderIntoDocument(`<button data-testid="foo">submit</button>`)

Expand Down
1 change: 1 addition & 0 deletions src/config.js
Expand Up @@ -33,6 +33,7 @@ let config = {
_disableExpensiveErrorDiagnostics: false,
}

export const DEFAULT_IGNORE_TAGS = 'script, style'
export function runWithExpensiveErrorDiagnosticsDisabled(callback) {
try {
config._disableExpensiveErrorDiagnostics = true
Expand Down
5 changes: 3 additions & 2 deletions src/queries/text.js
@@ -1,11 +1,12 @@
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {DEFAULT_IGNORE_TAGS} from '../config'
import {
fuzzyMatches,
matches,
makeNormalizer,
getNodeText,
buildQueries,
} from './all-utils'
import {wrapAllByQueryWithSuggestion} from '../query-helpers'

function queryAllByText(
container,
Expand All @@ -15,7 +16,7 @@ function queryAllByText(
exact = true,
collapseWhitespace,
trim,
ignore = 'script, style',
ignore = DEFAULT_IGNORE_TAGS,
normalizer,
} = {},
) {
Expand Down
4 changes: 2 additions & 2 deletions src/query-helpers.js
Expand Up @@ -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)
Expand All @@ -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 = [
Expand Down
6 changes: 4 additions & 2 deletions 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()

Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't mean it should be the default ignore but rather whatever ignore configuration was passed to the query. Is that possible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's not necessary. The only reason it would be suggesting getByText is if it got something lower down the list and afaict none of those would use ignore. And the only 2 above byText are label and role neither of which have an ignore option

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if you're already using text it won't suggest text again

return makeSuggestion('Text', textContent, {variant})
}

Expand Down