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

getByRole("option", { name: "..." }) does not match element contents #1274

Closed
TrevorRice opened this issue Nov 1, 2023 · 3 comments
Closed

Comments

@TrevorRice
Copy link

TrevorRice commented Nov 1, 2023

  • @testing-library/dom version: 8.13.0
  • @testing-library/jest-dom version: 5.16.4
  • @testing-library/react version: 13.2.0

Relevant code or config:

I have an Option component that simply wraps its children in a <div role="option">:

const Option = (props) => {
  return <div id="..." role="option" aria-selected="...">{props.children}</div>
}

// DOM
<div id="..." role="option" aria-selected="...">Dog</div>

What you did:

I've been trying to query for a specific option via getByRole in my unit tests:

getByRole("option", { name: "Dog" })

What happened:

I receive the following error:

TestingLibraryElementError: Unable to find an element with the role "option" and name "Dog"

Here are the available roles:
...
 --------------------------------------------------
      option:

      Name "":
      <div
        aria-selected="false"
        id=":r3l:"
        role="option"
      />

Despite the element's accessible name being "Dog" as demonstrated by inspecting the accessibility tree:

Accessibility panel of Chrome's development tools noting "dog" as the accessible name of the element

Accessibility panel of Firefox's development tools noting "dog" as the accessible name of the element

Reproduction:

Problem description:

getByRole does not seem to respect an element's accessible name when not explicitly defined via a method like aria-label or aria-labelledby.

Suggested solution:

I would expect the name option to respect an element's accessible name regardless of how it was constructed.

Please let me know if I'm totally overlooking something!

Copy link

github-actions bot commented Nov 1, 2023

Uh oh! @TrevorRice, the image you shared is missing helpful alt text. Check your issue body.

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

@TrevorRice
Copy link
Author

Apologies, this turned out to an issue with how we were using a Floating UI hook.

@okonet
Copy link

okonet commented Jan 27, 2025

@TrevorRice I think I'm facing a similar issue. Can you point me to the problem you had please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants