30 lines
1.3 KiB
TypeScript
30 lines
1.3 KiB
TypeScript
// src/hooks/usePairPoll.test.tsx
|
|
import { renderHook, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { usePairPoll } from "./usePairPoll";
|
|
import * as driver from "@/api/driver";
|
|
import { useAuth } from "@/store/auth";
|
|
|
|
beforeEach(() => { useAuth.getState().clear(); vi.restoreAllMocks(); });
|
|
|
|
describe("usePairPoll", () => {
|
|
it("sets session on authenticated", async () => {
|
|
vi.spyOn(driver, "tgPoll").mockResolvedValue({ status: "authenticated", token: "T", driver_id: 7 } as any);
|
|
const { result } = renderHook(() => usePairPoll("SESS"));
|
|
await waitFor(() => expect(result.current.status).toBe("authenticated"));
|
|
expect(useAuth.getState().token).toBe("T");
|
|
expect(useAuth.getState().driverId).toBe(7);
|
|
});
|
|
it("stays pending then expired", async () => {
|
|
vi.spyOn(driver, "tgPoll").mockResolvedValue({ status: "expired" } as any);
|
|
const { result } = renderHook(() => usePairPoll("SESS"));
|
|
await waitFor(() => expect(result.current.status).toBe("expired"));
|
|
});
|
|
it("does nothing when session is null", () => {
|
|
const spy = vi.spyOn(driver, "tgPoll");
|
|
const { result } = renderHook(() => usePairPoll(null));
|
|
expect(result.current.status).toBe("pending");
|
|
expect(spy).not.toHaveBeenCalled();
|
|
});
|
|
});
|