# All files / src/queries unsafeType.ts

82.6% Statements 19/23
100% Branches 2/2
88.88% Functions 8/9
82.6% Lines 19/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

    48x

    386x
    4x
    4x

    48x

    388x
    4x
    4x
    2x

    2x

    48x

    386x

    48x

    386x
    2x
    2x

    1x

    48x

    386x

     import type { ReactTestInstance } from 'react-test-renderer';
    import * as React from 'react';
    import { ErrorWithStack, prepareErrorMessage } from '../helpers/errors';
    import { createQueryByError } from '../helpers/errors';

    const UNSAFE_getByType = (
      instance: ReactTestInstance
    ): ((type: React.ComponentType<any>) => ReactTestInstance) =>
      function getByTypeFn(type: React.ComponentType<any>) {
        try {
          return instance.findByType(type);
        } catch (error) {
          throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
        }
      };

    const UNSAFE_getAllByType = (
      instance: ReactTestInstance
    ): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) =>
      function getAllByTypeFn(type: React.ComponentType<any>) {
        const results = instance.findAllByType(type);
        if (results.length === 0) {
          throw new ErrorWithStack('No instances found', getAllByTypeFn);
        }
        return results;
      };

    const UNSAFE_queryByType = (
      instance: ReactTestInstance
    ): ((type: React.ComponentType<any>) => ReactTestInstance | null) =>
      function queryByTypeFn(type: React.ComponentType<any>) {
        try {
          return UNSAFE_getByType(instance)(type);
        } catch (error) {
          return createQueryByError(error, queryByTypeFn);
        }
      };

    const UNSAFE_queryAllByType =
      (
        instance: ReactTestInstance
      ): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) =>
      (type: React.ComponentType<any>) => {
        try {
          return UNSAFE_getAllByType(instance)(type);
        } catch (error) {
          return [];
        }
      };

    // Unsafe aliases
    export type UnsafeByTypeQueries = {
      UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance;
      UNSAFE_getAllByType: <P>(
        type: React.ComponentType<P>
      ) => Array<ReactTestInstance>;
      UNSAFE_queryByType: <P>(
        type: React.ComponentType<P>
      ) => ReactTestInstance | null;
      UNSAFE_queryAllByType: <P>(
        type: React.ComponentType<P>
      ) => Array<ReactTestInstance>;
    };

    // TODO: migrate to makeQueries pattern
    export const bindUnsafeByTypeQueries = (
      instance: ReactTestInstance
    ): UnsafeByTypeQueries => ({
      UNSAFE_getByType: UNSAFE_getByType(instance),
      UNSAFE_getAllByType: UNSAFE_getAllByType(instance),
      UNSAFE_queryByType: UNSAFE_queryByType(instance),
      UNSAFE_queryAllByType: UNSAFE_queryAllByType(instance),
    });