# All files / src/queries testId.ts

100% Statements 14/14
100% Branches 3/3
100% Functions 7/7
100% Lines 14/14

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

    48x

    602x
    602x

    48x

    582x
    198x

    1431x

    48x
    2x
    48x
    50x

    48x

    48x

    386x

     import type { ReactTestInstance } from 'react-test-renderer';
    import { findAll } from '../helpers/findAll';
    import { matches, TextMatch, TextMatchOptions } from '../matches';
    import { makeQueries } from './makeQueries';
    import type {
      FindAllByQuery,
      FindByQuery,
      GetAllByQuery,
      GetByQuery,
      QueryAllByQuery,
      QueryByQuery,
    } from './makeQueries';
    import type { CommonQueryOptions } from './options';

    type ByTestIdOptions = CommonQueryOptions & TextMatchOptions;

    const getNodeByTestId = (
      node: ReactTestInstance,
      testID: TextMatch,
      options: TextMatchOptions = {}
    ) => {
      const { exact, normalizer } = options;
      return matches(testID, node.props.testID, normalizer, exact);
    };

    const queryAllByTestId = (
      instance: ReactTestInstance
    ): ((
      testId: TextMatch,
      queryOptions?: ByTestIdOptions
    ) => Array<ReactTestInstance>) =>
      function queryAllByTestIdFn(testId, queryOptions) {
        return findAll(
          instance,
          (node) =>
            typeof node.type === 'string' &&
            getNodeByTestId(node, testId, queryOptions),
          queryOptions
        );
      };

    const getMultipleError = (testId: TextMatch) =>
      `Found multiple elements with testID: ${String(testId)}`;
    const getMissingError = (testId: TextMatch) =>
      `Unable to find an element with testID: ${String(testId)}`;

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

    export type ByTestIdQueries = {
      getByTestId: GetByQuery<TextMatch, ByTestIdOptions>;
      getAllByTestId: GetAllByQuery<TextMatch, ByTestIdOptions>;
      queryByTestId: QueryByQuery<TextMatch, ByTestIdOptions>;
      queryAllByTestId: QueryAllByQuery<TextMatch, ByTestIdOptions>;
      findByTestId: FindByQuery<TextMatch, ByTestIdOptions>;
      findAllByTestId: FindAllByQuery<TextMatch, ByTestIdOptions>;
    };

    export const bindByTestIdQueries = (
      instance: ReactTestInstance
    ): ByTestIdQueries => ({
      getByTestId: getBy(instance),
      getAllByTestId: getAllBy(instance),
      queryByTestId: queryBy(instance),
      queryAllByTestId: queryAllBy(instance),
      findByTestId: findBy(instance),
      findAllByTestId: findAllBy(instance),
    });