# All files / src/helpers/matchers accessibilityState.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 3/3
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49

    48x

    427x
    1368x

    1368x

     import { AccessibilityState } from 'react-native';
    import { ReactTestInstance } from 'react-test-renderer';
    import { accessibilityStateKeys } from '../accessiblity';

    // This type is the same as AccessibilityState from `react-native` package
    // It is re-declared here due to issues with migration from `@types/react-native` to
    // built in `react-native` types.
    // See: https://github.com/callstack/react-native-testing-library/issues/1351
    export interface AccessibilityStateMatcher {
      disabled?: boolean;
      selected?: boolean;
      checked?: boolean | 'mixed';
      busy?: boolean;
      expanded?: boolean;
    }

    /**
     * Default accessibility state values based on experiments using accessibility
     * inspector/screen reader on iOS and Android.
     *
     * @see https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State
     */
    const defaultState: AccessibilityState = {
      disabled: false,
      selected: false,
      checked: undefined,
      busy: false,
      expanded: undefined,
    };

    export function matchAccessibilityState(
      node: ReactTestInstance,
      matcher: AccessibilityStateMatcher
    ) {
      const state = node.props.accessibilityState;
      return accessibilityStateKeys.every((key) => matchState(state, matcher, key));
    }

    function matchState(
      state: AccessibilityState,
      matcher: AccessibilityStateMatcher,
      key: keyof AccessibilityState
    ) {
      return (
        matcher[key] === undefined ||
        matcher[key] === (state?.[key] ?? defaultState[key])
      );
    }