summaryrefslogtreecommitdiff
path: root/cli/js/tests/event_target_test.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-03-10 01:06:47 +0100
committerGitHub <noreply@github.com>2020-03-10 01:06:47 +0100
commit68119e1d7ed23421ccdcba20532ebe9ae3df9f18 (patch)
tree455dd170024112e3388adc27ebebb119b0ecda38 /cli/js/tests/event_target_test.ts
parentdad8036766dca3417b79858b9a04d90447f88605 (diff)
reorg: move js runtime tests to cli/js/tests/ (#4250)
All Deno runtime test files were moved to cli/js/tests/ directory. It makes a clear distinction that cli/js/tests/ contains code that is run under Deno runtime as opposed to code in cli/js/ which is used to create bundle and snapshot with "deno_typescript".
Diffstat (limited to 'cli/js/tests/event_target_test.ts')
-rw-r--r--cli/js/tests/event_target_test.ts231
1 files changed, 231 insertions, 0 deletions
diff --git a/cli/js/tests/event_target_test.ts b/cli/js/tests/event_target_test.ts
new file mode 100644
index 000000000..09d47f704
--- /dev/null
+++ b/cli/js/tests/event_target_test.ts
@@ -0,0 +1,231 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { unitTest, assertEquals } from "./test_util.ts";
+
+unitTest(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);
+});
+
+unitTest(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);
+});
+
+unitTest(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);
+});
+
+unitTest(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);
+});
+
+unitTest(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);
+});
+
+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
+ });
+ 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);
+});
+
+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);
+});
+
+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);
+});
+
+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);
+ }
+);