-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
124 lines (104 loc) · 3.09 KB
/
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
var assert = require('assert');
import EventEmitter from './src/multi-event-es6';
///// some tests from https://github.com/alexanderGugel/micro-events
var _noop = () => {};
describe('EventEmitter', () => {
it('#on() and #emit()', (done) => {
var eventEmitter = new EventEmitter();
var originalEvent = {};
eventEmitter.on('boom', function(event) {
assert(event, originalEvent);
done();
});
eventEmitter.emit('boom', originalEvent);
});
it('#emit() should work with multiple events in arguments', (done) => {
var eventEmitter = new EventEmitter();
var originalEvent1 = {};
var originalEvent2 = {};
var originalEvent3 = {};
eventEmitter.on('boom', function(event1, event2, event3) {
assert(event1, originalEvent1);
assert(event2, originalEvent2);
assert(event3, originalEvent3);
done();
});
eventEmitter.emit('boom', originalEvent1, originalEvent2, originalEvent3);
});
it('#on() should only register handler once', (done) => {
var eventEmitter = new EventEmitter();
var originalEvent = {};
var handler = function(event) {
done();
};
eventEmitter.on('boom', handler);
eventEmitter.on('boom', handler);
eventEmitter.emit('boom', originalEvent);
});
it('functions should return this', () => {
var eventEmitter = new EventEmitter();
assert(eventEmitter.on('boom', _noop), eventEmitter);
assert(eventEmitter.emit('boom', _noop), eventEmitter);
});
it('functions should check arguments', () => {
var eventEmitter = new EventEmitter();
assert.throws(() => {
eventEmitter.on();
});
assert.throws(() => {
eventEmitter.on('boom', 'Not a function');
});
assert.throws(() => {
eventEmitter.emit();
});
});
});
describe('Pipe EventsSubSet', ()=>{
let mtOne = new EventEmitter();
let mtTwo = new EventEmitter();
mtOne.pipe('subSet.*', mtTwo);
it('#on event in the sub set (basic test)', (done)=>{
mtTwo.on('subSet.pipeEventOk', (...arg)=>{
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){
done();
}
});
mtOne.emit('subSet.pipeEventOk', 'an argument', 'another');
});
it('#on event in the wrong sub set', (done)=>{
mtTwo.on('subSet.pipeEventOk2', (...arg)=>{
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){
done();
}
});
mtTwo.on('subSet2.pipeEventOk2', (...arg)=>{
if(arg.length === 2 && arg[0] === 'an argument' && arg[1] === 'another'){
done("error");
}
});
mtOne.emit('subSet2.pipeEventOk2', 'an argument', 'another');
mtOne.emit('subSet.pipeEventOk2', 'an argument', 'another');
});
});
describe('MultiEventEmitter', () => {
it('#on() and #emit() multi', (done) => {
let multiEventEmitter = new EventEmitter();
multiEventEmitter
.on('a.*.c', function multi(args) {
done();
});
multiEventEmitter.emit('a.b.c');
});
it('#on() and #emit() listen on two events', (done) => {
let multiEventEmitter = new EventEmitter();
let countCalls = 0;
multiEventEmitter
.on('a.b.*', function multi(args) {
countCalls ++;
});
multiEventEmitter.emit('a.b.x');
multiEventEmitter.emit('a.b.y');
assert(countCalls, 2);
done();
});
});