/
bindings.test.js
168 lines (135 loc) · 3.71 KB
/
bindings.test.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const keybinds = require('../src/index.ts')
let isOSMac = process.platform === 'darwin'
const map = {}
window.addEventListener = jest.fn((event, cb) => {
map[event] = cb
})
// global.console = {
// warn: jest.fn(),
// log: jest.fn()
// }
keybinds.Keybindings({
debug: true,
el: window,
binds: [
{
bindName: 'Bind Test',
key: 'd',
desc: 'Initial Bind Test',
keydown: (event, details) => {
console.log('keydown test')
},
keyup: (event, details) => {
console.log('keyup test')
}
}
]
})
// Test that the current platform is checked and matches
test('OS Platform Test', () => {
const isMac = expect(keybinds.isMac).toBe(isOSMac)
})
// Test that binds are being set using `Keybindings`
test('Intial Bind', () => {
const binds = keybinds.getAllBinds()
expect(binds.length).toBe(1)
})
// Test `attachBind` is correctly adding binds
test('Attach Second Bind', () => {
keybinds.attachBind({
bindName: 'Second Bind',
key: 'shift+t',
desc: 'Second Bind'
})
const binds = keybinds.getAllBinds()
expect(binds.length).toBe(2)
})
// Test removal of binds by bind name
test('Remove Bind By Name', () => {
keybinds.attachBind({
bindName: 'Remove Bind By Name',
key: 'r',
desc: 'test 2'
})
keybinds.removeBind('Remove Bind By Name')
const binds = keybinds.getAllBinds()
expect(binds.length).toBe(2)
})
// Test removal of binds by bind key
test('Remove Bind By Key', () => {
keybinds.attachBind({
bindName: 'Remove Bind By Key',
key: 'shift+r',
desc: 'test 2'
})
keybinds.removeBind('shift+r')
const binds = keybinds.getAllBinds()
expect(binds.length).toBe(2)
})
// Test event listeners
test('Keydown bind test', () => {
const log = jest.spyOn(global.console, 'log')
map.keydown({
altKey: false,
code: 'KeyD',
ctrlKey: false,
key: 'd',
keyCode: 68,
metaKey: false,
repeat: false,
shiftKey: false,
type: 'keydown',
which: 68
})
expect(log).toHaveBeenCalledWith('keydown test')
})
test('keyup bind test', () => {
const log = jest.spyOn(global.console, 'log')
map.keydown({
altKey: false,
code: 'KeyD',
ctrlKey: false,
key: 'd',
keyCode: 68,
metaKey: false,
repeat: false,
shiftKey: false,
type: 'keyup',
which: 68
})
expect(log).toHaveBeenCalledWith('keyup test')
})
// Test duplicate bind warning
test('Attach Second Bind With Duplicate Key Warning', () => {
const log = jest.spyOn(global.console, 'warn')
keybinds.attachBind({
bindName: 'Second Bind Duplicate',
key: 'shift+t',
desc: 'Second Bind'
})
expect(log).toHaveBeenCalledWith('A bind already exists for shift+t called Second Bind')
})
// Test duplicate bind forced same name warning
test('Attach Second Bind Forced With Duplicate Name Warning', () => {
const log = jest.spyOn(global.console, 'warn')
keybinds.attachBind({
bindName: 'Second Bind',
key: 'shift+t',
desc: 'Second Bind',
forceBind: true
})
expect(log).toHaveBeenCalledWith(
'A bind already exists named "Second Bind". Multiple binds with exact names not allowed.'
)
})
// Test duplicate bind forced
test('Attach Second Bind Forced', () => {
keybinds.attachBind({
bindName: 'Second Bind Forced',
key: 'shift+t',
desc: 'Second Bind Forced',
forceBind: true
})
const binds = keybinds.getAllBinds()
expect(binds.length).toBe(3)
})