Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50660 views
1
var basicEvents = require('nodeunit').testCase;
2
3
/////helper///////
4
function setHelper (emitter, test, testName){
5
var eventNames = [
6
testName,
7
testName + '.*',
8
testName + '.ns1',
9
testName + '.ns1.ns2',
10
testName + '.ns2.*'
11
];
12
13
for (var i = 0; i < eventNames.length; i++) {
14
emitter.on(eventNames[i], function () {
15
test.ok(true, eventNames[i] + 'has fired');
16
});
17
}
18
19
return eventNames;
20
};
21
22
module.exports = basicEvents({
23
24
setUp: function (callback) {
25
var EventEmitter2;
26
27
if(typeof require !== 'undefined') {
28
EventEmitter2 = require('../../lib/eventemitter2').EventEmitter2;
29
}
30
else {
31
EventEmitter2 = window.EventEmitter2;
32
}
33
34
this.emitter = new EventEmitter2({
35
wildcard: true,
36
verbose: true
37
});
38
39
callback();
40
},
41
42
tearDown: function (callback) {
43
//clean up?
44
callback();
45
},
46
47
'1. An event can be namespaced.': function (test) {
48
49
var emitter = this.emitter;
50
51
emitter.on('test1.ns1', function () {
52
test.ok(true, 'The event was raised');
53
});
54
55
emitter.emit('test1.ns1');
56
57
test.expect(1);
58
test.done();
59
60
},
61
'2. An event can be namespaced and accept values.': function (test) {
62
63
var emitter = this.emitter;
64
65
emitter.on('test2.ns1', function(value1) {
66
test.ok(true, 'The event was raised');
67
test.ok(typeof value1 !== 'undefined', 'The event was raised with the value `' + value1 + '`.');
68
});
69
70
emitter.emit('test2.ns1', 1);
71
72
test.expect(2);
73
test.done();
74
75
},
76
'3. A namespaced event can be raised multiple times and accept values.': function (test) {
77
78
var emitter = this.emitter;
79
80
emitter.on('test3.ns1', function (value1, value2, value3) {
81
test.ok(true, 'The event was raised');
82
test.ok(arguments.length === 3, 'The event was raised with the correct number of arguments');
83
test.ok(value1 === 1 || value1 === 4, 'The event was raised with the value `' + value1 + '`.');
84
test.ok(value2 === 2 || value2 === 5, 'The event was raised with the value `' + value2 + '`.');
85
test.ok(value3 === 3 || value3 === 6, 'The event was raised with the value `' + value3 + '`.');
86
});
87
88
emitter.emit('test3.ns1', 1, 2, 3);
89
emitter.emit('test3.ns1', 4, 5, 6);
90
91
test.expect(10);
92
test.done();
93
},
94
'4. A listener should support wild cards.': function (test) {
95
96
var emitter = this.emitter;
97
98
emitter.on('test4.*', function () {
99
test.ok(true, 'The event was raised');
100
});
101
102
emitter.emit('test4.ns1');
103
104
test.expect(1);
105
test.done();
106
107
},
108
'5. Emitting an event should support wildcards.': function (test) {
109
110
var emitter = this.emitter;
111
112
emitter.on('test5A.test5B', function () {
113
test.ok(true, 'The event was raised');
114
});
115
116
emitter.emit('test5A.*');
117
118
test.expect(1);
119
test.done();
120
121
},
122
'6. A listener should support complex wild cards.': function (test) {
123
124
var emitter = this.emitter;
125
126
emitter.on('test10.*.foo', function () {
127
test.ok(true, 'The event was raised');
128
});
129
130
emitter.emit('test10.ns1.foo');
131
132
test.expect(1);
133
test.done();
134
135
},
136
'7. Emitting an event should support complex wildcards.': function (test) {
137
138
var emitter = this.emitter;
139
140
emitter.on('test11.ns1.foo', function () {
141
test.ok(true, 'The event was raised');
142
});
143
144
emitter.emit('test11.*.foo');
145
146
test.expect(1);
147
test.done();
148
149
},
150
'8. Emitting an event should support complex wildcards multiple times, a valid listener should accept values.': function (test) {
151
152
var emitter = this.emitter;
153
154
emitter.on('test12.ns1.ns2', function (value1, value2, value3) {
155
test.ok(true, 'The event was raised');
156
test.ok(arguments.length === 3, 'The event was raised with the correct number of arguments');
157
test.ok(value1 === 1 || value1 === 4, 'The event was raised with the value `' + value1 + '`.');
158
test.ok(value2 === 2 || value2 === 5, 'The event was raised with the value `' + value1 + '`.');
159
test.ok(value3 === 3 || value3 === 6, 'The event was raised with the value `' + value1 + '`.');
160
});
161
162
emitter.emit('test12.*.ns2', 1, 2, 3);
163
emitter.emit('test12.*.ns2', 4, 5, 6);
164
165
test.expect(10);
166
test.done();
167
168
},
169
'9. List all the listeners for a particular event.': function(test) {
170
171
var emitter = this.emitter;
172
173
emitter.on('test13', function (event) {
174
test.ok(true,'raised one');
175
});
176
177
emitter.on('test13', function (event) {
178
test.ok(true,'raised two');
179
});
180
181
var listeners = emitter.listeners('test13');
182
183
test.ok(listeners.length === 2, 'The event `test13` should have 2 listeners');
184
test.expect(1);
185
test.done();
186
187
},
188
'10. should be able to listen on any event' : function (test) {
189
190
var emitter = this.emitter;
191
192
var fn = function (foo, bar) {
193
test.equal(this.event, 'test23.ns5.ns5')
194
test.equal(foo, 'foo');
195
test.equal(bar, 1);
196
test.ok(true, 'raised test23.ns5.ns5');
197
}
198
199
emitter.onAny(fn);
200
emitter.emit('test23.ns5.ns5', 'foo', 1);
201
test.expect(4);
202
test.done();
203
204
},
205
/*,
206
'11. A listener should support total wild card.': function (test) {
207
208
var emitter = this.emitter;
209
210
emitter.on('*', function () {
211
test.ok(true, 'The event was raised');
212
});
213
214
emitter.emit('test14');
215
emitter.emit('test14.ns1');
216
emitter.emit('test14.ns1.ns2');
217
218
test.expect(1);
219
test.done();
220
221
},
222
223
'12. A listener should support complex total wild card.': function (test) {
224
225
var emitter = this.emitter;
226
227
emitter.on('*', function () {
228
test.ok(true, 'The event was raised');
229
});
230
231
emitter.emit('test15.*');
232
emitter.emit('test15.*.ns2')
233
emitter.emit('*');
234
235
test.expect(1);
236
test.done();
237
238
},
239
'13. Should be able to fire with wildcard start.' : function (test) {
240
var emitter = this.emitter;
241
242
emitter.on('test16', function () {
243
test.ok(true, 'The event test15 was raised');
244
});
245
emitter.on('test16.ns1', function () {
246
test.ok(true, 'The event test15.ns1 was raised');
247
});
248
249
emitter.emit('*');
250
emitter.emit('*.ns1');
251
252
test.expect(2);
253
test.done();
254
},
255
'14. Should fail if delimiter is used to start or end event name.' : function (test) {
256
var emitter = this.emitter;
257
258
//nothing should emit, so here is a all-listener
259
emitter.on('*.*.*', function () {
260
test.ok(false, 'an event was raised!');
261
});
262
emitter.on('*.*', function () {
263
test.ok(false, 'an event was raised!');
264
});
265
emitter.on('*', function () {
266
test.ok(false, 'an event was raised!');
267
});
268
269
try {
270
emitter.on('.ns4', function () {
271
test.ok(false, 'The event .ns4 was raised');
272
});
273
274
emitter.emit('.ns4');
275
}
276
catch(ex) {
277
test.ok(true, 'The event .ns4 was not raised');
278
}
279
280
try {
281
282
emitter.on('ns4.', function () {
283
test.ok(false, 'The event .ns4 was raised');
284
});
285
286
emitter.emit('ns4.');
287
}
288
catch(ex) {
289
test.ok(true, 'The event .ns4 was not raised');
290
}
291
292
try {
293
294
emitter.on('.ns4', function () {
295
test.ok(false, 'The event .ns4 was raised');
296
});
297
298
emitter.emit('ns4.');
299
}
300
catch(ex) {
301
test.ok(true, 'The event .ns4 was not raised');
302
}
303
304
try {
305
306
emitter.on('.ns4', function () {
307
test.ok(false, 'The event .ns4 was raised');
308
});
309
310
}
311
catch(ex) {
312
test.ok(true, 'The event .ns4 was not raised');
313
}
314
315
try {
316
317
emitter.emit('ns4.');
318
319
}
320
catch(ex) {
321
test.ok(true, 'The event .ns4 was not raised');
322
}
323
324
try {
325
emitter.emit('some..bad');
326
}
327
catch (ex) {
328
test.ok(true, 'error was raised');
329
}
330
331
try {
332
emitter.on('some..bad', function () {
333
test.ok(false, 'a bad event was raised');
334
});
335
}
336
catch (ex){
337
test.ok(true,'error was raised');
338
}
339
340
test.expect(7);
341
test.done();
342
},
343
344
'15. Should provide case sensitive option.' : function (test) {
345
var emitter = this.emitter;
346
347
emitter.on('test18', function () {
348
test.ok(false, 'The event test18 was raised');
349
});
350
emitter.on('test18.ns1', function () {
351
test.ok(false, 'The event test18.ns1 was raised');
352
});
353
354
emitter.emit('Test18');
355
356
test.expect(0);
357
test.done();
358
},
359
360
'16. one emit should be able to fire on multiple namespaces.' : function (test) {
361
var emitter = this.emitter;
362
363
emitter.on('test19.*', function () {
364
test.ok(true, 'test19.* was raised');
365
});
366
emitter.on('test19.foo', function () {
367
test.ok(true, 'test19.foo was raised');
368
});
369
370
emitter.emit('test19.foo');
371
test.expect(2);
372
test.done();
373
},
374
375
'17. should support case insensitivty (complex).' : function (test) {
376
if(typeof require !== 'undefined') {
377
EventEmitter2 = require('../lib/ee2').EventEmitter2;
378
}
379
else {
380
EventEmitter2 = window.EventEmitter2;
381
}
382
383
var emitter = new EventEmitter2({ caseSensitive : false});
384
385
emitter.on('test20', function () {
386
test.ok(true, 'The event test20 was raised');
387
});
388
emitter.on('test20.ns1', function () {
389
test.ok(true, 'The event test20.ns1 was raised');
390
});
391
emitter.on('*.ns1', function () {
392
test.ok(true, 'The event *.ns1 was raised');
393
});
394
emitter.on('*.ns2', function () {
395
test.ok(false, 'The event *.ns2 was raised');
396
});
397
398
emitter.emit('Test20');
399
emitter.emit('TeSt20.nS1');
400
401
test.expect(3);
402
test.done();
403
},
404
405
'18. should be able to removeListeners' : function (test) {
406
var emitter = this.emitter;
407
408
var someFun = function () {
409
test.ok(true, 'someFunc was raised');
410
}
411
412
emitter.on('test21', someFun);
413
emitter.on('test21.*', someFun);
414
emitter.on('test21.ns1', someFun);
415
emitter.on('test21.ns1.ns2', someFun);
416
417
emitter.emit('test21'); //1
418
emitter.emit('test21.ns2'); //1
419
emitter.emit('test21.ns1'); //2
420
421
var listeners = emitter.listeners('test21');
422
test.ok(listeners.length === 1, 'there should be 1 listener');
423
424
emitter.removeListener('test21', someFun);
425
listeners = emitter.listeners('test21');
426
test.ok(listeners.length === 0, 'there should be 0 listener (empty array)');
427
428
// should be able to add more listeners after removing
429
emitter.on('test21', someFun);
430
emitter.on('test21', someFun);
431
listeners = emitter.listeners('test21');
432
test.ok(listeners.length === 2, 'there should be 2 listeners'); //1
433
434
emitter.emit('test21'); //2
435
436
emitter.removeListener('test21', someFun); //this removes all listeners
437
listeners = emitter.listeners('test21');
438
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
439
emitter.removeListener('test21', someFun); //this removes all listeners
440
listeners = emitter.listeners('test21');
441
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
442
443
emitter.emit('test21'); //0
444
445
listeners = emitter.listeners('test21.ns1');
446
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
447
448
emitter.removeListener('test21.ns1', someFun); // remove one
449
listeners = emitter.listeners('test21.ns1');
450
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
451
452
listeners = emitter.listeners('test21.*');
453
test.ok(listeners.length === 1, 'there should be 1 listeners'); //1
454
emitter.removeListener('test21.*', someFun); // remove one
455
listeners = emitter.listeners('test21.*');
456
test.ok(listeners.length === 0, 'there should be 0 listeners'); //1
457
458
test.expect(15);
459
test.done();
460
},
461
462
'19. should be able to remove all listeners' : function (test) {
463
464
var emitter = this.emitter,
465
addedEvents = setHelper(emitter, test, 'test22');
466
467
emitter.emit('test22'); //1
468
469
var listeners = emitter.listeners('test22');
470
test.ok(listeners.length === 1, 'there should be 1 listener'); //1
471
472
emitter.removeAllListeners('test22');
473
listeners = emitter.listeners('test22');
474
test.ok(listeners.length === 0, 'there should be 0 listener'); //1
475
476
emitter.removeAllListeners('test22.ns1');
477
listeners = emitter.listeners('test22.ns1');
478
test.ok(listeners.length === 0, 'there should be 0 listener'); //1
479
480
emitter.removeAllListeners(); //removing all possible
481
for (var i = 0; i < addedEvents.length; i++) {
482
listeners = emitter.listeners(addedEvents[i]);
483
test.ok(listeners.length === 0, 'there shouldn\'t be at a listener');
484
}
485
486
test.expect(addedEvents.length + 4 );
487
test.done();
488
},
489
490
'19. should be able to fire once and done' : function (test) {
491
var emitter = this.emitter,
492
addedEvents = setHelper(emitter,test,'test24');
493
494
emitter.once('test24once', function () {
495
test.ok(true, 'fired once');
496
});
497
498
emitter.emit('test24');
499
emitter.emit('test24once');
500
emitter.emit('test24once');
501
502
test.expect(2);
503
test.done();
504
},
505
506
'20. should be able to fire many and done' : function (test) {
507
508
var emitter = this.emitter,
509
addedEvents = setHelper(emitter,test,'test25');
510
511
emitter.many('test25many', 5, function () {
512
test.ok(true, 'test25many pewpew');
513
});
514
515
emitter.emit('test25'); //1
516
for (var i= 0; i < 5; i++) {
517
emitter.emit('test25many'); //1
518
}
519
emitter.emit('test25many'); //0
520
521
test.expect(6);
522
test.done();
523
},
524
525
'21. should be able to list all onAny listeners' : function (test) {
526
var emitter = this.emitter,
527
addedEvents = setHelper(emitter, test, 'test26'),
528
fn = function (tag) {
529
if (tag !== 'addListener') {
530
test.equals(tag, 'test26.ns5.ns5', 'emitted tag, and raised tag should match');
531
test.ok(true, 'something happened somewhere');
532
}
533
};
534
535
emitter.onAny(fn);
536
emitter.emit('test26.ns5.ns5'); //2
537
var listeners = emitter.listenersAny();
538
test.equals(listeners.length, 1, 'should be one any listeners');
539
540
emitter.offAny(fn);
541
listeners = emitter.listenersAny();
542
test.ok(listeners.length === 0, 'should be no any listeners');
543
544
emitter.onAny(fn);
545
emitter.onAny(fn);
546
listeners = emitter.listenersAny();
547
test.equals(listeners.length, 2, 'should be two any listeners');
548
549
emitter.offAny();
550
listeners = emitter.listenersAny();
551
test.ok(listeners.length === 0, 'should be no any listeners');
552
553
test.expect(6);
554
test.done();
555
},
556
557
'22. should not expand beyond the namespace' : function (test) {
558
var emitter = this.emitter,
559
addedEvents = setHelper(emitter,test,'test27');
560
561
emitter.emit('test27.ns2.ns3'); //1
562
emitter.emit('test27.ns2.ns3.ns4'); //0
563
564
test.expect(1);
565
test.done();
566
},
567
568
'23. should raise errors, if error is emitted and not caught' : function (test) {
569
var emitter = this.emitter,
570
error = new Error('Something Funny Happened');
571
572
try {
573
emitter.emit('error');
574
}
575
catch (ex) {
576
test.ok(ex instanceof Error, 'should be an Error');
577
}
578
579
try {
580
emitter.emit('error', error);
581
}
582
catch (ex) {
583
test.equal(error, ex, 'should have passed up the argument');
584
}
585
586
emitter.on('error', function (event, err) {
587
test.ok(true, 'error event was raised');
588
test.equal(err, error, 'of the error');
589
});
590
591
emitter.emit('error',error);
592
593
test.expect(4);
594
test.done();
595
},
596
597
'24. should raise errors on namespaces, if error is emitted and not caught' : function (test) {
598
var emitter = this.emitter,
599
error = new Error('Something Funny Happened');
600
601
emitter.on('foo.bar', function(){});
602
603
try {
604
emitter.emit('foo.error');
605
}
606
catch (ex) {
607
test.ok(ex instanceof Error, 'should be an Error');
608
}
609
610
try {
611
emitter.emit('foo.error', error);
612
}
613
catch (ex) {
614
test.equal(error, ex, 'should have passed up the argument');
615
}
616
617
emitter.on('error', function (event, err) {
618
test.ok(true, 'error event was raised');
619
test.equal(err, error, 'of the error');
620
});
621
622
emitter.emit('error',error);
623
624
test.expect(4);
625
test.done();
626
},
627
628
'25. should support old config for EE2' : function (test) {
629
if(typeof require !== 'undefined') {
630
EventEmitter2 = require('../lib/ee2').EventEmitter2;
631
}
632
else {
633
EventEmitter2 = window.EventEmitter2;
634
}
635
var emitter = new EventEmitter2({
636
caseSensitive : true,
637
delimiter : '?'
638
});
639
640
emitter.on('test30?a?b', function () {
641
test.ok(true, 'test30?a?b did emit');
642
});
643
644
emitter.emit('test30?a?b');
645
646
test.expect(1);
647
test.done();
648
},
649
650
'26. should reject bad wildcard inputs' : function (test) {
651
var emitter = this.emitter;
652
addedEvents = setHelper(emitter,test,'test31');
653
654
emitter.onAny(function () {
655
test.ok(false, 'no event should be emitted, ever');
656
});
657
658
// try listening on a bad
659
try {
660
emitter.on('test31*', function () {
661
test.ok(false, 'should never registered');
662
});
663
}
664
catch (ex) {
665
test.ok(ex instanceof Error, 'expected an error');
666
}
667
// bad wildcard at the front
668
try {
669
emitter.on('*test31', function () {
670
test.ok(false, 'should never registered');
671
});
672
}
673
catch (ex) {
674
test.ok(ex instanceof Error, 'expected an error');
675
}
676
// bad wildcard at the front
677
try {
678
emitter.on('test*31', function () {
679
test.ok(false, 'should never registered');
680
});
681
}
682
catch (ex) {
683
test.ok(ex instanceof Error, 'expected an error');
684
}
685
// bad wildcard at the front
686
try {
687
emitter.on('test31.*a', function () {
688
test.ok(false, 'should never registered');
689
});
690
}
691
catch (ex) {
692
test.ok(ex instanceof Error, 'expected an error');
693
}
694
// bad wildcard at the front
695
try {
696
emitter.on('*test31.a*', function () {
697
test.ok(false, 'should never registered');
698
});
699
}
700
catch (ex) {
701
test.ok(ex instanceof Error, 'expected an error');
702
}
703
// bad wildcard at the front
704
try {
705
emitter.on('*test31.a*a', function () {
706
test.ok(false, 'should never registered');
707
});
708
}
709
catch (ex) {
710
test.ok(ex instanceof Error, 'expected an error');
711
}
712
713
//now try emittering with a bad wildcard
714
try {
715
emitter.emit('test31*')
716
}
717
catch (ex) {
718
test.ok(ex instanceof Error, 'expected an error');
719
}
720
// bad wildcard at the front
721
try {
722
emitter.on('*test31');
723
}
724
catch (ex) {
725
test.ok(ex instanceof Error, 'expected an error');
726
}
727
// bad wildcard at the front
728
try {
729
emitter.on('test*31');
730
}
731
catch (ex) {
732
test.ok(ex instanceof Error, 'expected an error');
733
}
734
// bad wildcard at the front
735
try {
736
emitter.on('test31.*a');
737
}
738
catch (ex) {
739
test.ok(ex instanceof Error, 'expected an error');
740
}
741
// bad wildcard at the front
742
try {
743
emitter.on('*test31.a*');
744
}
745
catch (ex) {
746
test.ok(ex instanceof Error, 'expected an error');
747
}
748
// bad wildcard at the front
749
try {
750
emitter.on('*test31.a*a');
751
}
752
catch (ex) {
753
test.ok(ex instanceof Error, 'expected an error');
754
}
755
756
test.expect(12);
757
test.done();
758
},
759
760
'27. Should be able to start with 0 max listeners' : function (test) {
761
762
if(typeof require !== 'undefined') {
763
EventEmitter2 = require('../lib/ee2').EventEmitter2;
764
}
765
else {
766
EventEmitter2 = window.EventEmitter2;
767
}
768
try {
769
var emitter = new EventEmitter2({
770
maxListeners : 0
771
});
772
emitter.on('no listeners', function () {
773
test.ok(false, 'no listener was raised');
774
});
775
test.ok(true, 'was able to make something');
776
}
777
catch (ex) {
778
test.ok(false, 'Error was raised');
779
}
780
781
test.expect(1);
782
test.done();
783
},
784
785
'28. should raise maxListeners when too many are registerd' : function (test) {
786
var emitter = this.emitter;
787
788
emitter.on('maxListeners', function () {
789
test.ok(true, 'maxListeners fired');
790
});
791
792
for (var i = 0; i < 11 ; i++){
793
emitter.on('test33', function () {
794
test.ok(false, 'event was raised');
795
});
796
}
797
798
var listeners = emitter.listeners('test33');
799
test.equal(listeners.length, 10, '10 listeners in total');
800
801
test.expect(2);
802
test.done();
803
} */
804
805
'29. No warning should be raised if we set maxListener to be greater before adding' : function (test) {
806
var emitter = this.emitter;
807
var type = 'test29.*';
808
809
// set to 20
810
emitter.setMaxListeners(20);
811
812
for (var i = 0; i < 15 ; i++) {
813
emitter.on(type, function () {
814
test.ok(true, 'event was raised');
815
});
816
}
817
818
// require('eyes').inspect(emitter._events);
819
820
var listeners = emitter.listeners(type);
821
test.equal(listeners.length, 15, 'should have 15');
822
test.ok(!(emitter.listenerTree[ 'test29' ]['*']._listeners.warned), 'should not have been set');
823
824
test.expect(2);
825
test.done();
826
}
827
828
829
});
830
831