2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-06-04 19:09:29 -04:00
|
|
|
|
2022-06-17 11:05:02 -04:00
|
|
|
import { assertEquals, assertThrows } from "./test_util.ts";
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function addEventListenerTest() {
|
2019-01-05 10:02:44 -05:00
|
|
|
const document = new EventTarget();
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(document.addEventListener("x", null, false), undefined);
|
|
|
|
assertEquals(document.addEventListener("x", null, true), undefined);
|
|
|
|
assertEquals(document.addEventListener("x", null), undefined);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function constructedEventTargetCanBeUsedAsExpected() {
|
2019-01-05 10:02:44 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = (e: Event) => {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e, event);
|
2019-01-05 10:02:44 -05:00
|
|
|
++callCount;
|
2019-05-27 09:20:34 -04:00
|
|
|
};
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.addEventListener("foo", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 1);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 2);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.removeEventListener("foo", listener);
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 2);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function anEventTargetCanBeSubclassed() {
|
2019-01-05 10:02:44 -05:00
|
|
|
class NicerEventTarget extends EventTarget {
|
2020-02-19 15:36:18 -05:00
|
|
|
on(
|
|
|
|
type: string,
|
2020-03-05 08:36:13 -05:00
|
|
|
callback: ((e: Event) => void) | null,
|
2020-07-14 15:24:17 -04:00
|
|
|
options?: AddEventListenerOptions,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2019-01-23 07:20:53 -05:00
|
|
|
this.addEventListener(type, callback, options);
|
2019-01-05 10:02:44 -05:00
|
|
|
}
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
off(
|
|
|
|
type: string,
|
2020-03-05 08:36:13 -05:00
|
|
|
callback: ((e: Event) => void) | null,
|
2020-07-14 15:24:17 -04:00
|
|
|
options?: EventListenerOptions,
|
2021-08-05 07:08:58 -04:00
|
|
|
) {
|
2019-01-05 10:02:44 -05:00
|
|
|
this.removeEventListener(type, callback, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const target = new NicerEventTarget();
|
2019-03-09 12:30:38 -05:00
|
|
|
new Event("foo", { bubbles: true, cancelable: false });
|
2019-01-05 10:02:44 -05:00
|
|
|
let callCount = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = () => {
|
2019-01-05 10:02:44 -05:00
|
|
|
++callCount;
|
2019-05-27 09:20:34 -04:00
|
|
|
};
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.on("foo", listener);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 0);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.off("foo", listener);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 0);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function removingNullEventListenerShouldSucceed() {
|
2019-01-05 10:02:44 -05:00
|
|
|
const document = new EventTarget();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(document.removeEventListener("x", null, false), undefined);
|
|
|
|
assertEquals(document.removeEventListener("x", null, true), undefined);
|
|
|
|
assertEquals(document.removeEventListener("x", null), undefined);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
2019-03-30 08:18:19 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function constructedEventTargetUseObjectPrototype() {
|
2019-03-30 08:18:19 -04:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("toString", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = (e: Event) => {
|
2019-03-30 08:18:19 -04:00
|
|
|
assertEquals(e, event);
|
|
|
|
++callCount;
|
2019-05-27 09:20:34 -04:00
|
|
|
};
|
2019-03-30 08:18:19 -04:00
|
|
|
|
|
|
|
target.addEventListener("toString", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
|
|
|
|
target.removeEventListener("toString", listener);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
});
|
2019-04-03 08:41:05 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function toStringShouldBeWebCompatible() {
|
2019-04-03 08:41:05 -04:00
|
|
|
const target = new EventTarget();
|
|
|
|
assertEquals(target.toString(), "[object EventTarget]");
|
|
|
|
});
|
2019-04-18 21:56:33 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function dispatchEventShouldNotThrowError() {
|
2019-04-18 21:56:33 -04:00
|
|
|
let hasThrown = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("hasOwnProperty", {
|
|
|
|
bubbles: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
cancelable: false,
|
2019-04-18 21:56:33 -04:00
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = () => {};
|
2019-05-27 09:20:34 -04:00
|
|
|
target.addEventListener("hasOwnProperty", listener);
|
2019-04-18 21:56:33 -04:00
|
|
|
target.dispatchEvent(event);
|
|
|
|
} catch {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(hasThrown, false);
|
|
|
|
});
|
2019-09-11 12:53:01 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function eventTargetThisShouldDefaultToWindow() {
|
2019-09-11 12:53:01 -04:00
|
|
|
const {
|
|
|
|
addEventListener,
|
|
|
|
dispatchEvent,
|
2020-03-28 13:03:49 -04:00
|
|
|
removeEventListener,
|
2019-09-11 12:53:01 -04:00
|
|
|
} = EventTarget.prototype;
|
|
|
|
let n = 1;
|
|
|
|
const event = new Event("hello");
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = () => {
|
2019-09-11 12:53:01 -04:00
|
|
|
n = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
addEventListener("hello", listener);
|
2024-06-04 19:09:29 -04:00
|
|
|
globalThis.dispatchEvent(event);
|
2019-09-11 12:53:01 -04:00
|
|
|
assertEquals(n, 2);
|
|
|
|
n = 1;
|
|
|
|
removeEventListener("hello", listener);
|
2024-06-04 19:09:29 -04:00
|
|
|
globalThis.dispatchEvent(event);
|
2019-09-11 12:53:01 -04:00
|
|
|
assertEquals(n, 1);
|
|
|
|
|
2024-06-04 19:09:29 -04:00
|
|
|
globalThis.addEventListener("hello", listener);
|
2019-09-11 12:53:01 -04:00
|
|
|
dispatchEvent(event);
|
|
|
|
assertEquals(n, 2);
|
|
|
|
n = 1;
|
2024-06-04 19:09:29 -04:00
|
|
|
globalThis.removeEventListener("hello", listener);
|
2019-09-11 12:53:01 -04:00
|
|
|
dispatchEvent(event);
|
|
|
|
assertEquals(n, 1);
|
|
|
|
});
|
2020-03-05 08:36:13 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function eventTargetShouldAcceptEventListenerObject() {
|
2020-03-05 08:36:13 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
|
|
|
const listener = {
|
2021-08-05 07:08:58 -04:00
|
|
|
handleEvent(e: Event) {
|
2020-03-05 08:36:13 -05:00
|
|
|
assertEquals(e, event);
|
|
|
|
++callCount;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-05 08:36:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
target.addEventListener("foo", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
|
|
|
|
target.removeEventListener("foo", listener);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function eventTargetShouldAcceptAsyncFunction() {
|
2020-03-05 08:36:13 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const listener = (e: Event) => {
|
2020-03-05 08:36:13 -05:00
|
|
|
assertEquals(e, event);
|
|
|
|
++callCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
target.addEventListener("foo", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
|
|
|
|
target.removeEventListener("foo", listener);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-08-05 07:08:58 -04:00
|
|
|
function eventTargetShouldAcceptAsyncFunctionForEventListenerObject() {
|
2020-03-05 08:36:13 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
|
|
|
const listener = {
|
2021-08-05 07:08:58 -04:00
|
|
|
handleEvent(e: Event) {
|
2020-03-05 08:36:13 -05:00
|
|
|
assertEquals(e, event);
|
|
|
|
++callCount;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-05 08:36:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
target.addEventListener("foo", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
|
|
|
|
target.removeEventListener("foo", listener);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-05 08:36:13 -05:00
|
|
|
);
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function eventTargetDispatchShouldSetTargetNoListener() {
|
2020-11-02 12:42:22 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo");
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(event.target, target);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function eventTargetDispatchShouldSetTargetInListener() {
|
2020-11-02 12:42:22 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo");
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
let called = false;
|
|
|
|
target.addEventListener("foo", (e) => {
|
|
|
|
assertEquals(e.target, target);
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(called, true);
|
|
|
|
});
|
2022-06-17 11:05:02 -04:00
|
|
|
|
2023-06-04 20:03:44 -04:00
|
|
|
Deno.test(function eventTargetDispatchShouldFireCurrentListenersOnly() {
|
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo");
|
|
|
|
let callCount = 0;
|
|
|
|
target.addEventListener("foo", () => {
|
|
|
|
++callCount;
|
|
|
|
target.addEventListener("foo", () => {
|
|
|
|
++callCount;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
});
|
|
|
|
|
2022-06-17 11:05:02 -04:00
|
|
|
Deno.test(function eventTargetAddEventListenerGlobalAbort() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const c = new AbortController();
|
|
|
|
|
|
|
|
c.signal.addEventListener("abort", () => resolve());
|
|
|
|
addEventListener("test", () => {}, { signal: c.signal });
|
|
|
|
c.abort();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function eventTargetBrandChecking() {
|
|
|
|
const self = {};
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
EventTarget.prototype.addEventListener.call(self, "test", null);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
EventTarget.prototype.removeEventListener.call(self, "test", null);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
EventTarget.prototype.dispatchEvent.call(self, new Event("test"));
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
);
|
|
|
|
});
|