-
Notifications
You must be signed in to change notification settings - Fork 470
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 fails if filtering by accessible name with in it #904
Comments
I'm using version "@testing-library/react": "^11.2.5",. My code works using in this way. <label
class="sc-gsTCUz kHIuWv"
for="senderAddress.postcode"
>
post code
</label>
<input
class="sc-dlfnbm sc-hKgILt fxqgV code"
id="senderAddress.postcode"
name="senderAddress.postcode"
value="sender postcode"
variant=""
/>
<small
class="sc-eCssSg dEHZqf"
/>
</div> and here is my testing code expect(screen.getByRole("textbox", { name: /post code/i })).toHaveValue(
senderAddress.postcode
); |
I plan to work on this issue today. It looks like the issue is related (if not similar) to #890 |
…and `ByRole` queries - first step to fix testing-library#904 - not similar behaviour of `ByLabelText` and `ByRole, {name: ''}` with nbsp at DOM
Closing since it's unclear if we really want this. The normalizer would also collapse newlines and it should be obvious in tests if you really want newlines in the name. Note that you can always do
If you have a strong argument for collapsing whitespace then please file an issue against https://github.com/w3c/accname though w3c/accname#95 might already be similar. There may be an argument for replacing |
For future reference, the ARIA spec and soon the WPT tests align on ASCII white space which does not include non-breaking white space. So it was correct to close this. NBSP should never be converted or collapsed to “ “ in the label context. |
I understand the decision not to update |
@testing-library/dom
version: 7.28.1 (viareact-testing-library
11.2.5)Relevant code or config:
DOM:
Test code:
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.Succeds on getByLabelText
If searching for the name with a regular space in
getByLabelText
, the test will succeed.Succeeds on getByRole with
\xa0
instead of a regular spaceIf searching for the accessible name with a JS representation of
 
,\xa0
ingetByRole
, the test will succeed.Fails on getByLabelText with
\xa0
instead of a regular spaceIf searching for the name with a JS representation of
 
,\xa0
, ingetByLabelText
the test will fail.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:
Suggested solution:
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. ✨
The text was updated successfully, but these errors were encountered: