Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { afterEach, describe, expect, it, vi } from "vitest";import { startImportPoll } from "$lib/components/repo/importPoll";import type { ImportState } from "$lib/api/repo";
const importing: ImportState = { content: "fetching", source: "GitHub" };
describe("startImportPoll", () => { afterEach(() => { vi.useRealTimers(); });
it("reports the terminal state and stops once the import is gone", async () => { vi.useFakeTimers(); const fetchState = vi.fn().mockResolvedValueOnce(importing).mockResolvedValue(null); const onState = vi.fn(); const onSettled = vi.fn(); const stop = startImportPoll({ fetchState, onState, onSettled, minDelayMs: 100, maxDelayMs: 200 });
await vi.advanceTimersByTimeAsync(100); expect(onState).toHaveBeenLastCalledWith(importing);
await vi.advanceTimersByTimeAsync(200); expect(onState).toHaveBeenLastCalledWith(null);
const calls = fetchState.mock.calls.length; await vi.advanceTimersByTimeAsync(10_000); expect(fetchState).toHaveBeenCalledTimes(calls); stop(); });
it("stops after a failed import", async () => { vi.useFakeTimers(); const failed: ImportState = { content: "failed", reason: "clone timed out" }; const fetchState = vi.fn().mockResolvedValue(failed); const onSettled = vi.fn(); const stop = startImportPoll({ fetchState, onState: vi.fn(), onSettled, minDelayMs: 100 });
await vi.advanceTimersByTimeAsync(100); expect(onSettled).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(10_000); expect(fetchState).toHaveBeenCalledTimes(1); stop(); });
it("stops polling after cancellation", async () => { vi.useFakeTimers(); const fetchState = vi.fn().mockResolvedValue(importing); const stop = startImportPoll({ fetchState, onState: vi.fn(), minDelayMs: 100 });
await vi.advanceTimersByTimeAsync(100); stop(); await vi.advanceTimersByTimeAsync(10_000); expect(fetchState).toHaveBeenCalledTimes(1); });});