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

Extra space added around embedded expressions in JSX when querying by role + name #591

Closed
wild-lotus opened this issue May 28, 2020 · 8 comments · Fixed by eps1lon/dom-accessibility-api#273 or #606
Labels

Comments

@wild-lotus
Copy link

wild-lotus commented May 28, 2020

  • @testing-library/dom version: 7.5.9
  • Testing Framework and version: jest 25.5.4
  • DOM Environment: jsdom

Relevant code or config:

export default ({ name }) => <h1>Hello {name}!</h1>;
screen.getByRole("heading", { name: /Hello Jill!/i })

What you did:

Tried to query an element by role + name

What happened:

The existing element cannot be found.
If the name is in JSX with an embedded expression, potentially inexistent spaces are added around it:

Unable to find an accessible element with the role "heading" and name `/Hello Jill!/i`

Here are the accessible roles:

  heading:

  Name "Hello Jill !":
  <h1 />

Note the extra space here:

 Name "Hello Jill !":
                 ^

Reproduction:

https://codesandbox.io/s/react-testing-library-demo-09wqi

Problem description:

I cannot create accurate queries in those cases (embedded expressions without surrounding space)

@pranjaljately
Copy link

For some reason, I am having issues running tests in your codesandbox but I think you will find that the cause of the issue is in your hello.js component.
export default ({ name }) => <h1>Hello {name} !</h1>; <-- Notice the extra space you have between {name} and !. Removing that space should fix your issue 👍

@wild-lotus
Copy link
Author

wild-lotus commented Jun 4, 2020

Hello @pranjaljately! Thank you for your comment! I updated the reproduction case, the space in the hello.js component was unintentional! New one --> https://codesandbox.io/s/react-testing-library-demo-09wqi
Still, there seems to be a problem with Codesandbox at present, even the original template (https://codesandbox.io/s/5z6x4r7n0p) is not working now. I hope that it will get fixed soon, so that we can easily reproduce the issue!

@pranjaljately
Copy link

pranjaljately commented Jun 5, 2020

Thanks, @CarlosGines. I have seen the updated CS and AFAIK the test should not be failing.

Any reason why this could be? @kentcdodds
Happy to try and implement a fix if needed...

@kentcdodds
Copy link
Member

Hi friends,

Thanks for the issue. I'm guessing that's a bug in the calculation of the accessible name. What do you think @eps1lon?

@eps1lon
Copy link
Member

eps1lon commented Jun 5, 2020

The expected behavior makes more sense, yes.

This is caused by react adding 3 text nodes instead of one and accname being too vague about whitespace in certain steps.

I'll see how we can reconcile these issues.

@eps1lon
Copy link
Member

eps1lon commented Jun 5, 2020

Didn't know I github auto-closes issues across repos. #606 will actually close.

Otherwise bump dom-accessibility-api in your lockfile.

@eps1lon eps1lon reopened this Jun 5, 2020
@wild-lotus
Copy link
Author

Awesome! Such a quick reply and fix! Thank you folks!
I did check the CodeSandbox of the PR and it does work as expected ^__^

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 7.10.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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