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>
131 lines
5.5 KiB
TypeScript
131 lines
5.5 KiB
TypeScript
// HierarchyCheckModal — what a row hands to « Rattacher » (#393).
|
|
//
|
|
// A static render clicks nothing. React's hooks come from the shared harness
|
|
// (src/test/hookHarness.ts, #409): the window runs as a plain function, its
|
|
// state survives from one call to the next, the test calls the props it hands
|
|
// to each CategoryCombobox and to its buttons, then renders again.
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import type { ReactElement, 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());
|
|
// Pass-through spy on the pure module, to pin what each row offers.
|
|
vi.mock("./parentOptions", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("./parentOptions")>();
|
|
return { ...actual, buildParentOptions: vi.fn(actual.buildParentOptions) };
|
|
});
|
|
|
|
import { hookHarness } from "../../test/hookHarness";
|
|
import { find } from "../../test/elementTree";
|
|
import HierarchyCheckModal from "./HierarchyCheckModal";
|
|
import CategoryCombobox from "../shared/CategoryCombobox";
|
|
import { buildParentOptions } from "./parentOptions";
|
|
import type { CategoryTreeNode } from "../../shared/types";
|
|
import type { OrphanCategory } from "../../utils/categoryHierarchy";
|
|
|
|
const harness = hookHarness();
|
|
|
|
type Props = Record<string, unknown> & { children?: ReactNode };
|
|
|
|
const node =(id: number, parent_id: number | null): CategoryTreeNode => ({
|
|
id,
|
|
name: `c${id}`,
|
|
parent_id,
|
|
color: null,
|
|
icon: null,
|
|
type: "expense",
|
|
is_active: true,
|
|
is_inputable: true,
|
|
sort_order: id,
|
|
keyword_count: 0,
|
|
i18n_key: null,
|
|
children: [],
|
|
});
|
|
|
|
// Roots 1 and 2 (2 has a child, 21); 3 <-> 5 is a cycle; 20's parent is gone.
|
|
const CATEGORIES = [node(1, null), node(2, null), node(21, 2), node(3, 5), node(5, 3), node(20, 99)];
|
|
const CYCLE: OrphanCategory = { id: 3, reason: "cycle" };
|
|
const GONE: OrphanCategory = { id: 20, reason: "missing_parent" };
|
|
|
|
const onReattach = vi.fn();
|
|
const onClose = vi.fn();
|
|
|
|
const view = (orphans: OrphanCategory[] | null = [CYCLE, GONE], categories = CATEGORIES) => {
|
|
const tree = harness.render(() =>
|
|
HierarchyCheckModal({ orphans, error: null, categories, isSaving: false, onReattach, onClose }),
|
|
);
|
|
return {
|
|
comboboxes: find(tree, (el) => el.type === CategoryCombobox).map((el) => el.props),
|
|
reattach: find(tree, (el) => el.type === "button" && el.props.children === "t(categories.hierarchy.reattach)"),
|
|
close: find(tree, (el) => el.type === "button" && el.props.children === "t(common.close)"),
|
|
};
|
|
};
|
|
|
|
const ids = (list: unknown) => (list as CategoryTreeNode[]).map((c) => c.id).sort((a, b) => a - b);
|
|
|
|
/** Calls the `onClick` the window hands to a button. */
|
|
const click = (button: ReactElement<Props>) => (button.props.onClick as () => void)();
|
|
|
|
beforeEach(() => {
|
|
harness.clear();
|
|
onReattach.mockReset();
|
|
onClose.mockReset();
|
|
vi.mocked(buildParentOptions).mockClear();
|
|
});
|
|
|
|
describe("HierarchyCheckModal — a row's new parent (#393)", () => {
|
|
it("offers the category form's parents, the row's category treated as the one edited, the root first", () => {
|
|
const { comboboxes } = view();
|
|
expect(buildParentOptions).toHaveBeenCalledWith(CATEGORIES, { editingId: 3 });
|
|
expect(buildParentOptions).toHaveBeenCalledWith(CATEGORIES, { editingId: 20 });
|
|
// The cycle member carries the other member (5): roots only, its own cycle left out.
|
|
// 20, whose parent is gone, shows as a root in the tree, and is offered as one.
|
|
expect(ids(comboboxes[0].categories)).toEqual([1, 2, 20]);
|
|
// A leaf: roots and level-1 categories, never a cycle member nor itself.
|
|
expect(ids(comboboxes[1].categories)).toEqual([1, 2, 21]);
|
|
for (const props of comboboxes) {
|
|
expect(props.extraOptions).toEqual([{ value: "", label: "t(categories.hierarchy.root)" }]);
|
|
expect(props.value).toBeNull();
|
|
expect(props.activeExtra).toBe("");
|
|
}
|
|
});
|
|
|
|
it("« Rattacher » hands the root by default", () => {
|
|
click(view().reattach[1]);
|
|
expect(onReattach).toHaveBeenCalledWith(GONE, null);
|
|
});
|
|
|
|
it("hands the parent picked on the row, and the root again once it is picked back", () => {
|
|
(view().comboboxes[1].onChange as (id: number) => void)(21);
|
|
let current = view();
|
|
expect(current.comboboxes[1].value).toBe(21);
|
|
expect(current.comboboxes[1].activeExtra).toBeNull();
|
|
// The other row keeps the root.
|
|
expect(current.comboboxes[0].value).toBeNull();
|
|
click(current.reattach[1]);
|
|
expect(onReattach).toHaveBeenLastCalledWith(GONE, 21);
|
|
|
|
(current.comboboxes[1].onExtraSelect as (value: string) => void)("");
|
|
current = view();
|
|
expect(current.comboboxes[1].value).toBeNull();
|
|
expect(current.comboboxes[1].activeExtra).toBe("");
|
|
click(current.reattach[1]);
|
|
expect(onReattach).toHaveBeenLastCalledWith(GONE, null);
|
|
});
|
|
|
|
it("falls back to the root when the parent picked is no longer offered", () => {
|
|
(view().comboboxes[1].onChange as (id: number) => void)(21);
|
|
// After a reload, 21 is gone from the categories.
|
|
const current = view([CYCLE, GONE], CATEGORIES.filter((c) => c.id !== 21));
|
|
expect(current.comboboxes[1].value).toBeNull();
|
|
click(current.reattach[1]);
|
|
expect(onReattach).toHaveBeenCalledWith(GONE, null);
|
|
});
|
|
|
|
it("« Fermer » closes the window", () => {
|
|
const { close } = view();
|
|
expect(close).toHaveLength(1);
|
|
click(close[0]);
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|