Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
72 lines
3.0 KiB
TypeScript
72 lines
3.0 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
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";
|
|
import CompanyPicker from "@/app/components/app/CompanyPicker";
|
|
|
|
const COMPANIES = [
|
|
{ id: 1, name: "Ins Co", code: "INS", status: "A" },
|
|
{ id: 2, name: "CV Co", code: "CV", status: "A" },
|
|
];
|
|
|
|
function Probe() {
|
|
const s = useCompanyScope();
|
|
return <p data-testid="probe">{String(s.companyId)}|{String(s.showCompanyColumn)}|{s.codeOf(2)}</p>;
|
|
}
|
|
const renderScope = () =>
|
|
render(
|
|
<CompanyScopeProvider>
|
|
<CompanyPicker />
|
|
<Probe />
|
|
</CompanyScopeProvider>,
|
|
);
|
|
|
|
beforeEach(() => {
|
|
sessionStorage.clear();
|
|
mocks.listAdminCompanies.mockReset().mockResolvedValue(COMPANIES);
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ isSuper: true });
|
|
});
|
|
|
|
describe("CompanyPicker", () => {
|
|
it("is shown to a super admin with All companies plus each company", async () => {
|
|
renderScope();
|
|
const picker = await screen.findByRole("combobox", { name: "Company" });
|
|
await waitFor(() => expect(picker.querySelectorAll("option")).toHaveLength(3));
|
|
expect([...picker.querySelectorAll("option")].map((o) => o.textContent)).toEqual(["All companies", "INS", "CV"]);
|
|
expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV");
|
|
});
|
|
|
|
it("is not shown to staff, who never load companies or get a companyId", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ isSuper: false });
|
|
sessionStorage.setItem("company-scope", "2");
|
|
renderScope();
|
|
expect(screen.queryByRole("combobox", { name: "Company" })).not.toBeInTheDocument();
|
|
expect(mocks.listAdminCompanies).not.toHaveBeenCalled();
|
|
expect(screen.getByTestId("probe")).toHaveTextContent("undefined|false|");
|
|
});
|
|
|
|
it("feeds the chosen company into the scope and keeps it for the tab; All companies clears it", async () => {
|
|
const user = userEvent.setup();
|
|
renderScope();
|
|
const picker = await screen.findByRole("combobox", { name: "Company" });
|
|
await waitFor(() => expect(picker.querySelectorAll("option")).toHaveLength(3));
|
|
await user.selectOptions(picker, "2");
|
|
expect(screen.getByTestId("probe")).toHaveTextContent("2|false|CV");
|
|
expect(sessionStorage.getItem("company-scope")).toBe("2");
|
|
await user.selectOptions(picker, "");
|
|
expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV");
|
|
expect(sessionStorage.getItem("company-scope")).toBeNull();
|
|
});
|
|
|
|
it("restores the choice from sessionStorage", async () => {
|
|
sessionStorage.setItem("company-scope", "1");
|
|
renderScope();
|
|
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("1|false|CV"));
|
|
});
|
|
});
|