deno.land / std@0.166.0 / node / _tools / test / parallel / test-whatwg-events-add-event-listener-options-signal.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
require('../common');
const { strictEqual,} = require('assert');
// Manually ported from: wpt@dom/events/AddEventListenerOptions-signal.any.js
{ // Passing an AbortSignal to addEventListener does not prevent // removeEventListener let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); et.addEventListener('test', handler, { signal: controller.signal }); et.dispatchEvent(new Event('test')); strictEqual(count, 1, 'Adding a signal still adds a listener'); et.dispatchEvent(new Event('test')); strictEqual(count, 2, 'The listener was not added with the once flag'); controller.abort(); et.dispatchEvent(new Event('test')); strictEqual(count, 2, 'Aborting on the controller removes the listener'); // See: https://github.com/nodejs/node/pull/37696 , adding an event listener // should always return undefined. strictEqual( et.addEventListener('test', handler, { signal: controller.signal }), undefined); et.dispatchEvent(new Event('test')); strictEqual(count, 2, 'Passing an aborted signal never adds the handler');}
{ // Passing an AbortSignal to addEventListener works with the once flag let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); et.addEventListener('test', handler, { signal: controller.signal }); et.removeEventListener('test', handler); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ // Removing a once listener works with a passed signal let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); const options = { signal: controller.signal, once: true }; et.addEventListener('test', handler, options); controller.abort(); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); const options = { signal: controller.signal, once: true }; et.addEventListener('test', handler, options); et.removeEventListener('test', handler); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ // Passing an AbortSignal to multiple listeners let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); const options = { signal: controller.signal, once: true }; et.addEventListener('first', handler, options); et.addEventListener('second', handler, options); controller.abort(); et.dispatchEvent(new Event('first')); et.dispatchEvent(new Event('second')); strictEqual(count, 0, 'The listener was still removed');}
{ // Passing an AbortSignal to addEventListener works with the capture flag let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); const options = { signal: controller.signal, capture: true }; et.addEventListener('test', handler, options); controller.abort(); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ // Aborting from a listener does not call future listeners let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); const options = { signal: controller.signal }; et.addEventListener('test', () => { controller.abort(); }, options); et.addEventListener('test', handler, options); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ // Adding then aborting a listener in another listener does not call it let count = 0; function handler() { count++; } const et = new EventTarget(); const controller = new AbortController(); et.addEventListener('test', () => { et.addEventListener('test', handler, { signal: controller.signal }); controller.abort(); }, { signal: controller.signal }); et.dispatchEvent(new Event('test')); strictEqual(count, 0, 'The listener was still removed');}
{ // Aborting from a nested listener should remove it const et = new EventTarget(); const ac = new AbortController(); let count = 0; et.addEventListener('foo', () => { et.addEventListener('foo', () => { count++; if (count > 5) ac.abort(); et.dispatchEvent(new Event('foo')); }, { signal: ac.signal }); et.dispatchEvent(new Event('foo')); }, { once: true }); et.dispatchEvent(new Event('foo'));}
Version Info