1'use strict'; 2// Flags: --expose-internals --no-warnings 3const common = require('../common'); 4const { kWeakHandler } = require('internal/event_target'); 5 6const { 7 deepStrictEqual, 8 throws, 9} = require('assert'); 10 11const { getEventListeners, EventEmitter } = require('events'); 12 13// Test getEventListeners on EventEmitter 14{ 15 const fn1 = common.mustNotCall(); 16 const fn2 = common.mustNotCall(); 17 const emitter = new EventEmitter(); 18 emitter.on('foo', fn1); 19 emitter.on('foo', fn2); 20 emitter.on('baz', fn1); 21 emitter.on('baz', fn1); 22 deepStrictEqual(getEventListeners(emitter, 'foo'), [fn1, fn2]); 23 deepStrictEqual(getEventListeners(emitter, 'bar'), []); 24 deepStrictEqual(getEventListeners(emitter, 'baz'), [fn1, fn1]); 25} 26// Test getEventListeners on EventTarget 27{ 28 const fn1 = common.mustNotCall(); 29 const fn2 = common.mustNotCall(); 30 const target = new EventTarget(); 31 target.addEventListener('foo', fn1); 32 target.addEventListener('foo', fn2); 33 target.addEventListener('baz', fn1); 34 target.addEventListener('baz', fn1); 35 deepStrictEqual(getEventListeners(target, 'foo'), [fn1, fn2]); 36 deepStrictEqual(getEventListeners(target, 'bar'), []); 37 deepStrictEqual(getEventListeners(target, 'baz'), [fn1]); 38} 39 40{ 41 throws(() => { 42 getEventListeners('INVALID_EMITTER'); 43 }, /ERR_INVALID_ARG_TYPE/); 44} 45{ 46 // Test weak listeners 47 const target = new EventTarget(); 48 const fn = common.mustNotCall(); 49 target.addEventListener('foo', fn, { [kWeakHandler]: {} }); 50 const listeners = getEventListeners(target, 'foo'); 51 deepStrictEqual(listeners, [fn]); 52} 53