Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,176 @@
|
||||
// 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(<Harness result={null} />);
|
||||
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(<Harness result={{ intent: "save" }} />);
|
||||
expect(custom).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports a server-confirmed save in green", () => {
|
||||
render(<Harness result={{ intent: "save", ok: true }} />);
|
||||
|
||||
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(<Harness result={{ intent: "save", error: "projects.error.scheduleConflictOverrideRequired" }} />);
|
||||
|
||||
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(<Harness result={{ intent: "save", errorKey: "projects.error.scheduleConflictOverrideRequired" }} />);
|
||||
|
||||
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(<Harness result={{ error: "unknown.error.key" }} />);
|
||||
|
||||
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(<Harness result={result} />);
|
||||
view.rerender(<Harness result={result} />);
|
||||
view.rerender(<Harness result={result} />);
|
||||
|
||||
expect(custom).toHaveBeenCalledTimes(1);
|
||||
|
||||
// A new submission is a new object, and does report again.
|
||||
view.rerender(<Harness result={{ intent: "save", ok: true }} />);
|
||||
expect(custom).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("ignores results of intents the screen did not opt into", () => {
|
||||
render(<Harness result={{ intent: "search", ok: true }} options={{ intents: ["save"] }} />);
|
||||
expect(custom).not.toHaveBeenCalled();
|
||||
|
||||
render(<Harness result={{ intent: "save", ok: true }} options={{ intents: ["save"] }} />);
|
||||
expect(custom).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("can keep visible successes quiet while still reporting failures", () => {
|
||||
const view = render(
|
||||
<Harness result={{ intent: "mark-read", ok: true }} options={{ reportSuccess: false }} />
|
||||
);
|
||||
expect(custom).not.toHaveBeenCalled();
|
||||
|
||||
view.rerender(
|
||||
<Harness
|
||||
result={{ intent: "mark-read", error: "notifications.errors.deliveryRequired" }}
|
||||
options={{ reportSuccess: false }}
|
||||
/>
|
||||
);
|
||||
expect(custom).toHaveBeenCalledTimes(1);
|
||||
renderLastToast();
|
||||
expect(document.body.textContent).toContain("notifications.errors.deliveryRequired");
|
||||
});
|
||||
|
||||
it("takes a screen-specific success headline", () => {
|
||||
render(<Harness result={{ ok: true }} options={{ successKey: "crm.toast.contactSaved" }} />);
|
||||
|
||||
renderLastToast();
|
||||
expect(document.body.textContent).toContain("crm.toast.contactSaved");
|
||||
});
|
||||
|
||||
it("varies the headline by confirmed intent", () => {
|
||||
render(
|
||||
<Harness
|
||||
result={{ intent: "archive", ok: true }}
|
||||
options={{ successKeyByIntent: { archive: "emails.toast.archived", restore: "emails.toast.restored" } }}
|
||||
/>
|
||||
);
|
||||
|
||||
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(<Harness result={{ ok: true, messageKey: "emails.toast.archived", messageValues: { count: 3 } }} />);
|
||||
|
||||
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(<Harness result={{ ok: true, messageKey: "hr.passwordSafe.feedback.vaultArchived" }} options={{ successKey: "crm.toast.contactSaved" }} />);
|
||||
|
||||
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(<Harness result={{ ok: true, messageKey: "unknown.toast.key" }} />);
|
||||
|
||||
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(<Harness result={{ ok: true }} />);
|
||||
renderLastToast();
|
||||
|
||||
expect(document.querySelector("[data-ui-id='ui.saveToast.success.dismiss']")).toBeNull();
|
||||
expect(custom.mock.lastCall?.[1]).toMatchObject({ duration: SAVE_TOAST_DURATION_MS });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user