This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
command-registry-spec.js
445 lines (368 loc) · 16.5 KB
/
command-registry-spec.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const CommandRegistry = require('../src/command-registry');
const _ = require('underscore-plus');
const {it, fit, ffit, fffit, beforeEach, afterEach} = require('./async-spec-helpers');
describe("CommandRegistry", () => {
let registry, parent, child, grandchild;
beforeEach(() => {
parent = document.createElement("div");
child = document.createElement("div");
grandchild = document.createElement("div");
parent.classList.add('parent');
child.classList.add('child');
grandchild.classList.add('grandchild');
child.appendChild(grandchild);
parent.appendChild(child);
document.querySelector('#jasmine-content').appendChild(parent);
registry = new CommandRegistry;
registry.attach(parent);
});
afterEach(() => registry.destroy());
describe("when a command event is dispatched on an element", () => {
it("invokes callbacks with selectors matching the target", () => {
let called = false;
registry.add('.grandchild', 'command', function (event) {
expect(this).toBe(grandchild);
expect(event.type).toBe('command');
expect(event.eventPhase).toBe(Event.BUBBLING_PHASE);
expect(event.target).toBe(grandchild);
expect(event.currentTarget).toBe(grandchild);
called = true;
});
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(called).toBe(true);
});
it("invokes callbacks with selectors matching ancestors of the target", () => {
const calls = [];
registry.add('.child', 'command', function (event) {
expect(this).toBe(child);
expect(event.target).toBe(grandchild);
expect(event.currentTarget).toBe(child);
calls.push('child');
});
registry.add('.parent', 'command', function (event) {
expect(this).toBe(parent);
expect(event.target).toBe(grandchild);
expect(event.currentTarget).toBe(parent);
calls.push('parent');
});
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual(['child', 'parent']);
});
it("invokes inline listeners prior to listeners applied via selectors", () => {
const calls = [];
registry.add('.grandchild', 'command', () => calls.push('grandchild'));
registry.add(child, 'command', () => calls.push('child-inline'));
registry.add('.child', 'command', () => calls.push('child'));
registry.add('.parent', 'command', () => calls.push('parent'));
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual(['grandchild', 'child-inline', 'child', 'parent']);
});
it("orders multiple matching listeners for an element by selector specificity", () => {
child.classList.add('foo', 'bar');
const calls = [];
registry.add('.foo.bar', 'command', () => calls.push('.foo.bar'));
registry.add('.foo', 'command', () => calls.push('.foo'));
registry.add('.bar', 'command', () => calls.push('.bar')); // specificity ties favor commands added later, like CSS
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual(['.foo.bar', '.bar', '.foo']);
});
it("orders inline listeners by reverse registration order", () => {
const calls = [];
registry.add(child, 'command', () => calls.push('child1'));
registry.add(child, 'command', () => calls.push('child2'));
child.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual(['child2', 'child1']);
});
it("stops bubbling through ancestors when .stopPropagation() is called on the event", () => {
const calls = [];
registry.add('.parent', 'command', () => calls.push('parent'));
registry.add('.child', 'command', () => calls.push('child-2'));
registry.add('.child', 'command', (event) => {
calls.push('child-1');
event.stopPropagation();
});
const dispatchedEvent = new CustomEvent('command', {bubbles: true});
spyOn(dispatchedEvent, 'stopPropagation');
grandchild.dispatchEvent(dispatchedEvent);
expect(calls).toEqual(['child-1', 'child-2']);
expect(dispatchedEvent.stopPropagation).toHaveBeenCalled();
});
it("stops invoking callbacks when .stopImmediatePropagation() is called on the event", () => {
const calls = [];
registry.add('.parent', 'command', () => calls.push('parent'));
registry.add('.child', 'command', () => calls.push('child-2'));
registry.add('.child', 'command', (event) => {
calls.push('child-1');
event.stopImmediatePropagation();
});
const dispatchedEvent = new CustomEvent('command', {bubbles: true});
spyOn(dispatchedEvent, 'stopImmediatePropagation');
grandchild.dispatchEvent(dispatchedEvent);
expect(calls).toEqual(['child-1']);
expect(dispatchedEvent.stopImmediatePropagation).toHaveBeenCalled();
});
it("forwards .preventDefault() calls from the synthetic event to the original", () => {
registry.add('.child', 'command', event => event.preventDefault());
const dispatchedEvent = new CustomEvent('command', {bubbles: true});
spyOn(dispatchedEvent, 'preventDefault');
grandchild.dispatchEvent(dispatchedEvent);
expect(dispatchedEvent.preventDefault).toHaveBeenCalled();
});
it("forwards .abortKeyBinding() calls from the synthetic event to the original", () => {
registry.add('.child', 'command', event => event.abortKeyBinding());
const dispatchedEvent = new CustomEvent('command', {bubbles: true});
dispatchedEvent.abortKeyBinding = jasmine.createSpy('abortKeyBinding');
grandchild.dispatchEvent(dispatchedEvent);
expect(dispatchedEvent.abortKeyBinding).toHaveBeenCalled();
});
it("copies non-standard properties from the original event to the synthetic event", () => {
let syntheticEvent = null;
registry.add('.child', 'command', event => syntheticEvent = event);
const dispatchedEvent = new CustomEvent('command', {bubbles: true});
dispatchedEvent.nonStandardProperty = 'testing';
grandchild.dispatchEvent(dispatchedEvent);
expect(syntheticEvent.nonStandardProperty).toBe('testing');
});
it("allows listeners to be removed via a disposable returned by ::add", () => {
let calls = [];
const disposable1 = registry.add('.parent', 'command', () => calls.push('parent'));
const disposable2 = registry.add('.child', 'command', () => calls.push('child'));
disposable1.dispose();
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual(['child']);
calls = [];
disposable2.dispose();
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(calls).toEqual([]);
});
it("allows multiple commands to be registered under one selector when called with an object", () => {
let calls = [];
const disposable = registry.add('.child', {
'command-1'() {
calls.push('command-1');
},
'command-2'() {
calls.push('command-2');
}
});
grandchild.dispatchEvent(new CustomEvent('command-1', {bubbles: true}));
grandchild.dispatchEvent(new CustomEvent('command-2', {bubbles: true}));
expect(calls).toEqual(['command-1', 'command-2']);
calls = [];
disposable.dispose();
grandchild.dispatchEvent(new CustomEvent('command-1', {bubbles: true}));
grandchild.dispatchEvent(new CustomEvent('command-2', {bubbles: true}));
expect(calls).toEqual([]);
});
it("invokes callbacks registered with ::onWillDispatch and ::onDidDispatch", () => {
const sequence = [];
registry.onDidDispatch(event => sequence.push(['onDidDispatch', event]));
registry.add('.grandchild', 'command', event => sequence.push(['listener', event]));
registry.onWillDispatch(event => sequence.push(['onWillDispatch', event]));
grandchild.dispatchEvent(new CustomEvent('command', {bubbles: true}));
expect(sequence[0][0]).toBe('onWillDispatch');
expect(sequence[1][0]).toBe('listener');
expect(sequence[2][0]).toBe('onDidDispatch');
expect(sequence[0][1] === sequence[1][1] && sequence[1][1] === sequence[2][1]).toBe(true);
expect(sequence[0][1].constructor).toBe(CustomEvent);
expect(sequence[0][1].target).toBe(grandchild);
});
});
describe("::add(selector, commandName, callback)", () => {
it("throws an error when called with an invalid selector", () => {
const badSelector = '<>';
let addError = null;
try {
registry.add(badSelector, 'foo:bar', () => {});
} catch (error) {
addError = error;
}
expect(addError.message).toContain(badSelector);
});
it("throws an error when called with a null callback and selector target", () => {
const badCallback = null;
expect(() => {
registry.add('.selector', 'foo:bar', badCallback);
}).toThrow(new Error('Cannot register a command with a null listener.'));
});
it("throws an error when called with a null callback and object target", () => {
const badCallback = null;
expect(() => {
registry.add(document.body, 'foo:bar', badCallback);
}).toThrow(new Error('Cannot register a command with a null listener.'));
});
it("throws an error when called with an object listener without a didDispatch method", () => {
const badListener = {
title: 'a listener without a didDispatch callback',
description: 'this should throw an error'
};
expect(() => {
registry.add(document.body, 'foo:bar', badListener);
}).toThrow(new Error('Listener must be a callback function or an object with a didDispatch method.'));
});
});
describe("::findCommands({target})", () => {
it("returns command descriptors that can be invoked on the target or its ancestors", () => {
registry.add('.parent', 'namespace:command-1', () => {});
registry.add('.child', 'namespace:command-2', () => {});
registry.add('.grandchild', 'namespace:command-3', () => {});
registry.add('.grandchild.no-match', 'namespace:command-4', () => {});
registry.add(grandchild, 'namespace:inline-command-1', () => {});
registry.add(child, 'namespace:inline-command-2', () => {});
const commands = registry.findCommands({target: grandchild});
const nonJqueryCommands = _.reject(commands, cmd => cmd.jQuery);
expect(nonJqueryCommands).toEqual([
{name: 'namespace:inline-command-1', displayName: 'Namespace: Inline Command 1'},
{name: 'namespace:command-3', displayName: 'Namespace: Command 3'},
{name: 'namespace:inline-command-2', displayName: 'Namespace: Inline Command 2'},
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'},
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]);
});
it("returns command descriptors with arbitrary metadata if set in a listener object", () => {
registry.add('.grandchild', 'namespace:command-1', () => {});
registry.add('.grandchild', 'namespace:command-2', {
displayName: 'Custom Command 2',
metadata: {
some: 'other',
object: 'data'
},
didDispatch() {}
});
registry.add('.grandchild', 'namespace:command-3', {
name: 'some:other:incorrect:commandname',
displayName: 'Custom Command 3',
metadata: {
some: 'other',
object: 'data'
},
didDispatch() {}
});
const commands = registry.findCommands({target: grandchild});
expect(commands).toEqual([
{
displayName: 'Namespace: Command 1',
name: 'namespace:command-1'
},
{
displayName: 'Custom Command 2',
metadata: {
some : 'other',
object : 'data'
},
name: 'namespace:command-2'
},
{
displayName: 'Custom Command 3',
metadata: {
some : 'other',
object : 'data'
},
name: 'namespace:command-3'
}
]);
});
it("returns command descriptors with arbitrary metadata if set on a listener function", () => {
function listener () {}
listener.displayName = 'Custom Command 2'
listener.metadata = {
some: 'other',
object: 'data'
};
registry.add('.grandchild', 'namespace:command-2', listener);
const commands = registry.findCommands({target: grandchild});
expect(commands).toEqual([
{
displayName : 'Custom Command 2',
metadata: {
some: 'other',
object: 'data'
},
name: 'namespace:command-2'
}
]);
});
});
describe("::dispatch(target, commandName)", () => {
it("simulates invocation of the given command ", () => {
let called = false;
registry.add('.grandchild', 'command', function (event) {
expect(this).toBe(grandchild);
expect(event.type).toBe('command');
expect(event.eventPhase).toBe(Event.BUBBLING_PHASE);
expect(event.target).toBe(grandchild);
expect(event.currentTarget).toBe(grandchild);
called = true;
});
registry.dispatch(grandchild, 'command');
expect(called).toBe(true);
});
it("returns a promise if any listeners matched the command", () => {
registry.add('.grandchild', 'command', () => {});
expect(registry.dispatch(grandchild, 'command').constructor.name).toBe("Promise");
expect(registry.dispatch(grandchild, 'bogus')).toBe(null);
expect(registry.dispatch(parent, 'command')).toBe(null);
});
it("returns a promise that resolves when the listeners resolve", async () => {
jasmine.useRealClock();
registry.add('.grandchild', 'command', () => 1);
registry.add('.grandchild', 'command', () => Promise.resolve(2));
registry.add('.grandchild', 'command', () => new Promise((resolve) => {
setTimeout(() => { resolve(3); }, 1);
}));
const values = await registry.dispatch(grandchild, 'command');
expect(values).toEqual([3, 2, 1]);
});
it("returns a promise that rejects when a listener is rejected", async () => {
jasmine.useRealClock();
registry.add('.grandchild', 'command', () => 1);
registry.add('.grandchild', 'command', () => Promise.resolve(2));
registry.add('.grandchild', 'command', () => new Promise((resolve, reject) => {
setTimeout(() => { reject(3); }, 1);
}));
let value;
try {
value = await registry.dispatch(grandchild, 'command');
} catch (err) {
value = err;
}
expect(value).toBe(3);
});
});
describe("::getSnapshot and ::restoreSnapshot", () =>
it("removes all command handlers except for those in the snapshot", () => {
registry.add('.parent', 'namespace:command-1', () => {});
registry.add('.child', 'namespace:command-2', () => {});
const snapshot = registry.getSnapshot();
registry.add('.grandchild', 'namespace:command-3', () => {});
expect(registry.findCommands({target: grandchild}).slice(0, 3)).toEqual([
{name: 'namespace:command-3', displayName: 'Namespace: Command 3'},
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'},
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]);
registry.restoreSnapshot(snapshot);
expect(registry.findCommands({target: grandchild}).slice(0, 2)).toEqual([
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'},
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]);
registry.add('.grandchild', 'namespace:command-3', () => {});
registry.restoreSnapshot(snapshot);
expect(registry.findCommands({target: grandchild}).slice(0, 2)).toEqual([
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'},
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]);
})
);
describe("::attach(rootNode)", () =>
it("adds event listeners for any previously-added commands", () => {
const registry2 = new CommandRegistry;
const commandSpy = jasmine.createSpy('command-callback');
registry2.add('.grandchild', 'command-1', commandSpy);
grandchild.dispatchEvent(new CustomEvent('command-1', {bubbles: true}));
expect(commandSpy).not.toHaveBeenCalled();
registry2.attach(parent);
grandchild.dispatchEvent(new CustomEvent('command-1', {bubbles: true}));
expect(commandSpy).toHaveBeenCalled();
})
);
});