# All files / src/queries unsafeProps.ts

100% Statements 23/23
100% Branches 2/2
100% Functions 9/9
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

    48x

    388x
    5x
    5x

    2x

    48x

    388x
    4x
    4x
    2x

    2x

    48x

    386x
    2x
    2x

    1x

    48x

    386x
    2x
    2x

    1x

    48x

    386x

     import type { ReactTestInstance } from 'react-test-renderer';
    import prettyFormat from 'pretty-format';
    import { ErrorWithStack, prepareErrorMessage } from '../helpers/errors';
    import { createQueryByError } from '../helpers/errors';

    const UNSAFE_getByProps = (
      instance: ReactTestInstance
    ): ((props: { [propName: string]: any }) => ReactTestInstance) =>
      function getByPropsFn(props: { [propName: string]: any }) {
        try {
          return instance.findByProps(props);
        } catch (error) {
          throw new ErrorWithStack(prepareErrorMessage(error), getByPropsFn);
        }
      };

    const UNSAFE_getAllByProps = (
      instance: ReactTestInstance
    ): ((props: { [propName: string]: any }) => Array<ReactTestInstance>) =>
      function getAllByPropsFn(props: { [propName: string]: any }) {
        const results = instance.findAllByProps(props);
        if (results.length === 0) {
          throw new ErrorWithStack(
            `No instances found with props:\n${prettyFormat(props)}`,
            getAllByPropsFn
          );
        }
        return results;
      };

    const UNSAFE_queryByProps = (
      instance: ReactTestInstance
    ): ((props: { [propName: string]: any }) => ReactTestInstance | null) =>
      function queryByPropsFn(props: { [propName: string]: any }) {
        try {
          return UNSAFE_getByProps(instance)(props);
        } catch (error) {
          return createQueryByError(error, queryByPropsFn);
        }
      };

    const UNSAFE_queryAllByProps =
      (
        instance: ReactTestInstance
      ): ((props: { [propName: string]: any }) => Array<ReactTestInstance>) =>
      (props: { [propName: string]: any }) => {
        try {
          return UNSAFE_getAllByProps(instance)(props);
        } catch (error) {
          return [];
        }
      };

    // Unsafe aliases
    export type UnsafeByPropsQueries = {
      UNSAFE_getByProps: (props: { [key: string]: any }) => ReactTestInstance;
      UNSAFE_getAllByProps: (props: {
        [key: string]: any;
      }) => Array<ReactTestInstance>;
      UNSAFE_queryByProps: (props: {
        [key: string]: any;
      }) => ReactTestInstance | null;
      UNSAFE_queryAllByProps: (props: {
        [key: string]: any;
      }) => Array<ReactTestInstance>;
    };

    // TODO: migrate to makeQueries pattern
    export const bindUnsafeByPropsQueries = (
      instance: ReactTestInstance
    ): UnsafeByPropsQueries => ({
      UNSAFE_getByProps: UNSAFE_getByProps(instance),
      UNSAFE_getAllByProps: UNSAFE_getAllByProps(instance),
      UNSAFE_queryByProps: UNSAFE_queryByProps(instance),
      UNSAFE_queryAllByProps: UNSAFE_queryAllByProps(instance),
    });