Querying by role with name on nested touchables return multiple elements
See original GitHub issueDescribe the bug
We have a problem when querying nested touchables, see the example for more details:
describe('nested touchables', () => {
it('conflicting elements', () => {
const { getByRole } = render(
<Pressable accessibilityRole="button">
<Text>Some other text</Text>
<Pressable accessibilityRole="button">
<Text>Save</Text>
</Pressable>
</Pressable>
);
// Found multiple elements with accessibilityRole: button
// Here I'm not sure what we should do. This is not an accessible pattern.
// At least we should suggest a better error (might be just a warning/error on nested touchables)
expect(getByRole('button', { name: 'Save' })).toBeTruthy();
});
it('with accessible={false}', () => {
const { getByRole } = render(
<Pressable accessibilityRole="button" accessible={false}>
<Text>Some other text</Text>
<Pressable accessibilityRole="button">
<Text>Save</Text>
</Pressable>
</Pressable>
);
// Found multiple elements with accessibilityRole: button
// Here it should work and only getting the lower Pressable.
// `accessible={false}` means it won't be read to the screen-reader.
// This is a common pattern I've seen where you make a whole area implicitly touchable for better UX
// but add a nested explicit button for better discoverability (and accessibility)
expect(getByRole('button', { name: 'Save' })).toBeTruthy();
});
});
Versions
RNTL 11.2
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Found multiple elements error in React Testing Library
Just a quick tip, if you have multiple matching elements, you can query like this: HTML: <a href="/my-element">My element</a> <a ...
Read more >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 >getByText()/queryByText() (or some new query) should search ...
I think adding an example with simple nested content eg: ... approach that will allow querying elements with roles by their accessible name....
Read more >Making sure you're using the correct query - Tim Deschryver
This can be used to query every element that is exposed in the accessibility tree. With the name option you can filter the...
Read more >React Testing Library => within nested queries
React Testing Library => within nested queries. Wow, React Testing Library has a within helper to get nested searches on the dom.
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
I think that this might actually be a more general aspect of out tree walking code that uses
findAll
from RTR.I imagine that it also should manifest itself for any scenario with matching nested elements, like:
*ByLabel
with following setup:or
*ByA11yStates
with following setup:Essentially for any case when component structure allows for nesting elements matching query predicate, it currently will result in matching both the inner and outer one. This might be more visible in certain scenarios like
*ByRole
queries, but probably affects all nesting scenarios.It seems that RTL/DTL should behave the same as our code because their queries rely on using
querySelectorAll
which seems to have the same behavior of returning both outer and inner for nested elements.I think we should first investigate how RTL would behave in a similar case, and then decide if this is a bug or a feature. @AugustinLF wdyt?
Closing in favour of #1180 with more clear scope.