-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ts
118 lines (93 loc) · 3.72 KB
/
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
import { ActivityMonitor, ACTIVE, INACTIVE } from './'
jest.useFakeTimers()
it('should not notify of inactivity immediately when the user becomes inactive', () => {
const { document, active, inactive } = create(false)
document.hidden = true
document.dispatchEvent(new Event('visibilitychange'))
expect(active).not.toBeCalled()
expect(inactive).not.toBeCalled()
})
it('should notify of inactivity after the INACTIVITY_THRESHOLD when the user becomes inactive', () => {
const { document, active, inactive } = create(false)
document.hidden = true
document.dispatchEvent(new Event('visibilitychange'))
jest.runAllTimers()
expect(active).not.toBeCalled()
expect(inactive).toBeCalled()
})
it('should not notify of inactivity if the user becomes inactive, then becomes active again before the INACTIVITY_THRESHOLD has elapsed', () => {
const { document, active, inactive } = create(false)
document.hidden = true
document.dispatchEvent(new Event('visibilitychange'))
document.hidden = false
document.dispatchEvent(new Event('visibilitychange'))
jest.runAllTimers()
expect(active).not.toBeCalled()
expect(inactive).not.toBeCalled()
})
it('should notify of activity immediately when the user becomes active', () => {
const { document, active, inactive } = create(true)
document.hidden = false
document.dispatchEvent(new Event('visibilitychange'))
expect(active).toBeCalled()
expect(inactive).not.toBeCalled()
})
it('should notify of activity and inactivity if the user becomes active, then becomes inactive again before the INACTIVITY_THRESHOLD has elapsed', () => {
const { document, active, inactive } = create(true)
document.hidden = false
document.dispatchEvent(new Event('visibilitychange'))
document.hidden = true
document.dispatchEvent(new Event('visibilitychange'))
jest.runAllTimers()
expect(active).toBeCalled()
expect(inactive).toBeCalled()
})
it('should notify of activity only if the previous notification was for inactivity', () => {
const { document, active, inactive } = create(false)
document.hidden = false
document.dispatchEvent(new Event('visibilitychange'))
document.dispatchEvent(new Event('visibilitychange'))
document.dispatchEvent(new Event('visibilitychange'))
jest.runAllTimers()
expect(active).not.toBeCalled()
expect(inactive).not.toBeCalled()
})
it('should notify of inactivity only if the previous notification was for activity', () => {
const { document, active, inactive } = create(true)
document.hidden = true
document.dispatchEvent(new Event('visibilitychange'))
document.dispatchEvent(new Event('visibilitychange'))
document.dispatchEvent(new Event('visibilitychange'))
jest.runAllTimers()
expect(active).not.toBeCalled()
expect(inactive).not.toBeCalled()
})
it('should throw an Error if the browser does not support the Page Visibility API', () => {
expect(() => create(false, {})).toThrowError(/support/)
})
it('should clean up after itself when the #destroy method is called', () => {
const { document, monitor } = create(true)
document.removeEventListener = jest.fn()
monitor.destroy()
expect(document.removeEventListener).toBeCalled()
})
function create(initiallyHidden: boolean, customDocument?: any) {
const document: any = {
_listener: null,
addEventListener(_type: string, fn: () => {}) { this._listener = fn },
dispatchEvent() { this._listener && this._listener() },
removeEventListener() { this._listener = null },
hidden: initiallyHidden
}
const active = jest.fn()
const inactive = jest.fn()
const monitor = new ActivityMonitor(customDocument || document)
monitor.on(ACTIVE, active)
monitor.on(INACTIVE, inactive)
return {
document: customDocument || document,
active,
inactive,
monitor
}
}