Simpl-Resultat/src/components/balance/LinkTransfersModal.wiring.test.ts
le king fu 1d2cb32c29 test: share texts and move the last local find copies onto src/test/elementTree.ts (#495)
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>
2026-10-26 20:12:04 -04:00

121 lines
4.1 KiB
TypeScript

// LinkTransfersModal — what the category filter changes (#429).
//
// A static render runs no effect and 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 its CategoryCombobox, renders again to read the
// filter, and `commit()` runs the effect that reloads the transactions.
// `createPortal` is the identity and `document` a stub: the window is a portal.
import { describe, it, expect, vi, beforeAll, afterAll, 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());
vi.mock("react-dom", async (orig) => ({
...(await orig<typeof import("react-dom")>()),
createPortal: (children: ReactNode) => children,
}));
const { getTransactionPage } = vi.hoisted(() => ({ getTransactionPage: vi.fn() }));
vi.mock("../../services/transactionService", () => ({ getTransactionPage }));
vi.mock("../../services/balance.service", () => ({
linkTransfer: vi.fn(),
suggestTransferDirection: vi.fn(() => "in"),
BalanceServiceError: class extends Error {},
}));
import { hookHarness } from "../../test/hookHarness";
import { find } from "../../test/elementTree";
import LinkTransfersModal from "./LinkTransfersModal";
import CategoryCombobox from "../shared/CategoryCombobox";
import type { Category } from "../../shared/types";
const harness = hookHarness();
const TRANSFERS: Category = {
id: 7,
name: "Virements",
type: "transfer",
is_active: true,
is_inputable: true,
sort_order: 1,
created_at: "",
};
const CATEGORIES = [TRANSFERS];
const PROPS = {
accountId: 1,
accountName: "CELI",
categories: CATEGORIES,
initialFrom: "2026-07-01",
initialTo: "2026-09-30",
onClose: vi.fn(),
};
/** One render of the mounted window; its category filter. */
const filter = () => {
const [combobox] = find(harness.render(() => LinkTransfersModal(PROPS)), (el) => el.type === CategoryCombobox);
return combobox.props;
};
/** The category of the last transaction query. */
const queriedCategory = () => {
const calls = getTransactionPage.mock.calls;
return (calls[calls.length - 1][0] as { categoryId: number | null }).categoryId;
};
/** Lets the query's promise settle. */
const flush = async () => {
for (let i = 0; i < 10; i++) await Promise.resolve();
};
beforeAll(() => {
vi.stubGlobal("document", { body: {} });
});
afterAll(() => {
vi.unstubAllGlobals();
});
beforeEach(() => {
harness.clear();
getTransactionPage.mockReset();
getTransactionPage.mockResolvedValue({ rows: [], total: 0 });
});
describe("LinkTransfersModal — category filter (#429)", () => {
it("opens on 'any category', first of the list, with the categories it is given", () => {
const props = filter();
expect(props.extraOptions).toEqual([{ value: "", label: "t(balance.transfers.modal.anyCategory)" }]);
expect(props.activeExtra).toBe("");
expect(props.value).toBeNull();
expect(props.categories).toBe(CATEGORIES);
expect(props.compact).toBe(true);
expect(props.ariaLabel).toBe("t(balance.transfers.modal.category)");
});
it("filters on the category picked, then 'any category' empties the filter again", async () => {
filter();
harness.commit();
await flush();
expect(getTransactionPage).toHaveBeenCalledTimes(1);
expect(queriedCategory()).toBeNull();
(filter().onChange as (id: number) => void)(TRANSFERS.id);
let props = filter();
expect(props.value).toBe(TRANSFERS.id);
expect(props.activeExtra).toBeNull();
harness.commit();
await flush();
expect(getTransactionPage).toHaveBeenCalledTimes(2);
expect(queriedCategory()).toBe(TRANSFERS.id);
(props.onExtraSelect as (value: string) => void)("");
props = filter();
expect(props.value).toBeNull();
expect(props.activeExtra).toBe("");
harness.commit();
await flush();
expect(getTransactionPage).toHaveBeenCalledTimes(3);
expect(queriedCategory()).toBeNull();
});
});