-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
test.js
413 lines (305 loc) · 8.41 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
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
const test = require('ava');
const Emitter = require('./index.js');
function Custom() {
Emitter.call(this);
}
Object.setPrototypeOf(Custom.prototype, Emitter.prototype);
test('Custom emitter extends base Emitter', t => {
t.plan(1);
const emitter = new Custom();
emitter.on('foo', () => {
t.pass();
});
emitter.emit('foo');
});
test('Emitter.on adds listeners to events', t => {
const emitter = new Emitter();
const calls = [];
emitter.on('foo', value => {
calls.push('one', value);
});
emitter.on('foo', value => {
calls.push('two', value);
});
emitter.emit('foo', 1);
emitter.emit('bar', 1);
emitter.emit('foo', 2);
t.deepEqual(calls, ['one', 1, 'two', 1, 'one', 2, 'two', 2]);
});
test('Emitter.once adds a single-shot listener', t => {
const emitter = new Emitter();
const calls = [];
emitter.once('foo', value => {
calls.push('one', value);
});
emitter.emit('foo', 1);
emitter.emit('foo', 2);
emitter.emit('foo', 3);
emitter.emit('bar', 1);
t.deepEqual(calls, ['one', 1]);
});
test('Emitter.off removes a specified listener from an event', t => {
const emitter = new Emitter();
const calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('foo', two);
emitter.off('foo', two);
emitter.emit('foo');
t.deepEqual(calls, ['one']);
});
test('Emitter.off works with Emitter.once', t => {
const emitter = new Emitter();
const calls = [];
function one() {
calls.push('one');
}
emitter.once('foo', one);
emitter.once('fee', one);
emitter.off('foo', one);
emitter.emit('foo');
t.deepEqual(calls, []);
});
test('Emitter.off works when called from within an event emission', t => {
let isCalled = false;
const emitter = new Emitter();
function b() {
isCalled = true;
}
emitter.on('tobi', () => {
emitter.off('tobi', b);
});
emitter.on('tobi', b);
emitter.emit('tobi');
t.true(isCalled);
isCalled = false;
emitter.emit('tobi');
t.false(isCalled);
});
test('Emitter.off removes all listeners for a specified event', t => {
const emitter = new Emitter();
const calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('foo', two);
emitter.off('foo');
emitter.emit('foo');
emitter.emit('foo');
t.deepEqual(calls, []);
});
test('Emitter.off with no arguments removes all listeners', t => {
const emitter = new Emitter();
const calls = [];
function one() {
calls.push('one');
}
function two() {
calls.push('two');
}
emitter.on('foo', one);
emitter.on('bar', two);
emitter.emit('foo');
emitter.emit('bar');
emitter.off();
emitter.emit('foo');
emitter.emit('bar');
t.deepEqual(calls, ['one', 'two']);
});
test('Emitter.listeners returns callbacks for a specified event', t => {
const emitter = new Emitter();
function foo() {}
emitter.on('foo', foo);
t.deepEqual(emitter.listeners('foo'), [foo]);
});
test('Emitter.listeners returns an empty array for events without handlers', t => {
const emitter = new Emitter();
t.deepEqual(emitter.listeners('foo'), []);
});
test('Emitter.hasListeners returns true when handlers are present for an event', t => {
const emitter = new Emitter();
emitter.on('foo', () => {});
t.true(emitter.hasListeners('foo'));
});
test('Emitter.hasListeners returns false for events without handlers', t => {
const emitter = new Emitter();
t.false(emitter.hasListeners('foo'));
});
test('Mixin functionality with Emitter(object) works as expected', t => {
t.plan(1);
const proto = {};
Emitter(proto); // eslint-disable-line new-cap
proto.on('something', () => {
t.pass();
});
proto.emit('something');
});
test.failing('Listener removal during emit should not be called', t => {
const emitter = new Emitter();
let isCalled = false;
function listener() {
emitter.off('foo', listener);
isCalled = true;
}
emitter.on('foo', listener);
emitter.emit('foo');
emitter.emit('foo'); // Second emit to test if listener is called again
t.false(isCalled);
});
test('Emitting an event without listeners does not cause errors', t => {
const emitter = new Emitter();
t.notThrows(() => {
emitter.emit('foo');
});
});
test('Removing a non-existent listener does not cause issues', t => {
const emitter = new Emitter();
t.notThrows(() => {
emitter.off('foo', () => {});
});
});
test('Listeners are called in the order they were added', t => {
const emitter = new Emitter();
const calls = [];
emitter.on('foo', () => {
calls.push('first');
});
emitter.on('foo', () => {
calls.push('second');
});
emitter.emit('foo');
t.deepEqual(calls, ['first', 'second']);
});
test('Removing a listener multiple times does not cause issues', t => {
const emitter = new Emitter();
const listener = () => {};
emitter.on('foo', listener);
emitter.off('foo', listener);
t.notThrows(() => {
emitter.off('foo', listener);
});
});
test('Adding the same listener multiple times results in multiple calls', t => {
const emitter = new Emitter();
let callCount = 0;
const listener = () => callCount++;
emitter.on('foo', listener);
emitter.on('foo', listener);
emitter.emit('foo');
t.is(callCount, 2);
});
test('Method chaining works as expected', t => {
const emitter = new Emitter();
const chain = emitter.on('foo', () => {}).once('bar', () => {}).off('baz');
t.is(chain, emitter);
});
test('Listeners receive correct arguments', t => {
const emitter = new Emitter();
let receivedArguments = [];
emitter.on('foo', (...arguments_) => {
receivedArguments = arguments_;
});
emitter.emit('foo', 'arg1', 'arg2');
t.deepEqual(receivedArguments, ['arg1', 'arg2']);
});
test('Emitter.once with different events should only remove listener from specific event', t => {
const emitter = new Emitter();
let isFooCalled = false;
let isBarCalled = false;
const listener = () => {
isFooCalled = true;
};
emitter.once('foo', listener);
emitter.once('bar', () => {
isBarCalled = true;
});
emitter.emit('bar');
emitter.emit('foo');
t.true(isBarCalled);
t.true(isFooCalled);
});
test('Emitting event with no arguments should not cause errors', t => {
const emitter = new Emitter();
t.notThrows(() => {
emitter.emit('foo');
});
});
test('Listener addition and removal within another listener', t => {
const emitter = new Emitter();
let dynamicListenerCalled = false;
function dynamicListener() {
dynamicListenerCalled = true;
}
emitter.on('foo', () => {
emitter.on('bar', dynamicListener);
emitter.off('bar', dynamicListener);
});
emitter.emit('foo');
emitter.emit('bar');
t.false(dynamicListenerCalled);
});
test('Emitting an event with multiple arguments should pass all arguments to listeners', t => {
const emitter = new Emitter();
let receivedArguments = [];
emitter.on('foo', (...arguments_) => {
receivedArguments = arguments_;
});
emitter.emit('foo', 'arg1', 'arg2', 'arg3');
t.deepEqual(receivedArguments, ['arg1', 'arg2', 'arg3']);
});
test('Alias methods should work as expected', t => {
const emitter = new Emitter();
let isCalled = false;
function listener() {
isCalled = true;
}
emitter.addEventListener('foo', listener);
emitter.emit('foo');
t.true(isCalled);
isCalled = false;
emitter.removeEventListener('foo', listener);
emitter.emit('foo');
t.false(isCalled);
});
test('Emitter.listenerCount returns the correct count for a specific event', t => {
const emitter = new Emitter();
emitter.on('foo', () => {});
emitter.on('foo', () => {});
emitter.once('bar', () => {});
t.is(emitter.listenerCount('foo'), 2);
t.is(emitter.listenerCount('bar'), 1);
t.is(emitter.listenerCount('baz'), 0);
});
test('Emitter.listenerCount returns the correct count for all events', t => {
const emitter = new Emitter();
emitter.on('foo', () => {});
emitter.on('bar', () => {});
emitter.once('baz', () => {});
t.is(emitter.listenerCount(), 3);
});
test('Emitter.hasListeners returns true when there are listeners for a specific event', t => {
const emitter = new Emitter();
emitter.on('foo', () => {});
emitter.on('bar', () => {});
emitter.once('baz', () => {});
t.true(emitter.hasListeners('foo'));
t.true(emitter.hasListeners('bar'));
t.true(emitter.hasListeners('baz'));
t.false(emitter.hasListeners('qux'));
});
test('Emitter.hasListeners returns true when there are listeners for any event', t => {
const emitter = new Emitter();
emitter.on('foo', () => {});
emitter.once('bar', () => {});
emitter.once('baz', () => {});
t.true(emitter.hasListeners());
});