-
Notifications
You must be signed in to change notification settings - Fork 428
/
typer.spec.js
96 lines (78 loc) · 2.88 KB
/
typer.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
import typer from './typer';
jest.useFakeTimers();
describe('typer', () => {
const dispatchEventSpy = jest.fn();
const initMouseEventSpy = jest.fn();
let consoleMock;
let inputText;
let chanceMock;
let config;
beforeEach(() => {
consoleMock = { log: jest.fn() };
chanceMock = {
natural: ({ min, max }) => (min ? 65 : max), // 65 = a
pick: (types) => types[0],
};
inputText = document.createElement('input');
inputText.setAttribute('type', 'text');
inputText.setAttribute('id', 'myid');
document.body.appendChild(inputText);
// 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 }));
config = {
logger: consoleMock,
randomizer: chanceMock,
window,
};
});
afterEach(() => {
document.body.innerHTML = '';
});
it('should log the typer', () => {
const species = typer({ log: true })(config);
species();
expect(consoleMock.log).toHaveBeenCalledTimes(1);
expect(consoleMock.log).toHaveBeenCalledWith('gremlin', 'typer type', 'A', 'at', 10, 10);
});
it("should type on element but don't show element", () => {
const species = typer({ showAction: false })(config);
species();
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
expect(dispatchEventSpy).toHaveBeenCalledWith({
initMouseEvent: initMouseEventSpy,
keyCode: 65,
which: 65,
keyCodeVal: 65,
});
expect(document.getElementsByTagName('div')).toHaveLength(0);
});
it('should type on myid element and add new element', () => {
const species = typer()(config);
species();
expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
expect(dispatchEventSpy).toHaveBeenCalledWith({
initMouseEvent: initMouseEventSpy,
keyCode: 65,
which: 65,
keyCodeVal: 65,
});
expect(document.getElementsByTagName('div')).toHaveLength(1);
expect(setTimeout).toHaveBeenCalledTimes(2);
expect(setTimeout).toHaveBeenNthCalledWith(1, expect.any(Function), 1000);
expect(setTimeout).toHaveBeenNthCalledWith(2, expect.any(Function), 50);
});
});