Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
30 lines
1.3 KiB
TypeScript
30 lines
1.3 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ useAdminAuth: vi.fn(), listAdminCompanies: vi.fn() }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("@/app/lib/services/adminCompanies", () => ({ listAdminCompanies: mocks.listAdminCompanies }));
|
|
|
|
import { CompanyScopeProvider, useCompanyScope } from "./CompanyScope";
|
|
|
|
function Probe() {
|
|
const s = useCompanyScope();
|
|
return <p data-testid="probe">{String(s.companyId)}</p>;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
sessionStorage.clear();
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ isSuper: true });
|
|
mocks.listAdminCompanies.mockReset().mockResolvedValue([{ id: 1, name: "Ins Co", code: "INS", status: "A" }]);
|
|
});
|
|
|
|
describe("stored company choice", () => {
|
|
it("falls back to All companies when the stored id is not in the companies list", async () => {
|
|
sessionStorage.setItem("company-scope", "99");
|
|
render(<CompanyScopeProvider><Probe /></CompanyScopeProvider>);
|
|
await waitFor(() => expect(mocks.listAdminCompanies).toHaveBeenCalled());
|
|
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("undefined"));
|
|
expect(sessionStorage.getItem("company-scope")).toBeNull();
|
|
});
|
|
});
|