Files
co-lite/solvway-admin-portal/app/lib/CompanyScope.test.tsx
T

91 lines
3.8 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 });
});
const openPicker = async (user: ReturnType<typeof userEvent.setup>) => {
const picker = await screen.findByRole("combobox", { name: "Company" });
await user.click(picker);
return picker;
};
describe("CompanyPicker", () => {
it("is shown to a super admin with All companies plus each company", async () => {
const user = userEvent.setup();
renderScope();
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV"));
await openPicker(user);
expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["All companies", "INS", "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();
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV"));
await openPicker(user);
await user.click(await screen.findByRole("option", { name: "CV" }));
expect(screen.getByTestId("probe")).toHaveTextContent("2|false|CV");
expect(sessionStorage.getItem("company-scope")).toBe("2");
await openPicker(user);
await user.click(await screen.findByRole("option", { name: "All companies" }));
expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV");
expect(sessionStorage.getItem("company-scope")).toBeNull();
});
it("opens with Enter, moves with the arrow keys and chooses with Enter", async () => {
const user = userEvent.setup();
renderScope();
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("undefined|true|CV"));
const picker = await screen.findByRole("combobox", { name: "Company" });
picker.focus();
await user.keyboard("{Enter}");
await screen.findByRole("option", { name: "INS" });
await user.keyboard("{ArrowDown}{Enter}");
expect(screen.getByTestId("probe")).toHaveTextContent("1|false|CV");
});
it("restores the choice from sessionStorage", async () => {
sessionStorage.setItem("company-scope", "1");
renderScope();
await waitFor(() => expect(screen.getByTestId("probe")).toHaveTextContent("1|false|CV"));
});
});