# All files / src/queries text.ts

100% Statements 11/11
100% Branches 3/3
100% Functions 6/6
100% Lines 11/11

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

    48x

    747x
    409x

    2113x

    48x
    1x

    48x
    187x

    48x

    48x

    386x

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

    type ByTextOptions = CommonQueryOptions & TextMatchOptions;

    const queryAllByText = (
      instance: ReactTestInstance
    ): ((text: TextMatch, options?: ByTextOptions) => Array<ReactTestInstance>) =>
      function queryAllByTextFn(text, options = {}) {
        return findAll(
          instance,
          (node) =>
            filterNodeByType(node, getHostComponentNames().text) &&
            matchTextContent(node, text, options),
          {
            ...options,
            matchDeepestOnly: true,
          }
        );
      };

    const getMultipleError = (text: TextMatch) =>
      `Found multiple elements with text: ${String(text)}`;

    const getMissingError = (text: TextMatch) =>
      `Unable to find an element with text: ${String(text)}`;

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

    export type ByTextQueries = {
      getByText: GetByQuery<TextMatch, ByTextOptions>;
      getAllByText: GetAllByQuery<TextMatch, ByTextOptions>;
      queryByText: QueryByQuery<TextMatch, ByTextOptions>;
      queryAllByText: QueryAllByQuery<TextMatch, ByTextOptions>;
      findByText: FindByQuery<TextMatch, ByTextOptions>;
      findAllByText: FindAllByQuery<TextMatch, ByTextOptions>;
    };

    export const bindByTextQueries = (
      instance: ReactTestInstance
    ): ByTextQueries => ({
      getByText: getBy(instance),
      getAllByText: getAllBy(instance),
      queryByText: queryBy(instance),
      queryAllByText: queryAllBy(instance),
      findByText: findBy(instance),
      findAllByText: findAllBy(instance),
    });