forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.ts
60 lines (53 loc) · 1.39 KB
/
event.ts
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
import { ComponentPublicInstance } from 'vue';
import { VueWrapper } from '@vue/test-utils';
function getTouch(el: Element | Window, x: number, y: number) {
return {
identifier: Date.now(),
target: el,
pageX: x,
pageY: y,
clientX: x,
clientY: y,
radiusX: 2.5,
radiusY: 2.5,
rotationAngle: 10,
force: 0.5,
};
}
// Trigger pointer/touch event
export function trigger(
wrapper: VueWrapper<ComponentPublicInstance> | HTMLElement | Window,
eventName: string,
x = 0,
y = 0,
options: any = {}
) {
const el = 'element' in wrapper ? wrapper.element : wrapper;
const touchList = options.touchList || [getTouch(el, x, y)];
if (options.x || options.y) {
touchList.push(getTouch(el, options.x, options.y));
}
const event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, true, true, {});
Object.assign(event, {
clientX: x,
clientY: y,
touches: touchList,
targetTouches: touchList,
changedTouches: touchList,
});
el.dispatchEvent(event);
}
// simulate drag gesture
export function triggerDrag(
el: VueWrapper<ComponentPublicInstance> | HTMLElement,
x = 0,
y = 0
): void {
trigger(el, 'touchstart', 0, 0);
trigger(el, 'touchmove', x / 4, y / 4);
trigger(el, 'touchmove', x / 3, y / 3);
trigger(el, 'touchmove', x / 2, y / 2);
trigger(el, 'touchmove', x, y);
trigger(el, 'touchend', x, y);
}