Skip to content

Commit 5eb1a26

Browse files
committed
Improve readability of byRole function and udpate docs for default accessibilityState
1 parent a4a22dd commit 5eb1a26

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/queries/role.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ByRoleOptions = {
1717
name?: TextMatch;
1818
} & AccessibilityState;
1919

20-
type AccessibilityStateField = keyof AccessibilityState;
20+
type AccessibilityStateKey = keyof AccessibilityState;
2121

2222
const matchAccessibleNameIfNeeded = (
2323
node: ReactTestInstance,
@@ -33,7 +33,7 @@ const matchAccessibleNameIfNeeded = (
3333

3434
// disabled:undefined is equivalent to disabled:false, same for selected. busy not, but it makes
3535
// sense from a testing/voice-over perspective. checked and expanded do behave differently
36-
const implicityFalseState: AccessibilityStateField[] = [
36+
const implicityFalseState: AccessibilityStateKey[] = [
3737
'disabled',
3838
'selected',
3939
'busy',
@@ -60,7 +60,7 @@ const matchAccessibleStateIfNeeded = (
6060
}
6161
});
6262

63-
const accessibilityStates: AccessibilityStateField[] = [
63+
const accessibilityStates: AccessibilityStateKey[] = [
6464
'disabled',
6565
'selected',
6666
'checked',
@@ -72,20 +72,15 @@ const queryAllByRole = (
7272
instance: ReactTestInstance
7373
): ((role: TextMatch, options?: ByRoleOptions) => Array<ReactTestInstance>) =>
7474
function queryAllByRoleFn(role, options) {
75-
return instance.findAll((node) => {
76-
// run the cheapest checks first, and early exit too avoid unneeded computations
77-
const matchRole =
78-
typeof node.type === 'string' &&
79-
matchStringProp(node.props.accessibilityRole, role);
80-
81-
if (!matchRole) return false;
82-
83-
if (!matchAccessibleStateIfNeeded(node, options)) {
84-
return false;
85-
}
75+
return instance.findAll(
76+
(node) =>
77+
// run the cheapest checks first, and early exit too avoid unneeded computations
8678

87-
return matchAccessibleNameIfNeeded(node, options?.name);
88-
});
79+
typeof node.type === 'string' &&
80+
matchStringProp(node.props.accessibilityRole, role) &&
81+
matchAccessibleStateIfNeeded(node, options) &&
82+
matchAccessibleNameIfNeeded(node, options?.name)
83+
);
8984
};
9085

9186
const buildErrorMessage = (role: TextMatch, options: ByRoleOptions = {}) => {

website/docs/Queries.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ const element = screen.getByRole('button');
255255

256256
`name`: Finds an element with given `accessibilityRole` and an accessible name (equivalent to `byText` or `byLabelText` query).
257257

258-
`disabled`: You can filter elements by their disabled state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `disabled` state.
258+
`disabled`: You can filter elements by their disabled state. The possible values are `true` or `false`. Querying `disabled: false` will also query 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.
259259

260-
`selected`: You can filter elements by their selected state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `selected` state.
260+
`selected`: You can filter elements by their selected state. The possible values are `true` or `false`. Querying `selected: false` will also query elements with `selected: 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 `selected` state.
261261

262262
`checked`: You can filter elements by their checked state. The possible values are `true`, `false`, or `"mixed"`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `checked` state.
263263

264-
`busy`: You can filter elements by their busy state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `busy` state.
264+
`busy`: You can filter elements by their busy state. The possible values are `true` or `false`. Querying `busy: false` will also query elements with `busy: 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 `busy` state.
265265

266266
`expanded`: You can filter elements by their expanded state. The possible values are `true` or `false`. See [React Native's accessibilityState](https://reactnative.dev/docs/accessibility#accessibilitystate) docs to learn more about the `expanded` state.
267267

0 commit comments

Comments
 (0)