# All files / src render.tsx

100% Statements 40/40
75% Branches 15/20
100% Functions 16/16
100% Lines 39/39

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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170

    352x

    352x
    10x

    342x
    353x

    342x

    342x

    10x
    12x
    2x

    10x
    11x

    10x

    10x

    4x

    346x
    346x

    346x
    322x
    322x

    346x

    346x

    15x

    346x

    2x

    346x
    346x

    346x
    12x
    12x

    10x

    10x

    10x

    1x

    10x
    10x
    10x

    346x
    346x

     import type { ReactTestInstance, ReactTestRenderer } from 'react-test-renderer';
    import * as React from 'react';
    import { Profiler } from 'react';
    import act from './act';
    import { addToCleanupQueue } from './cleanup';
    import { getConfig } from './config';
    import { getHostChildren } from './helpers/component-tree';
    import debugDeep, { DebugOptions } from './helpers/debugDeep';
    import debugShallow from './helpers/debugShallow';
    import { configureHostComponentNamesIfNeeded } from './helpers/host-component-names';
    import { validateStringsRenderedWithinText } from './helpers/stringValidation';
    import { renderWithAct } from './render-act';
    import { setRenderResult, screen } from './screen';
    import { getQueriesForElement } from './within';

    export type RenderOptions = {
      wrapper?: React.ComponentType<any>;
      createNodeMock?: (element: React.ReactElement) => any;
      unstable_validateStringsRenderedWithinText?: boolean;
    };

    export type RenderResult = ReturnType<typeof render>;

    /**
     * Renders test component deeply using react-test-renderer and exposes helpers
     * to assert on the output.
     */
    export default function render<T>(
      component: React.ReactElement<T>,
      {
        wrapper: Wrapper,
        createNodeMock,
        unstable_validateStringsRenderedWithinText,
      }: RenderOptions = {}
    ) {
      configureHostComponentNamesIfNeeded();

      if (unstable_validateStringsRenderedWithinText) {
        return renderWithStringValidation(component, {
          wrapper: Wrapper,
          createNodeMock,
        });
      }

      const wrap = (element: React.ReactElement) =>
        Wrapper ? <Wrapper>{element}</Wrapper> : element;

      const renderer = renderWithAct(
        wrap(component),
        createNodeMock ? { createNodeMock } : undefined
      );

      return buildRenderResult(renderer, wrap);
    }

    function renderWithStringValidation<T>(
      component: React.ReactElement<T>,
      {
        wrapper: Wrapper,
        createNodeMock,
      }: Omit<RenderOptions, 'unstable_validateStringsRenderedWithinText'> = {}
    ) {
      const handleRender: React.ProfilerProps['onRender'] = (_, phase) => {
        if (phase === 'update') {
          validateStringsRenderedWithinText(screen.toJSON());
        }
      };

      const wrap = (element: React.ReactElement) => (
        <Profiler id="renderProfiler" onRender={handleRender}>
          {Wrapper ? <Wrapper>{element}</Wrapper> : element}
        </Profiler>
      );

      const renderer = renderWithAct(
        wrap(component),
        createNodeMock ? { createNodeMock } : undefined
      );
      validateStringsRenderedWithinText(renderer.toJSON());

      return buildRenderResult(renderer, wrap);
    }

    function buildRenderResult(
      renderer: ReactTestRenderer,
      wrap: (element: React.ReactElement) => JSX.Element
    ) {
      const update = updateWithAct(renderer, wrap);
      const instance = renderer.root;

      const unmount = () => {
        act(() => {
          renderer.unmount();
        });
      };

      addToCleanupQueue(unmount);

      const result = {
        ...getQueriesForElement(instance),
        update,
        unmount,
        rerender: update, // alias for `update`
        toJSON: renderer.toJSON,
        debug: debug(instance, renderer),
        get root() {
          return getHostChildren(instance)[0];
        },
        UNSAFE_root: instance,
      };

      // Add as non-enumerable property, so that it's safe to enumerate
      // `render` result, e.g. using destructuring rest syntax.
      Object.defineProperty(result, 'container', {
        enumerable: false,
        get() {
          throw new Error(
            "'container' property has been renamed to 'UNSAFE_root'.\n\n" +
              "Consider using 'root' property which returns root host element."
          );
        },
      });

      setRenderResult(result);
      return result;
    }

    function updateWithAct(
      renderer: ReactTestRenderer,
      wrap: (innerElement: React.ReactElement) => React.ReactElement
    ) {
      return function (component: React.ReactElement) {
        act(() => {
          renderer.update(wrap(component));
        });
      };
    }

    interface DebugFunction {
      (options?: DebugOptions | string): void;
      shallow: (message?: string) => void;
    }

    function debug(
      instance: ReactTestInstance,
      renderer: ReactTestRenderer
    ): DebugFunction {
      function debugImpl(options?: DebugOptions | string) {
        const { defaultDebugOptions } = getConfig();
        const debugOptions =
          typeof options === 'string'
            ? { ...defaultDebugOptions, message: options }
            : { ...defaultDebugOptions, ...options };

        if (typeof options === 'string') {
          // eslint-disable-next-line no-console
          console.warn(
            'Using debug("message") is deprecated and will be removed in future release, please use debug({ message; "message" }) instead.'
          );
        }

        const json = renderer.toJSON();
        Eif (json) {
          return debugDeep(json, debugOptions);
        }
      }
      debugImpl.shallow = (message?: string) => debugShallow(instance, message);
      return debugImpl;
    }