-
Notifications
You must be signed in to change notification settings - Fork 86
/
idleManager.test.ts
97 lines (88 loc) · 3.52 KB
/
idleManager.test.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
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
import IdleManager from './idleManager';
jest.useFakeTimers();
describe('IdleManager tests', () => {
it('should call its callback after time spent inactive', () => {
const cb = jest.fn();
const manager = IdleManager.create({ onIdle: cb, captureScroll: true });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 10 minutes
jest.advanceTimersByTime(10 * 60 * 1000);
expect(cb).toHaveBeenCalled();
manager.exit();
});
it('should delay allow configuration of the timeout', () => {
const cb = jest.fn();
const extraDelay = 100;
IdleManager.create({ onIdle: cb, idleTimeout: 10 * 60 * 1000 + extraDelay });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 10 minutes
jest.advanceTimersByTime(10 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
jest.advanceTimersByTime(extraDelay);
expect(cb).toHaveBeenCalled();
});
it('should delay its callback on keyboard events', () => {
const cb = jest.fn();
const manager = IdleManager.create({ onIdle: cb });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
document.dispatchEvent(new KeyboardEvent('keypress'));
// wait 5 minutes
jest.advanceTimersByTime(5 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).toHaveBeenCalled();
});
it('should delay its callback on mouse events', () => {
const cb = jest.fn();
const manager = IdleManager.create({ onIdle: cb });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user moving the mouse
document.dispatchEvent(new MouseEvent('mousemove'));
// wait 5 minutes
jest.advanceTimersByTime(5 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).toHaveBeenCalled();
});
it('should delay its callback on touch events', () => {
const cb = jest.fn();
const manager = IdleManager.create({ onIdle: cb });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user touching the screen
document.dispatchEvent(new TouchEvent('touchstart'));
// wait 5 minutes
jest.advanceTimersByTime(5 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).toHaveBeenCalled();
});
it('should delay its callback on scroll events', () => {
const cb = jest.fn();
const scrollDebounce = 100;
const manager = IdleManager.create({ onIdle: cb, captureScroll: true, scrollDebounce });
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes
jest.advanceTimersByTime(9 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user scrolling
document.dispatchEvent(new WheelEvent('scroll'));
// wait 5 minutes
jest.advanceTimersByTime(5 * 60 * 1000);
expect(cb).not.toHaveBeenCalled();
// simulate user being inactive for 9 minutes, plus the debounce
jest.advanceTimersByTime(9 * 60 * 1000 + scrollDebounce);
expect(cb).toHaveBeenCalled();
});
});