Simpl-Resultat/src/pages/ReportsCartesPage.render.test.tsx
le king fu ef2932ecc1
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 3m22s
fix(reports): open the category zoom from a card on the card's month
A card's increases and decreases compare its reference month to the
month before, but their links only copied the URL: the zoom opened on
the reports period (the year to date by default), never on that month.
A mover's link now carries the month of the snapshot the list shows,
from its first to its last day (period=custom, from, to), with cat and
the other params kept (Max's answer to the review of PR #551).

test: reproduces #548 (a mover's link, the month both pages hand the
lists, the page's box on a leaf and with the subcategories left out)

Refs #548

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:44:38 -05:00

128 lines
4.8 KiB
TypeScript

// ReportsCartesPage hands « Solde net » the note of its snapshot (#392, D5).
// No jsdom: react-dom/server renders the page in vitest's node environment.
// The data hook and the widgets are stubbed, and the fake KpiCard records the
// props it receives.
import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { MemoryRouter } from "react-router-dom";
import type { CartesKpi, CartesSnapshot } from "../shared/types";
vi.mock("react-i18next", async () => (await import("../test/i18nMock")).i18nMock());
let snapshot: CartesSnapshot | null = null;
vi.mock("../hooks/useCartes", () => ({
useCartes: () => ({
year: 2026,
month: 10,
mode: "month",
snapshot,
isLoading: false,
error: null,
setReferencePeriod: vi.fn(),
setMode: vi.fn(),
}),
}));
// Hoisted with the mocks below, which run before the imports.
const { marker, kpiProps, moversProps } = vi.hoisted(() => ({
marker: (name: string) => ({ default: () => <i data-marker={name} /> }),
kpiProps: new Map<string, Record<string, unknown>>(),
moversProps: [] as Record<string, unknown>[],
}));
vi.mock("../components/reports/CompareReferenceMonthPicker", () => marker("month-picker"));
vi.mock("../components/reports/cards/CartesPeriodModeToggle", () => marker("mode-toggle"));
vi.mock("../components/reports/cards/KpiCard", () => ({
default: (props: Record<string, unknown>) => {
kpiProps.set(props.id as string, props);
return <i data-marker="kpi" />;
},
}));
vi.mock("../components/reports/cards/IncomeExpenseOverlayChart", () => marker("overlay"));
vi.mock("../components/reports/cards/TopMoversList", () => ({
default: (props: Record<string, unknown>) => {
moversProps.push(props);
return <i data-marker="movers" />;
},
}));
vi.mock("../components/reports/cards/BudgetAdherenceCard", () => marker("adherence"));
vi.mock("../components/reports/cards/SeasonalityCard", () => marker("seasonality"));
import ReportsCartesPage from "./ReportsCartesPage";
import { netNote } from "../components/reports/cards/netNote";
import { markedT } from "../test/i18nMock";
const kpi = (current: number): CartesKpi => ({
current,
previousMonth: null,
previousYear: null,
deltaMoMAbs: null,
deltaMoMPct: null,
deltaYoYAbs: null,
deltaYoYPct: null,
sparkline: [],
});
const render = () =>
renderToStaticMarkup(
<MemoryRouter>
<ReportsCartesPage />
</MemoryRouter>,
);
beforeEach(() => {
snapshot = null;
kpiProps.clear();
moversProps.length = 0;
});
describe("ReportsCartesPage — the note under « Solde net » (#392, D5)", () => {
it("hands the net card the note of the snapshot's breakdown, and no other card a note", () => {
const netBreakdown = { uncategorized: 40, transfers: -300 };
snapshot = {
referenceYear: 2026,
referenceMonth: 10,
kpis: { income: kpi(1000), expenses: kpi(30), net: kpi(710), savingsRate: kpi(97) },
netBreakdown,
flow12Months: [],
topMoversUp: [],
topMoversDown: [],
budgetAdherence: { categoriesInTarget: 0, categoriesTotal: 0, worstOverruns: [] },
seasonality: { referenceAmount: 30, historicalYears: [], historicalAverage: null, deviationPct: null },
};
render();
expect([...kpiProps.keys()]).toEqual(["income", "expenses", "net", "savingsRate"]);
expect(kpiProps.get("net")!.note).toContain("t(reports.cartes.netNote.both[");
expect(kpiProps.get("net")!.note).toBe(netNote(markedT, "fr", netBreakdown));
for (const id of ["income", "expenses", "savingsRate"]) expect(kpiProps.get(id)!.note).toBeUndefined();
});
it("renders no card without a snapshot", () => {
expect(render()).toContain("t(reports.empty.noData)");
expect(kpiProps.size).toBe(0);
});
});
describe("ReportsCartesPage — the movers link to the month of their snapshot (#548)", () => {
it("hands both lists the snapshot's month, not the picker's, while the next snapshot loads", () => {
// The picker is on October (the hook's month); September's snapshot is
// still on screen until October's arrives.
snapshot = {
referenceYear: 2026,
referenceMonth: 9,
kpis: { income: kpi(1000), expenses: kpi(30), net: kpi(970), savingsRate: kpi(97) },
netBreakdown: { uncategorized: 0, transfers: 0 },
flow12Months: [],
topMoversUp: [],
topMoversDown: [],
budgetAdherence: { categoriesInTarget: 0, categoriesTotal: 0, worstOverruns: [] },
seasonality: { referenceAmount: 30, historicalYears: [], historicalAverage: null, deviationPct: null },
};
render();
expect(moversProps.map((p) => [p.direction, p.year, p.month])).toEqual([
["up", 2026, 9],
["down", 2026, 9],
]);
expect(moversProps[0].movers).toBe(snapshot.topMoversUp);
expect(moversProps[1].movers).toBe(snapshot.topMoversDown);
});
});