# All files / src/queries a11yValue.ts

100% Statements 23/23
100% Branches 4/4
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

    518x
    137x

    1249x

    48x
    111x

    111x
    444x
    118x

    111x

    48x
    22x

    48x
    89x

    48x

    48x

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

    386x

     import type { ReactTestInstance } from 'react-test-renderer';
    import { accessiblityValueKeys } from '../helpers/accessiblity';
    import { deprecateQueries } from '../helpers/deprecation';
    import { findAll } from '../helpers/findAll';
    import {
      AccessibilityValueMatcher,
      matchAccessibilityValue,
    } from '../helpers/matchers/accessibilityValue';
    import { makeQueries } from './makeQueries';
    import type {
      FindAllByQuery,
      FindByQuery,
      GetAllByQuery,
      GetByQuery,
      QueryAllByQuery,
      QueryByQuery,
    } from './makeQueries';
    import { CommonQueryOptions } from './options';

    const queryAllByA11yValue = (
      instance: ReactTestInstance
    ): ((
      value: AccessibilityValueMatcher,
      queryOptions?: CommonQueryOptions
    ) => Array<ReactTestInstance>) =>
      function queryAllByA11yValueFn(value, queryOptions) {
        return findAll(
          instance,
          (node) =>
            typeof node.type === 'string' && matchAccessibilityValue(node, value),
          queryOptions
        );
      };

    const formatQueryParams = (matcher: AccessibilityValueMatcher) => {
      const params: string[] = [];

      accessiblityValueKeys.forEach((valueKey) => {
        if (matcher[valueKey] !== undefined) {
          params.push(`${valueKey} value: ${matcher[valueKey]}`);
        }
      });

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

    const getMultipleError = (matcher: AccessibilityValueMatcher) =>
      `Found multiple elements with ${formatQueryParams(matcher)}`;

    const getMissingError = (matcher: AccessibilityValueMatcher) =>
      `Unable to find an element with ${formatQueryParams(matcher)}`;

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

    export type ByA11yValueQueries = {
      getByA11yValue: GetByQuery<AccessibilityValueMatcher, CommonQueryOptions>;
      getAllByA11yValue: GetAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      queryByA11yValue: QueryByQuery<AccessibilityValueMatcher, CommonQueryOptions>;
      queryAllByA11yValue: QueryAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      findByA11yValue: FindByQuery<AccessibilityValueMatcher, CommonQueryOptions>;
      findAllByA11yValue: FindAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;

      getByAccessibilityValue: GetByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      getAllByAccessibilityValue: GetAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      queryByAccessibilityValue: QueryByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      queryAllByAccessibilityValue: QueryAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      findByAccessibilityValue: FindByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
      findAllByAccessibilityValue: FindAllByQuery<
        AccessibilityValueMatcher,
        CommonQueryOptions
      >;
    };

    export const bindByA11yValueQueries = (
      instance: ReactTestInstance
    ): ByA11yValueQueries => {
      const getByA11yValue = getBy(instance);
      const getAllByA11yValue = getAllBy(instance);
      const queryByA11yValue = queryBy(instance);
      const queryAllByA11yValue = queryAllBy(instance);
      const findByA11yValue = findBy(instance);
      const findAllByA11yValue = findAllBy(instance);

      return {
        ...deprecateQueries(
          {
            getByA11yValue,
            getAllByA11yValue,
            queryByA11yValue,
            queryAllByA11yValue,
            findByA11yValue,
            findAllByA11yValue,
            getByAccessibilityValue: getByA11yValue,
            getAllByAccessibilityValue: getAllByA11yValue,
            queryByAccessibilityValue: queryByA11yValue,
            queryAllByAccessibilityValue: queryAllByA11yValue,
            findByAccessibilityValue: findByA11yValue,
            findAllByAccessibilityValue: findAllByA11yValue,
          },
          'Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or {queryPrefix}ByRole(role, { value: ... }) query instead.'
        ),
      };
    };