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 { assert, assertEquals, unitTest } from "./test_util.ts";
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventInitializedWithType(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const type = "click";
|
|
|
|
const event = new Event(type);
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.isTrusted, false);
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
assertEquals(event.currentTarget, null);
|
|
|
|
assertEquals(event.type, "click");
|
|
|
|
assertEquals(event.bubbles, false);
|
|
|
|
assertEquals(event.cancelable, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventInitializedWithTypeAndDict(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const init = "submit";
|
2019-09-17 12:17:12 -04:00
|
|
|
const eventInit = { bubbles: true, cancelable: true } as EventInit;
|
|
|
|
const event = new Event(init, eventInit);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.isTrusted, false);
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
assertEquals(event.currentTarget, null);
|
|
|
|
assertEquals(event.type, "submit");
|
|
|
|
assertEquals(event.bubbles, true);
|
|
|
|
assertEquals(event.cancelable, true);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventComposedPathSuccess(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const type = "click";
|
|
|
|
const event = new Event(type);
|
|
|
|
const composedPath = event.composedPath();
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(composedPath, []);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventStopPropagationSuccess(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const type = "click";
|
|
|
|
const event = new Event(type);
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.cancelBubble, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
event.stopPropagation();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.cancelBubble, true);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventStopImmediatePropagationSuccess(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const type = "click";
|
|
|
|
const event = new Event(type);
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.cancelBubble, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
event.stopImmediatePropagation();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.cancelBubble, true);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventPreventDefaultSuccess(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const type = "click";
|
|
|
|
const event = new Event(type);
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.defaultPrevented, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
event.preventDefault();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(event.defaultPrevented, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2019-09-17 12:17:12 -04:00
|
|
|
const eventInit = { bubbles: true, cancelable: true } as EventInit;
|
|
|
|
const cancelableEvent = new Event(type, eventInit);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(cancelableEvent.defaultPrevented, false);
|
2019-01-05 10:02:44 -05:00
|
|
|
cancelableEvent.preventDefault();
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(cancelableEvent.defaultPrevented, true);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
2019-03-26 07:42:26 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventInitializedWithNonStringType(): void {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-02-19 15:36:18 -05:00
|
|
|
const type: any = undefined;
|
2019-03-26 07:42:26 -04:00
|
|
|
const event = new Event(type);
|
|
|
|
|
|
|
|
assertEquals(event.isTrusted, false);
|
|
|
|
assertEquals(event.target, null);
|
|
|
|
assertEquals(event.currentTarget, null);
|
|
|
|
assertEquals(event.type, "undefined");
|
|
|
|
assertEquals(event.bubbles, false);
|
|
|
|
assertEquals(event.cancelable, false);
|
|
|
|
});
|
2019-06-20 08:21:43 -04:00
|
|
|
|
|
|
|
// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(function eventIsTrusted(): void {
|
2019-06-20 08:21:43 -04:00
|
|
|
const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(desc1);
|
2019-06-20 08:21:43 -04:00
|
|
|
assertEquals(typeof desc1.get, "function");
|
|
|
|
|
|
|
|
const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(desc2);
|
|
|
|
assertEquals(typeof desc2!.get, "function");
|
2019-06-20 08:21:43 -04:00
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
assertEquals(desc1!.get, desc2!.get);
|
2019-06-20 08:21:43 -04:00
|
|
|
});
|
2020-10-26 18:22:03 -04:00
|
|
|
|
|
|
|
unitTest(function eventInspectOutput(): void {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-10-26 18:22:03 -04:00
|
|
|
const cases: Array<[any, (event: any) => string]> = [
|
|
|
|
[
|
|
|
|
new Event("test"),
|
|
|
|
(event: Event) =>
|
2021-02-13 09:58:12 -05:00
|
|
|
`Event {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "test"\n}`,
|
2020-10-26 18:22:03 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new ErrorEvent("error"),
|
|
|
|
(event: Event) =>
|
2021-02-13 09:58:12 -05:00
|
|
|
`ErrorEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "error",\n message: "",\n filename: "",\n lineno: 0,\n colno: 0,\n error: null\n}`,
|
2020-10-26 18:22:03 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new CloseEvent("close"),
|
|
|
|
(event: Event) =>
|
2021-02-13 09:58:12 -05:00
|
|
|
`CloseEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "close",\n wasClean: false,\n code: 0,\n reason: ""\n}`,
|
2020-10-26 18:22:03 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new CustomEvent("custom"),
|
|
|
|
(event: Event) =>
|
2021-02-13 09:58:12 -05:00
|
|
|
`CustomEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "custom",\n detail: undefined\n}`,
|
2020-10-26 18:22:03 -04:00
|
|
|
],
|
|
|
|
[
|
|
|
|
new ProgressEvent("progress"),
|
|
|
|
(event: Event) =>
|
2021-02-13 09:58:12 -05:00
|
|
|
`ProgressEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n srcElement: null,\n target: null,\n returnValue: true,\n timeStamp: ${event.timeStamp},\n type: "progress",\n lengthComputable: false,\n loaded: 0,\n total: 0\n}`,
|
2020-10-26 18:22:03 -04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const [event, outputProvider] of cases) {
|
2021-02-09 10:31:46 -05:00
|
|
|
assertEquals(Deno.inspect(event), outputProvider(event));
|
2020-10-26 18:22:03 -04:00
|
|
|
}
|
|
|
|
});
|