# All files / src/user-event/utils dispatch-event.ts

100% Statements 27/27
100% Branches 16/16
100% Functions 8/8
100% Lines 27/27

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

    16x
    16x
    1x

    15x
    15x

    199x
    199x
    44x

    155x
    155x

    18x
    18x
    15x

    3x
    3x
    1x

    2x

    217x
    217x
    217x
    170x

    47x

    217x
    217x
    170x

    47x

    217x

     import { ReactTestInstance } from 'react-test-renderer';
    import act from '../../act';
    import { isEventEnabled, isTouchResponder } from '../../fireEvent';
    import { getHostParent } from '../../helpers/component-tree';

    type EventHandler = (event: unknown) => void;

    /**
     * Dispatch event function used by User Event module.
     *
     * @param element element trigger event on
     * @param eventName name of the event
     * @param event event payload
     */
    export function dispatchHostEvent(
      element: ReactTestInstance,
      eventName: string,
      event: unknown
    ) {
      const handler = findEnabledEventHandler(element, eventName);
      if (!handler) {
        return;
      }

      act(() => {
        handler(event);
      });
    }

    /**
     * Dispatch event functio only to given element. Does not look event handler
     * in the ancestors.
     *
     * @param element element trigger event on
     * @param eventName name of the event
     * @param event event payload
     */
    export function dispatchOwnHostEvent(
      element: ReactTestInstance,
      eventName: string,
      event: unknown
    ) {
      const handler = getEnabledEventHandler(element, eventName);
      if (!handler) {
        return;
      }

      act(() => {
        handler(event);
      });
    }

    function findEnabledEventHandler(
      element: ReactTestInstance,
      eventName: string
    ): EventHandler | null {
      const handler = getEnabledEventHandler(element, eventName);
      if (handler) {
        return handler;
      }

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

      return findEnabledEventHandler(parent, eventName);
    }

    function getEnabledEventHandler(
      element: ReactTestInstance,
      eventName: string
    ): EventHandler | null {
      const touchResponder = isTouchResponder(element) ? element : undefined;
      const handler = getEventHandler(element, eventName);
      if (handler && isEventEnabled(element, eventName, touchResponder)) {
        return handler;
      }

      return null;
    }

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

      return undefined;
    }

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