summaryrefslogtreecommitdiff
path: root/op_crates/web/event_test.js
blob: 8107f3bca8149a93a98820f611979dd7840222e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
function assert(cond) {
  if (!cond) {
    throw Error("assert");
  }
}

function eventInitializedWithType() {
  const type = "click";
  const event = new Event(type);

  assert(event.isTrusted === false);
  assert(event.target === null);
  assert(event.currentTarget === null);
  assert(event.type === "click");
  assert(event.bubbles === false);
  assert(event.cancelable === false);
}

function eventInitializedWithTypeAndDict() {
  const init = "submit";
  const eventInit = { bubbles: true, cancelable: true };
  const event = new Event(init, eventInit);

  assert(event.isTrusted === false);
  assert(event.target === null);
  assert(event.currentTarget === null);
  assert(event.type === "submit");
  assert(event.bubbles === true);
  assert(event.cancelable === true);
}

function eventComposedPathSuccess() {
  const type = "click";
  const event = new Event(type);
  const composedPath = event.composedPath();

  assert(composedPath.length === 0);
}

function eventStopPropagationSuccess() {
  const type = "click";
  const event = new Event(type);

  assert(event.cancelBubble === false);
  event.stopPropagation();
  assert(event.cancelBubble === true);
}

function eventStopImmediatePropagationSuccess() {
  const type = "click";
  const event = new Event(type);

  assert(event.cancelBubble === false);
  event.stopImmediatePropagation();
  assert(event.cancelBubble === true);
}

function eventPreventDefaultSuccess() {
  const type = "click";
  const event = new Event(type);

  assert(event.defaultPrevented === false);
  event.preventDefault();
  assert(event.defaultPrevented === false);

  const eventInit = { bubbles: true, cancelable: true };
  const cancelableEvent = new Event(type, eventInit);
  assert(cancelableEvent.defaultPrevented === false);
  cancelableEvent.preventDefault();
  assert(cancelableEvent.defaultPrevented === true);
}

function eventInitializedWithNonStringType() {
  const type = undefined;
  const event = new Event(type);

  assert(event.isTrusted === false);
  assert(event.target === null);
  assert(event.currentTarget === null);
  assert(event.type === "undefined");
  assert(event.bubbles === false);
  assert(event.cancelable === false);
}

// ref https://github.com/web-platform-tests/wpt/blob/master/dom/events/Event-isTrusted.any.js
function eventIsTrusted() {
  const desc1 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
  assert(desc1);
  assert(typeof desc1.get === "function");

  const desc2 = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
  assert(desc2);
  assert(typeof desc2.get === "function");

  assert(desc1.get === desc2.get);
}

function eventIsTrustedGetterName() {
  const { get } = Object.getOwnPropertyDescriptor(new Event("x"), "isTrusted");
  assert(get.name === "get isTrusted");
  try {
    Reflect.construct(get);
    throw new Error("Should not have reached here");
  } catch (e) {
    assert(e.message.includes("not a constructor"));
  }
}
function main() {
  eventInitializedWithType();
  eventInitializedWithTypeAndDict();
  eventComposedPathSuccess();
  eventStopPropagationSuccess();
  eventStopImmediatePropagationSuccess();
  eventPreventDefaultSuccess();
  eventInitializedWithNonStringType();
  eventIsTrusted();
  eventIsTrustedGetterName();
}

main();