test: pin the signed stacking and the uncategorized label of the trend chart (#392)
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 3m7s

Recharts draws nothing under a static render, so the chart runs under the
shared hook harness and the test reads the props it hands to Recharts:
stackOffset="sign" on the bar and the area charts (D7), and the
uncategorized series named from i18n with its data key unchanged, in the
legend and in a hidden-category chip. Removing either stackOffset, or the
label, now turns the suite red.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
le king fu 2026-11-07 22:47:59 -05:00
commit c404f10974

View file

@ -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)");
});
});