/
main-test.js
308 lines (247 loc) · 7.53 KB
/
main-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
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
var EventYoshi = require('..')
, EventEmitter = require('events').EventEmitter
, assert = require('assert')
describe('Listen for an event', function() {
var ee1 = new EventEmitter()
, ee2 = new EventEmitter()
, yoshi = new EventYoshi()
.add(ee1)
var lastFoo, lastFooChild;
var foo = function(s) {
lastFoo = s;
lastFooChild = this.child;
};
yoshi.on('foo', foo);
var lastCat, lastCatChild;
var cat = function(s) {
lastCat = s;
lastCatChild = this.child;
};
yoshi.on('cat', cat);
it('Emits event to eventyoshi from emitter', function() {
ee1.emit('foo', 'bar');
assert.equal(lastFoo, 'bar');
assert.equal(lastFooChild, ee1);
ee1.emit('cat', 'doggy');
assert.equal(lastCat, 'doggy');
assert.equal(lastCatChild, ee1);
});
describe('Add another event emitter', function() {
it('Routes its events to yoshi', function() {
yoshi.add(ee2);
ee2.emit('foo', 'bar2');
assert.equal(lastFoo, 'bar2');
assert.equal(lastFooChild, ee2);
ee2.emit('cat', 'dog2');
assert.equal(lastCat, 'dog2');
assert.equal(lastCatChild, ee2);
});
it('Does not emit events not listened to', function() {
ee2.emit('nope', 'yes');
assert.equal(lastFoo, 'bar2');
assert.equal(lastFooChild, ee2);
assert.equal(lastCat, 'dog2');
assert.equal(lastCatChild, ee2);
});
});
describe('Remove listener', function() {
it('Does not emit for those events anymore', function() {
yoshi.removeListener('foo', foo);
ee1.emit('foo', 'bar3');
assert.equal(lastFoo, 'bar2');
assert.equal(lastFooChild, ee2);
});
it('Still emits other listened to events', function() {
ee1.emit('cat', 'dog3');
assert.equal(lastCat, 'dog3');
assert.equal(lastCatChild, ee1);
});
describe('Remove first event emitter', function() {
it('Does not route events to yoshi anymore', function() {
yoshi.remove(ee1);
ee1.emit('cat', 'dog4');
assert.equal(lastCat, 'dog3');
});
it('Still routes events from second event emitter', function() {
ee2.emit('cat', 'dog5');
assert.equal(lastCat, 'dog5');
assert.equal(lastCatChild, ee2);
});
});
});
describe('Add more listeners', function() {
var lastHello;
var hello = function(s) {
lastHello = s;
};
yoshi.on('hello', hello);
yoshi.on('hello', foo);
yoshi.on('hello', cat);
it('Old listeners are still called', function() {
ee2.emit('cat', 'diggity');
assert.equal(lastCat, 'diggity');
assert.equal(lastCatChild, ee2);
});
it('New listeners are called on new events', function() {
ee2.emit('hello', 'world');
assert.equal(lastFoo, 'world');
assert.equal(lastCat, 'world');
assert.equal(lastHello, 'world');
});
it('Listeners are not called when other events emitted', function() {
ee2.emit('boy', 'meets world');
assert.equal(lastFoo, 'world');
assert.equal(lastCat, 'world');
assert.equal(lastHello, 'world');
});
describe('Remove all listeners specifying event', function() {
it('Does not call listeners for that event anymore', function() {
yoshi.removeAllListeners('hello');
ee2.emit('hello', 'world2');
assert.equal(lastFoo, 'world');
assert.equal(lastCat, 'world');
assert.equal(lastHello, 'world');
});
it('Still calls listeners for other events', function() {
ee2.emit('cat', 'dogton');
assert.equal(lastCat, 'dogton');
});
});
});
});
describe('Listen for `newListener` event', function() {
var ee1 = new EventEmitter()
, yoshi = new EventYoshi()
.add(ee1)
var lastEmitter, lastEvent, lastListener;
var newListener = function(event, listener) {
lastEmitter = this.child;
lastEvent = event;
lastListener = listener
};
yoshi.on('newListener', newListener);
var f = function() {};
describe('Add a new listener to yoshi', function() {
it('Calls listener', function() {
yoshi.on('what', f);
assert.equal(lastEmitter, yoshi);
assert.equal(lastEvent, 'what');
assert.equal(lastListener, f);
});
});
describe('Add a new listener to event emitter', function() {
it('Does not call listener', function() {
ee1.on('butt', function() {});
assert.equal(lastEmitter, yoshi);
assert.equal(lastEvent, 'what');
assert.equal(lastListener, f);
});
});
describe('Remove `newListener` listener', function() {
it('Does not call listener anymore', function() {
yoshi.removeListener('newListener', newListener);
yoshi.on('nothing', function() {});
assert.equal(lastEmitter, yoshi);
assert.equal(lastEvent, 'what');
assert.equal(lastListener, f);
});
});
});
describe('Listener for `newChildListener` event', function() {
var ee1 = new EventEmitter()
, ee2 = new EventEmitter()
, yoshi = new EventYoshi()
.add(ee1)
.add(ee2)
var lastEmitter, lastEvent, lastListener;
var newChildListener = function(event, listener) {
lastEmitter = this.child;
lastEvent = event;
lastListener = listener;
};
yoshi.on('newChildListener', newChildListener);
describe('Add listener to yoshi', function() {
it('Does not emit newChildListener', function() {
yoshi.on('foo', function() {});
assert.equal(lastEmitter, undefined);
assert.equal(lastEvent, undefined);
assert.equal(lastListener, undefined);
});
});
describe('Add listener to child event emitter', function() {
it('Emits newChildListener', function() {
var f = function() {};
ee1.on('bar', f);
assert.equal(lastEmitter, ee1);
assert.equal(lastEvent, 'bar');
assert.equal(lastListener, f);
});
});
});
describe('Listen once', function() {
var ee1 = new EventEmitter()
, yoshi = new EventYoshi()
.add(ee1)
var last;
var f = function(s) {
last = s;
};
yoshi.once('soap', f);
it('Calls listener just once', function() {
ee1.emit('soap', 'a');
ee1.emit('soap', 'b');
ee1.emit('soap', 'c');
assert.equal(last, 'a');
});
describe('and then removeListener', function() {
it('Does not call listener when emitted', function() {
yoshi.once('bar', f);
yoshi.removeListener('bar', f);
ee1.emit('bar', 1);
assert.equal(last, 'a');
});
});
});
describe('Emit on eventyoshi', function() {
var ee1 = new EventEmitter()
, yoshi = new EventYoshi()
.add(ee1)
var lastYoshi, lastEE;
yoshi.on('a', function(s) {
lastYoshi = s;
});
ee1.on('a', function(s) {
lastEE = s;
});
it('Emits for both eventyoshi and child event emitters', function() {
yoshi.emit('a', 'b');
assert.equal(lastYoshi, 'b');
assert.equal(lastEE, 'b');
});
});
describe('Proxy a custom function', function() {
var ee1 = new EventEmitter()
, ee2 = new EventEmitter()
, ee1foo, ee2foo
, yoshi = new EventYoshi()
.add(ee1)
.add(ee2)
yoshi.proxy('foo', 'bar');
ee1.foo = function() {
ee1foo = true;
return 'a';
};
ee2.foo = function(a, b) {
ee2foo = a + b;
return 'b';
};
ee2.bar = function(a) {
return a;
};
it('Calls proxied child functions', function() {
assert.deepEqual(yoshi.foo(2, 3), ['a', 'b']);
assert.equal(ee1foo, true);
assert.equal(ee2foo, 5);
assert.equal(yoshi.bar('hello'), 'hello');
});
});