-
Notifications
You must be signed in to change notification settings - Fork 471
Add contains option to getByRole to allow filtering. #354
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
Comments
Hi @jsphstls, Thanks for bringing this up. What do you think of this: const someOption = getByRole((role, element) => role === 'option' && element.textContent === 'Some Option') That is supported today and gives you the most amount of flexibility. I'm not sure that I want to promote this use case to a simpler API, so I'm thinking that the current API should be enough for you. What does everyone watching think? |
Another idea within(getByLabelText('select something')).getByText('the option I want') |
Thanks folks, that's some speedy feedback 😄 I made a codesandbox: https://codesandbox.io/s/dom-testing-libraryissues354-zscq0 @kentcdodds I tried out your suggestion but the feedback for failed selections was cryptic:
Once I realized I needed a @alexkrolick I forgot about the magical power of Overall, there are ways to currently achieve this but each comes with their own fragility. What I am after is:
|
IDK, maybe there should be a way of combining queries. Someone did propose that before. This would work for combining _.intersection(queryAllByRole('option'), queryAllByText('whatever')) https://lodash.com/docs/4.17.15#intersection If we had a builtin version of that it would need to have a way to do something like the get/find/(All) variants |
Why not have an API like
|
I guess |
How about a more generic queryAllBy({ text: /some option/, role: 'option' }) And why not, the corresponding versions without |
Someone suggested something like that a while ago, but we weren't sure it was worth making a substantial API change to support this use case. #266 |
I might be falling into the habit of teasing too often but this would be covered by accessible name computation as well. The API would be as simple as |
That could be misinterpreted as querying within multiple parents. |
I'm really intrigued by this accessible name query @eps1lon :) |
I'm going to go ahead and close this in favor of the accessible name computation @eps1lon is working on. |
Hi, accessible name answers this ticket but what about having multiple select with the same options? I struggle to find the right selector for this. To give an example: you have 2 selectors "headerItems", "footerItems", both with options "A" and "B" => Its probably documented somewhere but I struggle to find out sorry. Edit: aaand I immediately found relevant answer 1 second after posting: https://testing-library.com/docs/dom-testing-library/api-within. So the definitive solution as of 2021 is:
and if you have only one select: Hope it may help lost googlers. |
Describe the feature you'd like:
I want to be able to select an option by the text that it contains, or improve documentation to make an existing strategy easier to find.
I use
react-testing-library
andcypress-testing-library
.Suggested implementation:
getByRole('option', { contains: 'Some Option' })
Describe alternatives you've considered:
Best:
getByText('Some Option', { selector: '[role=option]' })
Not best:
Teachability, Documentation, Adoption, Migration Strategy:
If a new option would not break anything. I realize that
getByRole
supports every role available. Some roles likenavigation
would be a bad case for this enhancement. Other roles such aslink
would be improved.The text was updated successfully, but these errors were encountered: