2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assertEquals, unitTest } from "./test_util.ts";
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function addEventListenerTest(): void {
|
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
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -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;
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
const listener = (e: Event): void => {
|
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
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function anEventTargetCanBeSubclassed(): void {
|
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,
|
2020-02-19 15:36:18 -05:00
|
|
|
): void {
|
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,
|
2020-02-19 15:36:18 -05:00
|
|
|
): void {
|
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;
|
|
|
|
|
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);
|
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
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function removingNullEventListenerShouldSucceed(): void {
|
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
|
|
|
|
2020-03-04 11:31:14 -05: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;
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
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);
|
|
|
|
});
|
2019-04-03 08:41:05 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function toStringShouldBeWebCompatible(): void {
|
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
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function dispatchEventShouldNotThrowError(): void {
|
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
|
|
|
});
|
2019-05-27 09:20:34 -04:00
|
|
|
const listener = (): void => {};
|
|
|
|
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
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventTargetThisShouldDefaultToWindow(): void {
|
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");
|
|
|
|
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:36:13 -05:00
|
|
|
|
2020-03-05 08:55:11 -05:00
|
|
|
unitTest(function eventTargetShouldAcceptEventListenerObject(): void {
|
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 = {
|
|
|
|
handleEvent(e: Event): void {
|
|
|
|
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-03-05 08:55:11 -05:00
|
|
|
unitTest(function eventTargetShouldAcceptAsyncFunction(): void {
|
2020-03-05 08:36:13 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
const listener = (e: Event): void => {
|
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);
|
|
|
|
});
|
|
|
|
|
2020-03-05 08:55:11 -05:00
|
|
|
unitTest(
|
2020-03-05 08:36:13 -05:00
|
|
|
function eventTargetShouldAcceptAsyncFunctionForEventListenerObject(): void {
|
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
|
|
|
const listener = {
|
2020-03-20 09:38:34 -04:00
|
|
|
handleEvent(e: Event): void {
|
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
|
|
|
);
|
2020-11-02 12:42:22 -05:00
|
|
|
unitTest(function eventTargetDispatchShouldSetTargetNoListener(): void {
|
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo");
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(event.target, target);
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest(function eventTargetDispatchShouldSetTargetInListener(): void {
|
|
|
|
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);
|
|
|
|
});
|