initial commit
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
||||
import { MemoryRouter, Route, Routes } from "react-router-dom";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { ReportsPage } from "@/pages/ReportsPage";
|
||||
import { ReportDetailPage } from "@/pages/ReportDetailPage";
|
||||
import * as reportsApi from "@/api/reports";
|
||||
import { departmentsApi } from "@/api/masters";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
|
||||
vi.mock("recharts", async () => {
|
||||
const actual = await vi.importActual<typeof import("recharts")>("recharts");
|
||||
return {
|
||||
...actual,
|
||||
ResponsiveContainer: ({ children }: { children: React.ReactNode }) => (
|
||||
<div style={{ width: 600, height: 240 }}>{children}</div>
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
function renderWithRouter(ui: React.ReactNode, initialEntries = ["/"]) {
|
||||
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
return render(
|
||||
<QueryClientProvider client={qc}>
|
||||
<MemoryRouter initialEntries={initialEntries}>{ui}</MemoryRouter>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
||||
describe("Phase 7 Reports", () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.spyOn(departmentsApi, "list").mockResolvedValue({
|
||||
items: [{ id: 1, name: "ICU", code: "ICU", description: null, is_active: true }],
|
||||
total: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("ReportsPage renders all 8 reports for management and hides Daily Department Visit Report for department_user", () => {
|
||||
useAuthStore.setState({
|
||||
user: {
|
||||
id: 5,
|
||||
email: "mgmt@poona.org",
|
||||
name: "Director",
|
||||
role: "management",
|
||||
department_id: null,
|
||||
department_name: null,
|
||||
is_active: true,
|
||||
},
|
||||
token: "t-mgmt",
|
||||
});
|
||||
|
||||
const { rerender } = renderWithRouter(<ReportsPage />);
|
||||
expect(screen.getByText("Monthly Maintenance Summary")).toBeInTheDocument();
|
||||
expect(screen.getByText("Daily Department Visit Report")).toBeInTheDocument();
|
||||
expect(screen.getByText("Demo Equipment Report")).toBeInTheDocument();
|
||||
|
||||
useAuthStore.setState({
|
||||
user: {
|
||||
id: 3,
|
||||
email: "icu@poona.org",
|
||||
name: "ICU Nurse",
|
||||
role: "department_user",
|
||||
department_id: 1,
|
||||
department_name: "ICU",
|
||||
is_active: true,
|
||||
},
|
||||
token: "t-dept",
|
||||
});
|
||||
|
||||
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
rerender(
|
||||
<QueryClientProvider client={qc}>
|
||||
<MemoryRouter>
|
||||
<ReportsPage />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
|
||||
expect(screen.getByText("Monthly Maintenance Summary")).toBeInTheDocument();
|
||||
expect(
|
||||
screen.queryByText("Daily Department Visit Report")
|
||||
).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("ReportDetailPage renders Monthly Maintenance summary, How this is calculated popover, and Export Excel button", async () => {
|
||||
useAuthStore.setState({
|
||||
user: {
|
||||
id: 5,
|
||||
email: "mgmt@poona.org",
|
||||
name: "Director",
|
||||
role: "management",
|
||||
department_id: null,
|
||||
department_name: null,
|
||||
is_active: true,
|
||||
},
|
||||
token: "t-mgmt",
|
||||
});
|
||||
|
||||
vi.spyOn(reportsApi, "fetchReport").mockResolvedValue({
|
||||
report_key: "monthly-maintenance",
|
||||
title: "Monthly Maintenance Summary",
|
||||
filters_applied: { month: "2026-11" },
|
||||
formula_info: {
|
||||
scheduled_hours_per_day: 24,
|
||||
downtime_end_milestone: "restored_at",
|
||||
days_in_month: 30,
|
||||
uptime_formula: "Scheduled Minutes - Clipped Downtime Minutes",
|
||||
downtime_formula: "Sum of breakdown intervals clipped to month in IST",
|
||||
},
|
||||
summary: {
|
||||
total_assets: 10,
|
||||
total_complaints: 3,
|
||||
downtime_formatted: "1d 2h 0m",
|
||||
downtime_days_count: 3,
|
||||
uptime_formatted: "298d 22h 0m",
|
||||
downtime_pct: 0.36,
|
||||
},
|
||||
columns: [
|
||||
{ key: "department_name", label: "Department" },
|
||||
{ key: "asset_count", label: "Assets" },
|
||||
{ key: "downtime_formatted", label: "Downtime" },
|
||||
{ key: "downtime_pct", label: "Downtime %" },
|
||||
],
|
||||
rows: [
|
||||
{
|
||||
department_name: "ICU",
|
||||
asset_count: 10,
|
||||
downtime_formatted: "1d 2h 0m",
|
||||
downtime_pct: 0.36,
|
||||
},
|
||||
],
|
||||
chart_data: [
|
||||
{ department: "ICU", downtime_hours: 26, uptime_hours: 7174 },
|
||||
],
|
||||
});
|
||||
|
||||
const exportSpy = vi
|
||||
.spyOn(reportsApi, "exportReport")
|
||||
.mockResolvedValue(undefined);
|
||||
|
||||
renderWithRouter(
|
||||
<Routes>
|
||||
<Route path="/reports/:reportKey" element={<ReportDetailPage />} />
|
||||
</Routes>,
|
||||
["/reports/monthly-maintenance"]
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByRole("heading", { name: /Monthly Maintenance Summary/i })
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByText("Distinct Downtime Days")).toBeInTheDocument();
|
||||
expect(screen.getAllByText("99.6 %").length).toBeGreaterThanOrEqual(2);
|
||||
expect(screen.getByRole("cell", { name: "ICU" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const formulaBtn = screen.getByRole("button", {
|
||||
name: /How this is calculated/i,
|
||||
});
|
||||
fireEvent.click(formulaBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByText(/Uptime & Downtime Calculation Methodology/i)
|
||||
).toBeInTheDocument();
|
||||
expect(screen.getByText(/24 hrs/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/30 days/i)).toBeInTheDocument();
|
||||
expect(screen.getByText("restored_at")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const exportXlsxBtn = screen.getByRole("button", {
|
||||
name: /Export Excel \(\.xlsx\)/i,
|
||||
});
|
||||
fireEvent.click(exportXlsxBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(exportSpy).toHaveBeenCalledWith(
|
||||
"monthly-maintenance",
|
||||
"xlsx",
|
||||
expect.any(Object)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user