1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00
denoland-deno/cli/tests/unit/event_test.ts

162 lines
5.7 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertStringIncludes,
unitTest,
} from "./test_util.ts";
2019-01-05 10:02:44 -05:00
unitTest(function eventInitializedWithType() {
2019-01-05 10:02:44 -05:00
const type = "click";
const event = new Event(type);
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
});
unitTest(function eventInitializedWithTypeAndDict() {
2019-01-05 10:02:44 -05:00
const init = "submit";
const eventInit = { bubbles: true, cancelable: true } as EventInit;
const event = new Event(init, eventInit);
2019-01-05 10:02:44 -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
});
unitTest(function eventComposedPathSuccess() {
2019-01-05 10:02:44 -05:00
const type = "click";
const event = new Event(type);
const composedPath = event.composedPath();
assertEquals(composedPath, []);
2019-01-05 10:02:44 -05:00
});
unitTest(function eventStopPropagationSuccess() {
2019-01-05 10:02:44 -05:00
const type = "click";
const event = new Event(type);
assertEquals(event.cancelBubble, false);
2019-01-05 10:02:44 -05:00
event.stopPropagation();
assertEquals(event.cancelBubble, true);
2019-01-05 10:02:44 -05:00
});
unitTest(function eventStopImmediatePropagationSuccess() {
2019-01-05 10:02:44 -05:00
const type = "click";
const event = new Event(type);
assertEquals(event.cancelBubble, false);
2019-01-05 10:02:44 -05:00
event.stopImmediatePropagation();
assertEquals(event.cancelBubble, true);
2019-01-05 10:02:44 -05:00
});
unitTest(function eventPreventDefaultSuccess() {
2019-01-05 10:02:44 -05:00
const type = "click";
const event = new Event(type);
assertEquals(event.defaultPrevented, false);
2019-01-05 10:02:44 -05:00
event.preventDefault();
assertEquals(event.defaultPrevented, false);
2019-01-05 10:02:44 -05:00
const eventInit = { bubbles: true, cancelable: true } as EventInit;
const cancelableEvent = new Event(type, eventInit);
assertEquals(cancelableEvent.defaultPrevented, false);
2019-01-05 10:02:44 -05:00
cancelableEvent.preventDefault();
assertEquals(cancelableEvent.defaultPrevented, true);
2019-01-05 10:02:44 -05:00
});
unitTest(function eventInitializedWithNonStringType() {
// deno-lint-ignore no-explicit-any
const type: any = undefined;
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);
});
// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js
unitTest(function eventIsTrusted() {
const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assert(desc1);
assertEquals(typeof desc1.get, "function");
const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
assert(desc2);
assertEquals(typeof desc2!.get, "function");
assertEquals(desc1!.get, desc2!.get);
});
unitTest(function eventInspectOutput() {
// deno-lint-ignore no-explicit-any
const cases: Array<[any, (event: any) => string]> = [
[
new Event("test"),
(event: Event) =>
`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}`,
],
[
new ErrorEvent("error"),
(event: Event) =>
`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}`,
],
[
new CloseEvent("close"),
(event: Event) =>
`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}`,
],
[
new CustomEvent("custom"),
(event: Event) =>
`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}`,
],
[
new ProgressEvent("progress"),
(event: Event) =>
`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}`,
],
];
for (const [event, outputProvider] of cases) {
assertEquals(Deno.inspect(event), outputProvider(event));
}
});
unitTest(function inspectEvent() {
// has a customInspect implementation that previously would throw on a getter
assertEquals(
Deno.inspect(Event.prototype),
`Event {
bubbles: [Getter],
cancelable: [Getter],
composed: [Getter],
currentTarget: [Getter],
defaultPrevented: [Getter],
eventPhase: [Getter],
srcElement: [Getter/Setter],
target: [Getter],
returnValue: [Getter/Setter],
timeStamp: [Getter],
type: [Getter]
}`,
);
// ensure this still works
assertStringIncludes(
Deno.inspect(new Event("test")),
// check a substring because one property is a timestamp
`Event {\n bubbles: false,\n cancelable: false,`,
);
});