// useSaveToast unit test. The toast layer is stubbed so the assertions are // about WHEN a toast fires and with which copy, not about sonner's rendering. // i18n is mocked so `t` echoes the key. import { render } from "@testing-library/react"; import { describe, expect, it, vi, beforeEach } from "vitest"; const custom = vi.fn(); const dismiss = vi.fn(); vi.mock("sonner", () => ({ toast: { custom: (...args: unknown[]) => custom(...args), dismiss: (...args: unknown[]) => dismiss(...args) } })); vi.mock("react-i18next", () => ({ useTranslation: () => ({ // Mirrors i18next: an unknown key falls back to the supplied defaultValue. t: (key: string, options?: { defaultValue?: string; count?: number }) => key.startsWith("unknown.") ? (options?.defaultValue ?? key) : options?.count === undefined ? key : `${key}:${options.count}`, i18n: { language: "de" }, }), })); import { SAVE_TOAST_DURATION_MS, useSaveToast, type SaveActionResult, type UseSaveToastOptions } from "./use-save-toast"; function Harness({ result, options }: { result: SaveActionResult; options?: UseSaveToastOptions }) { useSaveToast(result, options); return null; } /** Render the toast body sonner was handed, so its copy can be asserted. */ function renderLastToast() { const factory = custom.mock.lastCall?.[0] as (id: string) => React.ReactElement; render(factory("toast-1")); } beforeEach(() => { custom.mockClear(); dismiss.mockClear(); }); describe("useSaveToast", () => { it("stays silent while there is no result", () => { render(); expect(custom).not.toHaveBeenCalled(); }); it("stays silent for a result the server has not confirmed", () => { // No `ok`, no `error` — a submission that produced data but no verdict must // never read as "Gespeichert". render(); expect(custom).not.toHaveBeenCalled(); }); it("reports a server-confirmed save in green", () => { render(); expect(custom).toHaveBeenCalledTimes(1); expect(custom.mock.lastCall?.[1]).toMatchObject({ duration: SAVE_TOAST_DURATION_MS }); renderLastToast(); expect(document.querySelector("[data-ui-id='ui.saveToast.success']")).not.toBeNull(); expect(document.body.textContent).toContain("shared.saveToast.successTitle"); }); it("reports a failure in red and names what went wrong", () => { render(); renderLastToast(); expect(document.querySelector("[data-ui-id='ui.saveToast.error']")).not.toBeNull(); expect(document.body.textContent).toContain("shared.saveToast.errorTitle"); expect(document.body.textContent).toContain("projects.error.scheduleConflictOverrideRequired"); }); it("accepts the errorKey alias used by newer action contracts", () => { render(); renderLastToast(); expect(document.querySelector("[data-ui-id='ui.saveToast.error']")).not.toBeNull(); expect(document.body.textContent).toContain("projects.error.scheduleConflictOverrideRequired"); }); it("falls back to plain text rather than printing an untranslated key", () => { render(); renderLastToast(); expect(document.body.textContent).toContain("shared.saveToast.errorFallback"); expect(document.body.textContent).not.toContain("unknown.error.key"); }); it("fires once per result, not once per render", () => { const result: SaveActionResult = { intent: "save", ok: true }; const view = render(); view.rerender(); view.rerender(); expect(custom).toHaveBeenCalledTimes(1); // A new submission is a new object, and does report again. view.rerender(); expect(custom).toHaveBeenCalledTimes(2); }); it("ignores results of intents the screen did not opt into", () => { render(); expect(custom).not.toHaveBeenCalled(); render(); expect(custom).toHaveBeenCalledTimes(1); }); it("can keep visible successes quiet while still reporting failures", () => { const view = render( ); expect(custom).not.toHaveBeenCalled(); view.rerender( ); expect(custom).toHaveBeenCalledTimes(1); renderLastToast(); expect(document.body.textContent).toContain("notifications.errors.deliveryRequired"); }); it("takes a screen-specific success headline", () => { render(); renderLastToast(); expect(document.body.textContent).toContain("crm.toast.contactSaved"); }); it("varies the headline by confirmed intent", () => { render( ); renderLastToast(); expect(document.body.textContent).toContain("emails.toast.archived"); expect(document.body.textContent).not.toContain("emails.toast.restored"); }); it("passes interpolation values from the server-confirmed result", () => { render(); renderLastToast(); expect(document.body.textContent).toContain("emails.toast.archived:3"); }); it("lets the action name the confirmation itself", () => { // Several routes already answer with a headline of their own; it has to win // over the screen default, otherwise every save reads "Gespeichert". render(); renderLastToast(); expect(document.body.textContent).toContain("hr.passwordSafe.feedback.vaultArchived"); expect(document.body.textContent).not.toContain("crm.toast.contactSaved"); }); it("falls back to plain text when a success key is unknown", () => { render(); renderLastToast(); expect(document.body.textContent).toContain("shared.saveToast.successTitle"); expect(document.body.textContent).not.toContain("unknown.toast.key"); }); it("carries no close button - it removes itself", () => { render(); renderLastToast(); expect(document.querySelector("[data-ui-id='ui.saveToast.success.dismiss']")).toBeNull(); expect(custom.mock.lastCall?.[1]).toMatchObject({ duration: SAVE_TOAST_DURATION_MS }); }); });