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

Recent danger button update breaks react testing library getRyRole #8374

Closed
finken2 opened this issue Apr 12, 2021 · 5 comments
Closed

Recent danger button update breaks react testing library getRyRole #8374

finken2 opened this issue Apr 12, 2021 · 5 comments

Comments

@finken2
Copy link
Contributor

finken2 commented Apr 12, 2021

In the most recent carbon react build (7.32.1), a span was added inside <Button> when it's a danger button. From the storybook:

<button tabindex="0" class="bx--btn bx--btn--danger" type="button" aria-describedby="danger-description-2">
<span id="danger-description-2" class="bx--visually-hidden">danger</span>
Button</button>

The proper way to use react-testing-library is to use getByRole('button', {name: 'blah'}); With this update, danger buttons are no longer found.
https://testing-library.com/docs/queries/byrole

One possible fix is adding a aria-label to the Button tag, but there's other possibilities.

@finken2
Copy link
Contributor Author

finken2 commented Apr 14, 2021

One temporary workaround is to use a regex for the name property. This isn't optimal though, b/c it requires the user to know the inner structure of the button and as we've seen, that can change.
getByRole('button', { name: /Remove$/ })
In this case, i need the $ to ensure i don't match other buttons that start with Remove. It's also saying that we expect the name to be the last value in the button contents, which it currently is, but... yeah, still better to fix this.

@joshblack
Copy link
Contributor

Hi @finken2! 👋 I believe #8222 added in the dangerDescription prop with the fallback of danger to close #8220.

I believe the original test could be updated to use the regex as you mentioned, or to include danger or provide a custom dangerDescription for the text to resolve correctly.

Let me know if I'm misunderstanding how RTL resolves what the text of something is, it's been a while since I've taken a look at how that works 👀

@finken2
Copy link
Contributor Author

finken2 commented Apr 14, 2021

@joshblack RTL allows you to specify "name" when using getByRole, which should go through the right assistive technology flow of looking at labeledby, aria-label, etc, eventually landing on text node in the button. Since we don't have any of the other labeling, it ends up looking in the body and doesn't match.
Here's that logic: https://www.w3.org/TR/accname-1.1/#mapping_additional_nd_te

I agree, we can work around it with the regex, but that's really error-prone and not how it should work. I'd rather add aria-label to the Button if we can.

@joshblack
Copy link
Contributor

@finken2 that's awesome, based on that it seems like doing getByRole('button', { name: 'danger Remove' }) should do the trick, and if the text needs to be configured then one can use the dangerDescription prop and include the value of that in the name.

When aria-label, aria-labelledby, etc come up I think we would run into the same problem with determining the label because the dangerDescription prop will be appended based on the intent on #8220.

@finken2
Copy link
Contributor Author

finken2 commented Apr 16, 2021

@joshblack yep, that does work. Good point about the appending w/ aria-label too. Once we put that "danger" description in there, we're probably stuck with something unintuitive for the test.
using "danger " seems reasonable, definitely better than the regex workaround.

Thanks for talking through it.

@finken2 finken2 closed this as completed Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants