Skip to content

Commit 9494fdc

Browse files
authored
fix(suggestions): only warn about inaccessible elements when actually showing the suggestion (#827)
1 parent 9b688f8 commit 9494fdc

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/__tests__/suggestions.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,16 +551,44 @@ test('should get the first label with aria-labelledby contains multiple ids', ()
551551
})
552552
})
553553

554-
test('should suggest hidden option if element is not in the accessibilty tree', () => {
554+
test('should not suggest or warn about hidden element when suggested query is already used.', () => {
555555
console.warn.mockImplementation(() => {})
556556

557-
const {container} = renderIntoDocument(`
557+
renderIntoDocument(`
558558
<input type="text" aria-hidden=true />
559559
`)
560560

561-
expect(
562-
getSuggestedQuery(container.querySelector('input'), 'get', 'role'),
563-
).toMatchObject({
561+
expect(() => screen.getByRole('textbox', {hidden: true})).not.toThrowError()
562+
expect(console.warn).not.toHaveBeenCalled()
563+
})
564+
test('should suggest and warn about if element is not in the accessibility tree', () => {
565+
console.warn.mockImplementation(() => {})
566+
567+
renderIntoDocument(`
568+
<input type="text" data-testid="foo" aria-hidden=true />
569+
`)
570+
571+
expect(() => screen.getByTestId('foo', {hidden: true})).toThrowError(
572+
/getByRole\('textbox', \{ hidden: true \}\)/,
573+
)
574+
expect(console.warn).toHaveBeenCalledWith(
575+
expect.stringContaining(`Element is inaccessible.`),
576+
)
577+
})
578+
579+
test('should suggest hidden option if element is not in the accessibility tree', () => {
580+
console.warn.mockImplementation(() => {})
581+
582+
const {container} = renderIntoDocument(`
583+
<input type="text" data-testid="foo" aria-hidden=true />
584+
`)
585+
586+
const suggestion = getSuggestedQuery(
587+
container.querySelector('input'),
588+
'get',
589+
'role',
590+
)
591+
expect(suggestion).toMatchObject({
564592
queryName: 'Role',
565593
queryMethod: 'getByRole',
566594
queryArgs: ['textbox', {hidden: true}],
@@ -569,6 +597,7 @@ test('should suggest hidden option if element is not in the accessibilty tree',
569597
If you are using the aria-hidden prop, make sure this is the right choice for your case.
570598
`,
571599
})
600+
suggestion.toString()
572601

573602
expect(console.warn.mock.calls).toMatchInlineSnapshot(`
574603
Array [

src/suggestions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function makeSuggestion(queryName, element, content, {variant, name}) {
3333
warning = `Element is inaccessible. This means that the element and all its children are invisible to screen readers.
3434
If you are using the aria-hidden prop, make sure this is the right choice for your case.
3535
`
36-
console.warn(warning)
3736
}
3837
if (Object.keys(queryOptions).length > 0) {
3938
queryArgs.push(queryOptions)
@@ -48,6 +47,9 @@ function makeSuggestion(queryName, element, content, {variant, name}) {
4847
variant,
4948
warning,
5049
toString() {
50+
if (warning) {
51+
console.warn(warning)
52+
}
5153
let [text, options] = queryArgs
5254

5355
text = typeof text === 'string' ? `'${text}'` : text

0 commit comments

Comments
 (0)