# All files / src/helpers stringValidation.ts

81.25% Statements 13/16
83.33% Branches 10/12
100% Functions 3/3
86.66% Lines 13/15

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

    48x

    12x

    12x

    12x

    48x

    17x
    3x

    14x
    10x
    10x
    8x

    6x
    5x

     import { ReactTestRendererNode } from 'react-test-renderer';

    export const validateStringsRenderedWithinText = (
      rendererJSON: ReactTestRendererNode | Array<ReactTestRendererNode> | null
    ) => {
      Iif (!rendererJSON) return;

      Iif (Array.isArray(rendererJSON)) {
        rendererJSON.forEach(validateStringsRenderedWithinTextForNode);
        return;
      }

      return validateStringsRenderedWithinTextForNode(rendererJSON);
    };

    const validateStringsRenderedWithinTextForNode = (
      node: ReactTestRendererNode
    ) => {
      if (typeof node === 'string') {
        return;
      }

      if (node.type !== 'Text') {
        node.children?.forEach((child) => {
          if (typeof child === 'string') {
            throw new Error(
              `Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "${child}" string within a <${node.type}> component.`
            );
          }
        });
      }

      if (node.children) {
        node.children.forEach(validateStringsRenderedWithinTextForNode);
      }
    };