|
| 1 | +import * as common from '../common/index.mjs'; |
| 2 | +import * as events from 'node:events'; |
| 3 | +import * as assert from 'node:assert'; |
| 4 | +import { describe, it } from 'node:test'; |
| 5 | + |
| 6 | +describe('events.addAbortListener', () => { |
| 7 | + it('should throw if signal not provided', () => { |
| 8 | + assert.throws(() => events.addAbortListener(), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should throw if provided signal is invalid', () => { |
| 12 | + assert.throws(() => events.addAbortListener(undefined), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 13 | + assert.throws(() => events.addAbortListener(null), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 14 | + assert.throws(() => events.addAbortListener({}), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should throw if listener is not a function', () => { |
| 18 | + const { signal } = new AbortController(); |
| 19 | + assert.throws(() => events.addAbortListener(signal), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 20 | + assert.throws(() => events.addAbortListener(signal, {}), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 21 | + assert.throws(() => events.addAbortListener(signal, undefined), { code: 'ERR_INVALID_ARG_TYPE' }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return a Disposable', () => { |
| 25 | + const { signal } = new AbortController(); |
| 26 | + const disposable = events.addAbortListener(signal, common.mustNotCall()); |
| 27 | + |
| 28 | + assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should execute the listener immediately for aborted runners', () => { |
| 32 | + const disposable = events.addAbortListener(AbortSignal.abort(), common.mustCall()); |
| 33 | + assert.strictEqual(typeof disposable[Symbol.dispose], 'function'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should execute the listener even when event propagation stopped', () => { |
| 37 | + const controller = new AbortController(); |
| 38 | + const { signal } = controller; |
| 39 | + |
| 40 | + signal.addEventListener('abort', (e) => e.stopImmediatePropagation()); |
| 41 | + events.addAbortListener( |
| 42 | + signal, |
| 43 | + common.mustCall((e) => assert.strictEqual(e.target, signal)), |
| 44 | + ); |
| 45 | + |
| 46 | + controller.abort(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should remove event listeners when disposed', () => { |
| 50 | + const controller = new AbortController(); |
| 51 | + const disposable = events.addAbortListener(controller.signal, common.mustNotCall()); |
| 52 | + disposable[Symbol.dispose](); |
| 53 | + controller.abort(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments