diff --git a/src/components/reports/CategoryOverTimeChart.wiring.test.ts b/src/components/reports/CategoryOverTimeChart.wiring.test.ts new file mode 100644 index 0000000..acd4be7 --- /dev/null +++ b/src/components/reports/CategoryOverTimeChart.wiring.test.ts @@ -0,0 +1,80 @@ +// CategoryOverTimeChart — negative amounts and the uncategorized series (#392). +// +// Recharts draws nothing under a static render, so the stacking cannot be read +// from the markup: the chart runs as a plain function under the shared hook +// harness (src/test/hookHarness.ts), and the test reads the props it hands to +// Recharts. With `stackOffset="sign"`, a negative value (an expense whose +// refunds exceed its purchases, a one-leg transfer) stacks below zero instead +// of overlapping the positive ones (D7); what that looks like was checked on +// screenshots (PR of #392). +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { AreaChart, Area, Bar, BarChart } from "recharts"; + +vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig())); +vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock()); + +import { hookHarness } from "../../test/hookHarness"; +import { find, texts } from "../../test/elementTree"; +import CategoryOverTimeChart, { type CategoryOverTimeChartType } from "./CategoryOverTimeChart"; +import type { CategoryOverTimeData } from "../../shared/types"; + +const harness = hookHarness(); + +const G = "Épicerie régulière"; +const U = "Uncategorized"; // the SQL's key of the rows without a category +const T = "Paiement carte crédit"; + +const DATA: CategoryOverTimeData = { + categories: [G, U, T], + data: [{ month: "2026-10", [G]: 80, [U]: 40, [T]: -300 }], + colors: { [G]: "#ea580c", [U]: "#9ca3af", [T]: "#16a34a" }, + categoryIds: { [G]: 1111, [U]: null, [T]: 1971 }, + types: { [G]: "expense", [U]: "uncategorized", [T]: "transfer" }, + tree: [], +}; + +const render = (chartType: CategoryOverTimeChartType, hidden: string[] = []) => + harness.render(() => + CategoryOverTimeChart({ + data: DATA, + hiddenCategories: new Set(hidden), + onToggleHidden: () => {}, + onShowAll: () => {}, + onViewDetails: () => {}, + chartType, + }), + ); + +beforeEach(() => { + harness.clear(); +}); + +describe("CategoryOverTimeChart", () => { + it("stacks a negative value below zero, in bars", () => { + const [chart] = find(render("bar"), (el) => el.type === BarChart); + expect(chart.props.stackOffset).toBe("sign"); + }); + + it("stacks a negative value below zero, in areas", () => { + const [chart] = find(render("area"), (el) => el.type === AreaChart); + expect(chart.props.stackOffset).toBe("sign"); + }); + + it("names the series of the rows without a category from i18n, its data key unchanged", () => { + for (const [type, series] of [ + ["bar", Bar], + ["area", Area], + ] as const) { + const names = find(render(type), (el) => el.type === series).map((el) => [el.props.dataKey, el.props.name]); + expect(names).toEqual([ + [G, G], + [U, "t(reports.uncategorized)"], + [T, T], + ]); + } + }); + + it("names a hidden uncategorized series from i18n in its chip", () => { + expect(texts(render("bar", [U]))).toContain("t(reports.uncategorized)"); + }); +});