Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Returns a `TestInstance` with matching `role` or `accessibilityRole` prop.
> [!INFO]
> In order for `*ByRole` queries to match an element it needs to be considered an accessibility element:
>
> 1. `Text`, `TextInput` and `Switch` elements are these by default.
> 1. `Text`, `TextInput` and `Switch` elements are these by default. On Android, `Text` stays one even with `accessible={false}`, which React Native's `Text` sets there when it has no press handler. There `accessible` only controls focus, and TalkBack still reads the text.
> 2. `View` elements need an explicit [`accessible`](https://reactnative.dev/docs/accessibility#accessible) prop set to `true`
> 3. Some React Native composite components like `Pressable` & `TouchableOpacity` render host `View` element with `accessible` prop already set.

Expand Down
33 changes: 32 additions & 1 deletion src/helpers/__tests__/accessibility.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import React from 'react';
import { Image, Pressable, Switch, Text, TextInput, TouchableOpacity, View } from 'react-native';
import {
Image,
Platform,
Pressable,
Switch,
Text,
TextInput,
TouchableOpacity,
View,
} from 'react-native';

import { isHiddenFromAccessibility, isInaccessible, render, screen } from '../..';
import {
Expand Down Expand Up @@ -315,6 +324,10 @@ describe('isHiddenFromAccessibility', () => {
});

describe('isAccessibilityElement', () => {
afterEach(() => {
jest.restoreAllMocks();
});

test('matches View component properly', async () => {
await render(
<View>
Expand Down Expand Up @@ -358,6 +371,24 @@ describe('isAccessibilityElement', () => {
expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});

test('matches Text component regardless of accessible prop on Android', async () => {
jest.replaceProperty(Platform, 'OS', 'android');
await render(
<View>
<Text testID="default">Default</Text>
<Text testID="true" accessible>
True
</Text>
<Text testID="false" accessible={false}>
False
</Text>
</View>,
);
expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
expect(isAccessibilityElement(screen.getByTestId('false'))).toBeTruthy();
});

test('matches Switch component properly', async () => {
await render(
<View>
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/accessibility.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AccessibilityRole, AccessibilityState, AccessibilityValue, Role } from 'react-native';
import { StyleSheet } from 'react-native';
import { Platform, StyleSheet } from 'react-native';
import type { TestInstance } from 'test-renderer';

import { getContainerInstance, getInstanceSiblings, isTestInstance } from './component-tree';
Expand Down Expand Up @@ -94,6 +94,14 @@ export function isAccessibilityElement(instance: TestInstance | null): boolean {
return true;
}

// On Android, `accessible` maps to native `focusable`, which does not stop TalkBack from reading
// a `Text`. React Native's `Text` sets `accessible={false}` on Android when it has no press handler.
// See: https://reactnative.dev/docs/accessibility#accessible
// See: https://github.com/facebook/react-native/blob/v0.79.7/packages/react-native/Libraries/Text/Text.js#L236-L243
if (Platform.OS === 'android' && isHostText(instance)) {
return true;
}

if (instance.props.accessible !== undefined) {
return instance.props.accessible;
}
Expand Down
43 changes: 43 additions & 0 deletions src/queries/__tests__/role.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import {
Button as RNButton,
Image,
Platform,
Pressable,
Switch,
Text,
Expand Down Expand Up @@ -981,6 +982,48 @@ describe('matches only accessible elements', () => {
});
});

describe('matches Text by its platform accessibility', () => {
afterEach(() => {
jest.restoreAllMocks();
});

// React Native's Text passes `accessible={false}` to its host on Android when it has no press handler.
test('matches Text with accessible={false} on Android', async () => {
jest.replaceProperty(Platform, 'OS', 'android');
await render(
<Text accessibilityRole="header" accessible={false}>
Title
</Text>,
);
expect(screen.getByRole('header', { name: 'Title' })).toBeOnTheScreen();
expect(screen.getByRole('header')).toBeOnTheScreen();
});

test('ignores hidden Text with accessible={false} on Android', async () => {
jest.replaceProperty(Platform, 'OS', 'android');
await render(
<Text
accessibilityRole="header"
accessible={false}
importantForAccessibility="no-hide-descendants"
>
Title
</Text>,
);
expect(screen.queryByRole('header', { name: 'Title' })).toBeNull();
});

test('ignores Text with accessible={false} on iOS', async () => {
jest.replaceProperty(Platform, 'OS', 'ios');
await render(
<Text accessibilityRole="header" accessible={false}>
Title
</Text>,
);
expect(screen.queryByRole('header', { name: 'Title' })).toBeNull();
});
});

test('error message renders the element tree, preserving only helpful props', async () => {
await render(<View accessibilityRole="button" key="3" />);

Expand Down
2 changes: 1 addition & 1 deletion website/docs/14.x/docs/api/queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Returns a `TestInstance` with matching `role` or `accessibilityRole` prop.
:::info
In order for `*ByRole` queries to match an element it needs to be considered an accessibility element:

1. `Text`, `TextInput` and `Switch` elements are these by default.
1. `Text`, `TextInput` and `Switch` elements are these by default. On Android, `Text` stays one even with `accessible={false}`, which React Native's `Text` sets there when it has no press handler. There `accessible` only controls focus, and TalkBack still reads the text.
2. `View` elements need an explicit [`accessible`](https://reactnative.dev/docs/accessibility#accessible) prop set to `true`
3. Some React Native composite components like `Pressable` & `TouchableOpacity` render host `View` element with `accessible` prop already set.

Expand Down