question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Querying by role with name on nested touchables return multiple elements

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjastrzebskicommented, Sep 29, 2022

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:

<View accessiblityLabel="hello">
  <View accessiblityLabel="hello">
    <Text>Hello</Text>
  </View>
</View>

or *ByA11yStates with following setup:

<View accessiblityStates={{ disabled: true }}>
  <View accessiblityStates={{ disabled: true }}>
    <Text>Hello</Text>
  </View>
</View>

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?

0reactions
mdjastrzebskicommented, Oct 17, 2022

Closing in favour of #1180 with more clear scope.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found