Backend: create-collection happy path (write-through + editable) and the create resync-failure 422 (honest reconcile detail), mirroring the rename/remove tests. Frontend: extract notifyCollectionEditError (the 409->nameTaken vs generic-fallback mapping that was inline in the New-collection create()) and unit-test it; wire create() through it.
32 lines
1.4 KiB
TypeScript
32 lines
1.4 KiB
TypeScript
// @vitest-environment jsdom
|
|
//
|
|
// jsdom (not node): importing ../api pulls in i18n, which reads navigator.language at module load.
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
// Spy on the toast sink so we can assert exactly what each error maps to.
|
|
vi.mock("./notifications", () => ({ notify: vi.fn(() => 0) }));
|
|
|
|
import { HttpError } from "./api";
|
|
import { notify } from "./notifications";
|
|
import { notifyCollectionEditError } from "./actionErrors";
|
|
|
|
const notifyMock = vi.mocked(notify);
|
|
beforeEach(() => notifyMock.mockClear());
|
|
|
|
describe("notifyCollectionEditError", () => {
|
|
it("maps a backend 409 to the friendly nameTaken message (not the raw server detail)", () => {
|
|
notifyCollectionEditError(new HttpError(409, "already exists"), "EDIT_FAILED", "NAME_TAKEN");
|
|
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "NAME_TAKEN" });
|
|
});
|
|
|
|
it("falls back to the server detail for a non-409 HttpError", () => {
|
|
notifyCollectionEditError(new HttpError(500, "server boom"), "EDIT_FAILED", "NAME_TAKEN");
|
|
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "server boom" });
|
|
});
|
|
|
|
it("falls back to the generic edit-failed message for a non-HttpError", () => {
|
|
notifyCollectionEditError(new Error("network"), "EDIT_FAILED", "NAME_TAKEN");
|
|
expect(notifyMock).toHaveBeenCalledWith({ level: "error", message: "EDIT_FAILED" });
|
|
});
|
|
});
|