Skip to content

Commit be3af13

Browse files
committed
refactor: self code review
1 parent 21527d5 commit be3af13

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/queries/a11yState.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ import type {
1010
QueryByQuery,
1111
} from './makeQueries';
1212

13+
export function matchAccessibilityState(
14+
node: ReactTestInstance,
15+
matcher: AccessibilityState
16+
) {
17+
const stateProp = node.props.accessibilityState;
18+
19+
// busy, disabled & selected states default to false,
20+
// while checked & expended states treat false and default as sepatate values
21+
return (
22+
matchState(stateProp?.busy ?? false, matcher.busy) &&
23+
matchState(stateProp?.disabled ?? false, matcher.disabled) &&
24+
matchState(stateProp?.selected ?? false, matcher.selected) &&
25+
matchState(stateProp?.checked, matcher.checked) &&
26+
matchState(stateProp?.expanded, matcher.expanded)
27+
);
28+
}
29+
1330
function matchState(value: unknown, matcher: unknown) {
1431
return matcher === undefined || value === matcher;
1532
}
@@ -19,17 +36,8 @@ const queryAllByA11yState = (
1936
): ((matcher: AccessibilityState) => Array<ReactTestInstance>) =>
2037
function queryAllByA11yStateFn(matcher) {
2138
return instance.findAll((node) => {
22-
const stateProp = node.props.accessibilityState;
23-
24-
// busy, disabled & selected states default to false,
25-
// while checked & expended states treat false and default as sepatate values
2639
return (
27-
typeof node.type === 'string' &&
28-
matchState(stateProp?.busy ?? false, matcher.busy) &&
29-
matchState(stateProp?.disabled ?? false, matcher.disabled) &&
30-
matchState(stateProp?.selected ?? false, matcher.selected) &&
31-
matchState(stateProp?.checked, matcher.checked) &&
32-
matchState(stateProp?.expanded, matcher.expanded)
40+
typeof node.type === 'string' && matchAccessibilityState(node, matcher)
3341
);
3442
});
3543
};

website/docs/Queries.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ title: Queries
2323
- [`ByRole`](#byrole)
2424
- [Options](#options-1)
2525
- [`ByA11yState`, `ByAccessibilityState`](#bya11ystate-byaccessibilitystate)
26+
- [Default state for: `disabled`, `selected`, and `busy` keys](#default-state-for-disabled-selected-and-busy-keys)
27+
- [Default state for: `checked` and `expanded` keys](#default-state-for-checked-and-expanded-keys)
2628
- [`ByA11Value`, `ByAccessibilityValue`](#bya11value-byaccessibilityvalue)
2729
- [TextMatch](#textmatch)
2830
- [Examples](#examples)
@@ -296,6 +298,30 @@ render(<Component />);
296298
const element = screen.getByA11yState({ disabled: true });
297299
```
298300

301+
:::note
302+
303+
#### Default state for: `disabled`, `selected`, and `busy` keys
304+
305+
Passing `false` matcher value will match both elements with explicit `false` state value and without explicit state value.
306+
307+
For instance, `getByA11yState({ disabled: false })` will match elements with following props:
308+
* `accessibilityState={{ disabled: false, ... }}`
309+
* no `disabled` key under `accessibilityState` prop, e.g. `accessibilityState={{}}`
310+
* no `accessibilityState` prop at all
311+
312+
#### Default state for: `checked` and `expanded` keys
313+
Passing `false` matcher value will only match elements with explicit `false` state value.
314+
315+
For instance, `getByA11yState({ checked: false })` will only match elements with:
316+
* `accessibilityState={{ checked: false, ... }}`
317+
318+
but will not match elements with following props:
319+
* no `checked` key under `accessibilityState` prop, e.g. `accessibilityState={{}}`
320+
* no `accessibilityState` prop at all
321+
322+
The difference in handling default values is made to reflect observed accessibility behaviour on iOS and Android platforms.
323+
:::
324+
299325
### `ByA11Value`, `ByAccessibilityValue`
300326

301327
> getByA11yValue, getAllByA11yValue, queryByA11yValue, queryAllByA11yValue, findByA11yValue, findAllByA11yValue

0 commit comments

Comments
 (0)