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

35 lines
1.5 KiB
TypeScript

import { render, screen, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, afterEach } from "vitest";
import { AdminAuthProvider, useAdminAuth } from "./AdminAuthContext";
function Probe() {
const { companyId, companyCode, isSuper, loading } = useAdminAuth();
return <div data-testid="p">{loading ? "loading" : `${companyId}|${companyCode}|${isSuper}`}</div>;
}
function stub(body: unknown, ok = true) {
vi.stubGlobal("fetch", vi.fn().mockResolvedValue({ ok, status: ok ? 200 : 401, json: async () => body }));
}
afterEach(() => vi.unstubAllGlobals());
describe("AdminAuthContext company", () => {
it("holds the company and isSuper from the session", async () => {
stub({ name: "J", roles: [], aclMenu: null, companyId: 7, companyCode: "ins", isSuper: false });
render(<AdminAuthProvider><Probe /></AdminAuthProvider>);
await waitFor(() => expect(screen.getByTestId("p")).toHaveTextContent("7|ins|false"));
});
it("holds isSuper for a super admin with no company", async () => {
stub({ name: "R", roles: [], aclMenu: null, companyId: null, companyCode: null, isSuper: true });
render(<AdminAuthProvider><Probe /></AdminAuthProvider>);
await waitFor(() => expect(screen.getByTestId("p")).toHaveTextContent("null|null|true"));
});
it("clears them when the session fails", async () => {
stub({}, false);
render(<AdminAuthProvider><Probe /></AdminAuthProvider>);
await waitFor(() => expect(screen.getByTestId("p")).toHaveTextContent("null|null|false"));
});
});