/
fake-pointer.ts
52 lines (44 loc) · 1.22 KB
/
fake-pointer.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
export class FakePointer {
private x: number = 0
private y: number = 0
constructor(private width: number, private height: number) {
this.width = width
this.height = height
}
get options() {
return {
screenX: this.x,
screenY: this.y,
}
}
randomize() {
this.x = Math.floor(Math.random() * this.width)
this.y = Math.floor(Math.random() * this.height)
}
advance(amount: number = 1) {
this.x += amount
if (this.x >= this.width) {
this.x %= this.width
this.y++
}
if (this.y >= this.height) {
this.y %= this.height
}
}
/**
* JSDOM does not support pointer events.
* Because of this when we try to set the pointer position it returns undefined so our checks fail.
*
* This runs the callback with the TEST_IGNORE_TRACKED_POINTER environment variable set to 1 so we bypass the checks.
*/
bypassingTrackingChecks(callback: () => void) {
let original = process.env.TEST_BYPASS_TRACKED_POINTER
process.env.TEST_BYPASS_TRACKED_POINTER = '1'
callback()
process.env.TEST_BYPASS_TRACKED_POINTER = original
}
}
/**
* A global pointer for use in pointer and mouse event checks
*/
export let pointer = new FakePointer(1920, 1080)