-
Notifications
You must be signed in to change notification settings - Fork 180
/
core.test.ts
297 lines (224 loc) · 8.26 KB
/
core.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import {simulate} from 'combokeys/test/lib/key-event';
import sniffr from '../global/sniffer';
import shortcuts, {ShortcutsMap} from './core';
describe('Shortcuts', () => {
const key = 'a';
const keyCode = 65;
const key2 = 'b';
const scopeId = 'scope scope scope';
let noop: () => void;
let noop2: () => boolean;
function trigger() {
simulate(key.charCodeAt(0), keyCode);
}
const wrapScope = shortcuts.wrapScope.bind(shortcuts);
beforeEach(() => {
shortcuts.reset();
shortcuts.setScope();
shortcuts.setFilter();
noop = sandbox.stub();
noop2 = sandbox.stub();
});
describe('bind', () => {
it('should throw without a handler', () => {
(() => {
// @ts-expect-error testing a wrong usage
shortcuts.bind();
}).should.throw(Error, 'Shortcut handler should exist');
});
it('should throw without a key', () => {
(() => {
// @ts-expect-error testing a wrong usage
shortcuts.bind({handler: sandbox.stub()});
}).should.throw(Error, 'Shortcut key should exist');
});
it('should bind to root scope', () => {
shortcuts.bind({key, handler: noop});
const scope = shortcuts._scopes[shortcuts.ROOT_SCOPE.scopeId];
should.exist(scope);
scope?.[key].should.equal(noop);
});
it('should bind to custom scope', () => {
shortcuts.bind({key, scope: scopeId, handler: noop});
const scope = shortcuts._scopes[scopeId];
should.exist(scope);
scope?.[key].should.equal(noop);
});
it('should bind array of keys', () => {
const keys = [key, key2];
shortcuts.bind({key: keys, handler: noop});
const scope = shortcuts._scopes[shortcuts.ROOT_SCOPE.scopeId];
should.exist(scope);
scope?.[key].should.equal(noop);
scope?.[key2].should.equal(noop);
});
});
describe('bindMap', () => {
it('should throw without a map', () => {
(() => {
// @ts-expect-error testing a wrong usage
shortcuts.bindMap();
}).should.throw(Error, 'Shortcuts map shouldn\'t be empty');
});
it('should throw with wrong handler', () => {
(() => {
// @ts-expect-error testing a wrong usage
shortcuts.bindMap({a: {}});
}).should.throw(Error, 'Shortcut handler should exist');
});
it('should bind map of keys to root scope', () => {
const keys: ShortcutsMap = {};
keys[key] = noop;
keys[key2] = noop2;
shortcuts.bindMap(keys);
const scope = shortcuts._scopes[shortcuts.ROOT_SCOPE.scopeId];
should.exist(scope);
scope?.[key].should.equal(noop);
scope?.[key2].should.equal(noop2);
});
it('should bind map of keys to custom scope', () => {
const keys: ShortcutsMap = {};
keys[key] = noop;
keys[key2] = noop2;
shortcuts.bindMap(keys, {scope: scopeId});
const scope = shortcuts._scopes[scopeId];
should.exist(scope);
scope?.[key].should.equal(noop);
scope?.[key2].should.equal(noop2);
});
});
describe('unbindScope', () => {
it('should clear scope', () => {
shortcuts.bind({key, scope: scopeId, handler: noop});
shortcuts.unbindScope(scopeId);
should.not.exist(shortcuts._scopes[scopeId]);
});
});
describe('hasKey', () => {
it('should clear scope', () => {
shortcuts.bind({key, scope: scopeId, handler: noop});
shortcuts.hasKey(key, scopeId).should.be.true;
shortcuts.hasKey(key, shortcuts.ROOT_SCOPE.scopeId).should.be.false;
});
});
describe('filter', () => {
it('should setFilter', () => {
shortcuts.setFilter(noop2);
shortcuts.bind({key, handler: noop});
trigger();
noop.should.have.been.called;
noop2.should.have.been.called;
});
it('should prevent handler run', () => {
const stop = sandbox.stub().returns(true);
shortcuts.setFilter(stop);
shortcuts.bind({key, handler: noop});
trigger();
stop.should.have.been.called;
noop.should.not.have.been.called;
});
});
describe('key press', () => {
it('should handle keys in root scope', () => {
shortcuts.bind({key, handler: noop});
trigger();
noop.should.have.been.called;
});
it('should handle keys in root scope with other scope defined', () => {
shortcuts.bind({key, handler: noop});
shortcuts.bind({key, scope: scopeId, handler: noop2});
trigger();
noop.should.have.been.called;
noop2.should.not.have.been.called;
});
it('should handle keys in top scope', () => {
shortcuts.bind({key, handler: noop});
shortcuts.bind({key, scope: scopeId, handler: noop2});
shortcuts.pushScope(scopeId);
trigger();
noop.should.not.have.been.called;
noop2.should.have.been.called;
});
it('should fall trough scopes when returning true', () => {
const fallthrough = sandbox.stub().returns(true);
shortcuts.bind({key, handler: noop});
shortcuts.bind({key, scope: scopeId, handler: fallthrough});
shortcuts.pushScope(scopeId);
trigger();
noop.should.have.been.called;
fallthrough.should.have.been.called;
});
it('should not fall trough modal scope', () => {
const fallthrough = sandbox.stub().returns(true);
shortcuts.bind({key, handler: noop});
shortcuts.bind({key, scope: scopeId, handler: fallthrough});
shortcuts.pushScope(scopeId, {modal: true});
trigger();
fallthrough.should.have.been.called;
noop.should.not.have.been.called;
});
it('should not fall trough modal scope even if it has no handler for key', () => {
const fallthrough = sandbox.stub().returns(true);
shortcuts.bind({key, handler: noop});
shortcuts.bind({key: key2, scope: scopeId, handler: fallthrough});
shortcuts.pushScope(scopeId, {modal: true});
trigger();
noop.should.not.have.been.called;
});
});
describe('scope chain operations', () => {
const scope1 = 'a';
const scope2 = 'bb';
const scope3 = 'ccc';
it('empty scope chain should be equal to default', () => {
shortcuts.getScope().should.deep.equal([]);
});
it('setScope should set full scope chain by string name', () => {
const myscope = 'aaaa';
shortcuts.setScope(myscope);
shortcuts.getScope().should.deep.equal([wrapScope(myscope)]);
});
it('setScope should set full scope chain by array of names', () => {
shortcuts.setScope([scope1, scope2]);
shortcuts.getScope().should.deep.equal([wrapScope(scope1), wrapScope(scope2)]);
});
it('pushScope should add scope to scope chain end', () => {
shortcuts.setScope(scope1);
shortcuts.pushScope(scope2);
shortcuts.getScope().should.deep.equal([wrapScope(scope1), wrapScope(scope2)]);
});
it('popScope should remove by name scope and next scopes from chain', () => {
shortcuts.setScope([scope1, scope2, scope3]);
shortcuts.popScope(scope2);
shortcuts.getScope().should.deep.equal([wrapScope(scope1)]);
});
it('spliceScope should remove by name scope from chain', () => {
shortcuts.setScope([scope1, scope2, scope3]);
shortcuts.spliceScope(scope2);
shortcuts.getScope().should.deep.equal([wrapScope(scope1), wrapScope(scope3)]);
});
it('should store options passed with scope', () => {
shortcuts.pushScope(scope1, {modal: true});
shortcuts.getScope().should.deep.equal([wrapScope(scope1, {modal: true})]);
});
it('should workaround system windows shortcuts', () => {
let eventType: string | undefined;
sandbox.stub(shortcuts.combokeys, 'bind').callsFake((param1, param2, param3) => {
eventType = param3;
});
sandbox.stub(sniffr, 'os').value({name: 'windows'});
shortcuts.bind({key: 'shift+ctrl+0', handler: noop});
should.exist(eventType);
eventType?.should.equal('keyup');
});
it('should not apply workaround for system windows shortcuts on other operating systems', () => {
let eventType;
sandbox.stub(shortcuts.combokeys, 'bind').callsFake((param1, param2, param3) => {
eventType = param3;
});
sandbox.stub(sniffr, 'os').value({name: 'macos'});
shortcuts.bind({key: 'shift+ctrl+0', handler: noop});
('keyup').should.not.equal(eventType);
});
});
});