From 63335e59136ca92729aa8cad702dd26cb4bfc7c5 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Fri, 15 Aug 2025 09:35:44 -0700 Subject: [PATCH] Refactor accessibility tests and add proper Role types (#53222) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/53222 # Changelog: [Internal] - This factors out bits of the previously existing "Text.role" prop testing into the general accessibility props tests, so those can be reused, also making sure that we use correct types for both `.role` prop (`Role` type) and `.accessibilityRole prop (`AccessibilityRole`, correspondingly). Note that the test suite for the `role` prop is a separate one, as `role` is only defined on the native side for certain component types (including `Text`), but not all. Also, in the existing suite we weren't really testing `role`, but instead running the same test twice for `accessibilityRole`, that is corrected as well. Reviewed By: andrewdacenko Differential Revision: D80084731 fbshipit-source-id: 545f01c00e9ea5ca53f664888b9bb7b24ded315e --- .../Libraries/Text/__tests__/Text-itest.js | 115 +------- .../utilities/accessibilityPropsSuite.js | 251 ++++++++++++------ 2 files changed, 178 insertions(+), 188 deletions(-) diff --git a/packages/react-native/Libraries/Text/__tests__/Text-itest.js b/packages/react-native/Libraries/Text/__tests__/Text-itest.js index 45891a5454d..2a05b002bb3 100644 --- a/packages/react-native/Libraries/Text/__tests__/Text-itest.js +++ b/packages/react-native/Libraries/Text/__tests__/Text-itest.js @@ -11,7 +11,6 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; -import type {Role} from '../../Components/View/ViewAccessibility'; import type {AccessibilityProps, HostInstance} from 'react-native'; import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance'; @@ -19,7 +18,9 @@ import * as Fantom from '@react-native/fantom'; import * as React from 'react'; import {createRef} from 'react'; import {Text} from 'react-native'; -import accessibilityPropsSuite from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; +import accessibilityPropsSuite, { + rolePropSuite, +} from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite'; import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText'; @@ -274,115 +275,6 @@ describe('', () => { }); }); - describe('role', () => { - it(`has none by default`, () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - root.render({TEST_TEXT}); - }); - - expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual( - {TEST_TEXT}, - ); - }); - - it(`maps invalid values to 'none'`, () => { - const root = Fantom.createRoot(); - - Fantom.runTask(() => { - // $FlowExpectedError[incompatible-type] - root.render({TEST_TEXT}); - }); - - expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual( - {TEST_TEXT}, - ); - }); - - it(`propagates correctly all possible values`, () => { - const root = Fantom.createRoot(); - ( - [ - 'alert', - 'alertdialog', - 'application', - 'article', - 'banner', - 'button', - 'cell', - 'checkbox', - 'columnheader', - 'combobox', - 'complementary', - 'contentinfo', - 'definition', - 'dialog', - 'directory', - 'document', - 'feed', - 'figure', - 'form', - 'grid', - 'group', - 'heading', - 'img', - 'link', - 'list', - 'listitem', - 'log', - 'main', - 'marquee', - 'math', - 'menu', - 'menubar', - 'menuitem', - 'meter', - 'navigation', - 'none', - 'note', - 'option', - 'presentation', - 'progressbar', - 'radio', - 'radiogroup', - 'region', - 'row', - 'rowgroup', - 'rowheader', - 'scrollbar', - 'searchbox', - 'separator', - 'slider', - 'spinbutton', - 'status', - 'summary', - 'switch', - 'tab', - 'table', - 'tablist', - 'tabpanel', - 'term', - 'timer', - 'toolbar', - 'tooltip', - 'tree', - 'treegrid', - 'treeitem', - 'treeitem', - ] as Array - ).forEach(propVal => { - Fantom.runTask(() => { - root.render({TEST_TEXT}); - }); - - expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual( - {TEST_TEXT}, - ); - }); - }); - }); - describe('selectable', () => { it(`can be set to "true"`, () => { const root = Fantom.createRoot(); @@ -530,4 +422,5 @@ describe('', () => { return {TEST_TEXT}; } accessibilityPropsSuite(ComponentWithAccessibilityProps, false); + rolePropSuite(ComponentWithAccessibilityProps); }); diff --git a/packages/react-native/src/private/__tests__/utilities/accessibilityPropsSuite.js b/packages/react-native/src/private/__tests__/utilities/accessibilityPropsSuite.js index e8c45b592bc..e28d6acf2c8 100644 --- a/packages/react-native/src/private/__tests__/utilities/accessibilityPropsSuite.js +++ b/packages/react-native/src/private/__tests__/utilities/accessibilityPropsSuite.js @@ -8,39 +8,177 @@ * @format */ +import type {AccessibilityRole, Role} from '../../../..'; import type {Root} from '@react-native/fantom'; import type {AccessibilityProps} from 'react-native'; import * as Fantom from '@react-native/fantom'; import * as React from 'react'; +const ACCESIBILITY_ROLE_VALUES: $ReadOnlyArray = [ + 'none', + 'button', + 'link', + 'search', + 'image', + 'keyboardkey', + 'text', + 'adjustable', + 'imagebutton', + 'header', + 'summary', + 'alert', + 'checkbox', + 'combobox', + 'menu', + 'menubar', + 'menuitem', + 'progressbar', + 'radio', + 'radiogroup', + 'scrollbar', + 'spinbutton', + 'switch', + 'tab', + 'tablist', + 'timer', + 'toolbar', +]; + +const ROLE_VALUES: $ReadOnlyArray = [ + 'alert', + 'alertdialog', + 'application', + 'article', + 'banner', + 'button', + 'cell', + 'checkbox', + 'columnheader', + 'combobox', + 'complementary', + 'contentinfo', + 'definition', + 'dialog', + 'directory', + 'document', + 'feed', + 'figure', + 'form', + 'grid', + 'group', + 'heading', + 'img', + 'link', + 'list', + 'listitem', + 'log', + 'main', + 'marquee', + 'math', + 'menu', + 'menubar', + 'menuitem', + 'meter', + 'navigation', + 'none', + 'note', + 'option', + 'presentation', + 'progressbar', + 'radio', + 'radiogroup', + 'region', + 'row', + 'rowgroup', + 'rowheader', + 'scrollbar', + 'searchbox', + 'separator', + 'slider', + 'spinbutton', + 'status', + 'summary', + 'switch', + 'tab', + 'table', + 'tablist', + 'tabpanel', + 'term', + 'timer', + 'toolbar', + 'tooltip', + 'tree', + 'treegrid', + 'treeitem', + 'treeitem', +]; + +let root: Root; + +function getAccessibilityProp( + content: React.MixedElement, + name: $Keys, +) { + Fantom.runTask(() => { + root.render(content); + }); + return root.getRenderedOutput({props: [name]}).toJSONObject().props[name]; +} + +function getAccessibilityProps( + content: React.MixedElement, + names: $ReadOnlyArray<$Keys>, +) { + Fantom.runTask(() => { + root.render(content); + }); + const props = root.getRenderedOutput({props: names}).toJSONObject().props; + return {...props}; +} + +export function rolePropSuite( + Component: component(...AccessibilityProps), +): void { + describe('role', () => { + beforeEach(() => { + root = Fantom.createRoot(); + }); + + afterEach(() => { + root.destroy(); + }); + + it(`'role' has none by default`, () => { + expect(getAccessibilityProps(, ['role'])).toEqual({}); + }); + + it(`'role' maps invalid values to 'none'`, () => { + expect( + getAccessibilityProps( + // $FlowExpectedError[incompatible-type] + , + ['role'], + ), + ).toEqual({['role']: 'none'}); + }); + + describe(`'role' propagation`, () => { + ROLE_VALUES.forEach(role => { + it(`can be set to ${role}`, () => { + expect( + getAccessibilityProp(, 'role'), + ).toEqual(role); + }); + }); + }); + }); +} + export default function accessibilityPropsSuite( Component: component(...AccessibilityProps), accessibleByDefault: boolean = true, ): void { - let root: Root; - - function getAccessibilityProp( - content: React.MixedElement, - name: $Keys, - ) { - Fantom.runTask(() => { - root.render(content); - }); - return root.getRenderedOutput({props: [name]}).toJSONObject().props[name]; - } - - function getAccessibilityProps( - content: React.MixedElement, - names: $ReadOnlyArray<$Keys>, - ) { - Fantom.runTask(() => { - root.render(content); - }); - const props = root.getRenderedOutput({props: names}).toJSONObject().props; - return {...props}; - } - describe('accessibility', () => { beforeEach(() => { root = Fantom.createRoot(); @@ -106,39 +244,9 @@ export default function accessibilityPropsSuite( }); }); - (['role', 'accessibilityRole'] as const).forEach(prop => - describe(prop, () => { - ( - [ - 'none', - 'button', - 'link', - 'search', - 'image', - 'keyboardkey', - 'text', - 'adjustable', - 'imagebutton', - 'header', - 'summary', - 'alert', - 'checkbox', - 'combobox', - 'menu', - 'menubar', - 'menuitem', - 'progressbar', - 'radio', - 'radiogroup', - 'scrollbar', - 'spinbutton', - 'switch', - 'tab', - 'tablist', - 'timer', - 'toolbar', - ] as const - ).forEach(role => { + describe('accessibilityRole', () => { + describe(`value propagation`, () => { + ACCESIBILITY_ROLE_VALUES.forEach(role => { it(`can be set to ${role}`, () => { expect( getAccessibilityProp( @@ -148,28 +256,17 @@ export default function accessibilityPropsSuite( ).toEqual(role); }); }); + }); - prop === 'accessibilityRole' && - it('has higher priority than "role"', () => { - expect( - getAccessibilityProp( - , - 'accessibilityRole', - ), - ).toEqual('button'); - }); - - prop === 'role' && - it('has lower priority than "accessibilityRole"', () => { - expect( - getAccessibilityProp( - , - 'accessibilityRole', - ), - ).toEqual('button'); - }); - }), - ); + it(`has 'accessibilityRole' of higher priority than "role"`, () => { + expect( + getAccessibilityProp( + , + 'accessibilityRole', + ), + ).toEqual('button'); + }); + }); describe('accessibilityState', () => { (['disabled', 'selected', 'busy', 'expanded'] as const).forEach(