Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor event-emitter-remove-all-listeners #13165

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@ function expect(expected) {

{
const ee = new events.EventEmitter();
ee.on('foo', common.noop);
ee.on('bar', common.noop);
ee.on('baz', common.noop);
ee.on('baz', common.noop);
const noop = common.mustNotCall();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not bring this up to the parent scope and use it in the other places below that are now manually calling common.mustNotCall()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
But give it a better name... maybe mustNotCall or if your thight with char dontCall or even doNot

Copy link
Member Author

@Trott Trott May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not bring this up to the parent scope and use it in the other places below that are now manually calling common.mustNotCall()?

@mscdex Happy to do that if there's consensus that it would be an improvement. I didn't do it because:

  • It's not needed as an identifier in the other scopes. (It's needed here because of the assert.deepStrictEqual() comparisons.)

  • Fewer global identifiers shared across different block scopes helps make those things easily cut-and-paste-able into separate test files for refactoring, etc. It also helps limit the possibility of side effects. (For example, if another test attaches a property to the noop function for some reason, that property will be set for all the other tests.)

But this isn't far off from a tabs-vs-spaces discussion, so I'm happy to just do it if everyone else thinks differently than me. :-D

ee.on('foo', noop);
ee.on('bar', noop);
ee.on('baz', noop);
ee.on('baz', noop);
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'), [common.noop]);
assert.deepStrictEqual(ee.listeners('foo'), [noop]);
assert.deepStrictEqual(ee.listeners('bar'), []);
assert.deepStrictEqual(ee.listeners('baz'), []);
// After calling removeAllListeners(),
// the old listeners array should stay unchanged.
assert.deepStrictEqual(fooListeners, [common.noop]);
assert.deepStrictEqual(barListeners, [common.noop]);
assert.deepStrictEqual(bazListeners, [common.noop, common.noop]);
assert.deepStrictEqual(fooListeners, [noop]);
assert.deepStrictEqual(barListeners, [noop]);
assert.deepStrictEqual(bazListeners, [noop, noop]);
// After calling removeAllListeners(),
// new listeners arrays is different from the old.
assert.notStrictEqual(ee.listeners('bar'), barListeners);
Expand All @@ -64,8 +65,8 @@ function expect(expected) {

{
const ee = new events.EventEmitter();
ee.on('foo', common.noop);
ee.on('bar', common.noop);
ee.on('foo', common.mustNotCall());
ee.on('bar', common.mustNotCall());
// Expect LIFO order
ee.on('removeListener', expect(['foo', 'bar', 'removeListener']));
ee.on('removeListener', expect(['foo', 'bar']));
Expand All @@ -76,7 +77,7 @@ function expect(expected) {

{
const ee = new events.EventEmitter();
ee.on('removeListener', common.noop);
ee.on('removeListener', common.mustNotCall());
// Check for regression where removeAllListeners() throws when
// there exists a 'removeListener' listener, but there exists
// no listeners for the provided event type.
Expand All @@ -89,9 +90,9 @@ function expect(expected) {
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);
ee.on('baz', common.mustNotCall());
ee.on('baz', common.mustNotCall());
ee.on('baz', common.mustNotCall());
assert.strictEqual(ee.listeners('baz').length, expectLength + 1);
ee.removeAllListeners('baz');
assert.strictEqual(ee.listeners('baz').length, 0);
Expand Down