// @vitest-environment jsdom // // useAccountRehydrate re-reads per-account state when the tab's account is pinned after mount (first // load). useActiveAccountId is mocked with a plain variable so the transition can be driven by hand. import { describe, it, expect, beforeEach, vi } from "vitest"; import { renderHook, act } from "@testing-library/react"; let account: number | null = null; vi.mock("./activeAccount", () => ({ useActiveAccountId: () => account, })); import { useAccountRehydrate } from "./useAccountRehydrate"; describe("useAccountRehydrate", () => { beforeEach(() => { account = null; }); it("runs rehydrate when the account transitions from null to a value", () => { const rehydrate = vi.fn(); const { rerender } = renderHook(() => useAccountRehydrate(rehydrate)); expect(rehydrate).not.toHaveBeenCalled(); // mount with no account: no transition account = 1; act(() => rerender()); expect(rehydrate).toHaveBeenCalledTimes(1); }); it("does not run when the account id is already known and stable", () => { account = 1; const rehydrate = vi.fn(); const { rerender } = renderHook(() => useAccountRehydrate(rehydrate)); act(() => rerender()); expect(rehydrate).not.toHaveBeenCalled(); }); it("skips rehydrate when marked dirty before the account pins (no clobber of a user edit)", () => { const rehydrate = vi.fn(); const { result, rerender } = renderHook(() => useAccountRehydrate(rehydrate)); act(() => result.current()); // markDirty — the owning setter ran pre-pin account = 1; act(() => rerender()); expect(rehydrate).not.toHaveBeenCalled(); }); });