-
Notifications
You must be signed in to change notification settings - Fork 88
/
idleManager.test.ts
124 lines (110 loc) · 4.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { IdleManager } from './idleManager';
jest.useFakeTimers();
const { location } = window;
beforeEach(() => {
delete (window as any).location;
(window as any).location = { reload: jest.fn() };
});
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 replace the default callback if a callback is passed during creation', () => {
const idleFn = jest.fn();
IdleManager.create({ onIdle: idleFn });
expect(window.location.reload).not.toHaveBeenCalled();
// simulate user being inactive for 10 minutes
jest.advanceTimersByTime(10 * 60 * 1000);
expect(window.location.reload).not.toHaveBeenCalled();
expect(idleFn).toBeCalled();
});
it('should replace the default callback if a callback is registered', () => {
const manager = IdleManager.create();
manager.registerCallback(jest.fn());
expect(window.location.reload).not.toHaveBeenCalled();
// simulate user being inactive for 10 minutes
jest.advanceTimersByTime(10 * 60 * 1000);
expect(window.location.reload).not.toHaveBeenCalled();
});
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('keydown'));
// 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();
});
});