119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
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 { PMPage } from "@/pages/PMPage";
|
|
import { pmApi, type PMRecordItem } from "@/api/pm";
|
|
import { departmentsApi } from "@/api/masters";
|
|
import { useAuthStore } from "@/hooks/useAuth";
|
|
|
|
function currentMonthDay(day = 15): string {
|
|
const now = new Date();
|
|
const y = now.getFullYear();
|
|
const m = String(now.getMonth() + 1).padStart(2, "0");
|
|
const d = String(day).padStart(2, "0");
|
|
return `${y}-${m}-${d}`;
|
|
}
|
|
|
|
const MOCK_RECORD: PMRecordItem = {
|
|
id: 101,
|
|
schedule_id: 11,
|
|
asset_id: 1,
|
|
asset_code: "PHRC-ICU-0001",
|
|
asset_name: "ICU Ventilator",
|
|
department_id: 1,
|
|
department_name: "ICU",
|
|
location_name: "ICU Bay 1",
|
|
service_mode: "in_house",
|
|
responsible_team: "Biomed Team A",
|
|
provider_id: null,
|
|
provider_name: null,
|
|
contract_id: null,
|
|
frequency_months: 6,
|
|
planned_date: currentMonthDay(15),
|
|
completed_date: null,
|
|
status: "due",
|
|
severity: "yellow",
|
|
service_details: null,
|
|
remarks: null,
|
|
rescheduled_to: null,
|
|
};
|
|
|
|
function renderPMPage() {
|
|
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<QueryClientProvider client={qc}>
|
|
<MemoryRouter>
|
|
<PMPage />
|
|
</MemoryRouter>
|
|
</QueryClientProvider>
|
|
);
|
|
}
|
|
|
|
describe("PMPage", () => {
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
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",
|
|
});
|
|
|
|
vi.spyOn(departmentsApi, "list").mockResolvedValue({
|
|
items: [{ id: 1, name: "ICU", code: "ICU", description: null, is_active: true }],
|
|
total: 1,
|
|
});
|
|
vi.spyOn(pmApi, "listRecords").mockResolvedValue({
|
|
items: [MOCK_RECORD],
|
|
total: 1,
|
|
summary: { upcoming: 3, due: 2, overdue: 1, completed_this_month: 5 },
|
|
});
|
|
vi.spyOn(pmApi, "listSchedules").mockResolvedValue({
|
|
items: [],
|
|
total: 0,
|
|
});
|
|
});
|
|
|
|
it("renders KPI strip counts and switches between Table and Calendar views", async () => {
|
|
renderPMPage();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getAllByText("Upcoming").length).toBeGreaterThanOrEqual(1);
|
|
expect(screen.getByText("Completed This Month")).toBeInTheDocument();
|
|
expect(screen.getByText("3")).toBeInTheDocument();
|
|
expect(screen.getByText("2")).toBeInTheDocument();
|
|
expect(screen.getByText("5")).toBeInTheDocument();
|
|
});
|
|
|
|
// Initially in Table view
|
|
expect(screen.getByText("PHRC-ICU-0001")).toBeInTheDocument();
|
|
expect(screen.getByText("ICU Ventilator")).toBeInTheDocument();
|
|
|
|
// Switch to Calendar view
|
|
const calendarBtn = screen.getByRole("button", { name: /Calendar/i });
|
|
fireEvent.click(calendarBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("Mon")).toBeInTheDocument();
|
|
expect(screen.getByText("Sun")).toBeInTheDocument();
|
|
expect(screen.getByText(/PHRC-ICU-0001/)).toBeInTheDocument();
|
|
});
|
|
|
|
// Switch back to Table view
|
|
const tableBtn = screen.getByRole("button", { name: /Table View/i });
|
|
fireEvent.click(tableBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.queryByText("Mon")).not.toBeInTheDocument();
|
|
expect(screen.getByText("ICU Ventilator")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|