2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, assertEquals } from "./test_util.ts";
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function addEventListenerTest(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const document = new EventTarget();
|
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function constructedEventTargetCanBeUsedAsExpected(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("foo", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function listener(e): void {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(e, event);
|
2019-01-05 10:02:44 -05:00
|
|
|
++callCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.addEventListener("foo", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 1);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 2);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.removeEventListener("foo", listener);
|
|
|
|
target.dispatchEvent(event);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 2);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function anEventTargetCanBeSubclassed(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
class NicerEventTarget extends EventTarget {
|
2019-03-09 12:30:38 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
off(type, callback?, options?): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
this.removeEventListener(type, callback, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const target = new NicerEventTarget();
|
2019-03-09 12:30:38 -05:00
|
|
|
new Event("foo", { bubbles: true, cancelable: false });
|
2019-01-05 10:02:44 -05:00
|
|
|
let callCount = 0;
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function listener(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
++callCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.on("foo", listener);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 0);
|
2019-01-05 10:02:44 -05:00
|
|
|
|
|
|
|
target.off("foo", listener);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(callCount, 0);
|
2019-01-05 10:02:44 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function removingNullEventListenerShouldSucceed(): void {
|
2019-01-05 10:02:44 -05:00
|
|
|
const document = new EventTarget();
|
2019-03-06 20:48:46 -05:00
|
|
|
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
|
|
|
});
|
2019-03-30 08:18:19 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function constructedEventTargetUseObjectPrototype(): void {
|
2019-03-30 08:18:19 -04:00
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("toString", { bubbles: true, cancelable: false });
|
|
|
|
let callCount = 0;
|
|
|
|
|
|
|
|
function listener(e): void {
|
|
|
|
assertEquals(e, event);
|
|
|
|
++callCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
target.addEventListener("toString", listener);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 1);
|
|
|
|
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
|
|
|
|
target.removeEventListener("toString", listener);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
assertEquals(callCount, 2);
|
|
|
|
});
|
2019-04-03 08:41:05 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function toStringShouldBeWebCompatibility(): void {
|
2019-04-03 08:41:05 -04:00
|
|
|
const target = new EventTarget();
|
|
|
|
assertEquals(target.toString(), "[object EventTarget]");
|
|
|
|
});
|
2019-04-18 21:56:33 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function dispatchEventShouldNotThrowError(): void {
|
2019-04-18 21:56:33 -04:00
|
|
|
let hasThrown = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const target = new EventTarget();
|
|
|
|
const event = new Event("hasOwnProperty", {
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: false
|
|
|
|
});
|
2019-04-21 16:40:10 -04:00
|
|
|
target.addEventListener("hasOwnProperty", (): void => {});
|
2019-04-18 21:56:33 -04:00
|
|
|
target.dispatchEvent(event);
|
|
|
|
} catch {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(hasThrown, false);
|
|
|
|
});
|