Skip to content

Commit 864528c

Browse files
committed
refactor: code review changes
1 parent be3af13 commit 864528c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/queries/a11yState.ts

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

13+
/**
14+
* Default accessibility state values based on experiments using accessibility
15+
* inspector/screen reader on iOS and Android.
16+
*
17+
* @see https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State
18+
*/
19+
const defaultState: AccessibilityState = {
20+
disabled: false,
21+
selected: false,
22+
checked: undefined,
23+
busy: false,
24+
expanded: undefined,
25+
};
26+
1327
export function matchAccessibilityState(
1428
node: ReactTestInstance,
1529
matcher: AccessibilityState
1630
) {
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
31+
const state = node.props.accessibilityState;
2132
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)
33+
matchState(state, matcher, 'disabled') &&
34+
matchState(state, matcher, 'selected') &&
35+
matchState(state, matcher, 'checked') &&
36+
matchState(state, matcher, 'busy') &&
37+
matchState(state, matcher, 'expanded')
2738
);
2839
}
2940

30-
function matchState(value: unknown, matcher: unknown) {
31-
return matcher === undefined || value === matcher;
41+
function matchState(
42+
value: AccessibilityState,
43+
matcher: AccessibilityState,
44+
key: keyof AccessibilityState
45+
) {
46+
const valueWithDefault = value?.[key] ?? defaultState[key];
47+
return matcher[key] === undefined || matcher[key] === valueWithDefault;
3248
}
3349

3450
const queryAllByA11yState = (

0 commit comments

Comments
 (0)