• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2
3test(() => {
4  const target = new EventTarget();
5  const event = new Event("foo", { bubbles: true, cancelable: false });
6  let callCount = 0;
7
8  function listener(e) {
9    assert_equals(e, event);
10    ++callCount;
11  }
12
13  target.addEventListener("foo", listener);
14
15  target.dispatchEvent(event);
16  assert_equals(callCount, 1);
17
18  target.dispatchEvent(event);
19  assert_equals(callCount, 2);
20
21  target.removeEventListener("foo", listener);
22  target.dispatchEvent(event);
23  assert_equals(callCount, 2);
24}, "A constructed EventTarget can be used as expected");
25
26test(() => {
27  class NicerEventTarget extends EventTarget {
28    on(...args) {
29      this.addEventListener(...args);
30    }
31
32    off(...args) {
33      this.removeEventListener(...args);
34    }
35
36    dispatch(type, detail) {
37      this.dispatchEvent(new CustomEvent(type, { detail }));
38    }
39  }
40
41  const target = new NicerEventTarget();
42  const event = new Event("foo", { bubbles: true, cancelable: false });
43  const detail = "some data";
44  let callCount = 0;
45
46  function listener(e) {
47    assert_equals(e.detail, detail);
48    ++callCount;
49  }
50
51  target.on("foo", listener);
52
53  target.dispatch("foo", detail);
54  assert_equals(callCount, 1);
55
56  target.dispatch("foo", detail);
57  assert_equals(callCount, 2);
58
59  target.off("foo", listener);
60  target.dispatch("foo", detail);
61  assert_equals(callCount, 2);
62}, "EventTarget can be subclassed");
63