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 6 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
2 changes: 1 addition & 1 deletion src/queries/text.js
@@ -1,11 +1,11 @@
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
import {
fuzzyMatches,
matches,
makeNormalizer,
getNodeText,
buildQueries,
} from './all-utils'
import {wrapAllByQueryWithSuggestion} from '../query-helpers'

function queryAllByText(
container,
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
5 changes: 3 additions & 2 deletions src/suggestions.js
@@ -1,6 +1,7 @@
import {computeAccessibleName} from 'dom-accessibility-api'
import {getRoles} from './role-helpers'
import {getDefaultNormalizer} from './matches'
import {getNodeText} from './get-node-text'

const normalize = getDefaultNormalizer()

Expand Down Expand Up @@ -57,8 +58,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('script, style')) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use the same ignores that are used by the query options? I think that would be best

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

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

Expand Down