/
iron-beam.test.ts
705 lines (662 loc) · 23.4 KB
/
iron-beam.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import chai = require('chai');
var expect = chai.expect;
import _ = require('lodash');
import IronBeam = require('./iron-beam');
describe('iron-beam', () => {
it("should call a method passed to 'addListener' every time a given event name is emitted with 'emit'", (done) => {
var ib = new IronBeam.EventEmitter();
var count = 0;
ib.addListener('test', () => {
count++;
if (count == 2) {
done();
}
});
expect(ib.emit('test')).to.be.true;
expect(ib.emit('test')).to.be.true;
});
it("should call a method passed to 'on' every time a given event name is emitted with 'emit'", (done) => {
var ib = new IronBeam.EventEmitter();
var count = 0;
ib.on('test', () => {
count++;
if (count == 2) {
done();
}
});
expect(ib.emit('test')).to.be.true;
expect(ib.emit('test')).to.be.true;
});
it("should call a method passed to 'once' one time when a given event name no matter how many times it is emitted with 'emit'", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('test', () => {
done();
});
expect(ib.emit('test')).to.be.true;
expect(ib.emit('test')).to.be.false;
});
it("should pass all arguments passed to 'emit' to the method passed to 'once'", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('test', (...args: any[]) => {
expect(args[0]).to.be.equal(1);
expect(args[1]).to.be.equal(2);
done();
});
expect(ib.emit('test', 1, 2)).to.be.true;
});
it("should not call a method passed to 'on' after 'removeAllListeners' is called with the event name", () => {
var ib = new IronBeam.EventEmitter();
ib.on('test1.test2', () => {
throw new Error("listener should not be called after removeAllListeners has been called");
});
ib.removeAllListeners('test1.test2');
expect(ib.emit('test1.test2')).to.be.false;
});
it("should not call a method passed to 'on' after 'removeAllListeners' is called", () => {
var ib = new IronBeam.EventEmitter();
ib.on('test', () => {
throw new Error("listener should not be called after removeAllListeners has been called");
});
ib.removeAllListeners();
expect(ib.emit('test')).to.be.false;
});
var times = 5;
it("should have zero listeners in array after adding " + times + " listeners, emitting to them, then removeAllListeners", (done) => {
var ib = new IronBeam.EventEmitter();
ib.setMaxListeners(times);
var count = 0;
for(var i = 0; i < times; i++) {
ib.on('test' + i, () => {
count++;
check();
});
}
for(var i = 0; i < times; i++) {
expect(ib.emit('test' + i)).to.be.true;
}
function check() {
if (count === times) {
for(var i = 0; i < times; i++) {
ib.removeAllListeners('test' + i);
}
expect(ib.allListeners().length).to.be.equal(0);
done();
}
}
});
it("should have a 'defaultMaxListeners' property that returns 10", () => {
var ib = new IronBeam.EventEmitter();
expect(ib.defaultMaxListeners).to.be.equal(10);
});
it("should have a 'listeners' method that returns an array of listeners functions for the given event name", () => {
var ib = new IronBeam.EventEmitter();
ib.on('test', () => {}).on('test', () => {});
expect(ib.listeners('test').length).to.be.equal(2);
});
it("should have a static 'listenerCount' method that returns the number of listeners for a given event name", () => {
var ib = new IronBeam.EventEmitter();
ib.on('test', () => {});
ib.on('test', () => {});
expect(IronBeam.EventEmitter.listenerCount(ib, 'test')).to.be.equal(2);
});
it("should emit a 'newListener' event when a listener is added", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('newListener', (e, l) => {
expect(e).to.be.equal('newListener');
expect(typeof l).to.be.equal("function");
done();
});
});
it("should emit a 'removeListener' event when a listener is removed", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('removeListener', (e, l) => {
expect(e).to.be.equal('test');
expect(typeof l).to.be.equal("function");
done();
});
ib.on('test', () => {});
ib.removeAllListeners('test');
});
it("should emit a 'removeListener' event when a listener is removed even if it is a listener to 'removeListener", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('removeListener', (e, l) => {
expect(e).to.be.equal('removeListener');
expect(typeof l).to.be.equal("function");
done();
});
ib.removeAllListeners('removeListener');
});
it("should throw an unhandled error on an error event if there is no listener", (done) => {
var ib = new IronBeam.EventEmitter();
expect(() => {
ib.emit('error');
}).to.throw(Error);
done();
});
it("should not throw an unhandled error on an error event if there is a listener", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('error', () => {
done();
});
ib.emit('error');
});
it("should emit the 'tooManyListeners' event when more listeners are added than what is passed to 'setMaxListeners'", (done) => {
var ib = new IronBeam.EventEmitter();
ib.setMaxListeners(1);
ib.once('tooManyListeners', (eventName, count) => {
expect(eventName).to.be.equal('test');
expect(count).to.be.equal(2);
done();
});
ib.on('test', () => {}).on('test', () => {});
});
it("should work with delimited event names", (done) => {
var ib = new IronBeam.EventEmitter({
delimiter: '#'
});
ib.on('1#2#3#4#5#6#7#8#9', () => {
done();
});
ib.emit('1#2#3#4#5#6#7#8#9');
});
it("should work with wildcarded custom delimited event names", (done) => {
var ib = new IronBeam.EventEmitter({
delimiter: '#'
});
ib.on('1#*', () => {
done();
});
ib.emit('1#2');
});
it("should work with a single wildcard event name", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('*', () => {
done();
});
ib.emit('x');
});
it("should work with wildcard event names", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('1.*.3.*.5.*.7.*.9', () => {
done();
});
ib.emit('1.2.3.4.5.6.7.8.9');
});
it("should call the function passed to preEmit when an event is intercepted", (done) => {
var ib = new IronBeam.EventEmitter();
var intercepted = false;
var test = 'arg';
ib.on('test', () => {
expect(intercepted).to.be.true;
done();
});
ib.intercept('test', {
preEmit: (stop, next, anno, ...args) => {
intercepted = true;
expect(_.isArray(args)).to.be.true;
expect(args[0]).to.be.equal(test);
next(args);
}
});
ib.emit('test', test);
});
it("should call the function passed to preEmit when an event is intercepted even if there are no listeners", (done) => {
var ib = new IronBeam.EventEmitter();
var test = 'arg';
ib.intercept('test', {
preEmit: (stop, next, anno, ...args) => {
expect(_.isArray(args)).to.be.true;
expect(args[0]).to.be.equal(test);
next(args);
done();
}
});
ib.emit('test', test);
});
it("should call the function passed to preEmit when an event is intercepted and pass through modified args", (done) => {
var ib = new IronBeam.EventEmitter();
var intercepted = false;
var test = 'test';
var not = 'not-test';
ib.on('test', (arg) => {
expect(intercepted).to.be.true;
expect(arg).to.be.equal(test);
done();
});
ib.intercept('test', {
preEmit: (stop, next, anno, ...args) => {
intercepted = true;
expect(_.isArray(args)).to.be.true;
expect(args[0]).to.be.equal(not);
args[0] = test;
next(args);
}
});
ib.emit('test', not);
});
it("should call the function passed to preListener before every listener when an event is intercepted", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('test', (data, cb) => {
cb(++data);
});
ib.on('test', (data, cb) => {
cb(++data);
});
ib.intercept('test', {
preListener: (stop, next, anno, ...args) => {
next([
args[0],
(data) => {
expect(data).to.be.equal(1);
args[1](0);
}
]);
}
});
var count = 0;
ib.emit('test', 0, (data) => {
expect(data).to.be.equal(0);
if (++count === 2) {
done();
}
});
});
it("should call the function passed to preListener before every listener when an event is intercepted", (done) => {
var ib = new IronBeam.EventEmitter();
ib.on('test', (data, cb) => {
cb(++data);
});
ib.on('test', (data, cb) => {
cb(++data);
});
ib.intercept('test', {
preListener: (stop, next, anno, ...args) => {
next([
args[0],
(data) => {
expect(data).to.be.equal(1);
args[1](0);
}
]);
}
});
var count = 0;
ib.emit('test', 0, (data) => {
expect(data).to.be.equal(0);
if (++count === 2) {
done();
}
});
});
it("should call the function passed to postEmit when an event is intercepted", (done) => {
var ib = new IronBeam.EventEmitter();
ib.intercept('test', {
postEmit: (stop, next, anno) => {
next();
done();
}
});
ib.emit('test');
});
it("should provide a chainable annotation method so that listeners get listener annotations", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
var testAnno = {
some: "annotation"
};
ib.annotate(testAnno).on('test', (req, cb, anno) => {
expect(req.some).to.be.equal(test.some);
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.some).to.be.equal(testAnno.some);
cb(null);
});
ib.emit('test', test, () => {
done();
});
});
it("should provide all listener's annotation objects to interceptors", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
var testAnno1 = {
some: "annotation",
anno1: "data"
};
var testAnno2 = {
some: "annotation",
anno2: "data"
};
var preEmit = false;
var preListen1 = false;
var listen1 = false;
var postListen1 = false;
var preListen2 = false;
var listen2 = false;
var postListen2 = false;
ib.annotate(testAnno1).on('test', (anno) => {
expect(anno.some).to.be.equal(testAnno1.some);
expect(anno.anno1).to.be.equal(testAnno1.anno1);
expect(_.isUndefined(anno.anno2)).to.be.true;
expect(preEmit).to.be.true;
expect(preListen1).to.be.true;
listen1 = true;
});
ib.annotate(testAnno2).on('test', (anno) => {
expect(anno.some).to.be.equal(testAnno2.some);
expect(anno.anno2).to.be.equal(testAnno2.anno2);
expect(_.isUndefined(anno.anno1)).to.be.true;
expect(preEmit).to.be.true;
expect(preListen1).to.be.true;
expect(listen1).to.be.true;
expect(postListen1).to.be.true;
expect(preListen2).to.be.true;
listen2 = true;
});
ib.intercept('test', {
preEmit: (stop, next, anno, ...args) => {
expect(anno.some).to.be.equal(testAnno1.some);
expect(anno.anno1).to.be.equal(testAnno1.anno1);
expect(anno.anno2).to.be.equal(testAnno2.anno2);
expect(args.length).to.be.equal(0);
preEmit = true;
next(args);
},
preListener: (stop, next, anno, ...args) => {
expect(anno.some).to.be.equal(testAnno1.some);
expect(anno.anno1).to.be.equal(testAnno1.anno1);
expect(anno.anno2).to.be.equal(testAnno2.anno2);
expect(args.length).to.be.equal(0);
expect(preEmit).to.be.true;
if (!preListen1) {
preListen1 = true;
}
else {
expect(postListen1).to.be.true;
preListen2 = true;
}
next(args);
},
postListener: (stop, next, anno, ...args) => {
expect(anno.some).to.be.equal(testAnno1.some);
expect(anno.anno1).to.be.equal(testAnno1.anno1);
expect(anno.anno2).to.be.equal(testAnno2.anno2);
expect(args.length).to.be.equal(0);
expect(preEmit).to.be.true;
expect(preListen1).to.be.true;
if (!postListen1) {
postListen1 = true;
}
else {
expect(listen1).to.be.true;
expect(postListen1).to.be.true;
expect(preListen2).to.be.true;
expect(listen2).to.be.true;
postListen2 = true;
}
next();
},
postEmit: (stop, next, anno, ...args) => {
expect(anno.some).to.be.equal(testAnno1.some);
expect(anno.anno1).to.be.equal(testAnno1.anno1);
expect(anno.anno2).to.be.equal(testAnno2.anno2);
expect(preEmit).to.be.true;
expect(preListen1).to.be.true;
expect(listen1).to.be.true;
expect(postListen1).to.be.true;
expect(preListen2).to.be.true;
expect(listen2).to.be.true;
expect(postListen2).to.be.true;
next();
done();
}
});
ib.emit('test');
});
it("should provide a chainable annotation method so that listeners get emit annotations", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
var testAnno = {
some: "annotation"
};
ib.on('test', (req, cb, anno) => {
expect(req.some).to.be.equal(test.some);
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.some).to.be.equal(testAnno.some);
cb(null);
});
ib.annotate(testAnno).emit('test', test, () => {
done();
});
});
it("should provide a chainable annotation method so that listeners get listener and emitter annotations", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
var listenerAnno = {
listener: "anno"
};
var emitAnno = {
emitter: "anno"
};
ib.annotate(listenerAnno).on('test', (req, cb, anno) => {
expect(req.some).to.be.equal(test.some);
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.listener).to.be.equal(listenerAnno.listener);
expect(anno.emitter).to.be.equal(emitAnno.emitter);
cb(null);
});
ib.annotate(emitAnno).emit('test', test, () => {
done();
});
});
it("should provide all listener's and emitter's annotation objects to interceptors", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
var listenerAnno = {
listener: "anno"
};
var emitAnno = {
emitter: "anno"
};
ib.annotate(listenerAnno).on('test', (cb, anno) => {
cb(null, test);
});
var count = 0;
ib.intercept('test', {
preEmit: (stop, next, anno, ...args) => {
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.listener).to.be.equal(listenerAnno.listener);
expect(anno.emitter).to.be.equal(emitAnno.emitter);
next(args);
count++;
},
preListener: (stop, next, anno, ...args) => {
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.listener).to.be.equal(listenerAnno.listener);
expect(anno.emitter).to.be.equal(emitAnno.emitter);
next(args);
count++;
},
postListener: (stop, next, anno, ...args) => {
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.listener).to.be.equal(listenerAnno.listener);
expect(anno.emitter).to.be.equal(emitAnno.emitter);
next();
count++;
},
postEmit: (stop, next, anno) => {
expect(!_.isUndefined(anno)).to.be.true;
expect(anno.listener).to.be.equal(listenerAnno.listener);
expect(anno.emitter).to.be.equal(emitAnno.emitter);
expect(count).to.be.equal(3);
next();
done();
}
});
ib.annotate(emitAnno).emit('test', (e, res) => {
expect(res.some).to.be.equal(test.some);
});
});
it("should provide the arguments to postEmit interceptors", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
ib.on('test', (anno) => {
});
ib.intercept('test', {
postEmit: (stop, next, anno, ...args) => {
expect(args).to.be.an('array');
expect(args.length).to.be.equal(1);
expect(args[0].some).to.be.equal(test.some);
next();
done();
}
});
ib.emit('test', test);
});
it("should provide the arguments to postEmit interceptors when there are no listeners", (done) => {
var ib = new IronBeam.EventEmitter();
var test = {
some: "data"
};
ib.intercept('test', {
postEmit: (stop, next, anno, ...args) => {
expect(args).to.be.an('array');
expect(args.length).to.be.equal(1);
expect(args[0].some).to.be.equal(test.some);
next();
done();
}
});
ib.emit('test', test);
});
it("should provide 'annotatedListeners' method that returns an array of listeners for the given event name whose annotations match", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('not-test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('not-test-wrong-anno', () => {});
ib.annotate({
x: 0,
y: 1
}).once('not-test-right-anno', () => {});
ib.once('test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('test', () => {});
ib.annotate({
x: 0,
y: 1
}).once('test', () => {});
var testAnyAnno = ib.annotatedListeners('test');
var testXAnno = ib.annotatedListeners('test', {
x: 0
});
expect(testAnyAnno.length).to.be.equal(2);
expect(testXAnno.length).to.be.equal(1);
done();
});
it("should provide 'allAnnotatedListeners' method that returns an array of listeners whose annotations match", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('not-test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('not-test-wrong-anno', () => {});
ib.annotate({
x: 0,
y: 1
}).once('not-test-right-anno', () => {});
ib.once('test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('test', () => {});
ib.annotate({
x: 0,
y: 1
}).once('test', () => {});
var allAnno = ib.allAnnotatedListeners();
var allXAnno = ib.allAnnotatedListeners({
x: 0
});
expect(allAnno.length).to.be.equal(4);
expect(allXAnno.length).to.be.equal(2);
done();
});
it("should provide 'removeAnnotatedListeners' method that removes any listeners for the given event name whose annotations match", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('not-test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('not-test-wrong-anno', () => {});
ib.annotate({
x: 0,
y: 1
}).once('not-test-right-anno', () => {});
ib.once('test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('test', () => {});
ib.annotate({
x: 0,
y: 1
}).once('test', () => {});
ib.removeAnnotatedListeners('test', {
x: 0
});
var testXAnno = ib.annotatedListeners('test', {
x: 0
});
expect(testXAnno.length).to.be.equal(0);
ib.removeAnnotatedListeners('test');
var testAnno = ib.annotatedListeners('test');
expect(testAnno.length).to.be.equal(0);
done();
});
it("should provide 'removeAllAnnotatedListeners' method that removes any listeners whose annotations match", (done) => {
var ib = new IronBeam.EventEmitter();
ib.once('not-test', () => {});
ib.annotate({
x: 0,
y: 1
}).once('not-test-wrong-anno', () => {});
ib.annotate({
x: 1,
y: 1
}).once('not-test-right-anno', () => {});
ib.once('test', () => {});
ib.annotate({
x: 1,
y: 1
}).once('test', () => {});
ib.annotate({
x: 0,
y: 1
}).once('test', () => {});
ib.removeAllAnnotatedListeners({
x: 0
});
var xAnno = ib.allAnnotatedListeners({
x: 0
});
expect(xAnno.length).to.be.equal(0);
ib.removeAllAnnotatedListeners();
var anyAnno = ib.allAnnotatedListeners();
expect(anyAnno.length).to.be.equal(0);
done();
});
});