Skip to content

*ByLabelText() with aria-hidden elements #618

@JeremyGrieshop

Description

@JeremyGrieshop

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions