Skip to content

Querying by role with name on nested touchables return multiple elements #1152

Description

@AugustinLF

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions