diff --git a/docs/api/queries.md b/docs/api/queries.md index 7388692a0..0e34bf386 100644 --- a/docs/api/queries.md +++ b/docs/api/queries.md @@ -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. diff --git a/src/helpers/__tests__/accessibility.test.tsx b/src/helpers/__tests__/accessibility.test.tsx index 1c0cda21f..9e764d5c7 100644 --- a/src/helpers/__tests__/accessibility.test.tsx +++ b/src/helpers/__tests__/accessibility.test.tsx @@ -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 { @@ -315,6 +324,10 @@ describe('isHiddenFromAccessibility', () => { }); describe('isAccessibilityElement', () => { + afterEach(() => { + jest.restoreAllMocks(); + }); + test('matches View component properly', async () => { await render( @@ -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( + + Default + + True + + + False + + , + ); + 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( diff --git a/src/helpers/accessibility.ts b/src/helpers/accessibility.ts index a441a99e6..ec8e6b45f 100644 --- a/src/helpers/accessibility.ts +++ b/src/helpers/accessibility.ts @@ -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'; @@ -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; } diff --git a/src/queries/__tests__/role.test.tsx b/src/queries/__tests__/role.test.tsx index c394b4b08..c557c2319 100644 --- a/src/queries/__tests__/role.test.tsx +++ b/src/queries/__tests__/role.test.tsx @@ -2,6 +2,7 @@ import * as React from 'react'; import { Button as RNButton, Image, + Platform, Pressable, Switch, Text, @@ -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( + + Title + , + ); + 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( + + Title + , + ); + expect(screen.queryByRole('header', { name: 'Title' })).toBeNull(); + }); + + test('ignores Text with accessible={false} on iOS', async () => { + jest.replaceProperty(Platform, 'OS', 'ios'); + await render( + + Title + , + ); + expect(screen.queryByRole('header', { name: 'Title' })).toBeNull(); + }); +}); + test('error message renders the element tree, preserving only helpful props', async () => { await render(); diff --git a/website/docs/14.x/docs/api/queries.mdx b/website/docs/14.x/docs/api/queries.mdx index c9e50977a..89a5bc375 100644 --- a/website/docs/14.x/docs/api/queries.mdx +++ b/website/docs/14.x/docs/api/queries.mdx @@ -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.