Skip to content

fix: match Android Text in *ByRole queries when accessible is false - #1966

Open
Priyanshu0007 wants to merge 1 commit into
callstack:mainfrom
Priyanshu0007:fix/android-text-by-role
Open

Priyanshu0007 wants to merge 1 commit into
callstack:mainfrom
Priyanshu0007:fix/android-text-by-role

Conversation

@Priyanshu0007

Copy link
Copy Markdown

Summary

Fixes #1963.

On Android, React Native's Text passes accessible={false} to its host when it has no onPress or onLongPress (Text.js). On iOS it passes true. isAccessibilityElement returned an explicit accessible prop as is, and *ByRole checks it before anything else. So on Android, no Text without a press handler was found by its role: getByRole('header') missed every heading and getByRole('text') every text.

On Android, accessible maps to native focusable, not to whether TalkBack reads the element (RN docs). A non-focusable Text is still read. This PR makes isAccessibilityElement treat a host Text as an accessibility element on Android whatever its accessible prop is.

  • iOS is unchanged: accessible={false} still removes a Text from *ByRole, as it does for VoiceOver.
  • Hidden Text is still excluded on Android: importantForAccessibility="no-hide-descendants", display: 'none', aria-hidden and modal siblings go through isHiddenFromAccessibility as before.
  • At the host level, React Native's default accessible={false} and one the user wrote can't be told apart. Natively they are the same (focusable=false), so both are handled the same way.
  • The bug didn't show with React Native's Jest preset because its Text mock doesn't set accessible. It showed after jest.unmock('react-native/Libraries/Text/Text'), or under runners that render React Native's own Text.
  • TextInput and Switch also map accessible to focusable on Android. They are left out of this PR to keep it focused.

The *ByRole docs note now says that Android Text stays an accessibility element with accessible={false}. docs/ was regenerated.

Test plan

  • Reproduced the issue on main (564a842) with React Native 0.88.0-rc.1, the repo's version: a test that unmocks Text and runs queryByRole('header', { name: 'Title' }) passed under an iOS Jest config (accessible=true) and failed under an Android one (haste.defaultPlatform: 'android', accessible=false). With this change it passes on both.
  • New tests, using jest.replaceProperty(Platform, 'OS', ...):
    • isAccessibilityElement: on Android, a host Text is an accessibility element with accessible undefined, true and false.
    • *ByRole: on Android, Text with accessible={false} is found by role, with and without name.
    • *ByRole: on Android, Text with importantForAccessibility="no-hide-descendants" is still not found.
    • *ByRole: on iOS, Text with accessible={false} is still not found.
  • The two Android tests fail without the fix in src/helpers/accessibility.ts and pass with it.
  • yarn validate passes: typecheck, 87 suites and 958 tests, lint, format.
  • Gap: the full suite runs on the preset's default iOS platform only. The end-to-end Android check used a temporary Jest config that is not part of this PR.

React Native's Text passes `accessible={false}` to its host on Android
when it has no press handler. On Android `accessible` maps to native
`focusable`, which does not stop TalkBack from reading the text, but
`isAccessibilityElement` returned the explicit prop, so `*ByRole` skipped
every such Text (e.g. `getByRole('header')`).

Treat host Text as an accessibility element on Android regardless of
`accessible`. iOS keeps honouring `accessible={false}`, and hidden Text
(`importantForAccessibility="no-hide-descendants"`, `display: none`, ...)
is still excluded.

Fixes callstack#1963

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

*ByRole finds no Text on Android: React Native's own Text passes accessible={false} there

1 participant