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>
220 lines
8.1 KiB
TypeScript
220 lines
8.1 KiB
TypeScript
// LinkTransfersModal — unlinking from the window (#522).
|
|
//
|
|
// `unlinkTransfer` had no caller: the window listed the filtered transactions
|
|
// unchecked, a linked one included, and only linked the checked rows. Now the
|
|
// account's links load once, with no date bound, and come first, checked; a
|
|
// save diffs the selection against them. Same harness as the wiring test of
|
|
// the category filter (src/test/hookHarness.ts): the window runs as a plain
|
|
// function, `commit()` runs its effects, the test clicks through the props of
|
|
// the elements it finds.
|
|
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 svc = vi.hoisted(() => {
|
|
class BalanceServiceError extends Error {
|
|
constructor(
|
|
readonly code: string,
|
|
message: string,
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
return {
|
|
getTransactionPage: vi.fn(),
|
|
listAccountTransfers: vi.fn(),
|
|
linkTransfer: vi.fn(),
|
|
unlinkTransfer: vi.fn(),
|
|
BalanceServiceError,
|
|
};
|
|
});
|
|
vi.mock("../../services/transactionService", () => ({ getTransactionPage: svc.getTransactionPage }));
|
|
vi.mock("../../services/balance.service", () => ({
|
|
linkTransfer: svc.linkTransfer,
|
|
unlinkTransfer: svc.unlinkTransfer,
|
|
listAccountTransfers: svc.listAccountTransfers,
|
|
suggestTransferDirection: (amount: number) => (amount < 0 ? "in" : "out"),
|
|
BalanceServiceError: svc.BalanceServiceError,
|
|
}));
|
|
|
|
import { hookHarness } from "../../test/hookHarness";
|
|
import { find, texts, type TreeProps } from "../../test/elementTree";
|
|
import LinkTransfersModal, { diffTransferSelection } from "./LinkTransfersModal";
|
|
|
|
const harness = hookHarness();
|
|
|
|
/** Linked in March, outside the window's period (July to September). */
|
|
const OLD_LINK = {
|
|
id: 90,
|
|
account_id: 1,
|
|
transaction_id: 501,
|
|
direction: "out" as const,
|
|
notes: null,
|
|
created_at: "",
|
|
transaction_date: "2026-03-02",
|
|
transaction_description: "Dépôt CELI mars",
|
|
transaction_amount: -500,
|
|
account_name: "CELI",
|
|
};
|
|
const FILTERED = [
|
|
{ id: 600, date: "2026-08-01", description: "Virement août", amount: -200 },
|
|
// The same transaction, also in the period: listed once, among the links.
|
|
{ id: 501, date: "2026-03-02", description: "Dépôt CELI mars", amount: -500 },
|
|
];
|
|
|
|
const onClose = vi.fn();
|
|
const onChanged = vi.fn();
|
|
const PROPS = {
|
|
accountId: 1,
|
|
accountName: "CELI",
|
|
categories: [],
|
|
initialFrom: "2026-07-01",
|
|
initialTo: "2026-09-30",
|
|
onClose,
|
|
onChanged,
|
|
};
|
|
|
|
const render = () => harness.render(() => LinkTransfersModal(PROPS));
|
|
|
|
const flush = async () => {
|
|
for (let i = 0; i < 10; i++) await Promise.resolve();
|
|
};
|
|
|
|
/** Mounts the window and lets both loads settle. */
|
|
async function mount() {
|
|
render();
|
|
harness.commit();
|
|
await flush();
|
|
return render();
|
|
}
|
|
|
|
const checkboxes = (tree: ReturnType<typeof render>) =>
|
|
find(tree, (el) => el.type === "input" && (el.props as TreeProps).type === "checkbox").map(
|
|
(el) => el.props as TreeProps & { checked: boolean; onChange: () => void; "aria-label": string },
|
|
);
|
|
|
|
const saveButton = (tree: ReturnType<typeof render>) => {
|
|
const buttons = find(tree, (el) => el.type === "button" && texts(el).includes("t(balance.transfers.modal.save)"));
|
|
expect(buttons).toHaveLength(1);
|
|
return buttons[0].props as TreeProps & { disabled: boolean; onClick: () => Promise<void> };
|
|
};
|
|
|
|
beforeAll(() => {
|
|
vi.stubGlobal("document", { body: {} });
|
|
});
|
|
|
|
afterAll(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
harness.clear();
|
|
for (const fn of [svc.getTransactionPage, svc.listAccountTransfers, svc.linkTransfer, svc.unlinkTransfer]) {
|
|
fn.mockReset();
|
|
}
|
|
onClose.mockReset();
|
|
onChanged.mockReset();
|
|
svc.getTransactionPage.mockResolvedValue({ rows: FILTERED, total: FILTERED.length });
|
|
svc.listAccountTransfers.mockResolvedValue([OLD_LINK]);
|
|
svc.linkTransfer.mockResolvedValue(1);
|
|
svc.unlinkTransfer.mockResolvedValue(undefined);
|
|
});
|
|
|
|
describe("LinkTransfersModal — linked transactions (#522)", () => {
|
|
it("loads the account's links with no date bound", async () => {
|
|
await mount();
|
|
expect(svc.listAccountTransfers).toHaveBeenCalledWith(1);
|
|
});
|
|
|
|
it("lists a link outside the filtered period first, checked, once, then the other rows unchecked", async () => {
|
|
const boxes = checkboxes(await mount());
|
|
expect(boxes.map((b) => [b["aria-label"], b.checked])).toEqual([
|
|
["select-501", true],
|
|
["select-600", false],
|
|
]);
|
|
});
|
|
|
|
it("marks the linked row and keeps its stored direction, with no flip button", async () => {
|
|
const tree = await mount();
|
|
const all = texts(tree);
|
|
expect(all).toContain("t(balance.transfers.modal.linkedBadge)");
|
|
expect(all.filter((s) => s === "t(balance.transfers.modal.linkedBadge)")).toHaveLength(1);
|
|
// Stored "out" although the negative amount would suggest "in".
|
|
expect(all).toContain("t(balance.transfers.direction.out)");
|
|
expect(find(tree, (el) => el.type === "button" && (el.props as TreeProps).title === "t(balance.transfers.modal.toggleDirection)")).toHaveLength(0);
|
|
});
|
|
|
|
it("keeps the save disabled while nothing changed", async () => {
|
|
expect(saveButton(await mount()).disabled).toBe(true);
|
|
});
|
|
|
|
it("unchecking a linked row and saving unlinks it, links nothing, reports the change and closes", async () => {
|
|
checkboxes(await mount())[0].onChange();
|
|
const save = saveButton(render());
|
|
expect(save.disabled).toBe(false);
|
|
await save.onClick();
|
|
|
|
expect(svc.unlinkTransfer).toHaveBeenCalledTimes(1);
|
|
expect(svc.unlinkTransfer).toHaveBeenCalledWith(1, 501);
|
|
expect(svc.linkTransfer).not.toHaveBeenCalled();
|
|
expect(onChanged).toHaveBeenCalledWith({ linked: 0, unlinked: 1 });
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("checking a new row links it with its suggested direction, and leaves the linked one alone", async () => {
|
|
checkboxes(await mount())[1].onChange();
|
|
await saveButton(render()).onClick();
|
|
|
|
expect(svc.linkTransfer).toHaveBeenCalledTimes(1);
|
|
expect(svc.linkTransfer).toHaveBeenCalledWith(1, 600, "in");
|
|
expect(svc.unlinkTransfer).not.toHaveBeenCalled();
|
|
expect(onChanged).toHaveBeenCalledWith({ linked: 1, unlinked: 0 });
|
|
});
|
|
|
|
it("unchecked then checked again: no change, nothing saved", async () => {
|
|
checkboxes(await mount())[0].onChange();
|
|
checkboxes(render())[0].onChange();
|
|
expect(saveButton(render()).disabled).toBe(true);
|
|
});
|
|
|
|
it("counts a row already unlinked elsewhere as done", async () => {
|
|
svc.unlinkTransfer.mockRejectedValue(new svc.BalanceServiceError("transfer_not_linked", "gone"));
|
|
checkboxes(await mount())[0].onChange();
|
|
await saveButton(render()).onClick();
|
|
expect(onChanged).toHaveBeenCalledWith({ linked: 0, unlinked: 1 });
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("reports a failed unlink with both counts and stays open", async () => {
|
|
svc.unlinkTransfer.mockRejectedValue(new Error("database is locked"));
|
|
const boxes = checkboxes(await mount());
|
|
boxes[0].onChange();
|
|
boxes[1].onChange();
|
|
await saveButton(render()).onClick();
|
|
|
|
expect(onChanged).toHaveBeenCalledWith({ linked: 1, unlinked: 0 });
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
const message = texts(render()).find((s) => s.includes("partialFailure"));
|
|
expect(message).toContain("t(balance.transfers.modal.partialFailure[done=1,total=2,linked=1,unlinked=0])");
|
|
expect(message).toContain("501: database is locked");
|
|
});
|
|
});
|
|
|
|
describe("diffTransferSelection (#522)", () => {
|
|
it("links the new rows and unlinks the linked rows left out", () => {
|
|
const linked = new Map([
|
|
[1, "in" as const],
|
|
[2, "out" as const],
|
|
]);
|
|
const selection = new Map([
|
|
[2, "out" as const],
|
|
[3, "in" as const],
|
|
]);
|
|
expect(diffTransferSelection(linked, selection)).toEqual({ toLink: [[3, "in"]], toUnlink: [1] });
|
|
});
|
|
});
|