Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions js/timer/Timer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import { cleanup, render } from "@self/tootils/render";

import Timer from "./Index.svelte";

describe("Timer", () => {
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});

test("registers an interval and dispatches tick when active and visible", async () => {
Object.defineProperty(document, "visibilityState", {
configurable: true,
value: "visible",
});

let intervalCallback: (() => void) | undefined;
const setIntervalSpy = vi
.spyOn(globalThis, "setInterval")
.mockImplementation(((fn: TimerHandler) => {
intervalCallback = fn as () => void;
return 1 as unknown as NodeJS.Timeout;
}) as typeof setInterval);

const { listen } = await render(Timer, {
value: 0.25,
active: true,
});
const tick = listen("tick");
intervalCallback?.();

expect(setIntervalSpy).toHaveBeenCalledTimes(1);
expect(setIntervalSpy.mock.calls[0]?.[1]).toBe(250);
expect(tick).toHaveBeenCalledTimes(1);
});

test("does not register an interval when inactive", async () => {
const setIntervalSpy = vi.spyOn(globalThis, "setInterval");

await render(Timer, {
value: 0.25,
active: false,
});

expect(setIntervalSpy).not.toHaveBeenCalled();
});

test("suppresses tick dispatches while the document is hidden", async () => {
Object.defineProperty(document, "visibilityState", {
configurable: true,
value: "hidden",
});

let intervalCallback: (() => void) | undefined;
const setIntervalSpy = vi
.spyOn(globalThis, "setInterval")
.mockImplementation(((fn: TimerHandler) => {
intervalCallback = fn as () => void;
return 1 as unknown as NodeJS.Timeout;
}) as typeof setInterval);

const { listen } = await render(Timer, {
value: 0.25,
active: true,
});
const tick = listen("tick");
intervalCallback?.();

expect(setIntervalSpy).toHaveBeenCalledTimes(1);
expect(tick).not.toHaveBeenCalled();
});
});
Loading