Add `texts` to src/test/elementTree.ts: every string of the tree, depth first, through arrays at any depth and fragments, each string as it is; a number is not a string and is left out, never rendered (Max's decision of 2026-10-24). Eight cases of elementTree.test.ts prove each point. The fifteen test files that kept a copy of `find` import the shared one, and the seven of them that kept a copy of `texts` import that too. The copies had the same traversal: same tests (137), same names, all green. The eleven calls by type become predicates (`(el) => el.type === X`), the Fragment search of TransactionTable.wiring.test.ts included. Only the imports tsc reports as unused go; `expand` keeps its own in OnboardingChecklist.test.tsx and ImportEmptyState.test.tsx. The `const texts` of CategoryDetailPanel.delete.test.ts and AccountsPage.delete.test.ts, of another meaning, stay as they are. Refs #495 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
4.2 KiB
TypeScript
108 lines
4.2 KiB
TypeScript
// AdjustmentForm — what a line's combobox changes (#429).
|
|
//
|
|
// A static render clicks nothing. React's hooks come from the shared harness
|
|
// (src/test/hookHarness.ts, #409): the form runs as a plain function, its
|
|
// state survives from one call to the next as between two renders of a
|
|
// mounted form, and the test calls the props the form hands to each
|
|
// CategoryCombobox and to its buttons, then renders again to read the lines.
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import type { ReactNode } from "react";
|
|
|
|
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 } from "../../test/elementTree";
|
|
import AdjustmentForm from "./AdjustmentForm";
|
|
import CategoryCombobox from "../shared/CategoryCombobox";
|
|
import type { Category } from "../../shared/types";
|
|
import type { AdjustmentFormData, EntryFormData } from "../../hooks/useAdjustments";
|
|
|
|
const harness = hookHarness();
|
|
|
|
/** Text of an element's children, elements left out. */
|
|
const text = (node: ReactNode): string =>
|
|
Array.isArray(node) ? node.map(text).join("") : typeof node === "string" ? node : "";
|
|
|
|
const category = (id: number, name: string): Category => ({
|
|
id,
|
|
name,
|
|
type: "expense",
|
|
is_active: true,
|
|
is_inputable: true,
|
|
sort_order: id,
|
|
created_at: "",
|
|
});
|
|
|
|
const RENT = category(30, "Loyer");
|
|
const CABIN = category(20, "Chalet");
|
|
/** In the list's order: a new line takes the first one. */
|
|
const CATEGORIES = [RENT, CABIN];
|
|
|
|
const DATA: AdjustmentFormData = { name: "Chalet", description: "", date: "2026-10-01", is_recurring: false };
|
|
|
|
let onSave = vi.fn();
|
|
|
|
/** One render of the mounted form; the props keep their identity from one render to the next. */
|
|
const mount = (entries: EntryFormData[]) => {
|
|
const props = {
|
|
initialData: DATA,
|
|
initialEntries: entries,
|
|
categories: CATEGORIES,
|
|
isCreating: false,
|
|
isSaving: false,
|
|
onSave,
|
|
onCancel: vi.fn(),
|
|
};
|
|
return () => harness.render(() => AdjustmentForm(props));
|
|
};
|
|
|
|
const comboboxes = (tree: ReactNode) => find(tree, (el) => el.type === CategoryCombobox);
|
|
const addButton = (tree: ReactNode) =>
|
|
find(tree, (el) => el.type === "button" && text(el.props.children) === "t(adjustments.addEntry)")[0];
|
|
const form = (tree: ReactNode) => find(tree, (el) => el.type === "form")[0];
|
|
|
|
beforeEach(() => {
|
|
harness.clear();
|
|
onSave = vi.fn();
|
|
});
|
|
|
|
describe("AdjustmentForm — a line's category (#429)", () => {
|
|
it("gives a new line the first category of the list", () => {
|
|
const render = mount([]);
|
|
let tree = render();
|
|
expect(comboboxes(tree)).toHaveLength(0);
|
|
(addButton(tree).props.onClick as () => void)();
|
|
tree = render();
|
|
const [combobox] = comboboxes(tree);
|
|
expect(combobox.props.value).toBe(RENT.id);
|
|
expect(combobox.props.categories).toBe(CATEGORIES);
|
|
expect(combobox.props.compact).toBe(true);
|
|
expect(combobox.props.ariaLabel).toBe("t(adjustments.category)");
|
|
});
|
|
|
|
it("changes the category of the line picked, and of that line only", () => {
|
|
const render = mount([
|
|
{ id: 1, category_id: RENT.id, amount: 1200, description: "" },
|
|
{ id: 2, category_id: RENT.id, amount: 80, description: "Électricité" },
|
|
]);
|
|
let tree = render();
|
|
(comboboxes(tree)[1].props.onChange as (id: number) => void)(CABIN.id);
|
|
tree = render();
|
|
expect(comboboxes(tree).map((c) => c.props.value)).toEqual([RENT.id, CABIN.id]);
|
|
});
|
|
|
|
it("saves every line with its category, its amount and its description, as before", () => {
|
|
const render = mount([{ id: 1, category_id: RENT.id, amount: 1200, description: "" }]);
|
|
let tree = render();
|
|
(addButton(tree).props.onClick as () => void)();
|
|
tree = render();
|
|
(comboboxes(tree)[1].props.onChange as (id: number) => void)(CABIN.id);
|
|
tree = render();
|
|
(form(tree).props.onSubmit as (e: { preventDefault: () => void }) => void)({ preventDefault: vi.fn() });
|
|
expect(onSave).toHaveBeenCalledWith(DATA, [
|
|
{ id: 1, category_id: RENT.id, amount: 1200, description: "" },
|
|
{ category_id: CABIN.id, amount: 0, description: "" },
|
|
]);
|
|
});
|
|
});
|