import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen, waitFor, fireEvent } from "@testing-library/react"; import { MemoryRouter } from "react-router-dom"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { CalibrationPage } from "@/pages/CalibrationPage"; import { calibrationApi, type CalibrationPlanItem, } from "@/api/calibration"; import { departmentsApi, vendorsApi } from "@/api/masters"; import { useAuthStore } from "@/hooks/useAuth"; const MOCK_PLAN: CalibrationPlanItem = { id: 50, asset_id: 1, asset_code: "PHRC-LAB-0009", asset_name: "Auto Analyzer", department_id: 2, department_name: "Pathology", frequency_months: 12, calibrations_per_year: 1, agency_id: 4, agency_name: "NABL Calibration Labs", approved_by_id: null, approved_by_name: null, approved_on: null, next_due_date: "2026-12-01", is_active: true, }; function renderCalibrationPage() { const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } }); return render( ); } describe("CalibrationPage", () => { beforeEach(() => { vi.restoreAllMocks(); vi.spyOn(departmentsApi, "list").mockResolvedValue({ items: [{ id: 2, name: "Pathology", code: "LAB", description: null, is_active: true }], total: 1, }); vi.spyOn(vendorsApi, "list").mockResolvedValue({ items: [], total: 0, }); vi.spyOn(calibrationApi, "listRecords").mockResolvedValue({ items: [], total: 0, summary: { upcoming: 1, due: 0, overdue: 0, completed: 2 }, }); vi.spyOn(calibrationApi, "listPlans").mockResolvedValue({ items: [MOCK_PLAN], total: 1, }); }); it("shows Approve / Edit Frequency button on Calibration Plans tab for management", async () => { useAuthStore.setState({ user: { id: 5, email: "mgmt@poona.org", name: "Hospital Director", role: "management", department_id: null, department_name: null, is_active: true, }, token: "test-token", }); renderCalibrationPage(); const plansTab = screen.getByRole("button", { name: /Calibration Plans/i }); fireEvent.click(plansTab); await waitFor(() => { expect(screen.getByText("PHRC-LAB-0009")).toBeInTheDocument(); expect( screen.getByRole("button", { name: /Approve \/ Edit Frequency/i }) ).toBeInTheDocument(); }); }); it("hides Approve / Edit Frequency button on Calibration Plans tab for biomedical", async () => { useAuthStore.setState({ user: { id: 2, email: "bio@poona.org", name: "Biomed Engineer", role: "biomedical", department_id: null, department_name: null, is_active: true, }, token: "test-token", }); renderCalibrationPage(); const plansTab = screen.getByRole("button", { name: /Calibration Plans/i }); fireEvent.click(plansTab); await waitFor(() => { expect(screen.getByText("PHRC-LAB-0009")).toBeInTheDocument(); }); expect( screen.queryByRole("button", { name: /Approve \/ Edit Frequency/i }) ).not.toBeInTheDocument(); expect( screen.queryByRole("button", { name: /Edit Frequency/i }) ).not.toBeInTheDocument(); expect( screen.getByRole("button", { name: /Add Equipment to Calibration/i }) ).toBeInTheDocument(); }); });