summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/abort/event.any.js
blob: bbbe28b233b551b4d3362cecbd9ea6788e11884b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
test(t => {
  const c = new AbortController(),
        s = c.signal;
  let state = "begin";

  assert_false(s.aborted);
  assert_true("reason" in s, "signal has reason property");
  assert_equals(s.reason, undefined, "signal.reason is initially undefined");

  s.addEventListener("abort",
    t.step_func(e => {
      assert_equals(state, "begin");
      state = "aborted";
    })
  );
  c.abort();

  assert_equals(state, "aborted");
  assert_true(s.aborted);
  assert_true(s.reason instanceof DOMException, "signal.reason is DOMException");
  assert_equals(s.reason.name, "AbortError", "signal.reason is AbortError");

  c.abort();
}, "AbortController abort() should fire event synchronously");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;
  assert_equals(controller.signal, signal,
                "value of controller.signal should not have changed");
  controller.abort();
  assert_equals(controller.signal, signal,
                "value of controller.signal should still not have changed");
}, "controller.signal should always return the same object");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;
  let eventCount = 0;
  signal.onabort = () => {
    ++eventCount;
  };
  controller.abort();
  assert_true(signal.aborted);
  assert_equals(eventCount, 1, "event handler should have been called once");
  controller.abort();
  assert_true(signal.aborted);
  assert_equals(eventCount, 1,
                "event handler should not have been called again");
}, "controller.abort() should do nothing the second time it is called");

test(t => {
  const controller = new AbortController();
  controller.abort();
  controller.signal.onabort =
      t.unreached_func("event handler should not be called");
}, "event handler should not be called if added after controller.abort()");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;
  signal.onabort = t.step_func(e => {
    assert_equals(e.type, "abort", "event type should be abort");
    assert_equals(e.target, signal, "event target should be signal");
    assert_false(e.bubbles, "event should not bubble");
    assert_true(e.isTrusted, "event should be trusted");
  });
  controller.abort();
}, "the abort event should have the right properties");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;

  assert_true("reason" in signal, "signal has reason property");
  assert_equals(signal.reason, undefined, "signal.reason is initially undefined");

  const reason = Error("hello");
  controller.abort(reason);

  assert_true(signal.aborted, "signal.aborted");
  assert_equals(signal.reason, reason, "signal.reason");
}, "AbortController abort(reason) should set signal.reason");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;

  assert_true("reason" in signal, "signal has reason property");
  assert_equals(signal.reason, undefined, "signal.reason is initially undefined");

  controller.abort();

  assert_true(signal.aborted, "signal.aborted");
  assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException");
  assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError");
}, "aborting AbortController without reason creates an \"AbortError\" DOMException");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;

  assert_true("reason" in signal, "signal has reason property");
  assert_equals(signal.reason, undefined, "signal.reason is initially undefined");

  controller.abort(undefined);

  assert_true(signal.aborted, "signal.aborted");
  assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException");
  assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError");
}, "AbortController abort(undefined) creates an \"AbortError\" DOMException");

test(t => {
  const controller = new AbortController();
  const signal = controller.signal;

  assert_true("reason" in signal, "signal has reason property");
  assert_equals(signal.reason, undefined, "signal.reason is initially undefined");

  controller.abort(null);

  assert_true(signal.aborted, "signal.aborted");
  assert_equals(signal.reason, null, "signal.reason");
}, "AbortController abort(null) should set signal.reason");

test(t => {
  const signal = AbortSignal.abort();

  assert_true(signal.aborted, "signal.aborted");
  assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException");
  assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError");
}, "static aborting signal should have right properties");

test(t => {
  const reason = Error("hello");
  const signal = AbortSignal.abort(reason);

  assert_true(signal.aborted, "signal.aborted");
  assert_equals(signal.reason, reason, "signal.reason");
}, "static aborting signal with reason should set signal.reason");

test(t => {
  const signal = AbortSignal.abort();

  assert_true(
    signal.reason instanceof DOMException,
    "signal.reason is a DOMException"
  );
  assert_equals(
    signal.reason,
    signal.reason,
    "signal.reason returns the same DOMException"
  );
}, "AbortSignal.reason returns the same DOMException");

test(t => {
  const controller = new AbortController();
  controller.abort();

  assert_true(
    controller.signal.reason instanceof DOMException,
    "signal.reason is a DOMException"
  );
  assert_equals(
    controller.signal.reason,
    controller.signal.reason,
    "signal.reason returns the same DOMException"
  );
}, "AbortController.signal.reason returns the same DOMException");

test(t => {
  const reason = new Error('boom');
  const signal = AbortSignal.abort(reason);
  assert_true(signal.aborted);
  assert_throws_exactly(reason, () => signal.throwIfAborted());
}, "throwIfAborted() should throw abort.reason if signal aborted");

test(t => {
  const signal = AbortSignal.abort('hello');
  assert_true(signal.aborted);
  assert_throws_exactly('hello', () => signal.throwIfAborted());
}, "throwIfAborted() should throw primitive abort.reason if signal aborted");

test(t => {
  const controller = new AbortController();
  assert_false(controller.signal.aborted);
  controller.signal.throwIfAborted();
}, "throwIfAborted() should not throw if signal not aborted");

done();