A toast left after four seconds (eight for an error) whether it had been read or not. The countdown stops while the pointer rests on it and runs on for at least 1.5 seconds after the pointer leaves.
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
// The toast list. Run by `npm test`, which Node runs with its built-in test
|
|
// runner and type stripping, so the `.ts` module is imported as it is.
|
|
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import {
|
|
currentToasts,
|
|
dismissToast,
|
|
showToast,
|
|
resumeDelay,
|
|
subscribeToasts,
|
|
timeLeft,
|
|
toastDuration,
|
|
} from "../src/ui/toast/toasts.ts";
|
|
|
|
test("an error stays longer than the other tones", () => {
|
|
assert.ok(toastDuration("error") > toastDuration("ok"));
|
|
assert.equal(toastDuration("info"), toastDuration("ok"));
|
|
});
|
|
|
|
test("a toast is listed with its tone and duration, oldest first", () => {
|
|
const a = showToast("first", "ok");
|
|
const b = showToast("second", "error", 100);
|
|
const list = currentToasts().filter((t) => t.id === a || t.id === b);
|
|
assert.deepEqual(
|
|
list.map((t) => [t.message, t.tone, t.duration]),
|
|
[
|
|
["first", "ok", toastDuration("ok")],
|
|
["second", "error", 100],
|
|
],
|
|
);
|
|
dismissToast(a);
|
|
dismissToast(b);
|
|
});
|
|
|
|
test("dismissing removes one toast and tells the subscribers once", () => {
|
|
const a = showToast("keep");
|
|
const b = showToast("drop");
|
|
let calls = 0;
|
|
const stop = subscribeToasts(() => calls++);
|
|
dismissToast(b);
|
|
dismissToast(b);
|
|
assert.equal(calls, 1);
|
|
assert.ok(currentToasts().some((t) => t.id === a));
|
|
assert.ok(!currentToasts().some((t) => t.id === b));
|
|
stop();
|
|
dismissToast(a);
|
|
assert.equal(calls, 1);
|
|
});
|
|
|
|
test("a toast's time left shrinks by how long it was shown, to zero", () => {
|
|
assert.equal(timeLeft(4000, 1000), 3000);
|
|
assert.equal(timeLeft(4000, 4000), 0);
|
|
assert.equal(timeLeft(4000, 9000), 0);
|
|
});
|
|
|
|
test("a toast the pointer leaves keeps a moment, or its time left if longer", () => {
|
|
assert.equal(resumeDelay(200), 1500);
|
|
assert.equal(resumeDelay(0), 1500);
|
|
assert.equal(resumeDelay(3000), 3000);
|
|
});
|