1test(t => { 2 const c = new AbortController(), 3 s = c.signal; 4 let state = "begin"; 5 6 assert_false(s.aborted); 7 assert_true("reason" in s, "signal has reason property"); 8 assert_equals(s.reason, undefined, "signal.reason is initially undefined"); 9 10 s.addEventListener("abort", 11 t.step_func(e => { 12 assert_equals(state, "begin"); 13 state = "aborted"; 14 }) 15 ); 16 c.abort(); 17 18 assert_equals(state, "aborted"); 19 assert_true(s.aborted); 20 assert_true(s.reason instanceof DOMException, "signal.reason is DOMException"); 21 assert_equals(s.reason.name, "AbortError", "signal.reason is AbortError"); 22 23 c.abort(); 24}, "AbortController abort() should fire event synchronously"); 25 26test(t => { 27 const controller = new AbortController(); 28 const signal = controller.signal; 29 assert_equals(controller.signal, signal, 30 "value of controller.signal should not have changed"); 31 controller.abort(); 32 assert_equals(controller.signal, signal, 33 "value of controller.signal should still not have changed"); 34}, "controller.signal should always return the same object"); 35 36test(t => { 37 const controller = new AbortController(); 38 const signal = controller.signal; 39 let eventCount = 0; 40 signal.onabort = () => { 41 ++eventCount; 42 }; 43 controller.abort(); 44 assert_true(signal.aborted); 45 assert_equals(eventCount, 1, "event handler should have been called once"); 46 controller.abort(); 47 assert_true(signal.aborted); 48 assert_equals(eventCount, 1, 49 "event handler should not have been called again"); 50}, "controller.abort() should do nothing the second time it is called"); 51 52test(t => { 53 const controller = new AbortController(); 54 controller.abort(); 55 controller.signal.onabort = 56 t.unreached_func("event handler should not be called"); 57}, "event handler should not be called if added after controller.abort()"); 58 59test(t => { 60 const controller = new AbortController(); 61 const signal = controller.signal; 62 signal.onabort = t.step_func(e => { 63 assert_equals(e.type, "abort", "event type should be abort"); 64 assert_equals(e.target, signal, "event target should be signal"); 65 assert_false(e.bubbles, "event should not bubble"); 66 assert_true(e.isTrusted, "event should be trusted"); 67 }); 68 controller.abort(); 69}, "the abort event should have the right properties"); 70 71test(t => { 72 const controller = new AbortController(); 73 const signal = controller.signal; 74 75 assert_true("reason" in signal, "signal has reason property"); 76 assert_equals(signal.reason, undefined, "signal.reason is initially undefined"); 77 78 const reason = Error("hello"); 79 controller.abort(reason); 80 81 assert_true(signal.aborted, "signal.aborted"); 82 assert_equals(signal.reason, reason, "signal.reason"); 83}, "AbortController abort(reason) should set signal.reason"); 84 85test(t => { 86 const controller = new AbortController(); 87 const signal = controller.signal; 88 89 assert_true("reason" in signal, "signal has reason property"); 90 assert_equals(signal.reason, undefined, "signal.reason is initially undefined"); 91 92 controller.abort(); 93 94 assert_true(signal.aborted, "signal.aborted"); 95 assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException"); 96 assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError"); 97}, "aborting AbortController without reason creates an \"AbortError\" DOMException"); 98 99test(t => { 100 const controller = new AbortController(); 101 const signal = controller.signal; 102 103 assert_true("reason" in signal, "signal has reason property"); 104 assert_equals(signal.reason, undefined, "signal.reason is initially undefined"); 105 106 controller.abort(undefined); 107 108 assert_true(signal.aborted, "signal.aborted"); 109 assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException"); 110 assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError"); 111}, "AbortController abort(undefined) creates an \"AbortError\" DOMException"); 112 113test(t => { 114 const controller = new AbortController(); 115 const signal = controller.signal; 116 117 assert_true("reason" in signal, "signal has reason property"); 118 assert_equals(signal.reason, undefined, "signal.reason is initially undefined"); 119 120 controller.abort(null); 121 122 assert_true(signal.aborted, "signal.aborted"); 123 assert_equals(signal.reason, null, "signal.reason"); 124}, "AbortController abort(null) should set signal.reason"); 125 126test(t => { 127 const signal = AbortSignal.abort(); 128 129 assert_true(signal.aborted, "signal.aborted"); 130 assert_true(signal.reason instanceof DOMException, "signal.reason is DOMException"); 131 assert_equals(signal.reason.name, "AbortError", "signal.reason is AbortError"); 132}, "static aborting signal should have right properties"); 133 134test(t => { 135 const reason = Error("hello"); 136 const signal = AbortSignal.abort(reason); 137 138 assert_true(signal.aborted, "signal.aborted"); 139 assert_equals(signal.reason, reason, "signal.reason"); 140}, "static aborting signal with reason should set signal.reason"); 141 142test(t => { 143 const reason = new Error('boom'); 144 const signal = AbortSignal.abort(reason); 145 assert_true(signal.aborted); 146 assert_throws_exactly(reason, () => signal.throwIfAborted()); 147}, "throwIfAborted() should throw abort.reason if signal aborted"); 148 149test(t => { 150 const signal = AbortSignal.abort('hello'); 151 assert_true(signal.aborted); 152 assert_throws_exactly('hello', () => signal.throwIfAborted()); 153}, "throwIfAborted() should throw primitive abort.reason if signal aborted"); 154 155test(t => { 156 const controller = new AbortController(); 157 assert_false(controller.signal.aborted); 158 controller.signal.throwIfAborted(); 159}, "throwIfAborted() should not throw if signal not aborted"); 160 161test(t => { 162 const signal = AbortSignal.abort(); 163 164 assert_true( 165 signal.reason instanceof DOMException, 166 "signal.reason is a DOMException" 167 ); 168 assert_equals( 169 signal.reason, 170 signal.reason, 171 "signal.reason returns the same DOMException" 172 ); 173}, "AbortSignal.reason returns the same DOMException"); 174 175test(t => { 176 const controller = new AbortController(); 177 controller.abort(); 178 179 assert_true( 180 controller.signal.reason instanceof DOMException, 181 "signal.reason is a DOMException" 182 ); 183 assert_equals( 184 controller.signal.reason, 185 controller.signal.reason, 186 "signal.reason returns the same DOMException" 187 ); 188}, "AbortController.signal.reason returns the same DOMException"); 189 190done(); 191