forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclicker.spec.js
99 lines (79 loc) · 2.96 KB
/
clicker.spec.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
import clicker from './clicker';
jest.useFakeTimers();
describe('clicker', () => {
const dispatchEventSpy = jest.fn();
const initMouseEventSpy = jest.fn();
let consoleMock;
let chanceMock;
beforeEach(() => {
consoleMock = { log: jest.fn() };
chanceMock = {
natural: ({ max }) => max,
pick: (types) => types[0], // return click types
};
document.body.innerHTML = '<div id="myid">my div</div>';
// can't be delete in afterEach...
if (document.elementFromPoint === undefined) {
Object.defineProperty(document, 'elementFromPoint', {
get: () => () => ({
...document.getElementById('myid'),
dispatchEvent: dispatchEventSpy,
}),
});
}
Object.defineProperty(document.documentElement, 'clientWidth', {
value: 11,
});
Object.defineProperty(document.documentElement, 'clientHeight', {
value: 11,
});
jest.spyOn(document, 'createEvent').mockImplementation(() => ({ initMouseEvent: initMouseEventSpy }));
});
it('should log the cliker', () => {
const species = clicker({ log: true })(consoleMock, chanceMock);
species();
expect(consoleMock.log).toHaveBeenCalledTimes(1);
expect(consoleMock.log).toHaveBeenCalledWith('gremlin', 'clicker ', 'click', 'at', 10, 10);
});
it("should click on element but don't show element", () => {
const species = clicker({ showAction: false })(consoleMock, chanceMock);
species();
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
expect(document.getElementsByTagName('div')).toHaveLength(1);
});
it("should try to click twice on element but can't click at the end", () => {
const canClickSpy = jest.fn();
const species = clicker({ canClick: canClickSpy, maxNbTries: 2 })(consoleMock, chanceMock);
species();
expect(canClickSpy).toHaveBeenCalledTimes(2);
expect(dispatchEventSpy).toHaveBeenCalledTimes(0);
});
it('should click on myid element and add new element', () => {
const species = clicker()(consoleMock, chanceMock);
species();
// Click on myid element
expect(initMouseEventSpy).toHaveBeenCalledWith(
'click',
true,
true,
window,
0,
0,
0,
10,
10,
false,
false,
false,
false,
0,
null
);
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
// Add div element
expect(document.getElementsByTagName('div')).toHaveLength(2);
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 50);
});
});