Simpl-Resultat/src/components/balance/LinkTransfersModal.wiring.test.ts
le king fu ff33bfa57e fix: unlink Balance transfers from the window and the import history (#522)
Linked transactions now show first in "Link transfers", checked, with no
date bound; a save diffs the selection and calls unlinkTransfer for the
unchecked ones. A delete of the import history refused for linked
transfers keeps a structured state instead of the English error text:
the panel names the accounts, counts the transfers with a COUNT(*), links
to the account's window when the edition opens the Balance sheet
(/balance?linkTransfers=<id>), and offers "Unlink and delete" in every
edition, one SQL transaction, the Journal line carrying numbers only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:05:12 -05:00

123 lines
4.2 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(),
unlinkTransfer: vi.fn(),
listAccountTransfers: vi.fn(async () => []),
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();
});
});