-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_input_manager.test.ts
More file actions
124 lines (96 loc) · 3.23 KB
/
keyboard_input_manager.test.ts
File metadata and controls
124 lines (96 loc) · 3.23 KB
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 { jest } from '@jest/globals'
const { KeyboardInputManager } = await import(
'../src/keyboard_input_manager.js'
)
describe('KeyboardInputManager', () => {
afterEach(() => {
jest.resetAllMocks()
})
describe('on()', () => {
it('Registers an event listener', async () => {
KeyboardInputManager.events['test'] = []
KeyboardInputManager.on('test', () => {})
expect(KeyboardInputManager.events['test'].length).toBe(1)
})
it('Creates an listener array if it does not exist', async () => {
KeyboardInputManager.events = {}
KeyboardInputManager.on('test', () => {})
expect(KeyboardInputManager.events['test'].length).toBe(1)
})
})
describe('emit()', () => {
let callback1: any
let callback2: any
beforeEach(() => {
callback1 = jest.fn()
callback2 = jest.fn()
})
it('Emits an event to all listeners', async () => {
KeyboardInputManager.events['test'] = [callback1, callback2]
KeyboardInputManager.emit('test')
expect(callback1).toHaveBeenCalledTimes(1)
expect(callback2).toHaveBeenCalledTimes(1)
})
it('Passes data to listeners', async () => {
KeyboardInputManager.events['test'] = [callback1, callback2]
KeyboardInputManager.emit('test', 'data')
expect(callback1).toHaveBeenCalledWith('data')
expect(callback2).toHaveBeenCalledWith('data')
})
it('Does nothing if the event does not exist', async () => {
KeyboardInputManager.events = {}
KeyboardInputManager.emit('test')
expect(callback1).not.toHaveBeenCalled()
expect(callback2).not.toHaveBeenCalled()
})
})
describe('restart()', () => {
it('Emits a restart event', async () => {
const preventDefault = jest.fn()
const event = {
preventDefault
}
const emit = jest
.spyOn(KeyboardInputManager, 'emit')
.mockImplementation(() => {})
KeyboardInputManager.restart(event as unknown as Event)
expect(emit).toHaveBeenCalledTimes(1)
expect(preventDefault).toHaveBeenCalledTimes(1)
})
})
describe('constructor', () => {
it('Sets events and listens', async () => {
// Lab 3: Git Bisect
const listen = jest
.spyOn(KeyboardInputManager, 'listen')
.mockImplementation(() => {})
new KeyboardInputManager()
expect(KeyboardInputManager.events).toMatchObject({})
expect(listen).toHaveBeenCalledTimes(1)
})
})
describe('keepPlaying()', () => {
it('Emits a keepPlaying event', async () => {
const preventDefault = jest.fn()
const event = {
preventDefault
}
const emit = jest
.spyOn(KeyboardInputManager, 'emit')
.mockImplementation(() => {})
KeyboardInputManager.keepPlaying(event as unknown as Event)
expect(emit).toHaveBeenCalledTimes(1)
expect(preventDefault).toHaveBeenCalledTimes(1)
})
})
describe('bindButtonPress()', () => {
it('Binds a button press', async () => {
const addEventListener = jest.fn()
document.querySelector = jest.fn().mockReturnValue({
addEventListener
})
KeyboardInputManager.bindButtonPress('.test', () => {})
expect(addEventListener).toHaveBeenCalledTimes(1)
})
})
})