# All files / src/helpers findAll.ts

100% Statements 17/17
100% Branches 12/12
100% Functions 4/4
100% Lines 17/17

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

    1592x

    1592x

    1592x
    28x

    1564x
    1564x
    823x

    13538x

    13538x
    13538x
    13478x
    1532x

    11946x

    13538x

    851x

    13538x

    13538x

     import { ReactTestInstance } from 'react-test-renderer';
    import { getConfig } from '../config';
    import { isHiddenFromAccessibility } from './accessiblity';

    interface FindAllOptions {
      /** Match elements hidden from accessibility */
      includeHiddenElements?: boolean;

      /** RTL-compatible alias to `includeHiddenElements` */
      hidden?: boolean;

      /* Exclude any ancestors of deepest matched elements even if they match the predicate */
      matchDeepestOnly?: boolean;
    }

    export function findAll(
      root: ReactTestInstance,
      predicate: (element: ReactTestInstance) => boolean,
      options?: FindAllOptions
    ) {
      const results = findAllInternal(root, predicate, options);

      const includeHiddenElements =
        options?.includeHiddenElements ??
        options?.hidden ??
        getConfig()?.defaultIncludeHiddenElements;

      if (includeHiddenElements) {
        return results;
      }

      const cache = new WeakMap<ReactTestInstance>();
      return results.filter(
        (element) => !isHiddenFromAccessibility(element, { cache })
      );
    }

    // Extracted from React Test Renderer
    // src: https://github.com/facebook/react/blob/8e2bde6f2751aa6335f3cef488c05c3ea08e074a/packages/react-test-renderer/src/ReactTestRenderer.js#L402
    function findAllInternal(
      root: ReactTestInstance,
      predicate: (element: ReactTestInstance) => boolean,
      options?: FindAllOptions
    ): Array<ReactTestInstance> {
      const results: ReactTestInstance[] = [];

      // Match descendants first but do not add them to results yet.
      const matchingDescendants: ReactTestInstance[] = [];
      root.children.forEach((child) => {
        if (typeof child === 'string') {
          return;
        }
        matchingDescendants.push(...findAllInternal(child, predicate, options));
      });

      if (
        // When matchDeepestOnly = true: add current element only if no descendants match
        (!options?.matchDeepestOnly || matchingDescendants.length === 0) &&
        predicate(root)
      ) {
        results.push(root);
      }

      // Add matching descendants after element to preserve original tree walk order.
      results.push(...matchingDescendants);

      return results;
    }