11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst {
61cb0ef41Sopenharmony_ci  defineEventHandler,
71cb0ef41Sopenharmony_ci  kWeakHandler,
81cb0ef41Sopenharmony_ci} = require('internal/event_target');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst {
111cb0ef41Sopenharmony_ci  ok,
121cb0ef41Sopenharmony_ci  deepStrictEqual,
131cb0ef41Sopenharmony_ci  strictEqual,
141cb0ef41Sopenharmony_ci  throws,
151cb0ef41Sopenharmony_ci} = require('assert');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst { once } = require('events');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst { inspect } = require('util');
201cb0ef41Sopenharmony_ciconst { setTimeout: delay } = require('timers/promises');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// The globals are defined.
231cb0ef41Sopenharmony_ciok(Event);
241cb0ef41Sopenharmony_ciok(EventTarget);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// The warning event has special behavior regarding attaching listeners
271cb0ef41Sopenharmony_cilet lastWarning;
281cb0ef41Sopenharmony_ciprocess.on('warning', (e) => {
291cb0ef41Sopenharmony_ci  lastWarning = e;
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Utility promise for parts of the test that need to wait for eachother -
331cb0ef41Sopenharmony_ci// Namely tests for warning events
341cb0ef41Sopenharmony_ci/* eslint-disable no-unused-vars */
351cb0ef41Sopenharmony_cilet asyncTest = Promise.resolve();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// First, test Event
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const ev = new Event('foo');
401cb0ef41Sopenharmony_ci  strictEqual(ev.type, 'foo');
411cb0ef41Sopenharmony_ci  strictEqual(ev.cancelable, false);
421cb0ef41Sopenharmony_ci  strictEqual(ev.defaultPrevented, false);
431cb0ef41Sopenharmony_ci  strictEqual(typeof ev.timeStamp, 'number');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  // Compatibility properties with the DOM
461cb0ef41Sopenharmony_ci  deepStrictEqual(ev.composedPath(), []);
471cb0ef41Sopenharmony_ci  strictEqual(ev.returnValue, true);
481cb0ef41Sopenharmony_ci  strictEqual(ev.bubbles, false);
491cb0ef41Sopenharmony_ci  strictEqual(ev.composed, false);
501cb0ef41Sopenharmony_ci  strictEqual(ev.isTrusted, false);
511cb0ef41Sopenharmony_ci  strictEqual(ev.eventPhase, 0);
521cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // Not cancelable
551cb0ef41Sopenharmony_ci  ev.preventDefault();
561cb0ef41Sopenharmony_ci  strictEqual(ev.defaultPrevented, false);
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  [
601cb0ef41Sopenharmony_ci    'foo',
611cb0ef41Sopenharmony_ci    1,
621cb0ef41Sopenharmony_ci    false,
631cb0ef41Sopenharmony_ci  ].forEach((i) => (
641cb0ef41Sopenharmony_ci    throws(() => new Event('foo', i), {
651cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
661cb0ef41Sopenharmony_ci      name: 'TypeError',
671cb0ef41Sopenharmony_ci      message: 'The "options" argument must be of type object.' +
681cb0ef41Sopenharmony_ci               common.invalidArgTypeHelper(i),
691cb0ef41Sopenharmony_ci    })
701cb0ef41Sopenharmony_ci  ));
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  const ev = new Event('foo');
741cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
751cb0ef41Sopenharmony_ci  ev.cancelBubble = true;
761cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci{
791cb0ef41Sopenharmony_ci  const ev = new Event('foo');
801cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
811cb0ef41Sopenharmony_ci  ev.stopPropagation();
821cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  const ev = new Event('foo');
861cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
871cb0ef41Sopenharmony_ci  ev.cancelBubble = 'some-truthy-value';
881cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci  // No argument behavior - throw TypeError
921cb0ef41Sopenharmony_ci  throws(() => {
931cb0ef41Sopenharmony_ci    new Event();
941cb0ef41Sopenharmony_ci  }, TypeError);
951cb0ef41Sopenharmony_ci  // Too many arguments passed behavior - ignore additional arguments
961cb0ef41Sopenharmony_ci  const ev = new Event('foo', {}, {});
971cb0ef41Sopenharmony_ci  strictEqual(ev.type, 'foo');
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci{
1001cb0ef41Sopenharmony_ci  const ev = new Event('foo');
1011cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
1021cb0ef41Sopenharmony_ci  ev.cancelBubble = true;
1031cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci{
1061cb0ef41Sopenharmony_ci  const ev = new Event('foo');
1071cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
1081cb0ef41Sopenharmony_ci  ev.stopPropagation();
1091cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
1101cb0ef41Sopenharmony_ci}
1111cb0ef41Sopenharmony_ci{
1121cb0ef41Sopenharmony_ci  const ev = new Event('foo');
1131cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, false);
1141cb0ef41Sopenharmony_ci  ev.cancelBubble = 'some-truthy-value';
1151cb0ef41Sopenharmony_ci  strictEqual(ev.cancelBubble, true);
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci{
1181cb0ef41Sopenharmony_ci  const ev = new Event('foo', { cancelable: true });
1191cb0ef41Sopenharmony_ci  strictEqual(ev.type, 'foo');
1201cb0ef41Sopenharmony_ci  strictEqual(ev.cancelable, true);
1211cb0ef41Sopenharmony_ci  strictEqual(ev.defaultPrevented, false);
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  ev.preventDefault();
1241cb0ef41Sopenharmony_ci  strictEqual(ev.defaultPrevented, true);
1251cb0ef41Sopenharmony_ci  throws(() => new Event(Symbol()), TypeError);
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci{
1281cb0ef41Sopenharmony_ci  const ev = new Event('foo');
1291cb0ef41Sopenharmony_ci  strictEqual(ev.isTrusted, false);
1301cb0ef41Sopenharmony_ci}
1311cb0ef41Sopenharmony_ci{
1321cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  const ev1 = common.mustCall(function(event) {
1351cb0ef41Sopenharmony_ci    strictEqual(event.type, 'foo');
1361cb0ef41Sopenharmony_ci    strictEqual(this, eventTarget);
1371cb0ef41Sopenharmony_ci    strictEqual(event.eventPhase, 2);
1381cb0ef41Sopenharmony_ci  }, 2);
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  const ev2 = {
1411cb0ef41Sopenharmony_ci    handleEvent: common.mustCall(function(event) {
1421cb0ef41Sopenharmony_ci      strictEqual(event.type, 'foo');
1431cb0ef41Sopenharmony_ci      strictEqual(this, ev2);
1441cb0ef41Sopenharmony_ci    }),
1451cb0ef41Sopenharmony_ci  };
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev1);
1481cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev2, { once: true });
1491cb0ef41Sopenharmony_ci  ok(eventTarget.dispatchEvent(new Event('foo')));
1501cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(new Event('foo'));
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  eventTarget.removeEventListener('foo', ev1);
1531cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(new Event('foo'));
1541cb0ef41Sopenharmony_ci}
1551cb0ef41Sopenharmony_ci{
1561cb0ef41Sopenharmony_ci  // event subclassing
1571cb0ef41Sopenharmony_ci  const SubEvent = class extends Event {};
1581cb0ef41Sopenharmony_ci  const ev = new SubEvent('foo');
1591cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
1601cb0ef41Sopenharmony_ci  const fn = common.mustCall((event) => strictEqual(event, ev));
1611cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', fn, { once: true });
1621cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(ev);
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci{
1661cb0ef41Sopenharmony_ci  // Same event dispatched multiple times.
1671cb0ef41Sopenharmony_ci  const event = new Event('foo');
1681cb0ef41Sopenharmony_ci  const eventTarget1 = new EventTarget();
1691cb0ef41Sopenharmony_ci  const eventTarget2 = new EventTarget();
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  eventTarget1.addEventListener('foo', common.mustCall((event) => {
1721cb0ef41Sopenharmony_ci    strictEqual(event.eventPhase, Event.AT_TARGET);
1731cb0ef41Sopenharmony_ci    strictEqual(event.target, eventTarget1);
1741cb0ef41Sopenharmony_ci    deepStrictEqual(event.composedPath(), [eventTarget1]);
1751cb0ef41Sopenharmony_ci  }));
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  eventTarget2.addEventListener('foo', common.mustCall((event) => {
1781cb0ef41Sopenharmony_ci    strictEqual(event.eventPhase, Event.AT_TARGET);
1791cb0ef41Sopenharmony_ci    strictEqual(event.target, eventTarget2);
1801cb0ef41Sopenharmony_ci    deepStrictEqual(event.composedPath(), [eventTarget2]);
1811cb0ef41Sopenharmony_ci  }));
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  eventTarget1.dispatchEvent(event);
1841cb0ef41Sopenharmony_ci  strictEqual(event.eventPhase, Event.NONE);
1851cb0ef41Sopenharmony_ci  strictEqual(event.target, eventTarget1);
1861cb0ef41Sopenharmony_ci  deepStrictEqual(event.composedPath(), []);
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  eventTarget2.dispatchEvent(event);
1901cb0ef41Sopenharmony_ci  strictEqual(event.eventPhase, Event.NONE);
1911cb0ef41Sopenharmony_ci  strictEqual(event.target, eventTarget2);
1921cb0ef41Sopenharmony_ci  deepStrictEqual(event.composedPath(), []);
1931cb0ef41Sopenharmony_ci}
1941cb0ef41Sopenharmony_ci{
1951cb0ef41Sopenharmony_ci  // Same event dispatched multiple times, without listeners added.
1961cb0ef41Sopenharmony_ci  const event = new Event('foo');
1971cb0ef41Sopenharmony_ci  const eventTarget1 = new EventTarget();
1981cb0ef41Sopenharmony_ci  const eventTarget2 = new EventTarget();
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci  eventTarget1.dispatchEvent(event);
2011cb0ef41Sopenharmony_ci  strictEqual(event.eventPhase, Event.NONE);
2021cb0ef41Sopenharmony_ci  strictEqual(event.target, eventTarget1);
2031cb0ef41Sopenharmony_ci  deepStrictEqual(event.composedPath(), []);
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci  eventTarget2.dispatchEvent(event);
2061cb0ef41Sopenharmony_ci  strictEqual(event.eventPhase, Event.NONE);
2071cb0ef41Sopenharmony_ci  strictEqual(event.target, eventTarget2);
2081cb0ef41Sopenharmony_ci  deepStrictEqual(event.composedPath(), []);
2091cb0ef41Sopenharmony_ci}
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci{
2121cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
2131cb0ef41Sopenharmony_ci  const event = new Event('foo', { cancelable: true });
2141cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', (event) => event.preventDefault());
2151cb0ef41Sopenharmony_ci  ok(!eventTarget.dispatchEvent(event));
2161cb0ef41Sopenharmony_ci}
2171cb0ef41Sopenharmony_ci{
2181cb0ef41Sopenharmony_ci  // Adding event listeners with a boolean useCapture
2191cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
2201cb0ef41Sopenharmony_ci  const event = new Event('foo');
2211cb0ef41Sopenharmony_ci  const fn = common.mustCall((event) => strictEqual(event.type, 'foo'));
2221cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', fn, false);
2231cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(event);
2241cb0ef41Sopenharmony_ci}
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci{
2271cb0ef41Sopenharmony_ci  // The `options` argument can be `null`.
2281cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
2291cb0ef41Sopenharmony_ci  const event = new Event('foo');
2301cb0ef41Sopenharmony_ci  const fn = common.mustCall((event) => strictEqual(event.type, 'foo'));
2311cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', fn, null);
2321cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(event);
2331cb0ef41Sopenharmony_ci}
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci{
2361cb0ef41Sopenharmony_ci  const target = new EventTarget();
2371cb0ef41Sopenharmony_ci  const listener = {};
2381cb0ef41Sopenharmony_ci  // AddEventListener should not require handleEvent to be
2391cb0ef41Sopenharmony_ci  // defined on an EventListener.
2401cb0ef41Sopenharmony_ci  target.addEventListener('foo', listener);
2411cb0ef41Sopenharmony_ci  listener.handleEvent = common.mustCall(function(event) {
2421cb0ef41Sopenharmony_ci    strictEqual(event.type, 'foo');
2431cb0ef41Sopenharmony_ci    strictEqual(this, listener);
2441cb0ef41Sopenharmony_ci  });
2451cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
2461cb0ef41Sopenharmony_ci}
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci{
2491cb0ef41Sopenharmony_ci  const target = new EventTarget();
2501cb0ef41Sopenharmony_ci  const listener = {};
2511cb0ef41Sopenharmony_ci  // do not throw
2521cb0ef41Sopenharmony_ci  target.removeEventListener('foo', listener);
2531cb0ef41Sopenharmony_ci  target.addEventListener('foo', listener);
2541cb0ef41Sopenharmony_ci  target.removeEventListener('foo', listener);
2551cb0ef41Sopenharmony_ci  listener.handleEvent = common.mustNotCall();
2561cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
2571cb0ef41Sopenharmony_ci}
2581cb0ef41Sopenharmony_ci
2591cb0ef41Sopenharmony_ci{
2601cb0ef41Sopenharmony_ci  const uncaughtException = common.mustCall((err, origin) => {
2611cb0ef41Sopenharmony_ci    strictEqual(err.message, 'boom');
2621cb0ef41Sopenharmony_ci    strictEqual(origin, 'uncaughtException');
2631cb0ef41Sopenharmony_ci  }, 4);
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci  // Make sure that we no longer call 'error' on error.
2661cb0ef41Sopenharmony_ci  process.on('error', common.mustNotCall());
2671cb0ef41Sopenharmony_ci  // Don't call rejection even for async handlers.
2681cb0ef41Sopenharmony_ci  process.on('unhandledRejection', common.mustNotCall());
2691cb0ef41Sopenharmony_ci  process.on('uncaughtException', uncaughtException);
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ci  const ev1 = async () => { throw new Error('boom'); };
2741cb0ef41Sopenharmony_ci  const ev2 = () => { throw new Error('boom'); };
2751cb0ef41Sopenharmony_ci  const ev3 = { handleEvent() { throw new Error('boom'); } };
2761cb0ef41Sopenharmony_ci  const ev4 = { async handleEvent() { throw new Error('boom'); } };
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci  // Errors in a handler won't stop calling the others.
2791cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev1, { once: true });
2801cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev2, { once: true });
2811cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev3, { once: true });
2821cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev4, { once: true });
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(new Event('foo'));
2851cb0ef41Sopenharmony_ci}
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci{
2881cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  // Once handler only invoked once
2911cb0ef41Sopenharmony_ci  const ev = common.mustCall((event) => {
2921cb0ef41Sopenharmony_ci    // Can invoke the same event name recursively
2931cb0ef41Sopenharmony_ci    eventTarget.dispatchEvent(new Event('foo'));
2941cb0ef41Sopenharmony_ci  });
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ci  // Errors in a handler won't stop calling the others.
2971cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', ev, { once: true });
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(new Event('foo'));
3001cb0ef41Sopenharmony_ci}
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci{
3031cb0ef41Sopenharmony_ci  // Coercion to string works
3041cb0ef41Sopenharmony_ci  strictEqual((new Event(1)).type, '1');
3051cb0ef41Sopenharmony_ci  strictEqual((new Event(false)).type, 'false');
3061cb0ef41Sopenharmony_ci  strictEqual((new Event({})).type, String({}));
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci  const target = new EventTarget();
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci  [
3111cb0ef41Sopenharmony_ci    'foo',
3121cb0ef41Sopenharmony_ci    {},  // No type event
3131cb0ef41Sopenharmony_ci    undefined,
3141cb0ef41Sopenharmony_ci    1,
3151cb0ef41Sopenharmony_ci    false,
3161cb0ef41Sopenharmony_ci  ].forEach((i) => {
3171cb0ef41Sopenharmony_ci    throws(() => target.dispatchEvent(i), {
3181cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
3191cb0ef41Sopenharmony_ci      name: 'TypeError',
3201cb0ef41Sopenharmony_ci      message: 'The "event" argument must be an instance of Event.' +
3211cb0ef41Sopenharmony_ci               common.invalidArgTypeHelper(i),
3221cb0ef41Sopenharmony_ci    });
3231cb0ef41Sopenharmony_ci  });
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci  const err = (arg) => ({
3261cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
3271cb0ef41Sopenharmony_ci    name: 'TypeError',
3281cb0ef41Sopenharmony_ci    message: 'The "listener" argument must be an instance of EventListener.' +
3291cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(arg),
3301cb0ef41Sopenharmony_ci  });
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci  [
3331cb0ef41Sopenharmony_ci    'foo',
3341cb0ef41Sopenharmony_ci    1,
3351cb0ef41Sopenharmony_ci    false,
3361cb0ef41Sopenharmony_ci  ].forEach((i) => throws(() => target.addEventListener('foo', i), err(i)));
3371cb0ef41Sopenharmony_ci}
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci{
3401cb0ef41Sopenharmony_ci  const target = new EventTarget();
3411cb0ef41Sopenharmony_ci  once(target, 'foo').then(common.mustCall());
3421cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci{
3461cb0ef41Sopenharmony_ci  const target = new EventTarget();
3471cb0ef41Sopenharmony_ci  const event = new Event('foo');
3481cb0ef41Sopenharmony_ci  event.stopImmediatePropagation();
3491cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustNotCall());
3501cb0ef41Sopenharmony_ci  target.dispatchEvent(event);
3511cb0ef41Sopenharmony_ci}
3521cb0ef41Sopenharmony_ci
3531cb0ef41Sopenharmony_ci{
3541cb0ef41Sopenharmony_ci  const target = new EventTarget();
3551cb0ef41Sopenharmony_ci  const event = new Event('foo');
3561cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall((event) => {
3571cb0ef41Sopenharmony_ci    event.stopImmediatePropagation();
3581cb0ef41Sopenharmony_ci  }));
3591cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustNotCall());
3601cb0ef41Sopenharmony_ci  target.dispatchEvent(event);
3611cb0ef41Sopenharmony_ci}
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci{
3641cb0ef41Sopenharmony_ci  const target = new EventTarget();
3651cb0ef41Sopenharmony_ci  const event = new Event('foo');
3661cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall((event) => {
3671cb0ef41Sopenharmony_ci    event.stopImmediatePropagation();
3681cb0ef41Sopenharmony_ci  }));
3691cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustNotCall());
3701cb0ef41Sopenharmony_ci  target.dispatchEvent(event);
3711cb0ef41Sopenharmony_ci}
3721cb0ef41Sopenharmony_ci
3731cb0ef41Sopenharmony_ci{
3741cb0ef41Sopenharmony_ci  const target = new EventTarget();
3751cb0ef41Sopenharmony_ci  const event = new Event('foo');
3761cb0ef41Sopenharmony_ci  strictEqual(event.target, null);
3771cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall((event) => {
3781cb0ef41Sopenharmony_ci    strictEqual(event.target, target);
3791cb0ef41Sopenharmony_ci    strictEqual(event.currentTarget, target);
3801cb0ef41Sopenharmony_ci    strictEqual(event.srcElement, target);
3811cb0ef41Sopenharmony_ci  }));
3821cb0ef41Sopenharmony_ci  target.dispatchEvent(event);
3831cb0ef41Sopenharmony_ci}
3841cb0ef41Sopenharmony_ci
3851cb0ef41Sopenharmony_ci{
3861cb0ef41Sopenharmony_ci  const target1 = new EventTarget();
3871cb0ef41Sopenharmony_ci  const target2 = new EventTarget();
3881cb0ef41Sopenharmony_ci  const event = new Event('foo');
3891cb0ef41Sopenharmony_ci  target1.addEventListener('foo', common.mustCall((event) => {
3901cb0ef41Sopenharmony_ci    throws(() => target2.dispatchEvent(event), {
3911cb0ef41Sopenharmony_ci      code: 'ERR_EVENT_RECURSION',
3921cb0ef41Sopenharmony_ci    });
3931cb0ef41Sopenharmony_ci  }));
3941cb0ef41Sopenharmony_ci  target1.dispatchEvent(event);
3951cb0ef41Sopenharmony_ci}
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci{
3981cb0ef41Sopenharmony_ci  const target = new EventTarget();
3991cb0ef41Sopenharmony_ci  const a = common.mustCall(() => target.removeEventListener('foo', a));
4001cb0ef41Sopenharmony_ci  const b = common.mustCall(2);
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci  target.addEventListener('foo', a);
4031cb0ef41Sopenharmony_ci  target.addEventListener('foo', b);
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4061cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4071cb0ef41Sopenharmony_ci}
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci{
4101cb0ef41Sopenharmony_ci  const target = new EventTarget();
4111cb0ef41Sopenharmony_ci  const a = common.mustCall(3);
4121cb0ef41Sopenharmony_ci
4131cb0ef41Sopenharmony_ci  target.addEventListener('foo', a, { capture: true });
4141cb0ef41Sopenharmony_ci  target.addEventListener('foo', a, { capture: false });
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4171cb0ef41Sopenharmony_ci  target.removeEventListener('foo', a, { capture: true });
4181cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4191cb0ef41Sopenharmony_ci  target.removeEventListener('foo', a, { capture: false });
4201cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4211cb0ef41Sopenharmony_ci}
4221cb0ef41Sopenharmony_ci{
4231cb0ef41Sopenharmony_ci  const target = new EventTarget();
4241cb0ef41Sopenharmony_ci  strictEqual(target.toString(), '[object EventTarget]');
4251cb0ef41Sopenharmony_ci  const event = new Event('');
4261cb0ef41Sopenharmony_ci  strictEqual(event.toString(), '[object Event]');
4271cb0ef41Sopenharmony_ci}
4281cb0ef41Sopenharmony_ci{
4291cb0ef41Sopenharmony_ci  const target = new EventTarget();
4301cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
4311cb0ef41Sopenharmony_ci  target.onfoo = common.mustCall();
4321cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4331cb0ef41Sopenharmony_ci}
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci{
4361cb0ef41Sopenharmony_ci  const target = new EventTarget();
4371cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
4381cb0ef41Sopenharmony_ci  strictEqual(target.onfoo, null);
4391cb0ef41Sopenharmony_ci}
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci{
4421cb0ef41Sopenharmony_ci  const target = new EventTarget();
4431cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
4441cb0ef41Sopenharmony_ci  let count = 0;
4451cb0ef41Sopenharmony_ci  target.onfoo = () => count++;
4461cb0ef41Sopenharmony_ci  target.onfoo = common.mustCall(() => count++);
4471cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4481cb0ef41Sopenharmony_ci  strictEqual(count, 1);
4491cb0ef41Sopenharmony_ci}
4501cb0ef41Sopenharmony_ci{
4511cb0ef41Sopenharmony_ci  const target = new EventTarget();
4521cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
4531cb0ef41Sopenharmony_ci  let count = 0;
4541cb0ef41Sopenharmony_ci  target.addEventListener('foo', () => count++);
4551cb0ef41Sopenharmony_ci  target.onfoo = common.mustCall(() => count++);
4561cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4571cb0ef41Sopenharmony_ci  strictEqual(count, 2);
4581cb0ef41Sopenharmony_ci}
4591cb0ef41Sopenharmony_ci{
4601cb0ef41Sopenharmony_ci  const target = new EventTarget();
4611cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
4621cb0ef41Sopenharmony_ci  const fn = common.mustNotCall();
4631cb0ef41Sopenharmony_ci  target.onfoo = fn;
4641cb0ef41Sopenharmony_ci  strictEqual(target.onfoo, fn);
4651cb0ef41Sopenharmony_ci  target.onfoo = null;
4661cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
4671cb0ef41Sopenharmony_ci}
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_ci{
4701cb0ef41Sopenharmony_ci  // `this` value of dispatchEvent
4711cb0ef41Sopenharmony_ci  const target = new EventTarget();
4721cb0ef41Sopenharmony_ci  const target2 = new EventTarget();
4731cb0ef41Sopenharmony_ci  const event = new Event('foo');
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci  ok(target.dispatchEvent.call(target2, event));
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci  [
4781cb0ef41Sopenharmony_ci    'foo',
4791cb0ef41Sopenharmony_ci    {},
4801cb0ef41Sopenharmony_ci    [],
4811cb0ef41Sopenharmony_ci    1,
4821cb0ef41Sopenharmony_ci    null,
4831cb0ef41Sopenharmony_ci    undefined,
4841cb0ef41Sopenharmony_ci    false,
4851cb0ef41Sopenharmony_ci    Symbol(),
4861cb0ef41Sopenharmony_ci    /a/,
4871cb0ef41Sopenharmony_ci  ].forEach((i) => {
4881cb0ef41Sopenharmony_ci    throws(() => target.dispatchEvent.call(i, event), {
4891cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_THIS',
4901cb0ef41Sopenharmony_ci    });
4911cb0ef41Sopenharmony_ci  });
4921cb0ef41Sopenharmony_ci}
4931cb0ef41Sopenharmony_ci
4941cb0ef41Sopenharmony_ci{
4951cb0ef41Sopenharmony_ci  // Event Statics
4961cb0ef41Sopenharmony_ci  strictEqual(Event.NONE, 0);
4971cb0ef41Sopenharmony_ci  strictEqual(Event.CAPTURING_PHASE, 1);
4981cb0ef41Sopenharmony_ci  strictEqual(Event.AT_TARGET, 2);
4991cb0ef41Sopenharmony_ci  strictEqual(Event.BUBBLING_PHASE, 3);
5001cb0ef41Sopenharmony_ci  strictEqual(new Event('foo').eventPhase, Event.NONE);
5011cb0ef41Sopenharmony_ci  const target = new EventTarget();
5021cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall((e) => {
5031cb0ef41Sopenharmony_ci    strictEqual(e.eventPhase, Event.AT_TARGET);
5041cb0ef41Sopenharmony_ci  }), { once: true });
5051cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
5061cb0ef41Sopenharmony_ci  // Event is a function
5071cb0ef41Sopenharmony_ci  strictEqual(Event.length, 1);
5081cb0ef41Sopenharmony_ci}
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ci{
5111cb0ef41Sopenharmony_ci  const target = new EventTarget();
5121cb0ef41Sopenharmony_ci  const ev = new Event('toString');
5131cb0ef41Sopenharmony_ci  const fn = common.mustCall((event) => strictEqual(event.type, 'toString'));
5141cb0ef41Sopenharmony_ci  target.addEventListener('toString', fn);
5151cb0ef41Sopenharmony_ci  target.dispatchEvent(ev);
5161cb0ef41Sopenharmony_ci}
5171cb0ef41Sopenharmony_ci{
5181cb0ef41Sopenharmony_ci  const target = new EventTarget();
5191cb0ef41Sopenharmony_ci  const ev = new Event('__proto__');
5201cb0ef41Sopenharmony_ci  const fn = common.mustCall((event) => strictEqual(event.type, '__proto__'));
5211cb0ef41Sopenharmony_ci  target.addEventListener('__proto__', fn);
5221cb0ef41Sopenharmony_ci  target.dispatchEvent(ev);
5231cb0ef41Sopenharmony_ci}
5241cb0ef41Sopenharmony_ci
5251cb0ef41Sopenharmony_ci{
5261cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
5271cb0ef41Sopenharmony_ci  // Single argument throws
5281cb0ef41Sopenharmony_ci  throws(() => eventTarget.addEventListener('foo'), TypeError);
5291cb0ef41Sopenharmony_ci  // Null events - does not throw
5301cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', null);
5311cb0ef41Sopenharmony_ci  eventTarget.removeEventListener('foo', null);
5321cb0ef41Sopenharmony_ci  eventTarget.addEventListener('foo', undefined);
5331cb0ef41Sopenharmony_ci  eventTarget.removeEventListener('foo', undefined);
5341cb0ef41Sopenharmony_ci  // Strings, booleans
5351cb0ef41Sopenharmony_ci  throws(() => eventTarget.addEventListener('foo', 'hello'), TypeError);
5361cb0ef41Sopenharmony_ci  throws(() => eventTarget.addEventListener('foo', false), TypeError);
5371cb0ef41Sopenharmony_ci  throws(() => eventTarget.addEventListener('foo', Symbol()), TypeError);
5381cb0ef41Sopenharmony_ci  asyncTest = asyncTest.then(async () => {
5391cb0ef41Sopenharmony_ci    const eventTarget = new EventTarget();
5401cb0ef41Sopenharmony_ci    // Single argument throws
5411cb0ef41Sopenharmony_ci    throws(() => eventTarget.addEventListener('foo'), TypeError);
5421cb0ef41Sopenharmony_ci    // Null events - does not throw
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci    eventTarget.addEventListener('foo', null);
5451cb0ef41Sopenharmony_ci    eventTarget.removeEventListener('foo', null);
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci    // Warnings always happen after nextTick, so wait for a timer of 0
5481cb0ef41Sopenharmony_ci    await delay(0);
5491cb0ef41Sopenharmony_ci    strictEqual(lastWarning.name, 'AddEventListenerArgumentTypeWarning');
5501cb0ef41Sopenharmony_ci    strictEqual(lastWarning.target, eventTarget);
5511cb0ef41Sopenharmony_ci    lastWarning = null;
5521cb0ef41Sopenharmony_ci    eventTarget.addEventListener('foo', undefined);
5531cb0ef41Sopenharmony_ci    await delay(0);
5541cb0ef41Sopenharmony_ci    strictEqual(lastWarning.name, 'AddEventListenerArgumentTypeWarning');
5551cb0ef41Sopenharmony_ci    strictEqual(lastWarning.target, eventTarget);
5561cb0ef41Sopenharmony_ci    eventTarget.removeEventListener('foo', undefined);
5571cb0ef41Sopenharmony_ci    // Strings, booleans
5581cb0ef41Sopenharmony_ci    throws(() => eventTarget.addEventListener('foo', 'hello'), TypeError);
5591cb0ef41Sopenharmony_ci    throws(() => eventTarget.addEventListener('foo', false), TypeError);
5601cb0ef41Sopenharmony_ci    throws(() => eventTarget.addEventListener('foo', Symbol()), TypeError);
5611cb0ef41Sopenharmony_ci  });
5621cb0ef41Sopenharmony_ci}
5631cb0ef41Sopenharmony_ci{
5641cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
5651cb0ef41Sopenharmony_ci  const event = new Event('foo');
5661cb0ef41Sopenharmony_ci  eventTarget.dispatchEvent(event);
5671cb0ef41Sopenharmony_ci  strictEqual(event.target, eventTarget);
5681cb0ef41Sopenharmony_ci}
5691cb0ef41Sopenharmony_ci{
5701cb0ef41Sopenharmony_ci  // Event target exported keys
5711cb0ef41Sopenharmony_ci  const eventTarget = new EventTarget();
5721cb0ef41Sopenharmony_ci  deepStrictEqual(Object.keys(eventTarget), []);
5731cb0ef41Sopenharmony_ci  deepStrictEqual(Object.getOwnPropertyNames(eventTarget), []);
5741cb0ef41Sopenharmony_ci  const parentKeys = Object.keys(Object.getPrototypeOf(eventTarget)).sort();
5751cb0ef41Sopenharmony_ci  const keys = ['addEventListener', 'dispatchEvent', 'removeEventListener'];
5761cb0ef41Sopenharmony_ci  deepStrictEqual(parentKeys, keys);
5771cb0ef41Sopenharmony_ci}
5781cb0ef41Sopenharmony_ci{
5791cb0ef41Sopenharmony_ci  // Subclassing
5801cb0ef41Sopenharmony_ci  class SubTarget extends EventTarget {}
5811cb0ef41Sopenharmony_ci  const target = new SubTarget();
5821cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall());
5831cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
5841cb0ef41Sopenharmony_ci}
5851cb0ef41Sopenharmony_ci{
5861cb0ef41Sopenharmony_ci  // Test event order
5871cb0ef41Sopenharmony_ci  const target = new EventTarget();
5881cb0ef41Sopenharmony_ci  let state = 0;
5891cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall(() => {
5901cb0ef41Sopenharmony_ci    strictEqual(state, 0);
5911cb0ef41Sopenharmony_ci    state++;
5921cb0ef41Sopenharmony_ci  }));
5931cb0ef41Sopenharmony_ci  target.addEventListener('foo', common.mustCall(() => {
5941cb0ef41Sopenharmony_ci    strictEqual(state, 1);
5951cb0ef41Sopenharmony_ci  }));
5961cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
5971cb0ef41Sopenharmony_ci}
5981cb0ef41Sopenharmony_ci{
5991cb0ef41Sopenharmony_ci  const target = new EventTarget();
6001cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
6011cb0ef41Sopenharmony_ci  const descriptor = Object.getOwnPropertyDescriptor(target, 'onfoo');
6021cb0ef41Sopenharmony_ci  strictEqual(descriptor.configurable, true);
6031cb0ef41Sopenharmony_ci  strictEqual(descriptor.enumerable, true);
6041cb0ef41Sopenharmony_ci}
6051cb0ef41Sopenharmony_ci{
6061cb0ef41Sopenharmony_ci  const target = new EventTarget();
6071cb0ef41Sopenharmony_ci  defineEventHandler(target, 'foo');
6081cb0ef41Sopenharmony_ci  const output = [];
6091cb0ef41Sopenharmony_ci  target.addEventListener('foo', () => output.push(1));
6101cb0ef41Sopenharmony_ci  target.onfoo = common.mustNotCall();
6111cb0ef41Sopenharmony_ci  target.addEventListener('foo', () => output.push(3));
6121cb0ef41Sopenharmony_ci  target.onfoo = () => output.push(2);
6131cb0ef41Sopenharmony_ci  target.addEventListener('foo', () => output.push(4));
6141cb0ef41Sopenharmony_ci  target.dispatchEvent(new Event('foo'));
6151cb0ef41Sopenharmony_ci  deepStrictEqual(output, [1, 2, 3, 4]);
6161cb0ef41Sopenharmony_ci}
6171cb0ef41Sopenharmony_ci{
6181cb0ef41Sopenharmony_ci  const et = new EventTarget();
6191cb0ef41Sopenharmony_ci  const listener = common.mustNotCall();
6201cb0ef41Sopenharmony_ci  et.addEventListener('foo', common.mustCall((e) => {
6211cb0ef41Sopenharmony_ci    et.removeEventListener('foo', listener);
6221cb0ef41Sopenharmony_ci  }));
6231cb0ef41Sopenharmony_ci  et.addEventListener('foo', listener);
6241cb0ef41Sopenharmony_ci  et.dispatchEvent(new Event('foo'));
6251cb0ef41Sopenharmony_ci}
6261cb0ef41Sopenharmony_ci
6271cb0ef41Sopenharmony_ci{
6281cb0ef41Sopenharmony_ci  const ev = new Event('test');
6291cb0ef41Sopenharmony_ci  const evConstructorName = inspect(ev, {
6301cb0ef41Sopenharmony_ci    depth: -1,
6311cb0ef41Sopenharmony_ci  });
6321cb0ef41Sopenharmony_ci  strictEqual(evConstructorName, 'Event');
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci  const inspectResult = inspect(ev, {
6351cb0ef41Sopenharmony_ci    depth: 1,
6361cb0ef41Sopenharmony_ci  });
6371cb0ef41Sopenharmony_ci  ok(inspectResult.includes('Event'));
6381cb0ef41Sopenharmony_ci}
6391cb0ef41Sopenharmony_ci
6401cb0ef41Sopenharmony_ci{
6411cb0ef41Sopenharmony_ci  const et = new EventTarget();
6421cb0ef41Sopenharmony_ci  const inspectResult = inspect(et, {
6431cb0ef41Sopenharmony_ci    depth: 1,
6441cb0ef41Sopenharmony_ci  });
6451cb0ef41Sopenharmony_ci  ok(inspectResult.includes('EventTarget'));
6461cb0ef41Sopenharmony_ci}
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci{
6491cb0ef41Sopenharmony_ci  const ev = new Event('test');
6501cb0ef41Sopenharmony_ci  strictEqual(ev.constructor.name, 'Event');
6511cb0ef41Sopenharmony_ci
6521cb0ef41Sopenharmony_ci  const et = new EventTarget();
6531cb0ef41Sopenharmony_ci  strictEqual(et.constructor.name, 'EventTarget');
6541cb0ef41Sopenharmony_ci}
6551cb0ef41Sopenharmony_ci{
6561cb0ef41Sopenharmony_ci  // Weak event listeners work
6571cb0ef41Sopenharmony_ci  const et = new EventTarget();
6581cb0ef41Sopenharmony_ci  const listener = common.mustCall();
6591cb0ef41Sopenharmony_ci  et.addEventListener('foo', listener, { [kWeakHandler]: et });
6601cb0ef41Sopenharmony_ci  et.dispatchEvent(new Event('foo'));
6611cb0ef41Sopenharmony_ci}
6621cb0ef41Sopenharmony_ci{
6631cb0ef41Sopenharmony_ci  // Weak event listeners can be removed and weakness is not part of the key
6641cb0ef41Sopenharmony_ci  const et = new EventTarget();
6651cb0ef41Sopenharmony_ci  const listener = common.mustNotCall();
6661cb0ef41Sopenharmony_ci  et.addEventListener('foo', listener, { [kWeakHandler]: et });
6671cb0ef41Sopenharmony_ci  et.removeEventListener('foo', listener);
6681cb0ef41Sopenharmony_ci  et.dispatchEvent(new Event('foo'));
6691cb0ef41Sopenharmony_ci}
6701cb0ef41Sopenharmony_ci{
6711cb0ef41Sopenharmony_ci  // Test listeners are held weakly
6721cb0ef41Sopenharmony_ci  const et = new EventTarget();
6731cb0ef41Sopenharmony_ci  et.addEventListener('foo', common.mustNotCall(), { [kWeakHandler]: {} });
6741cb0ef41Sopenharmony_ci  setImmediate(() => {
6751cb0ef41Sopenharmony_ci    global.gc();
6761cb0ef41Sopenharmony_ci    et.dispatchEvent(new Event('foo'));
6771cb0ef41Sopenharmony_ci  });
6781cb0ef41Sopenharmony_ci}
6791cb0ef41Sopenharmony_ci
6801cb0ef41Sopenharmony_ci{
6811cb0ef41Sopenharmony_ci  const et = new EventTarget();
6821cb0ef41Sopenharmony_ci
6831cb0ef41Sopenharmony_ci  throws(() => et.addEventListener(), {
6841cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
6851cb0ef41Sopenharmony_ci    name: 'TypeError',
6861cb0ef41Sopenharmony_ci  });
6871cb0ef41Sopenharmony_ci
6881cb0ef41Sopenharmony_ci  throws(() => et.addEventListener('foo'), {
6891cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
6901cb0ef41Sopenharmony_ci    name: 'TypeError',
6911cb0ef41Sopenharmony_ci  });
6921cb0ef41Sopenharmony_ci
6931cb0ef41Sopenharmony_ci  throws(() => et.removeEventListener(), {
6941cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
6951cb0ef41Sopenharmony_ci    name: 'TypeError',
6961cb0ef41Sopenharmony_ci  });
6971cb0ef41Sopenharmony_ci
6981cb0ef41Sopenharmony_ci  throws(() => et.removeEventListener('foo'), {
6991cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
7001cb0ef41Sopenharmony_ci    name: 'TypeError',
7011cb0ef41Sopenharmony_ci  });
7021cb0ef41Sopenharmony_ci
7031cb0ef41Sopenharmony_ci  throws(() => et.dispatchEvent(), {
7041cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
7051cb0ef41Sopenharmony_ci    name: 'TypeError',
7061cb0ef41Sopenharmony_ci  });
7071cb0ef41Sopenharmony_ci}
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci{
7101cb0ef41Sopenharmony_ci  const et = new EventTarget();
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci  throws(() => {
7131cb0ef41Sopenharmony_ci    et.addEventListener(Symbol('symbol'), () => {});
7141cb0ef41Sopenharmony_ci  }, TypeError);
7151cb0ef41Sopenharmony_ci
7161cb0ef41Sopenharmony_ci  throws(() => {
7171cb0ef41Sopenharmony_ci    et.removeEventListener(Symbol('symbol'), () => {});
7181cb0ef41Sopenharmony_ci  }, TypeError);
7191cb0ef41Sopenharmony_ci}
7201cb0ef41Sopenharmony_ci
7211cb0ef41Sopenharmony_ci{
7221cb0ef41Sopenharmony_ci  // Test that event listeners are removed by signal even when
7231cb0ef41Sopenharmony_ci  // signal's abort event propagation stopped
7241cb0ef41Sopenharmony_ci  const controller = new AbortController();
7251cb0ef41Sopenharmony_ci  const { signal } = controller;
7261cb0ef41Sopenharmony_ci  signal.addEventListener('abort', (e) => e.stopImmediatePropagation(), { once: true });
7271cb0ef41Sopenharmony_ci  const et = new EventTarget();
7281cb0ef41Sopenharmony_ci  et.addEventListener('foo', common.mustNotCall(), { signal });
7291cb0ef41Sopenharmony_ci  controller.abort();
7301cb0ef41Sopenharmony_ci  et.dispatchEvent(new Event('foo'));
7311cb0ef41Sopenharmony_ci}
732