Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.logic.browser.test.tsx

33 lines1.5 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { render } from "../../test/render";
4import { useSnoozeWakeClock } from "./Sidebar.logic";
5​
6const MOUNTED_AT_MS = Date.parse("2026-10-24T08:00:00.000Z");
7​
8function WakeClock(props: { readonly threads: ReadonlyArray<{ readonly snoozedUntil: string }> }) {
9 return <output>{useSnoozeWakeClock(props.threads)}</output>;
11​
12describe("useSnoozeWakeClock", () => {
13 it("settles in one step when many distinct wakes passed during a sleep", async () => {
14 using now = vi.spyOn(Date, "now").mockReturnValue(MOUNTED_AT_MS);
15 // More distinct wakes than React's 25 render-phase re-render limit.
16 const threads = Array.from({ length: 40 }, (_, index) => ({
17 snoozedUntil: new Date(MOUNTED_AT_MS + 1 + index * 60_000).toISOString(),
18 }));
19 await using screen = await render(<WakeClock threads={threads} />, {
20 // A bare hook host; nothing here links or navigates.
21 router: false,
22 });
23 await expect.element(screen.getByRole("status")).toHaveTextContent(String(MOUNTED_AT_MS));
24​
25 // The machine resumes after every wake has passed; the first wake's timer fires late.
26 const resumedAtMs = MOUNTED_AT_MS + 24 * 60 * 60_000;
27 now.mockReturnValue(resumedAtMs);
28​
29 // The clock lands on the last elapsed wake, so every woken thread has left the shelf.
30 const lastWakeMs = MOUNTED_AT_MS + 1 + 39 * 60_000;
31 await expect.element(screen.getByRole("status")).toHaveTextContent(String(lastWakeMs));
32 });
33});