Implements the wiki "Reports Mockup" as a Reports → Purchasing sidebar section, wired to real approved-PO spend. Two report families, each index → drill/detail: - Cost Centres (/reports/cost-centres) — spend compared across vessels; row opens a cost-centre report with a Top-accounting-codes breakdown re-pivotable by tier (Heading/Sub/Leaf) + Top-N. - Accounting Codes (/reports/accounting-codes) — drills the Account tree (headings → sub → leaves) via ?parent=; a leaf opens its report broken down by cost centre (or, for a non-leaf, by sub-account). Shared: a pinned filter toolbar (Granularity Monthly/Yearly, Financial Year, Show Top5/Top10/Bottom5/All) whose values live in the URL query so the server component re-renders — no client fetching. KPI tiles, recharts comparison/trend/ breakdown charts, per-row trend sparklines, and CSV export (/api/reports/spend). - lib/reports.ts: the pure, unit-tested aggregation core. Spend = a PO once it reaches POST_APPROVAL_STATUSES, dated by approvedAt, valued at totalAmount (the dashboard's basis); Indian Apr–Mar FY; each PO's leaf accountId rolled up to parents. One query in getReportDataset(), everything else pure. - Sidebar: new collapsible "Reports" section with a "Purchasing" subheading (subgroup support added to the Section model). Gated by view_analytics (Manager/SuperUser/Auditor/Admin); export by the same. Deferred (documented): synthetic Weekly granularity, the "Add to graph" custom multi-select, and line-item-level account allocation (v1 uses the PO-level account). Sites are not cost centres — only vessels. Tests: 11 unit cases for the aggregation core + 3 sidebar cases for the Reports section. Full unit suite 303 green; tsc clean. Smoke-tested all routes end to end against seed data (index/drill/detail/export 200; non-analytics role 307/403). Wiki: "Reports Mockup" marked implemented; "Pages and Navigation" lists the new routes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
163 lines
6.8 KiB
TypeScript
163 lines
6.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, within } from "@testing-library/react";
|
|
|
|
// usePathname is mockable per-test so we can exercise the auto-expand behaviour.
|
|
let mockPathname = "/dashboard";
|
|
vi.mock("next/navigation", () => ({ usePathname: () => mockPathname }));
|
|
|
|
import { Sidebar } from "@/components/layout/sidebar";
|
|
|
|
beforeEach(() => {
|
|
mockPathname = "/dashboard";
|
|
});
|
|
|
|
function headerButton(label: string) {
|
|
return screen.getByRole("button", { name: new RegExp(`^${label}`, "i") });
|
|
}
|
|
|
|
describe("Sidebar collapsible sections", () => {
|
|
it("renders section headings as toggle buttons, collapsed by default", () => {
|
|
// ADMIN sees a Purchasing-less layout? No — render a MANAGER who has
|
|
// Purchasing + Administration headed sections.
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
const purchasing = headerButton("Purchasing");
|
|
const administration = headerButton("Administration");
|
|
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "false");
|
|
expect(administration).toHaveAttribute("aria-expanded", "false");
|
|
|
|
// Collapsed → section links are not in the DOM.
|
|
expect(screen.queryByRole("link", { name: /Cost Centres/i })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("expands a section and reveals its links when its header is clicked", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
const purchasing = headerButton("Purchasing");
|
|
fireEvent.click(purchasing);
|
|
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "true");
|
|
expect(screen.getByRole("link", { name: /Cost Centres/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("collapses other sections when one is opened (single-open accordion)", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
const purchasing = headerButton("Purchasing");
|
|
const administration = headerButton("Administration");
|
|
|
|
fireEvent.click(purchasing);
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "true");
|
|
|
|
fireEvent.click(administration);
|
|
expect(administration).toHaveAttribute("aria-expanded", "true");
|
|
// Opening Administration collapses Purchasing.
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "false");
|
|
});
|
|
|
|
it("toggles a section closed when its header is clicked again", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
const purchasing = headerButton("Purchasing");
|
|
fireEvent.click(purchasing);
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "true");
|
|
|
|
fireEvent.click(purchasing);
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "false");
|
|
});
|
|
|
|
it("auto-expands the section containing the active route on mount", () => {
|
|
mockPathname = "/admin/vessels"; // Cost Centres lives under Administration (manager mgmt → Purchasing)
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
// /admin/vessels is in the Purchasing management block for a MANAGER.
|
|
const purchasing = headerButton("Purchasing");
|
|
expect(purchasing).toHaveAttribute("aria-expanded", "true");
|
|
expect(screen.getByRole("link", { name: /Cost Centres/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("keeps the PPMS brand outside any collapsible section", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
// Brand text is always visible regardless of section state.
|
|
expect(screen.getByText("PPMS")).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders the always-visible main links outside the sections", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
expect(screen.getByRole("link", { name: /Dashboard/i })).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /My Profile/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("scopes revealed links to the opened section", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
const administration = headerButton("Administration");
|
|
fireEvent.click(administration);
|
|
|
|
// Vendors appears under Administration for a manager.
|
|
const adminVendors = screen.getByRole("link", { name: /Vendors/i });
|
|
expect(adminVendors).toBeInTheDocument();
|
|
expect(within(adminVendors).queryByText("Vendors")).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe("Purchase Order links under Purchasing", () => {
|
|
it("renders the renamed PO links inside the Purchasing section (not top-level)", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
// Collapsed by default → PO links are not in the DOM until Purchasing opens.
|
|
expect(screen.queryByRole("link", { name: /New Purchase Order/i })).not.toBeInTheDocument();
|
|
|
|
fireEvent.click(headerButton("Purchasing"));
|
|
|
|
expect(screen.getByRole("link", { name: /New Purchase Order/i })).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /Closed Purchase Orders/i })).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /Import Purchase Order/i })).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: /Purchase Order History/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("auto-expands Purchasing when a PO route is active", () => {
|
|
mockPathname = "/po/new";
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
|
|
expect(headerButton("Purchasing")).toHaveAttribute("aria-expanded", "true");
|
|
expect(screen.getByRole("link", { name: /New Purchase Order/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("drops the old PO labels", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
fireEvent.click(headerButton("Purchasing"));
|
|
|
|
// Old labels were "New PO" / "Import PO" / "History".
|
|
expect(screen.queryByRole("link", { name: /^New PO$/i })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("link", { name: /^Import PO$/i })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole("link", { name: /^History$/i })).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("Reports section (Purchasing subheading)", () => {
|
|
it("reveals the report links under a Purchasing subheading for an analytics role", () => {
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
// Collapsed by default.
|
|
expect(screen.queryByRole("link", { name: /Accounting Codes/i })).not.toBeInTheDocument();
|
|
|
|
fireEvent.click(headerButton("Reports"));
|
|
|
|
expect(screen.getByRole("link", { name: /Cost Centres/i })).toHaveAttribute("href", "/reports/cost-centres");
|
|
expect(screen.getByRole("link", { name: /Accounting Codes/i })).toHaveAttribute("href", "/reports/accounting-codes");
|
|
// The "Purchasing" subheading is rendered in addition to the Purchasing section header.
|
|
expect(screen.getAllByText("Purchasing").length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it("auto-expands Reports when a report route is active", () => {
|
|
mockPathname = "/reports/accounting-codes";
|
|
render(<Sidebar userRole="MANAGER" />);
|
|
expect(headerButton("Reports")).toHaveAttribute("aria-expanded", "true");
|
|
expect(screen.getByRole("link", { name: /Accounting Codes/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("is hidden from roles without view_analytics", () => {
|
|
render(<Sidebar userRole="TECHNICAL" />);
|
|
expect(screen.queryByRole("button", { name: /^Reports/i })).not.toBeInTheDocument();
|
|
});
|
|
});
|