Simpl-Resultat/src/components/budget/TemplateActions.delete.test.ts
le king fu 41f3b15f4d test: pin four cases the reviews of the stack left unpinned
Non-blocking suggestions of the independent reviews of PRs #490 to #493,
applied in a follow-up link above #493 (Max's decision of 2026-10-24):

- locales.keys.test.ts: a variable name that differs by its case only is a
  gap (`{{version}}` against `{{Version}}`, review 27037, H8); the header says
  that `{{- name}}` and `{{name}}` render alike with `escapeValue: false`.
- db.test.ts: a `)` glued to a word ends no path
  (`C:\Users\jean\Projet(2024)final\x.db`, review 27073, R10).
- TemplateActions.delete.test.ts: three templates, the middle one deleted, so
  a handler bound to the first or the last fails (review 27088, R11).
- ProfileSwitcher.wiring.test.ts: a locked profile that has a PIN opens the
  upsell, never the PIN entry; in Base it asks for the PIN (review 27084, R5).
- elementTree.ts: an absence is proved with `find`, never with `holding`
  (review 27088, S2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 09:09:36 -04:00

106 lines
4.2 KiB
TypeScript

// TemplateActions — deleting a budget template waits for the confirmation (#421, #467).
//
// Without its `await`, `if (await confirmAction(…))` would test a Promise,
// always truthy: the template would go before the user answered, and "Cancel"
// would stop nothing. `native-dialog-guard.test.ts` checks the `await` in the
// source; this test checks what the handler does with the answer. The
// component runs as a plain function under the shared hook harness (#409). The
// test opens the template list by its button, as the user does, renders again,
// then calls the `onClick` of a template's delete button with a fake event,
// the confirmation answered by a mocked `confirmAction`. No slot is preset, so
// the test does not depend on the order of the component's hooks.
//
// Never `harness.commit()`: the component's effect calls
// `document.addEventListener`, and vitest's node environment has no `document`.
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("react", async (orig) => (await import("../../test/hookHarness")).fakeReact(await orig()));
vi.mock("react-i18next", async () => (await import("../../test/i18nMock")).i18nMock());
const mocks = vi.hoisted(() => ({
confirmAction: vi.fn<(message: string) => Promise<boolean>>(),
}));
vi.mock("../../services/confirmService", () => ({ confirmAction: mocks.confirmAction }));
import { BookTemplate, Trash2 } from "lucide-react";
import { hookHarness } from "../../test/hookHarness";
import { holding } from "../../test/elementTree";
import TemplateActions from "./TemplateActions";
import type { BudgetTemplate } from "../../shared/types";
const harness = hookHarness();
const SUMMER: BudgetTemplate = { id: 4, name: "Été", created_at: "" };
const WINTER: BudgetTemplate = { id: 9, name: "Hiver", created_at: "" };
const SPRING: BudgetTemplate = { id: 6, name: "Printemps", created_at: "" };
const CONFIRM = "t(budget.deleteTemplateConfirm)";
let onDelete = vi.fn<(templateId: number) => void>();
/** One render of the mounted component. */
const render = () =>
harness.render(() =>
TemplateActions({
templates: [SUMMER, WINTER, SPRING],
onApply: vi.fn(),
onApplyAllMonths: vi.fn(),
onSave: vi.fn(),
onDelete,
}),
);
/** Opens the template list by its button, then renders the open list. */
function openList() {
const toggles = holding(render(), BookTemplate);
expect(toggles).toHaveLength(1);
(toggles[0].props.onClick as () => void)();
return render();
}
type FakeEvent = { stopPropagation: ReturnType<typeof vi.fn> };
/**
* Clicks the delete button of the middle template of three and waits for the
* handler: a handler bound to the first or to the last template would delete
* another one. The event's propagation must stop before the answer: the click
* would otherwise reach the row, which selects the template.
*/
async function clickDeleteOfWinter(): Promise<FakeEvent> {
const buttons = holding(openList(), Trash2);
expect(buttons).toHaveLength(3);
const event: FakeEvent = { stopPropagation: vi.fn() };
const handled = (buttons[1].props.onClick as (e: FakeEvent) => Promise<void>)(event);
expect(event.stopPropagation).toHaveBeenCalledTimes(1);
await handled;
return event;
}
beforeEach(() => {
harness.clear();
mocks.confirmAction.mockReset();
onDelete = vi.fn<(templateId: number) => void>();
});
describe("TemplateActions template deletion (#421, #467)", () => {
it("keeps the template when the confirmation is refused, and stops the click there", async () => {
mocks.confirmAction.mockResolvedValue(false);
const event = await clickDeleteOfWinter();
expect(mocks.confirmAction).toHaveBeenCalledWith(CONFIRM);
expect(onDelete).not.toHaveBeenCalled();
expect(event.stopPropagation).toHaveBeenCalledTimes(1);
});
it("deletes the template of the button once confirmed, and stops the click there", async () => {
mocks.confirmAction.mockResolvedValue(true);
const event = await clickDeleteOfWinter();
expect(mocks.confirmAction).toHaveBeenCalledWith(CONFIRM);
expect(onDelete).toHaveBeenCalledTimes(1);
expect(onDelete).toHaveBeenCalledWith(WINTER.id);
expect(event.stopPropagation).toHaveBeenCalledTimes(1);
});
});