1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 17:19:06 -05:00
denoland-deno/cli/tests/unit/event_test.ts

95 lines
3 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, assert } from "./test_util.ts";
2019-01-05 10:02:44 -05:00
unitTest(function eventInitializedWithType(): void {
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(): void {
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(): void {
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(): void {
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(): void {
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(): void {
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(): void {
// eslint-disable-next-line @typescript-eslint/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(): void {
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);
});