# All files / src/helpers component-tree.ts

100% Statements 24/24
100% Branches 12/12
100% Functions 7/7
100% Lines 24/24

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

    29307x

    4603x
    1x

    4602x
    4602x
    9988x
    1665x

    8323x

    2937x

    19658x
    2884x

    16774x

    16774x
    18747x
    11x

    18736x
    6694x

    12042x

    16774x

    4497x

    4488x
    4488x
    4488x
    3393x

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

    /**
     * Checks if the given element is a host element.
     * @param element The element to check.
     */
    export function isHostElement(element?: ReactTestInstance | null) {
      return typeof element?.type === 'string';
    }

    /**
     * Returns first host ancestor for given element.
     * @param element The element start traversing from.
     */
    export function getHostParent(
      element: ReactTestInstance | null
    ): ReactTestInstance | null {
      if (element == null) {
        return null;
      }

      let current = element.parent;
      while (current) {
        if (isHostElement(current)) {
          return current;
        }

        current = current.parent;
      }

      return null;
    }

    /**
     * Returns host children for given element.
     * @param element The element start traversing from.
     */
    export function getHostChildren(
      element: ReactTestInstance | null
    ): ReactTestInstance[] {
      if (element == null) {
        return [];
      }

      const hostChildren: ReactTestInstance[] = [];

      element.children.forEach((child) => {
        if (typeof child !== 'object') {
          return;
        }

        if (isHostElement(child)) {
          hostChildren.push(child);
        } else {
          hostChildren.push(...getHostChildren(child));
        }
      });

      return hostChildren;
    }

    /**
     * Return the array of host elements that represent the passed element.
     *
     * @param element The element start traversing from.
     * @returns If the passed element is a host element, it will return an array containing only that element,
     * if the passed element is a composite element, it will return an array containing its host children (zero, one or many).
     */
    export function getHostSelves(
      element: ReactTestInstance | null
    ): ReactTestInstance[] {
      return typeof element?.type === 'string'
        ? [element]
        : getHostChildren(element);
    }

    /**
     * Returns host siblings for given element.
     * @param element The element start traversing from.
     */
    export function getHostSiblings(
      element: ReactTestInstance | null
    ): ReactTestInstance[] {
      const hostParent = getHostParent(element);
      const hostSelves = getHostSelves(element);
      return getHostChildren(hostParent).filter(
        (sibling) => !hostSelves.includes(sibling)
      );
    }