Skip to content

getByRole fails if filtering by accessible name with   in it #904

Closed
@jegtnes

Description

@jegtnes
  • @testing-library/dom version: 7.28.1 (via react-testing-library 11.2.5)
  • Testing Framework and version: Jest 26.6.3
  • DOM Environment: JSDom 16.2.1

Relevant code or config:

DOM:

<label for="testy">Fascinating&nbsp;input</label>
<input id="testy" name="ice-cream-flavour" />

Test code:

expect(
  screen.getByRole("textbox", { name: "Fascinating input" })
).toBeInTheDocument();

What you did:

Attempted to find an element by role with a non-breaking space in the accessible name, by searching for the accessible name without having to encode a non-breaking space in JavaScript where the space goes (\xa0).

What happened:

Fails on getByRole

If searching for the accessible name with a regular space in getByRole, the test will fail.

getByRole("textbox", { name: "Fascinating input" })

Succeds on getByLabelText

If searching for the name with a regular space in getByLabelText, the test will succeed.

getByLabelText("Fascinating input"))

Succeeds on getByRole with \xa0 instead of a regular space

If searching for the accessible name with a JS representation of &nbsp, \xa0 in getByRole, the test will succeed.

getByRole("textbox", { name: "Fascinating\xa0input" })

Fails on getByLabelText with \xa0 instead of a regular space

If searching for the name with a JS representation of &nbsp, \xa0, in getByLabelText the test will fail.

getByLabelText("Fascinating\xa0input")

Reproduction:

Codesandbox reproduction

Problem description:

This seems incongrugous with testing-library's philosophy of "testing what the user sees", and is additionally inconsistent with other methods. If there's a reason we must specifically encode a breaking space to test for an element with a breaking space in the accessible name, it might be a good idea to be clearer about this in the error messaging. I was converting an existing code base from Enzyme to react-testing-library, and seeing an error like this understandably confused me a bit:

Fails on getByRole. TestingLibraryElementError: Unable to find an accessible element with the role "textbox" and name "Fascinating input". Here are the accessible roles: textbox: Name "Fascinating input": <input />

Suggested solution:

  • Make the text matching/parsing consistent between different methods
  • If the above isn't feasible, make this error clearer in the error output by highlighting the breaking space, or lack thereof

As an aside

Since the issue template makes me seem extremely clinical, I just wanted to highlight I'm not an ungrateful jerk (I hope) and would like to thank all the contributors for a wonderful piece of work! The philosophy behind testing-library makes a lot of sense and I'm really enjoying working with it. ✨

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions