# All files / src/helpers/matchers matchLabelText.ts

100% Statements 7/7
100% Branches 8/8
100% Functions 4/4
100% Lines 7/7

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

    397x

    397x
    397x

    317x
    313x

    4x

    24x

     import { ReactTestInstance } from 'react-test-renderer';
    import { matches, TextMatch, TextMatchOptions } from '../../matches';
    import { findAll } from '../findAll';
    import { matchTextContent } from './matchTextContent';

    export function matchLabelText(
      root: ReactTestInstance,
      element: ReactTestInstance,
      text: TextMatch,
      options: TextMatchOptions = {}
    ) {
      return (
        matchAccessibilityLabel(element, text, options) ||
        matchAccessibilityLabelledBy(
          root,
          element.props.accessibilityLabelledBy,
          text,
          options
        )
      );
    }

    function matchAccessibilityLabel(
      element: ReactTestInstance,
      text: TextMatch,
      options: TextMatchOptions
    ) {
      const { exact, normalizer } = options;
      return matches(text, element.props.accessibilityLabel, normalizer, exact);
    }

    function matchAccessibilityLabelledBy(
      root: ReactTestInstance,
      nativeId: string | undefined,
      text: TextMatch,
      options: TextMatchOptions
    ) {
      if (!nativeId) {
        return false;
      }

      return (
        findAll(
          root,
          (node) =>
            typeof node.type === 'string' &&
            node.props.nativeID === nativeId &&
            matchTextContent(node, text, options)
        ).length > 0
      );
    }