Skip to content

getByLabelText does not match when id has a "." in the string. #12

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

Closed
ngbrown opened this issue Apr 9, 2018 · 4 comments
Closed

getByLabelText does not match when id has a "." in the string. #12

ngbrown opened this issue Apr 9, 2018 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@ngbrown
Copy link

ngbrown commented Apr 9, 2018

I had an element that looks like this:

<label for="activity.username">Username</label>
<input id="activity.username" />

and calling: getByLabelText('Username') throws because it won't find the with an id of activity.username. Having a period in the id is valid HTML.

The reason is that on this line:

https://github.com/kentcdodds/dom-testing-library/blob/4bf26d70eda829ca0470c79952f53f5d51ab423b/src/queries.js#L27

the CSS selector # needs to be escaped for a characters that are not valid CSS labels. In this case it is trying to run container.querySelector('#activity.username') but that needs to be escaped to container.querySelector('#activity\\.username'). (double slash to escape in javascript, so the actual string would only have one slash) (see MDN docs).

An alternative would be to use the [id='activity.shift'] selector, as that finds the element without escaping:

So the whole line would be:

return container.querySelector(`[id='${label.getAttribute('for')}']`)
@kentcdodds
Copy link
Member

Thanks for this @ngbrown! If you could please open a PR for this that would be fantastic. Please add a comment above that line pointing to this issue and briefly explaining why it's not simply using the '#' syntax 👍 Thank you so much for catching this! If you'd like, feel free to add a test that reproduces this bug without these changes as well to make sure we never break this in the future. Thanks again!

@kentcdodds kentcdodds added bug Something isn't working help wanted Extra attention is needed labels Apr 9, 2018
@timdeschryver
Copy link
Member

Is it OK if I take this?

@kentcdodds
Copy link
Member

Sure!

@alexkrolick
Copy link
Collaborator

#22 should have closed this it looks like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants