Skip to content

*ByRole queries with name option does not find elements based on text content in v14 #1923

Description

@kovbe21

Describe the bug

Repro repository: https://github.com/kovbe21/jest-reproductions

Using expo v55

According to the documents here (https://oss.callstack.com/react-native-testing-library/docs/api/queries#by-role-options), the name option search for the accessibility label or text content.

In v13, text content was working fine, but in v14, it does not anymore. Accessibility label based query-ing works though.

When query-ing with *getByRole like screen.getByRole('button', { name: 'rn-button-title'}), tests fail with error:

Unable to find an element with role: button, name: rn-button-title

    <View
      accessibilityLabel="rn-button-accessibility-label"
      accessibilityRole="button"
      accessible={true}
    >
      <View>
        <Text>
          rn-button-title
        </Text>
      </View>
    </View>

Expected behavior

When query-ing with *getByRole like screen.getByRole('button', { name: 'rn-button-title'}), tests should find the queried element.

Steps to Reproduce

In this example repository you can find the reproduction: https://github.com/kovbe21/jest-reproductions

But it is east to reproduce with a basic test:

  test('getByRole query succeeds when accessibility label is queried in the name param for RN Button', async () => {
    await render(<Button title="rn-button-title" accessibilityLabel="rn-button-accessibility-label"/>);

    simpleScreen.getByRole('button', { name: 'rn-button-title'});
  });

or

  test('getByRole query fails when text content is queried in the name param', async () => {
    await render(<TouchableOpacity accessibilityRole="button" accessibilityLabel="opacity-accessibility-label"><Text>opacity-text</Text></TouchableOpacity>);

    simpleScreen.getByRole('button', { name: 'opacity-text'});
  });

Versions

npmPackages:
@testing-library/react-native: ^14.0.1 => 14.0.1
react: 19.2.0 => 19.2.0
react-native: 0.83.6 => 0.83.6
test-renderer: ^1.2.0 => 1.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions