mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 06:19:07 +00:00

As much as I would like to do this everywhere and then modify the lint rule to enforce it, the churn would be too big. However if we're going to have relatively frequent nits for this sort of thing (as we do), I'd prefer we migrate a few files at a time to never actually getting around to doing it. Ref: https://github.com/nodejs/node/pull/45448#pullrequestreview-1179370442 PR-URL: https://github.com/nodejs/node/pull/45466 Reviewed-By: James M Snell <jasnell@gmail.com>
53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
// Flags: --expose-internals --no-warnings
|
|
const common = require('../common');
|
|
const { kWeakHandler } = require('internal/event_target');
|
|
|
|
const {
|
|
deepStrictEqual,
|
|
throws,
|
|
} = require('assert');
|
|
|
|
const { getEventListeners, EventEmitter } = require('events');
|
|
|
|
// Test getEventListeners on EventEmitter
|
|
{
|
|
const fn1 = common.mustNotCall();
|
|
const fn2 = common.mustNotCall();
|
|
const emitter = new EventEmitter();
|
|
emitter.on('foo', fn1);
|
|
emitter.on('foo', fn2);
|
|
emitter.on('baz', fn1);
|
|
emitter.on('baz', fn1);
|
|
deepStrictEqual(getEventListeners(emitter, 'foo'), [fn1, fn2]);
|
|
deepStrictEqual(getEventListeners(emitter, 'bar'), []);
|
|
deepStrictEqual(getEventListeners(emitter, 'baz'), [fn1, fn1]);
|
|
}
|
|
// Test getEventListeners on EventTarget
|
|
{
|
|
const fn1 = common.mustNotCall();
|
|
const fn2 = common.mustNotCall();
|
|
const target = new EventTarget();
|
|
target.addEventListener('foo', fn1);
|
|
target.addEventListener('foo', fn2);
|
|
target.addEventListener('baz', fn1);
|
|
target.addEventListener('baz', fn1);
|
|
deepStrictEqual(getEventListeners(target, 'foo'), [fn1, fn2]);
|
|
deepStrictEqual(getEventListeners(target, 'bar'), []);
|
|
deepStrictEqual(getEventListeners(target, 'baz'), [fn1]);
|
|
}
|
|
|
|
{
|
|
throws(() => {
|
|
getEventListeners('INVALID_EMITTER');
|
|
}, /ERR_INVALID_ARG_TYPE/);
|
|
}
|
|
{
|
|
// Test weak listeners
|
|
const target = new EventTarget();
|
|
const fn = common.mustNotCall();
|
|
target.addEventListener('foo', fn, { [kWeakHandler]: {} });
|
|
const listeners = getEventListeners(target, 'foo');
|
|
deepStrictEqual(listeners, [fn]);
|
|
}
|