All files / dom-event-testing-library/src index.js

85.71% Statements 24/28
100% Branches 0/0
86.96% Functions 20/23
85.19% Lines 23/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 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                              187x           5x     30x     3x           20x 20x 20x       17x     2x                       2x       106x       8x       36x       25x       17x       61x     19x     2x       3x                       2x           1x 1x                       14x                          
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
 
'use strict';
 
import { buttonType, buttonsType } from './constants';
import * as domEvents from './domEvents';
import * as domEventSequences from './domEventSequences';
import { hasPointerEvent, setPointerEvent, platform } from './domEnvironment';
import { describeWithPointerEvent, testWithPointerType } from './testHelpers';
 
const createEventTarget = (node) => ({
  node,
  /**
   * Simple events abstraction.
   */
  blur(payload) {
    node.dispatchEvent(domEvents.blur(payload));
  },
  click(payload) {
    node.dispatchEvent(domEvents.click(payload));
  },
  contextmenu(payload) {
    domEventSequences.contextmenu(node, payload);
  },
  error() {
    node.dispatchEvent(domEvents.error());
  },
  focus(payload) {
    domEventSequences.focus(node, payload);
    try {
      node.focus();
    } catch (e) {}
  },
  keydown(payload) {
    node.dispatchEvent(domEvents.keydown(payload));
  },
  keyup(payload) {
    node.dispatchEvent(domEvents.keyup(payload));
  },
  load(payload) {
    node.dispatchEvent(domEvents.load(payload));
  },
  /**
   * PointerEvent abstraction.
   * Dispatches the expected sequence of PointerEvents, MouseEvents, and
   * TouchEvents for a given environment.
   */
  // node no longer receives events for the pointer
  pointercancel(payload) {
    domEventSequences.pointercancel(node, payload);
  },
  // node dispatches down events
  pointerdown(payload) {
    domEventSequences.pointerdown(node, payload);
  },
  // node dispatches move events (pointer is not down)
  pointerhover(payload) {
    domEventSequences.pointerhover(node, payload);
  },
  // node dispatches move events (pointer is down)
  pointermove(payload) {
    domEventSequences.pointermove(node, payload);
  },
  // node dispatches enter & over events
  pointerover(payload) {
    domEventSequences.pointerover(node, payload);
  },
  // node dispatches exit & leave events
  pointerout(payload) {
    domEventSequences.pointerout(node, payload);
  },
  // node dispatches up events
  pointerup(payload) {
    domEventSequences.pointerup(node, payload);
  },
  scroll(payload) {
    node.dispatchEvent(domEvents.scroll(payload));
  },
  select(payload) {
    node.dispatchEvent(domEvents.select(payload));
  },
  // selectionchange is only dispatched on 'document'
  selectionchange(payload) {
    document.dispatchEvent(domEvents.selectionchange(payload));
  },
  /**
   * Gesture abstractions.
   * Helpers for event sequences expected in a gesture.
   * target.tap({ pointerType: 'touch' })
   */
  tap(payload) {
    domEventSequences.pointerdown(payload);
    domEventSequences.pointerup(payload);
  },
  virtualclick(payload) {
    node.dispatchEvent(domEvents.virtualclick(payload));
  },
  /**
   * Utilities
   */
  setBoundingClientRect({ x, y, width, height }) {
    node.getBoundingClientRect = function () {
      return {
        width,
        height,
        left: x,
        right: x + width,
        top: y,
        bottom: y + height
      };
    };
  }
});
 
const clearPointers = domEventSequences.clearPointers;
 
export {
  buttonType,
  buttonsType,
  clearPointers,
  createEventTarget,
  describeWithPointerEvent,
  platform,
  hasPointerEvent,
  setPointerEvent,
  testWithPointerType
};