2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assert, assertEquals } from "./test_util.ts";
|
2020-04-15 10:10:49 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function basicAbortController() {
|
2020-04-15 10:10:49 -04:00
|
|
|
const controller = new AbortController();
|
|
|
|
assert(controller);
|
|
|
|
const { signal } = controller;
|
|
|
|
assert(signal);
|
|
|
|
assertEquals(signal.aborted, false);
|
|
|
|
controller.abort();
|
|
|
|
assertEquals(signal.aborted, true);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function signalCallsOnabort() {
|
2020-04-15 10:10:49 -04:00
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
let called = false;
|
2021-08-05 07:08:58 -04:00
|
|
|
signal.onabort = (evt) => {
|
2020-04-15 10:10:49 -04:00
|
|
|
assert(evt);
|
|
|
|
assertEquals(evt.type, "abort");
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
controller.abort();
|
|
|
|
assert(called);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function signalEventListener() {
|
2020-04-15 10:10:49 -04:00
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
let called = false;
|
|
|
|
signal.addEventListener("abort", function (ev) {
|
|
|
|
assert(this === signal);
|
|
|
|
assertEquals(ev.type, "abort");
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
controller.abort();
|
|
|
|
assert(called);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function onlyAbortsOnce() {
|
2020-04-15 10:10:49 -04:00
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
let called = 0;
|
|
|
|
signal.addEventListener("abort", () => called++);
|
2021-08-05 07:08:58 -04:00
|
|
|
signal.onabort = () => {
|
2020-04-15 10:10:49 -04:00
|
|
|
called++;
|
|
|
|
};
|
|
|
|
controller.abort();
|
|
|
|
assertEquals(called, 2);
|
|
|
|
controller.abort();
|
|
|
|
assertEquals(called, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function controllerHasProperToString() {
|
2020-04-15 10:10:49 -04:00
|
|
|
const actual = Object.prototype.toString.call(new AbortController());
|
|
|
|
assertEquals(actual, "[object AbortController]");
|
|
|
|
});
|
2021-11-11 04:28:06 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function abortReason() {
|
2021-11-11 04:28:06 -05:00
|
|
|
const signal = AbortSignal.abort("hey!");
|
|
|
|
assertEquals(signal.aborted, true);
|
|
|
|
assertEquals(signal.reason, "hey!");
|
|
|
|
});
|