Skip to content

Commit 6dff5aa

Browse files
committed
feat: support role prop
1 parent 238c860 commit 6dff5aa

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/helpers/accessiblity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,7 @@ export function isAccessibilityElement(
111111
element?.type === hostComponentNames?.switch
112112
);
113113
}
114+
115+
export function getAccessibilityRole(element: ReactTestInstance) {
116+
return element.props.role ?? element.props.accessibilityRole;
117+
}

src/helpers/format-default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const propsToDisplay = [
1111
'accessibilityLabel',
1212
'accessibilityLabelledBy',
1313
'accessibilityHint',
14+
'role',
1415
'aria-hidden',
1516
'placeholder',
1617
'value',

src/queries/__tests__/role.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ test('getAllByRole, queryAllByRole, findAllByRole', async () => {
8686
);
8787
});
8888

89+
test('supports role prop', () => {
90+
const screen = render(
91+
<>
92+
<View accessible role="checkbox" />
93+
<View accessible role="radio" />
94+
<View accessible role="switch" />
95+
<View accessible role="tab" />
96+
<Text role="alert" />
97+
<Text role="heading" />
98+
<Text role="searchbox" />
99+
<Pressable role="button" />
100+
</>
101+
);
102+
103+
expect(screen.getByRole('checkbox')).toBeTruthy();
104+
expect(screen.getByRole('radio')).toBeTruthy();
105+
expect(screen.getByRole('switch')).toBeTruthy();
106+
expect(screen.getByRole('tab')).toBeTruthy();
107+
expect(screen.getByRole('alert')).toBeTruthy();
108+
expect(screen.getByRole('heading')).toBeTruthy();
109+
expect(screen.getByRole('searchbox')).toBeTruthy();
110+
expect(screen.getByRole('button')).toBeTruthy();
111+
});
112+
89113
describe('supports name option', () => {
90114
test('returns an element that has the corresponding role and a children with the name', () => {
91115
const { getByRole } = render(

src/queries/role.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ReactTestInstance } from 'react-test-renderer';
22
import {
33
accessibilityStateKeys,
44
accessiblityValueKeys,
5+
getAccessibilityRole,
56
isAccessibilityElement,
67
} from '../helpers/accessiblity';
78
import { findAll } from '../helpers/findAll';
@@ -68,7 +69,7 @@ const queryAllByRole = (
6869
(node) =>
6970
// run the cheapest checks first, and early exit to avoid unneeded computations
7071
isAccessibilityElement(node) &&
71-
matchStringProp(node.props.accessibilityRole, role) &&
72+
matchStringProp(getAccessibilityRole(node), role) &&
7273
matchAccessibleStateIfNeeded(node, options) &&
7374
matchAccessibilityValueIfNeeded(node, options?.value) &&
7475
matchAccessibleNameIfNeeded(node, options?.name),

website/docs/Queries.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ title: Queries
3030
- [Precision](#precision)
3131
- [Normalization](#normalization)
3232
- [Unit testing helpers](#unit-testing-helpers)
33-
- [`UNSAFE_ByType`](#unsafe_bytype)
34-
- [`UNSAFE_ByProps`](#unsafe_byprops)
33+
- [`UNSAFE_ByType`](#unsafebytype)
34+
- [`UNSAFE_ByProps`](#unsafebyprops)
3535

3636
## Variants
3737

@@ -116,7 +116,7 @@ getByRole(
116116
): ReactTestInstance;
117117
```
118118

119-
Returns a `ReactTestInstance` with matching `accessibilityRole` prop.
119+
Returns a `ReactTestInstance` with matching `role` or `accessibilityRole` prop.
120120

121121
:::info
122122
In order for `*ByRole` queries to match an element it needs to be considered an accessibility element:
@@ -140,7 +140,7 @@ const element3 = screen.getByRole('button', { name: 'Hello', disabled: true });
140140

141141
#### Options
142142

143-
`name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
143+
`name`: Finds an element with given `role`/`accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
144144

145145
`disabled`: You can filter elements by their disabled state. The possible values are `true` or `false`. Querying `disabled: false` will also match elements with `disabled: undefined` (see the [wiki](https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State) for more details). See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `disabled` state.
146146

0 commit comments

Comments
 (0)