mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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
This commit is contained in:
1 parent
0ac41fa386
commit
63335e5913
2 files changed
+178
-188
No files matched your search
+4
-111
@@ -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('<Text>', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('role', () => {
|
||||
it(`has none by default`, () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(<Text>{TEST_TEXT}</Text>);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual(
|
||||
<rn-paragraph>{TEST_TEXT}</rn-paragraph>,
|
||||
);
|
||||
});
|
||||
|
||||
it(`maps invalid values to 'none'`, () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
// $FlowExpectedError[incompatible-type]
|
||||
root.render(<Text role="__some_invalid_value">{TEST_TEXT}</Text>);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual(
|
||||
<rn-paragraph role="none">{TEST_TEXT}</rn-paragraph>,
|
||||
);
|
||||
});
|
||||
|
||||
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<Role>
|
||||
).forEach(propVal => {
|
||||
Fantom.runTask(() => {
|
||||
root.render(<Text role={propVal}>{TEST_TEXT}</Text>);
|
||||
});
|
||||
|
||||
expect(root.getRenderedOutput({props: ['role']}).toJSX()).toEqual(
|
||||
<rn-paragraph role={propVal}>{TEST_TEXT}</rn-paragraph>,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectable', () => {
|
||||
it(`can be set to "true"`, () => {
|
||||
const root = Fantom.createRoot();
|
||||
@@ -530,4 +422,5 @@ describe('<Text>', () => {
|
||||
return <Text {...props}>{TEST_TEXT}</Text>;
|
||||
}
|
||||
accessibilityPropsSuite(ComponentWithAccessibilityProps, false);
|
||||
rolePropSuite(ComponentWithAccessibilityProps);
|
||||
});
|
||||
+174
-77
@@ -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<AccessibilityRole> = [
|
||||
'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<Role> = [
|
||||
'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<AccessibilityProps>,
|
||||
) {
|
||||
Fantom.runTask(() => {
|
||||
root.render(content);
|
||||
});
|
||||
return root.getRenderedOutput({props: [name]}).toJSONObject().props[name];
|
||||
}
|
||||
|
||||
function getAccessibilityProps(
|
||||
content: React.MixedElement,
|
||||
names: $ReadOnlyArray<$Keys<AccessibilityProps>>,
|
||||
) {
|
||||
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(<Component />, ['role'])).toEqual({});
|
||||
});
|
||||
|
||||
it(`'role' maps invalid values to 'none'`, () => {
|
||||
expect(
|
||||
getAccessibilityProps(
|
||||
// $FlowExpectedError[incompatible-type]
|
||||
<Component role="__some_invalid_value" />,
|
||||
['role'],
|
||||
),
|
||||
).toEqual({['role']: 'none'});
|
||||
});
|
||||
|
||||
describe(`'role' propagation`, () => {
|
||||
ROLE_VALUES.forEach(role => {
|
||||
it(`can be set to ${role}`, () => {
|
||||
expect(
|
||||
getAccessibilityProp(<Component role={role} />, 'role'),
|
||||
).toEqual(role);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default function accessibilityPropsSuite(
|
||||
Component: component(...AccessibilityProps),
|
||||
accessibleByDefault: boolean = true,
|
||||
): void {
|
||||
let root: Root;
|
||||
|
||||
function getAccessibilityProp(
|
||||
content: React.MixedElement,
|
||||
name: $Keys<AccessibilityProps>,
|
||||
) {
|
||||
Fantom.runTask(() => {
|
||||
root.render(content);
|
||||
});
|
||||
return root.getRenderedOutput({props: [name]}).toJSONObject().props[name];
|
||||
}
|
||||
|
||||
function getAccessibilityProps(
|
||||
content: React.MixedElement,
|
||||
names: $ReadOnlyArray<$Keys<AccessibilityProps>>,
|
||||
) {
|
||||
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(
|
||||
<Component accessibilityRole="button" role="radio" />,
|
||||
'accessibilityRole',
|
||||
),
|
||||
).toEqual('button');
|
||||
});
|
||||
|
||||
prop === 'role' &&
|
||||
it('has lower priority than "accessibilityRole"', () => {
|
||||
expect(
|
||||
getAccessibilityProp(
|
||||
<Component accessibilityRole="button" role="radio" />,
|
||||
'accessibilityRole',
|
||||
),
|
||||
).toEqual('button');
|
||||
});
|
||||
}),
|
||||
);
|
||||
it(`has 'accessibilityRole' of higher priority than "role"`, () => {
|
||||
expect(
|
||||
getAccessibilityProp(
|
||||
<Component accessibilityRole="button" role="radio" />,
|
||||
'accessibilityRole',
|
||||
),
|
||||
).toEqual('button');
|
||||
});
|
||||
});
|
||||
|
||||
describe('accessibilityState', () => {
|
||||
(['disabled', 'selected', 'busy', 'expanded'] as const).forEach(
|
||||
|
||||
Reference in new issue
Block a user