# All files / src fireEvent.ts

100% Statements 51/51
100% Branches 42/42
100% Functions 12/12
100% Lines 51/51

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

    48x
    581x

    583x
    224x

    359x

    119x
    119x
    4x

    115x
    8x

    107x
    107x
    51x

    56x

    48x

    48x

    267x
    185x

    82x

    12x

    70x
    70x
    70x
    34x

    36x

    366x

    366x
    366x
    51x

    315x
    24x

    291x

    366x
    366x
    80x

    286x
    17x

    269x

    366x

    75x
    75x
    24x

    51x
    51x

    50x

    48x
    34x

    48x
    7x

    48x
    1x

     import { ReactTestInstance } from 'react-test-renderer';
    import act from './act';
    import { getHostParent, isHostElement } from './helpers/component-tree';
    import { getHostComponentNames } from './helpers/host-component-names';

    type EventHandler = (...args: unknown[]) => unknown;

    const isHostTextInput = (element?: ReactTestInstance) => {
      return element?.type === getHostComponentNames().textInput;
    };

    export function isTouchResponder(element: ReactTestInstance) {
      if (!isHostElement(element)) {
        return false;
      }

      return (
        Boolean(element.props.onStartShouldSetResponder) || isHostTextInput(element)
      );
    }

    export function isPointerEventEnabled(
      element: ReactTestInstance,
      isParent?: boolean
    ): boolean {
      const pointerEvents = element.props.pointerEvents;
      if (pointerEvents === 'none') {
        return false;
      }

      if (isParent ? pointerEvents === 'box-only' : pointerEvents === 'box-none') {
        return false;
      }

      const parent = getHostParent(element);
      if (!parent) {
        return true;
      }

      return isPointerEventEnabled(parent, true);
    }

    /**
     * List of events affected by `pointerEvents` prop.
     *
     * Note: `fireEvent` is accepting both `press` and `onPress` for event names,
     * so we need cover both forms.
     */
    const eventsAffectedByPointerEventsProp = new Set(['press', 'onPress']);

    /**
     * List of `TextInput` events not affected by `editable` prop.
     *
     * Note: `fireEvent` is accepting both `press` and `onPress` for event names,
     * so we need cover both forms.
     */
    const textInputEventsIgnoringEditableProp = new Set([
      'contentSizeChange',
      'onContentSizeChange',
      'layout',
      'onLayout',
      'scroll',
      'onScroll',
    ]);

    export function isEventEnabled(
      element: ReactTestInstance,
      eventName: string,
      nearestTouchResponder?: ReactTestInstance
    ) {
      if (isHostTextInput(nearestTouchResponder)) {
        return (
          nearestTouchResponder?.props.editable !== false ||
          textInputEventsIgnoringEditableProp.has(eventName)
        );
      }

      if (
        eventsAffectedByPointerEventsProp.has(eventName) &&
        !isPointerEventEnabled(element)
      ) {
        return false;
      }

      const touchStart = nearestTouchResponder?.props.onStartShouldSetResponder?.();
      const touchMove = nearestTouchResponder?.props.onMoveShouldSetResponder?.();
      if (touchStart || touchMove) {
        return true;
      }

      return touchStart === undefined && touchMove === undefined;
    }

    function findEventHandler(
      element: ReactTestInstance,
      eventName: string,
      nearestTouchResponder?: ReactTestInstance
    ): EventHandler | null {
      const touchResponder = isTouchResponder(element)
        ? element
        : nearestTouchResponder;

      const handler = getEventHandler(element, eventName);
      if (handler && isEventEnabled(element, eventName, touchResponder))
        return handler;

      if (element.parent === null || element.parent.parent === null) {
        return null;
      }

      return findEventHandler(element.parent, eventName, touchResponder);
    }

    function getEventHandler(element: ReactTestInstance, eventName: string) {
      const eventHandlerName = getEventHandlerName(eventName);
      if (typeof element.props[eventHandlerName] === 'function') {
        return element.props[eventHandlerName];
      }

      if (typeof element.props[eventName] === 'function') {
        return element.props[eventName];
      }

      return undefined;
    }

    function getEventHandlerName(eventName: string) {
      return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
    }

    function fireEvent(
      element: ReactTestInstance,
      eventName: string,
      ...data: unknown[]
    ) {
      const handler = findEventHandler(element, eventName);
      if (!handler) {
        return;
      }

      let returnValue;
      act(() => {
        returnValue = handler(...data);
      });

      return returnValue;
    }

    fireEvent.press = (element: ReactTestInstance, ...data: unknown[]) =>
      fireEvent(element, 'press', ...data);

    fireEvent.changeText = (element: ReactTestInstance, ...data: unknown[]) =>
      fireEvent(element, 'changeText', ...data);

    fireEvent.scroll = (element: ReactTestInstance, ...data: unknown[]) =>
      fireEvent(element, 'scroll', ...data);

    export default fireEvent;