forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-event-emitter-remove-all-listeners.js
113 lines (104 loc) · 3.98 KB
/
test-event-emitter-remove-all-listeners.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
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const common = require('../common');
const assert = require('assert');
const events = require('events');
function expect(expected) {
const actual = [];
process.on('exit', function() {
assert.deepStrictEqual(actual.sort(), expected.sort());
});
function listener(name) {
actual.push(name);
}
return common.mustCall(listener, expected.length);
}
{
const ee = new events.EventEmitter();
const fooListener = common.noop;
const barListener = common.noop;
const bazListener1 = common.noop;
const bazListener2 = common.noop;
ee.on('foo', fooListener);
ee.on('bar', barListener);
ee.on('baz', bazListener1);
ee.on('baz', bazListener2);
const fooListeners = ee.listeners('foo');
const barListeners = ee.listeners('bar');
const bazListeners = ee.listeners('baz');
ee.on('removeListener', expect(['bar', 'baz', 'baz']));
ee.removeAllListeners('bar');
ee.removeAllListeners('baz');
assert.deepStrictEqual(ee.listeners('foo'), [fooListener]);
assert.deepStrictEqual(ee.listeners('bar'), []);
assert.deepStrictEqual(ee.listeners('baz'), []);
// After calling removeAllListeners(),
// the old listeners array should stay unchanged.
assert.deepStrictEqual(fooListeners, [fooListener]);
assert.deepStrictEqual(barListeners, [barListener]);
assert.deepStrictEqual(bazListeners, [bazListener1, bazListener2]);
// After calling removeAllListeners(),
// new listeners arrays is different from the old.
assert.notStrictEqual(ee.listeners('bar'), barListeners);
assert.notStrictEqual(ee.listeners('baz'), bazListeners);
}
{
const ee = new events.EventEmitter();
ee.on('foo', common.noop);
ee.on('bar', common.noop);
// Expect LIFO order
ee.on('removeListener', expect(['foo', 'bar', 'removeListener']));
ee.on('removeListener', expect(['foo', 'bar']));
ee.removeAllListeners();
assert.deepStrictEqual([], ee.listeners('foo'));
assert.deepStrictEqual([], ee.listeners('bar'));
}
{
const ee = new events.EventEmitter();
ee.on('removeListener', common.noop);
// Check for regression where removeAllListeners() throws when
// there exists a 'removeListener' listener, but there exists
// no listeners for the provided event type.
assert.doesNotThrow(ee.removeAllListeners.bind(ee, 'foo'));
}
{
const ee = new events.EventEmitter();
let expectLength = 2;
ee.on('removeListener', function(name, noop) {
assert.strictEqual(expectLength--, this.listeners('baz').length);
});
ee.on('baz', common.noop);
ee.on('baz', common.noop);
ee.on('baz', common.noop);
assert.strictEqual(ee.listeners('baz').length, expectLength + 1);
ee.removeAllListeners('baz');
assert.strictEqual(ee.listeners('baz').length, 0);
}
{
const ee = new events.EventEmitter();
assert.deepStrictEqual(ee, ee.removeAllListeners());
}
{
const ee = new events.EventEmitter();
ee._events = undefined;
assert.strictEqual(ee, ee.removeAllListeners());
}