getByRole should accept a second argument to refine query as in react-testing-library
See original GitHub issueDescribe the Feature
Should be able to query a Button element as follows:
getByRole('button', {name: /submit/i})
At the present time, this is impossible so I have to add redundant accessiblityLabel
props, for example:
<Button accessibilityLabel="submit-button">Submit</Button>
And then, use queryByA11yLabel('submit-button')
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (9 by maintainers)
Top Results From Across the Web
ByRole | Testing Library
It can be used to query a specific element if multiple elements with the same role are present on the rendered content.
Read more >ByRole Queries in React Native Testing Library - YouTube
Big thanks to my employer, Test Double, for supporting my streaming! Find out more about how we can help you on your next...
Read more >getByRole method - RenderResult class - rtl.react library
Returns a single element with the given role value, defaulting to an exact match. Throws if no element is found. Use queryByRole if...
Read more >How to get second item in getByRole in React Testing Library ...
Use screen.getAllByRole('button')[1]. https://testing-library.com/docs/dom-testing-library/cheatsheet/#queries.
Read more >Testing - Recoil
It can be helpful to test Recoil state when testing a component. You can compare the new state against expected values using this...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A thing you’ll need to consider is that if you match by name, you should also match by label text, since you should definitely match
<Button accessibilityLabel="exit" onPress={...}><Icon type="door" /></Button>
withgetByRole('button', {name: 'exit'})
.Not necessarily relevant but which might be interesting to you, we’ve reimplemented that in userland in our codebase. As you can see the reasoning is the same (first find the element with the role, then try to match the name in its children). Since it does work for us (and does seem to work as well as web), I guess your implementation should do the job.
@thymikee Issued a draft PR. Appreciate your feedback