# All files / src/queries placeholderText.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
    73
    74
    75
    76
    77
    78
    79
    80

    48x

    215x

    215x

    48x

    449x
    65x

    215x

    48x
    1x
    48x
    45x

    48x

    48x

    386x

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

    type ByPlaceholderTextOptions = CommonQueryOptions & TextMatchOptions;

    const getTextInputNodeByPlaceholderText = (
      node: ReactTestInstance,
      placeholder: TextMatch,
      options: TextMatchOptions = {}
    ) => {
      const { exact, normalizer } = options;

      return (
        filterNodeByType(node, getHostComponentNames().textInput) &&
        matches(placeholder, node.props.placeholder, normalizer, exact)
      );
    };

    const queryAllByPlaceholderText = (
      instance: ReactTestInstance
    ): ((
      placeholder: TextMatch,
      queryOptions?: ByPlaceholderTextOptions
    ) => Array<ReactTestInstance>) =>
      function queryAllByPlaceholderFn(placeholder, queryOptions) {
        return findAll(
          instance,
          (node) =>
            getTextInputNodeByPlaceholderText(node, placeholder, queryOptions),
          queryOptions
        );
      };

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

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

    export type ByPlaceholderTextQueries = {
      getByPlaceholderText: GetByQuery<TextMatch, ByPlaceholderTextOptions>;
      getAllByPlaceholderText: GetAllByQuery<TextMatch, ByPlaceholderTextOptions>;
      queryByPlaceholderText: QueryByQuery<TextMatch, ByPlaceholderTextOptions>;
      queryAllByPlaceholderText: QueryAllByQuery<
        TextMatch,
        ByPlaceholderTextOptions
      >;
      findByPlaceholderText: FindByQuery<TextMatch, ByPlaceholderTextOptions>;
      findAllByPlaceholderText: FindAllByQuery<TextMatch, ByPlaceholderTextOptions>;
    };

    export const bindByPlaceholderTextQueries = (
      instance: ReactTestInstance
    ): ByPlaceholderTextQueries => ({
      getByPlaceholderText: getBy(instance),
      getAllByPlaceholderText: getAllBy(instance),
      queryByPlaceholderText: queryBy(instance),
      queryAllByPlaceholderText: queryAllBy(instance),
      findByPlaceholderText: findBy(instance),
      findAllByPlaceholderText: findAllBy(instance),
    });