1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/cli/js/tests/event_target_test.ts

232 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assertEquals } from "./test_util.ts";
2019-01-05 10:02:44 -05:00
unitTest(function addEventListenerTest(): void {
2019-01-05 10:02:44 -05:00
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);
2019-01-05 10:02:44 -05:00
});
unitTest(function constructedEventTargetCanBeUsedAsExpected(): void {
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): void => {
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
});
unitTest(function anEventTargetCanBeSubclassed(): void {
2019-01-05 10:02:44 -05:00
class NicerEventTarget extends EventTarget {
on(
type: string,
callback: ((e: Event) => void) | null,
options?: __domTypes.AddEventListenerOptions
): void {
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?: __domTypes.EventListenerOptions
): void {
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;
2019-05-27 09:20:34 -04:00
const listener = (): void => {
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
});
unitTest(function removingNullEventListenerShouldSucceed(): void {
2019-01-05 10:02:44 -05:00
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);
2019-01-05 10:02:44 -05:00
});
2019-03-30 08:18:19 -04:00
unitTest(function constructedEventTargetUseObjectPrototype(): void {
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): void => {
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);
});
unitTest(function toStringShouldBeWebCompatible(): void {
const target = new EventTarget();
assertEquals(target.toString(), "[object EventTarget]");
});
unitTest(function dispatchEventShouldNotThrowError(): void {
let hasThrown = false;
try {
const target = new EventTarget();
const event = new Event("hasOwnProperty", {
bubbles: true,
cancelable: false
});
2019-05-27 09:20:34 -04:00
const listener = (): void => {};
target.addEventListener("hasOwnProperty", listener);
target.dispatchEvent(event);
} catch {
hasThrown = true;
}
assertEquals(hasThrown, false);
});
unitTest(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);
});
2020-03-05 08:55:11 -05:00
unitTest(function eventTargetShouldAcceptEventListenerObject(): void {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = {
handleEvent(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);
});
2020-03-05 08:55:11 -05:00
unitTest(function eventTargetShouldAcceptAsyncFunction(): void {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = async (e: Event): Promise<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);
});
2020-03-05 08:55:11 -05:00
unitTest(
function eventTargetShouldAcceptAsyncFunctionForEventListenerObject(): void {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
const listener = {
async handleEvent(e: Event): Promise<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);
}
);