mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
90125566bb
Fixes #3324 Co-authored-by: Kitson Kelly <me@kitsonkelly.com>
156 lines
4 KiB
TypeScript
156 lines
4 KiB
TypeScript
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
import { test, assertEquals } from "./test_util.ts";
|
|
|
|
test(function addEventListenerTest(): void {
|
|
const document = new EventTarget();
|
|
|
|
// @ts-ignore tests ignoring the type system for resilience
|
|
assertEquals(document.addEventListener("x", null, false), undefined);
|
|
// @ts-ignore
|
|
assertEquals(document.addEventListener("x", null, true), undefined);
|
|
// @ts-ignore
|
|
assertEquals(document.addEventListener("x", null), undefined);
|
|
});
|
|
|
|
test(function constructedEventTargetCanBeUsedAsExpected(): void {
|
|
const target = new EventTarget();
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
let callCount = 0;
|
|
|
|
const listener = (e: Event): void => {
|
|
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);
|
|
});
|
|
|
|
test(function anEventTargetCanBeSubclassed(): void {
|
|
class NicerEventTarget extends EventTarget {
|
|
on(
|
|
type: string,
|
|
callback: (e: Event) => void | null,
|
|
options?: __domTypes.AddEventListenerOptions
|
|
): void {
|
|
this.addEventListener(type, callback, options);
|
|
}
|
|
|
|
off(
|
|
type: string,
|
|
callback: (e: Event) => void | null,
|
|
options?: __domTypes.EventListenerOptions
|
|
): void {
|
|
this.removeEventListener(type, callback, options);
|
|
}
|
|
}
|
|
|
|
const target = new NicerEventTarget();
|
|
new Event("foo", { bubbles: true, cancelable: false });
|
|
let callCount = 0;
|
|
|
|
const listener = (): void => {
|
|
++callCount;
|
|
};
|
|
|
|
target.on("foo", listener);
|
|
assertEquals(callCount, 0);
|
|
|
|
target.off("foo", listener);
|
|
assertEquals(callCount, 0);
|
|
});
|
|
|
|
test(function removingNullEventListenerShouldSucceed(): void {
|
|
const document = new EventTarget();
|
|
// @ts-ignore
|
|
assertEquals(document.removeEventListener("x", null, false), undefined);
|
|
// @ts-ignore
|
|
assertEquals(document.removeEventListener("x", null, true), undefined);
|
|
// @ts-ignore
|
|
assertEquals(document.removeEventListener("x", null), undefined);
|
|
});
|
|
|
|
test(function constructedEventTargetUseObjectPrototype(): void {
|
|
const target = new EventTarget();
|
|
const event = new Event("toString", { bubbles: true, cancelable: false });
|
|
let callCount = 0;
|
|
|
|
const listener = (e: Event): void => {
|
|
assertEquals(e, event);
|
|
++callCount;
|
|
};
|
|
|
|
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);
|
|
});
|
|
|
|
test(function toStringShouldBeWebCompatible(): void {
|
|
const target = new EventTarget();
|
|
assertEquals(target.toString(), "[object EventTarget]");
|
|
});
|
|
|
|
test(function dispatchEventShouldNotThrowError(): void {
|
|
let hasThrown = false;
|
|
|
|
try {
|
|
const target = new EventTarget();
|
|
const event = new Event("hasOwnProperty", {
|
|
bubbles: true,
|
|
cancelable: false
|
|
});
|
|
const listener = (): void => {};
|
|
target.addEventListener("hasOwnProperty", listener);
|
|
target.dispatchEvent(event);
|
|
} catch {
|
|
hasThrown = true;
|
|
}
|
|
|
|
assertEquals(hasThrown, false);
|
|
});
|
|
|
|
test(function eventTargetThisShouldDefaultToWindow(): void {
|
|
const {
|
|
addEventListener,
|
|
dispatchEvent,
|
|
removeEventListener
|
|
} = EventTarget.prototype;
|
|
let n = 1;
|
|
const event = new Event("hello");
|
|
const listener = (): void => {
|
|
n = 2;
|
|
};
|
|
|
|
addEventListener("hello", listener);
|
|
window.dispatchEvent(event);
|
|
assertEquals(n, 2);
|
|
n = 1;
|
|
removeEventListener("hello", listener);
|
|
window.dispatchEvent(event);
|
|
assertEquals(n, 1);
|
|
|
|
window.addEventListener("hello", listener);
|
|
dispatchEvent(event);
|
|
assertEquals(n, 2);
|
|
n = 1;
|
|
window.removeEventListener("hello", listener);
|
|
dispatchEvent(event);
|
|
assertEquals(n, 1);
|
|
});
|