forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidle.test.ts
58 lines (50 loc) · 1.65 KB
/
idle.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
import { IdleManager } from './idle'
describe('Browser idle manager', () => {
let instance: IdleManager
beforeEach(() => {
instance = new IdleManager()
})
test('Can schedule idle handlers', () => {
const mockCbs = {
idle: jest.fn(),
locked: jest.fn(),
active: jest.fn(),
}
instance.scheduleIdleCbs({
onIdle: mockCbs.idle,
onLocked: mockCbs.locked,
onActive: mockCbs.active,
})
// Check the idle handlers exist inside instance
const handlerSets = instance['handlers']
for (const idleState of ['idle', 'locked', 'active']) {
expect([...handlerSets[idleState]]).toEqual(
expect.arrayContaining([mockCbs[idleState]]),
)
}
})
test('Can invoke scheduled idle handlers', () => {
const mockCbs = {
idle: jest.fn(),
locked: jest.fn(),
active: jest.fn(),
}
instance.scheduleIdleCbs({
onIdle: mockCbs.idle,
onLocked: mockCbs.locked,
onActive: mockCbs.active,
})
// Should be scheduled, but not yet invoked
for (const mockFn of Object.values(mockCbs)) {
expect(mockFn.mock.calls.length).toBe(0)
}
const simulateIdleChange = state => {
expect(mockCbs[state].mock.calls.length).toBe(0)
instance.handleIdleStateChange(state)
expect(mockCbs[state].mock.calls.length).toBe(1)
}
simulateIdleChange('idle')
simulateIdleChange('active')
simulateIdleChange('locked')
})
})