Browse Source

test: expand test coverage of events.js

* test else path in emitMany function
* test calling removeAllListeners() in a event emitter instance
  with no events at all
* test calling removeListener() passing a event type that does
  not exist
* test calling eventNames() in a event emitter instance
  with no events at all

Refs: https://coverage.nodejs.org/coverage-ba776b3a56642d4c/root/events.js.html
PR-URL: https://github.com/nodejs/node/pull/10947
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
v6
Vinícius do Carmo 8 years ago
committed by Gibson Fahnestock
parent
commit
bee83e0bbc
  1. 17
      test/parallel/test-event-emitter-num-args.js
  2. 5
      test/parallel/test-event-emitter-remove-all-listeners.js
  3. 6
      test/parallel/test-event-emitter-remove-listeners.js
  4. 2
      test/parallel/test-event-emitter-special-event-names.js

17
test/parallel/test-event-emitter-num-args.js

@ -4,15 +4,20 @@ const assert = require('assert');
const events = require('events');
const e = new events.EventEmitter();
const num_args_emited = [];
const num_args_emitted = [];
e.on('numArgs', function() {
const numArgs = arguments.length;
console.log('numArgs: ' + numArgs);
num_args_emited.push(numArgs);
num_args_emitted.push(numArgs);
});
console.log('start');
e.on('foo', function() {
num_args_emitted.push(arguments.length);
});
e.on('foo', function() {
num_args_emitted.push(arguments.length);
});
e.emit('numArgs');
e.emit('numArgs', null);
@ -21,6 +26,8 @@ e.emit('numArgs', null, null, null);
e.emit('numArgs', null, null, null, null);
e.emit('numArgs', null, null, null, null, null);
e.emit('foo', null, null, null, null);
process.on('exit', function() {
assert.deepStrictEqual([0, 1, 2, 3, 4, 5], num_args_emited);
assert.deepStrictEqual([0, 1, 2, 3, 4, 5, 4, 4], num_args_emitted);
});

5
test/parallel/test-event-emitter-remove-all-listeners.js

@ -77,3 +77,8 @@ function listener() {}
ee.removeAllListeners('baz');
assert.strictEqual(ee.listeners('baz').length, 0);
}
{
const ee = new events.EventEmitter();
assert.deepStrictEqual(ee, ee.removeAllListeners());
}

6
test/parallel/test-event-emitter-remove-listeners.js

@ -116,6 +116,12 @@ function listener2() {}
ee.emit('hello');
}
{
const ee = new EventEmitter();
assert.deepStrictEqual(ee, ee.removeListener('foo', () => {}));
}
// Verify that the removed listener must be a function
assert.throws(() => {
const ee = new EventEmitter();

2
test/parallel/test-event-emitter-special-event-names.js

@ -7,6 +7,8 @@ const assert = require('assert');
const ee = new EventEmitter();
const handler = () => {};
assert.deepStrictEqual(ee.eventNames(), []);
assert.strictEqual(ee._events.hasOwnProperty, undefined);
assert.strictEqual(ee._events.toString, undefined);

Loading…
Cancel
Save