-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoo-test-helpers.js
112 lines (104 loc) · 3.51 KB
/
oo-test-helpers.js
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
;(function (global) {
const MockInteractions = window.MockInteractions
var HAS_NEW_MOUSE = (function() {
var has = false;
try {
has = Boolean(new MouseEvent('x'));
} catch (_) {}
return has;
})();
function makeMouseEvent(type, xy, node) {
var props = {
bubbles: true,
cancelable: true,
clientX: xy.x,
clientY: xy.y,
// Allow event to go outside a ShadowRoot.
composed: true,
// Make this a primary input.
buttons: 1 // http://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
};
var e;
if (HAS_NEW_MOUSE) {
e = new MouseEvent(type, props);
} else {
e = document.createEvent('MouseEvent');
e.initMouseEvent(
type, props.bubbles, props.cancelable,
null, /* view */
null, /* detail */
0, /* screenX */
0, /* screenY */
props.clientX, props.clientY,
false, /*ctrlKey */
false, /*altKey */
false, /*shiftKey */
false, /*metaKey */
0, /*button */
null /*relatedTarget*/);
}
node.dispatchEvent(e);
}
/**
* Returns the (x,y) coordinates representing the middle of a node.
*
* @param {!Element} node An element.
*/
function middleOfNode(node) {
var bcr = node.getBoundingClientRect();
return {
y: bcr.top + (bcr.height / 2),
x: bcr.left + (bcr.width / 2)
};
}
/**
* Generate a click event on a given node, optionally at a given coordinate.
* @param {!Element} node The node to fire the click event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should
* be fired from.
*/
function click(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('click', xy, node);
}
/**
* Fires a `mouseenter` mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable. If the (x,y) coordinates are
* not specified, the middle of the node will be used instead.
*
* @param {!Element} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should be fired from.
*/
function mouseenter(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('mouseenter', xy, node);
}
/**
* Fires a `mouseenter` mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable. If the (x,y) coordinates are
* not specified, the middle of the node will be used instead.
*
* @param {!Element} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should be fired from.
*/
function mouseover(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('mouseover', xy, node);
}
/**
* Fires a `mouseleave` mouse event on a specific node, at a given set of coordinates.
* This event bubbles and is cancellable. If the (x,y) coordinates are
* not specified, the middle of the node will be used instead.
*
* @param {!Element} node The node to fire the event on.
* @param {{ x: number, y: number }=} xy Optional. The (x,y) coordinates the mouse event should be fired from.
*/
function mouseleave(node, xy) {
xy = xy || middleOfNode(node);
makeMouseEvent('mouseleave', xy, node);
}
MockInteractions.click = click;
MockInteractions.mouseenter = mouseenter;
MockInteractions.mouseleave = mouseleave;
MockInteractions.mouseover = mouseover;
})(this)