mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 16:19:01 +00:00
React Query keeps the last successful data after a failed refetch, so an earlier true kept the entry visible. Require a non-error query per tier, and cover the real query path with a mocked API.
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { screen, waitFor } from "@testing-library/react";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { AppSidebar } from "@/components/layout/app-sidebar";
|
|
import { queryKeys } from "@/infra/query-key/query-key";
|
|
import { createTestQueryClient, renderWithProviders } from "@/test/test-utils";
|
|
|
|
const canApproveApiMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("@/domain/uplifts/api/uplifts-api", () => ({
|
|
upliftsApi: { canApprove: canApproveApiMock },
|
|
}));
|
|
|
|
describe("AppSidebar Uplift Approvals gating through the can-approve query", () => {
|
|
beforeEach(() => {
|
|
canApproveApiMock.mockReset();
|
|
});
|
|
|
|
it("shows the entry when the API allows tier 2 and fails for tier 1", async () => {
|
|
canApproveApiMock.mockImplementation(async (tier: number) => {
|
|
if (tier === 1) throw new Error("503");
|
|
return true;
|
|
});
|
|
|
|
renderWithProviders(<AppSidebar collapsed={false} />, { route: "/", withAuth: false });
|
|
|
|
expect(await screen.findByRole("link", { name: "Uplift Approvals" })).toHaveAttribute(
|
|
"href",
|
|
"/uplifts",
|
|
);
|
|
expect(canApproveApiMock).toHaveBeenCalledWith(1);
|
|
expect(canApproveApiMock).toHaveBeenCalledWith(2);
|
|
});
|
|
|
|
it("keeps the entry hidden once the API has denied both tiers", async () => {
|
|
canApproveApiMock.mockResolvedValue(false);
|
|
const queryClient = createTestQueryClient();
|
|
|
|
renderWithProviders(<AppSidebar collapsed={false} />, {
|
|
route: "/",
|
|
withAuth: false,
|
|
queryClient,
|
|
});
|
|
|
|
await waitFor(() => {
|
|
for (const tier of [1, 2]) {
|
|
expect(queryClient.getQueryState(queryKeys.uplifts.canApprove(tier))?.status).toBe(
|
|
"success",
|
|
);
|
|
}
|
|
});
|
|
expect(screen.queryByRole("link", { name: "Uplift Approvals" })).toBeNull();
|
|
expect(screen.getByRole("link", { name: "Services" })).toHaveAttribute(
|
|
"href",
|
|
"/settings/services",
|
|
);
|
|
});
|
|
});
|