1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/tests/unit/event_target_test.ts

296 lines
7.3 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "./test_util.ts";
2019-01-05 10:02:44 -05:00
Deno.test(function addEventListenerTest() {
2019-01-05 10:02:44 -05:00
const document = new EventTarget();
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
});
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;
const listener = (e: Event) => {
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);
assertEquals(callCount, 1);
2019-01-05 10:02:44 -05:00
target.dispatchEvent(event);
assertEquals(callCount, 2);
2019-01-05 10:02:44 -05:00
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
2019-01-05 10:02:44 -05:00
});
Deno.test(function anEventTargetCanBeSubclassed() {
2019-01-05 10:02:44 -05:00
class NicerEventTarget extends EventTarget {
on(
type: string,
callback: ((e: Event) => void) | null,
options?: AddEventListenerOptions,
) {
2019-01-23 07:20:53 -05:00
this.addEventListener(type, callback, options);
2019-01-05 10:02:44 -05:00
}
off(
type: string,
callback: ((e: Event) => void) | null,
options?: EventListenerOptions,
) {
2019-01-05 10:02:44 -05:00
this.removeEventListener(type, callback, options);
}
}
const target = new NicerEventTarget();
new Event("foo", { bubbles: true, cancelable: false });
2019-01-05 10:02:44 -05:00
let callCount = 0;
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);
assertEquals(callCount, 0);
2019-01-05 10:02:44 -05:00
target.off("foo", listener);
assertEquals(callCount, 0);
2019-01-05 10:02:44 -05:00
});
Deno.test(function removingNullEventListenerShouldSucceed() {
2019-01-05 10:02:44 -05:00
const document = new EventTarget();
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
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;
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);
});
Deno.test(function toStringShouldBeWebCompatible() {
const target = new EventTarget();
assertEquals(target.toString(), "[object EventTarget]");
});
Deno.test(function dispatchEventShouldNotThrowError() {
let hasThrown = false;
try {
const target = new EventTarget();
const event = new Event("hasOwnProperty", {
bubbles: true,
cancelable: false,
});
const listener = () => {};
2019-05-27 09:20:34 -04:00
target.addEventListener("hasOwnProperty", listener);
target.dispatchEvent(event);
} catch {
hasThrown = true;
}
assertEquals(hasThrown, false);
});
Deno.test(function eventTargetThisShouldDefaultToWindow() {
const {
addEventListener,
dispatchEvent,
removeEventListener,
} = EventTarget.prototype;
let n = 1;
const event = new Event("hello");
const listener = () => {
n = 2;
};
addEventListener("hello", listener);
globalThis.dispatchEvent(event);
assertEquals(n, 2);
n = 1;
removeEventListener("hello", listener);
globalThis.dispatchEvent(event);
assertEquals(n, 1);
globalThis.addEventListener("hello", listener);
dispatchEvent(event);
assertEquals(n, 2);
n = 1;
globalThis.removeEventListener("hello", listener);
dispatchEvent(event);
assertEquals(n, 1);
});
Deno.test(function eventTargetShouldAcceptEventListenerObject() {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = {
handleEvent(e: Event) {
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);
});
Deno.test(function eventTargetShouldAcceptAsyncFunction() {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = (e: Event) => {
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);
});
Deno.test(
function eventTargetShouldAcceptAsyncFunctionForEventListenerObject() {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = {
handleEvent(e: Event) {
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);
},
);
Deno.test(function eventTargetDispatchShouldSetTargetNoListener() {
const target = new EventTarget();
const event = new Event("foo");
assertEquals(event.target, null);
target.dispatchEvent(event);
assertEquals(event.target, target);
});
Deno.test(function eventTargetDispatchShouldSetTargetInListener() {
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);
});
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);
});
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,
);
});