Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 delayedStatusDeadline,
4 shownDelayedStatus,
5 stepDelayedStatus,
6 type DelayedStatusState,
7} from "./delayedStatus.ts";
8
9function step(
10 state: DelayedStatusState<string> | null,
11 value: string | null,
12 now: number,
13 key = "thread-1",
14) {
15 const deadline = state === null ? null : delayedStatusDeadline(state);
16 return stepDelayedStatus(
17 state,
18 { key, value, deadlinePassed: deadline !== null && now >= deadline },
19 () => now,
20 );
21}
22
23describe("stepDelayedStatus", () => {
24 it("shows a status only after it stays set for the show delay", () => {
25 const pending = step(null, "syncing", 0);
26 expect(delayedStatusDeadline(pending)).toBe(400);
27 expect(step(pending, "syncing", 399)).toBe(pending);
28 const shown = step(pending, "syncing", 400);
29 expect(shownDelayedStatus(shown, "thread-1")).toBe("syncing");
30 });
31
32 it("never shows a status that clears before the show delay", () => {
33 const pending = step(null, "syncing", 0);
34 const cleared = step(pending, null, 200);
35 expect(cleared.phase).toBe("idle");
36 expect(delayedStatusDeadline(step(cleared, "syncing", 300))).toBe(700);
37 });
38
39 it("shows the latest value when the show delay passes", () => {
40 const pending = step(null, "connecting", 0);
41 const changed = step(pending, "syncing", 200);
42 expect(changed).toBe(pending);
43 expect(shownDelayedStatus(step(changed, "syncing", 400), "thread-1")).toBe("syncing");
44 });
45
46 it("holds a shown status for the minimum visible time after it clears", () => {
47 const shown = step(step(null, "syncing", 0), "syncing", 400);
48 expect(delayedStatusDeadline(shown)).toBe(800);
49 expect(step(shown, null, 500)).toBe(shown);
50 expect(step(shown, null, 800).phase).toBe("idle");
51 });
52
53 it("hides immediately once the minimum visible time has passed", () => {
54 const shown = step(step(null, "syncing", 0), "syncing", 400);
55 expect(step(shown, "syncing", 900)).toBe(shown);
56 expect(step(shown, null, 1000).phase).toBe("idle");
57 });
58
59 it("restarts the minimum visible time when the shown value changes", () => {
60 const shown = step(step(null, "syncing", 0), "syncing", 400);
61 const replaced = step(shown, "replaying", 600);
62 expect(shownDelayedStatus(replaced, "thread-1")).toBe("replaying");
63 expect(delayedStatusDeadline(replaced)).toBe(1000);
64 });
65
66 it("hides the status and restarts the delay when the key changes", () => {
67 const shown = step(step(null, "syncing", 0), "syncing", 400);
68 expect(shownDelayedStatus(shown, "thread-2")).toBeNull();
69 const other = step(shown, "syncing", 500, "thread-2");
70 expect(shownDelayedStatus(other, "thread-2")).toBeNull();
71 expect(delayedStatusDeadline(other)).toBe(900);
72 });
73});