question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

*ByLabelText() with aria-hidden elements

See original GitHub issue

Versions

  • @testing-library/jest-dom version: 5.8.0
  • @jest-environment-jsdom-sixteen: version 1.0.3

Relevant code or config:

const component = () => 
    <div>
        <label id="MyLabel" for="MyInput">My Label<span aria-hidden="true">*</span></label>
        <input id="MyInput" value="some value" />
    </div>

What you did:

I was testing a material-ui component, using the “required” property, which adds an asterisk to the label automatically.

What happened:

The *ByLabelText() methods do not find the label, because the asterisk is included. However, I thought with the aria-hidden attribute in there, it would exclude that part, just as screen readers do to locate and announce labels.

Reproduction:

Here’s a forked codesandbox example:

https://codesandbox.io/s/react-testing-library-demo-pxex9?file=/src/__tests__/hello.js

Problem description:

I’m not sure if this is expected, but I thought getByLabelText("My Label") would return a valid input. Instead, I have to use getByLabelText("My Label*"), because it finds the asterisk in there.

Suggested solution:

Honor aria-hidden elements by skipping them when locating labels, to be consistent with screen readers.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Jun 13, 2020

It looks like there’s nothing actionable from here so let’s close this 👍

1reaction
delca85commented, Jun 11, 2020

cc: @delca85. Didn’t you have similar experiences with aria-hidden?

react-testing-library worked as expected for me with the attribute aria-hidden set to true, it doesn’t find any role for the selected element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByLabelText - Testing Library
This will search for the label that matches the given TextMatch , then find the element associated with that label.
Read more >
How to filter getByLabelText query when found multiple ...
Use a manual query and target by the specified id attribute. test("case-sensitive To", () => { const { container } = render ...
Read more >
aria-labelledby - Accessibility - MDN Web Docs
The aria-labelledby property enables authors to reference other elements on the page to define an accessible name. This is useful when using ...
Read more >
queryByLabelText method - RenderResult class - rtl.react library
Returns null if no element is found. Use getByLabelText if a RTE is expected. Related: queryAllByLabelText. See: testing-library.com/docs/queries/bylabeltext/ ...
Read more >
Element with aria-hidden has no content in sequential focus ...
This rule checks that elements with an aria-hidden attribute do not contain elements that are part of the sequential focus navigation and focusable....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found