1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/js/event_target_test.ts

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assertEquals } from "./test_util.ts";
2019-01-05 10:02:44 -05:00
test(function addEventListenerTest() {
const document = new EventTarget();
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
});
test(function constructedEventTargetCanBeUsedAsExpected() {
const target = new EventTarget();
const event = new Event("foo", { bubbles: true, cancelable: false });
let callCount = 0;
function listener(e): void {
assertEquals(e, event);
2019-01-05 10:02:44 -05:00
++callCount;
}
target.addEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 1);
2019-01-05 10:02:44 -05:00
target.dispatchEvent(event);
assertEquals(callCount, 2);
2019-01-05 10:02:44 -05:00
target.removeEventListener("foo", listener);
target.dispatchEvent(event);
assertEquals(callCount, 2);
2019-01-05 10:02:44 -05:00
});
test(function anEventTargetCanBeSubclassed() {
class NicerEventTarget extends EventTarget {
on(type, callback?, options?): void {
2019-01-23 07:20:53 -05:00
this.addEventListener(type, callback, options);
2019-01-05 10:02:44 -05:00
}
off(type, callback?, options?): void {
2019-01-05 10:02:44 -05:00
this.removeEventListener(type, callback, options);
}
}
const target = new NicerEventTarget();
new Event("foo", { bubbles: true, cancelable: false });
2019-01-05 10:02:44 -05:00
let callCount = 0;
function listener(): void {
2019-01-05 10:02:44 -05:00
++callCount;
}
target.on("foo", listener);
assertEquals(callCount, 0);
2019-01-05 10:02:44 -05:00
target.off("foo", listener);
assertEquals(callCount, 0);
2019-01-05 10:02:44 -05:00
});
test(function removingNullEventListenerShouldSucceed() {
const document = new EventTarget();
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
});