# All files / src/queries a11yState.ts

100% Statements 23/23
80% Branches 4/5
100% Functions 8/8
100% Lines 23/23

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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132

    48x

    554x
    173x

    1357x

    48x
    107x

    107x
    535x
    107x

    107x

    48x
    22x

    48x
    85x

    48x

    48x

    386x
    386x
    386x
    386x
    386x
    386x

    386x

     import type { ReactTestInstance } from 'react-test-renderer';
    import { accessibilityStateKeys } from '../helpers/accessiblity';
    import { deprecateQueries } from '../helpers/deprecation';
    import { findAll } from '../helpers/findAll';
    import {
      AccessibilityStateMatcher,
      matchAccessibilityState,
    } from '../helpers/matchers/accessibilityState';
    import { makeQueries } from './makeQueries';
    import type {
      FindAllByQuery,
      FindByQuery,
      GetAllByQuery,
      GetByQuery,
      QueryAllByQuery,
      QueryByQuery,
    } from './makeQueries';
    import { CommonQueryOptions } from './options';

    const queryAllByA11yState = (
      instance: ReactTestInstance
    ): ((
      matcher: AccessibilityStateMatcher,
      queryOptions?: CommonQueryOptions
    ) => Array<ReactTestInstance>) =>
      function queryAllByA11yStateFn(matcher, queryOptions) {
        return findAll(
          instance,
          (node) =>
            typeof node.type === 'string' && matchAccessibilityState(node, matcher),
          queryOptions
        );
      };

    const buildErrorMessage = (state: AccessibilityStateMatcher = {}) => {
      const errors: string[] = [];

      accessibilityStateKeys.forEach((stateKey) => {
        if (state[stateKey] !== undefined) {
          errors.push(`${stateKey} state: ${state[stateKey]}`);
        }
      });

      return errors.join(', ');
    };

    const getMultipleError = (state: AccessibilityStateMatcher) =>
      `Found multiple elements with ${buildErrorMessage(state)}`;

    const getMissingError = (state: AccessibilityStateMatcher) =>
      `Unable to find an element with ${buildErrorMessage(state)}`;

    const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
      queryAllByA11yState,
      getMissingError,
      getMultipleError
    );

    export type ByA11yStateQueries = {
      getByA11yState: GetByQuery<AccessibilityStateMatcher, CommonQueryOptions>;
      getAllByA11yState: GetAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      queryByA11yState: QueryByQuery<AccessibilityStateMatcher, CommonQueryOptions>;
      queryAllByA11yState: QueryAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      findByA11yState: FindByQuery<AccessibilityStateMatcher, CommonQueryOptions>;
      findAllByA11yState: FindAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;

      getByAccessibilityState: GetByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      getAllByAccessibilityState: GetAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      queryByAccessibilityState: QueryByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      queryAllByAccessibilityState: QueryAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      findByAccessibilityState: FindByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
      findAllByAccessibilityState: FindAllByQuery<
        AccessibilityStateMatcher,
        CommonQueryOptions
      >;
    };

    export const bindByA11yStateQueries = (
      instance: ReactTestInstance
    ): ByA11yStateQueries => {
      const getByA11yState = getBy(instance);
      const getAllByA11yState = getAllBy(instance);
      const queryByA11yState = queryBy(instance);
      const queryAllByA11yState = queryAllBy(instance);
      const findByA11yState = findBy(instance);
      const findAllByA11yState = findAllBy(instance);

      return {
        ...deprecateQueries(
          {
            getByA11yState,
            getAllByA11yState,
            queryByA11yState,
            queryAllByA11yState,
            findByA11yState,
            findAllByA11yState,
            getByAccessibilityState: getByA11yState,
            getAllByAccessibilityState: getAllByA11yState,
            queryByAccessibilityState: queryByA11yState,
            queryAllByAccessibilityState: queryAllByA11yState,
            findByAccessibilityState: findByA11yState,
            findAllByAccessibilityState: findAllByA11yState,
          },
          'Use {queryPrefix}ByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead.'
        ),
      };
    };