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