Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
594 lines
26 KiB
TypeScript
594 lines
26 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import type { CatalogRow } from "@/app/lib/types";
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const svc = vi.hoisted(() => ({
|
|
listCatalogRows: vi.fn(),
|
|
createCatalogRow: vi.fn(),
|
|
updateCatalogRow: vi.fn(),
|
|
toggleCatalogRowStatus: vi.fn(),
|
|
uploadCatalogImage: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminCatalog", () => ({
|
|
listCatalogRows: (list: string) => svc.listCatalogRows(list),
|
|
createCatalogRow: (list: string, p: unknown) => svc.createCatalogRow(list, p),
|
|
updateCatalogRow: (list: string, id: string, p: unknown) => svc.updateCatalogRow(list, id, p),
|
|
toggleCatalogRowStatus: (list: string, id: string) => svc.toggleCatalogRowStatus(list, id),
|
|
uploadCatalogImage: (list: string, input: unknown) => svc.uploadCatalogImage(list, input),
|
|
}));
|
|
|
|
import CatalogSetupPage from "./page";
|
|
|
|
const brandRows: CatalogRow[] = [
|
|
{ id: "1", code: "NIKE", name: "Nike", status: "Y" },
|
|
{ id: "2", code: "ADIDAS", name: "Adidas", status: "N" },
|
|
];
|
|
|
|
// Real backend wire shape (mobile-api's CatalogRow carries every list's stable key in the single
|
|
// `code` field — see spec "admin-catalog-api" / CatalogRow.java javadoc). Before the C1 fix the
|
|
// portal registry wrongly read `row.priceType`/`row.name` for these lists, which are never sent
|
|
// on the wire — these fixtures pin the REAL shape so the render/payload assertions below fail
|
|
// against the old `field:"priceType"`/`field:"name"` registry and pass once it reads `code`.
|
|
const priceTypeRows: CatalogRow[] = [
|
|
{ id: "3", code: "Retail", status: "Y" },
|
|
{ id: "4", code: "Wholesale", status: "Y" },
|
|
];
|
|
|
|
const categoryRows: CatalogRow[] = [{ id: "5", code: "Footwear", status: "Y" }];
|
|
|
|
function renderPage() {
|
|
return render(<CatalogSetupPage />);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReset();
|
|
auth.loading = false;
|
|
svc.listCatalogRows.mockReset();
|
|
svc.createCatalogRow.mockReset();
|
|
svc.updateCatalogRow.mockReset();
|
|
svc.toggleCatalogRowStatus.mockReset();
|
|
svc.uploadCatalogImage.mockReset();
|
|
svc.listCatalogRows.mockResolvedValue(brandRows);
|
|
});
|
|
|
|
describe("module gating (BO_CATALOG_SETUP)", () => {
|
|
it("renders the not-authorised state and no rows for a user lacking the module", async () => {
|
|
auth.hasModule.mockReturnValue(false);
|
|
renderPage();
|
|
|
|
expect(await screen.findByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(screen.queryByText("Nike")).not.toBeInTheDocument();
|
|
expect(svc.listCatalogRows).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("renders the management page for a user granted the module", async () => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
renderPage();
|
|
|
|
expect(await screen.findByText("Nike")).toBeInTheDocument();
|
|
expect(screen.queryByTestId("not-authorized")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("tabbed picker + registry-driven grid", () => {
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
});
|
|
|
|
it("lists exactly the three lists in the picker and loads brand first", async () => {
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
expect(svc.listCatalogRows).toHaveBeenCalledWith("brand");
|
|
for (const label of ["Brand", "Category", "Price types"]) {
|
|
expect(screen.getByRole("tab", { name: label })).toBeInTheDocument();
|
|
}
|
|
// Collection was removed (openspec/changes/bundle-by-sku-drop-collection) — Category is
|
|
// sufficient; it must not appear in the picker.
|
|
expect(screen.queryByRole("tab", { name: "Collection" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("puts the table inside the active tab's tabpanel", async () => {
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
expect(within(screen.getByRole("tabpanel")).getByRole("table")).toBeInTheDocument();
|
|
});
|
|
|
|
it("switching to Price types shows its columns, not Brand's", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(priceTypeRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("tab", { name: "Price types" }));
|
|
|
|
await waitFor(() => expect(svc.listCatalogRows).toHaveBeenCalledWith("priceType"));
|
|
expect(await screen.findByText("Retail")).toBeInTheDocument();
|
|
expect(screen.getByText("Price type")).toBeInTheDocument();
|
|
// No Brand rows / Code column under the Price types headers.
|
|
expect(screen.queryByText("Nike")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("Code")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("does not leave the previous list's rows on screen when the new fetch fails", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows
|
|
.mockResolvedValueOnce(brandRows)
|
|
.mockRejectedValueOnce(new Error("catalog service down"));
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("tab", { name: "Category" }));
|
|
|
|
expect(await screen.findByText("catalog service down")).toBeInTheDocument();
|
|
expect(screen.queryByText("Nike")).not.toBeInTheDocument();
|
|
expect(svc.listCatalogRows).toHaveBeenCalledWith("category");
|
|
});
|
|
|
|
it("switching lists closes any open dialog", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(categoryRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
expect(await screen.findByRole("dialog")).toBeInTheDocument();
|
|
|
|
// Radix's dialog overlay marks the rest of the page aria-hidden and inert while open, so a
|
|
// real pointer click can never reach the picker underneath — dispatch the DOM event directly
|
|
// to exercise the setListKey/setDialog(null) handler itself (the code path this scenario
|
|
// guards), the same way the underlying platform would deliver e.g. a keyboard-triggered tab
|
|
// change.
|
|
fireEvent.mouseDown(screen.getByText("Category"), { button: 0 });
|
|
|
|
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
|
});
|
|
|
|
it("surfaces a load failure inline", async () => {
|
|
svc.listCatalogRows.mockRejectedValueOnce(new Error("brand service down"));
|
|
renderPage();
|
|
expect(await screen.findByText("brand service down")).toBeInTheDocument();
|
|
});
|
|
|
|
// C1 regression guard: category/priceType rows carry their stable key in the wire field
|
|
// `code` (mobile-api's CatalogRow — see catalogTables.ts header). Before the fix the registry
|
|
// read `row.name`/`row.priceType`, which the backend never sends, so the grid rendered "—".
|
|
it("renders a category row's key under its Name column, not em-dash", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(categoryRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("tab", { name: "Category" }));
|
|
|
|
expect(await screen.findByText("Footwear")).toBeInTheDocument();
|
|
expect(screen.queryByText("—")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("renders a priceType row's key under its Price type column, not em-dash", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(priceTypeRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("tab", { name: "Price types" }));
|
|
|
|
expect(await screen.findByText("Retail")).toBeInTheDocument();
|
|
expect(await screen.findByText("Wholesale")).toBeInTheDocument();
|
|
expect(screen.queryByText("—")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("status badge + inline toggle", () => {
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
});
|
|
|
|
it("renders Active/Inactive badges from status, and offers no delete action", async () => {
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
expect(screen.getByText("Active")).toHaveAttribute("data-variant", "positive");
|
|
expect(screen.getByText("Inactive")).toHaveAttribute("data-variant", "neutral");
|
|
expect(screen.getByRole("heading", { level: 1, name: "Catalogue setup" })).toBeInTheDocument();
|
|
expect(screen.queryByRole("button", { name: /delete/i })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("deactivating an active row updates its badge inline", async () => {
|
|
const user = userEvent.setup();
|
|
svc.toggleCatalogRowStatus.mockResolvedValue({ ...brandRows[0], status: "N" });
|
|
svc.listCatalogRows
|
|
.mockResolvedValueOnce(brandRows)
|
|
.mockResolvedValueOnce([{ ...brandRows[0], status: "N" }, brandRows[1]]);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
const nikeRow = screen.getByText("Nike").closest("tr")!;
|
|
expect(within(nikeRow).getByText("Active")).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("button", { name: "More actions for NIKE" }));
|
|
const deactivate = await screen.findByRole("menuitem", { name: "Deactivate" });
|
|
expect(deactivate).toHaveAttribute("data-variant", "destructive");
|
|
await user.click(deactivate);
|
|
|
|
await waitFor(() =>
|
|
expect(svc.toggleCatalogRowStatus).toHaveBeenCalledWith("brand", "1"),
|
|
);
|
|
await waitFor(() => expect(svc.listCatalogRows).toHaveBeenCalledTimes(2));
|
|
// The row remains in the grid, and its badge now reflects the inactive status.
|
|
await waitFor(() =>
|
|
expect(within(screen.getByText("Nike").closest("tr")!).getByText("Inactive")).toBeInTheDocument(),
|
|
);
|
|
});
|
|
|
|
it("activating an inactive row shows an Activate action", async () => {
|
|
const user = userEvent.setup();
|
|
renderPage();
|
|
await screen.findByText("Adidas");
|
|
await user.click(screen.getByRole("button", { name: "More actions for ADIDAS" }));
|
|
expect(await screen.findByRole("menuitem", { name: "Activate" })).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("the add dialog", () => {
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
});
|
|
|
|
it("posts a brand create through createCatalogRow with the key field editable", async () => {
|
|
const user = userEvent.setup();
|
|
svc.createCatalogRow.mockResolvedValue({ id: "9", code: "PUMA", name: "Puma", status: "Y" });
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
const codeInput = within(dialog).getByLabelText("Code");
|
|
expect(codeInput).not.toHaveAttribute("readonly");
|
|
|
|
await user.type(codeInput, "PUMA");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Puma");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(svc.createCatalogRow).toHaveBeenCalledWith(
|
|
"brand",
|
|
expect.objectContaining({ code: "PUMA", name: "Puma" }),
|
|
),
|
|
);
|
|
// No status field is ever submitted from the dialog.
|
|
expect(svc.createCatalogRow.mock.calls[0][1]).not.toHaveProperty("status");
|
|
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
|
});
|
|
|
|
it("category's add dialog has only a Name field (no Code)", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(categoryRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
await user.click(screen.getByRole("tab", { name: "Category" }));
|
|
await screen.findByText("Footwear");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).getByLabelText("Name")).toBeInTheDocument();
|
|
expect(within(dialog).queryByLabelText("Code")).not.toBeInTheDocument();
|
|
});
|
|
|
|
// C1 regression guard: the Name field on category's dialog must post under the wire key
|
|
// `code` — mobile-api's AdminCatalogService.create() reads row.getCode() for every list,
|
|
// including category. Posting `{ name: "..." }` (the pre-fix payload) 422s downstream with
|
|
// "code is required."
|
|
it("creating a category posts a { code } payload to createCatalogRow, not { name }", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(categoryRows);
|
|
svc.createCatalogRow.mockResolvedValue({ id: "6", code: "Outerwear", status: "Y" });
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
await user.click(screen.getByRole("tab", { name: "Category" }));
|
|
await screen.findByText("Footwear");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Outerwear");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(svc.createCatalogRow).toHaveBeenCalledWith(
|
|
"category",
|
|
expect.objectContaining({ code: "Outerwear" }),
|
|
),
|
|
);
|
|
expect(svc.createCatalogRow.mock.calls[0][1]).not.toHaveProperty("name");
|
|
});
|
|
|
|
it("shows a duplicate-key 409 inline and keeps the dialog open (no false success)", async () => {
|
|
const user = userEvent.setup();
|
|
svc.createCatalogRow.mockRejectedValue(new Error("code already exists: NIKE"));
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.type(within(dialog).getByLabelText("Code"), "NIKE");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Nike");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
expect(await screen.findByText("code already exists: NIKE")).toBeInTheDocument();
|
|
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
expect(svc.listCatalogRows).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("shows a validation 422 inline and keeps the dialog open", async () => {
|
|
const user = userEvent.setup();
|
|
svc.createCatalogRow.mockRejectedValue(new Error("name must not be blank."));
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.type(within(dialog).getByLabelText("Code"), "PUMA");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
expect(await screen.findByText("name must not be blank.")).toBeInTheDocument();
|
|
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("the edit dialog", () => {
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
});
|
|
|
|
it("prefills from the row, locks the key field read-only, and saves via updateCatalogRow(id)", async () => {
|
|
const user = userEvent.setup();
|
|
svc.updateCatalogRow.mockResolvedValue({ ...brandRows[0], name: "Nike Inc" });
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: /edit nike/i }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).getByLabelText("Code")).toHaveValue("NIKE");
|
|
expect(within(dialog).getByLabelText("Name")).toHaveValue("Nike");
|
|
|
|
// Code is the stable key — read-only on edit, with a rename caveat.
|
|
expect(within(dialog).getByLabelText("Code")).toHaveAttribute("readonly");
|
|
expect(within(dialog).getByText(/cannot be renamed/i)).toBeInTheDocument();
|
|
// No status field in the dialog, even on edit.
|
|
expect(within(dialog).queryByLabelText("Status")).not.toBeInTheDocument();
|
|
|
|
await user.clear(within(dialog).getByLabelText("Name"));
|
|
await user.type(within(dialog).getByLabelText("Name"), "Nike Inc");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(svc.updateCatalogRow).toHaveBeenCalledWith(
|
|
"brand",
|
|
"1",
|
|
expect.objectContaining({ code: "NIKE", name: "Nike Inc" }),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("category's edit dialog locks Name (the key) read-only", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows.mockResolvedValueOnce(brandRows).mockResolvedValueOnce(categoryRows);
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
await user.click(screen.getByRole("tab", { name: "Category" }));
|
|
await screen.findByText("Footwear");
|
|
|
|
await user.click(screen.getByRole("button", { name: /edit footwear/i }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).getByLabelText("Name")).toHaveAttribute("readonly");
|
|
});
|
|
|
|
it("shows a conflict inline on edit too and keeps the dialog open", async () => {
|
|
const user = userEvent.setup();
|
|
svc.updateCatalogRow.mockRejectedValue(new Error("code already exists: ADIDAS"));
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
|
|
await user.click(screen.getByRole("button", { name: /edit nike/i }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.clear(within(dialog).getByLabelText("Name"));
|
|
await user.type(within(dialog).getByLabelText("Name"), "Nike Renamed");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
expect(await screen.findByText("code already exists: ADIDAS")).toBeInTheDocument();
|
|
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("catalogue images (add-catalog-images)", () => {
|
|
const png = () => new File(["x"], "logo.png", { type: "image/png" });
|
|
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
// jsdom has no object-URL support; the dialog previews a picked file through it.
|
|
URL.createObjectURL = vi.fn(() => "blob:preview");
|
|
URL.revokeObjectURL = vi.fn();
|
|
});
|
|
|
|
async function openAddBrand() {
|
|
const user = userEvent.setup();
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
return { user, dialog };
|
|
}
|
|
|
|
it("picking an image previews it and uploads nothing", async () => {
|
|
const { dialog } = await openAddBrand();
|
|
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), { target: { files: [png()] } });
|
|
|
|
expect(within(dialog).getByAltText("Image preview")).toHaveAttribute("src", "blob:preview");
|
|
expect(svc.uploadCatalogImage).not.toHaveBeenCalled();
|
|
expect(svc.createCatalogRow).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("submit uploads first, then creates with the returned imageUrl", async () => {
|
|
svc.uploadCatalogImage.mockResolvedValue({ url: "https://s3/b.png" });
|
|
svc.createCatalogRow.mockResolvedValue({ id: "9", code: "PUMA", name: "Puma", status: "Y" });
|
|
const { user, dialog } = await openAddBrand();
|
|
|
|
await user.type(within(dialog).getByLabelText("Code"), "PUMA");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Puma");
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), { target: { files: [png()] } });
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() =>
|
|
expect(svc.createCatalogRow).toHaveBeenCalledWith(
|
|
"brand",
|
|
expect.objectContaining({ code: "PUMA", name: "Puma", imageUrl: "https://s3/b.png" }),
|
|
),
|
|
);
|
|
expect(svc.uploadCatalogImage).toHaveBeenCalledWith(
|
|
"brand",
|
|
expect.objectContaining({ fileName: "logo.png", fileType: "image/png", base: expect.any(String) }),
|
|
);
|
|
expect(svc.uploadCatalogImage.mock.invocationCallOrder[0]).toBeLessThan(
|
|
svc.createCatalogRow.mock.invocationCallOrder[0],
|
|
);
|
|
});
|
|
|
|
it("a failed upload blocks the save and shows an upload error", async () => {
|
|
svc.uploadCatalogImage.mockRejectedValue(new Error("boom"));
|
|
const { user, dialog } = await openAddBrand();
|
|
|
|
await user.type(within(dialog).getByLabelText("Code"), "PUMA");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Puma");
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), { target: { files: [png()] } });
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
|
|
expect(await within(dialog).findByText(/Image upload failed/)).toBeInTheDocument();
|
|
expect(svc.createCatalogRow).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("retrying after a failed save reuses the uploaded image instead of uploading it again", async () => {
|
|
svc.uploadCatalogImage.mockResolvedValue({ url: "https://s3/b.png" });
|
|
svc.createCatalogRow
|
|
.mockRejectedValueOnce(new Error("Brand code already exists"))
|
|
.mockResolvedValueOnce({ id: "9", code: "PUMA2", name: "Puma", status: "Y" });
|
|
const { user, dialog } = await openAddBrand();
|
|
|
|
await user.type(within(dialog).getByLabelText("Code"), "PUMA");
|
|
await user.type(within(dialog).getByLabelText("Name"), "Puma");
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), { target: { files: [png()] } });
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
expect(await within(dialog).findByText(/already exists/)).toBeInTheDocument();
|
|
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(svc.createCatalogRow).toHaveBeenCalledTimes(2));
|
|
expect(svc.uploadCatalogImage).toHaveBeenCalledTimes(1);
|
|
expect(svc.createCatalogRow.mock.calls[1][1]).toEqual(expect.objectContaining({ imageUrl: "https://s3/b.png" }));
|
|
});
|
|
|
|
it("rejects a non-image or >2MB file inline without uploading", async () => {
|
|
const { dialog } = await openAddBrand();
|
|
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), {
|
|
target: { files: [new File(["x"], "a.pdf", { type: "application/pdf" })] },
|
|
});
|
|
expect(within(dialog).getByText(/Only PNG, JPEG, WebP or GIF/)).toBeInTheDocument();
|
|
|
|
const big = new File(["x"], "big.png", { type: "image/png" });
|
|
Object.defineProperty(big, "size", { value: 2 * 1024 * 1024 + 1 });
|
|
fireEvent.change(within(dialog).getByLabelText("Image"), { target: { files: [big] } });
|
|
expect(within(dialog).getByText(/2 MB or smaller/)).toBeInTheDocument();
|
|
|
|
expect(within(dialog).queryByAltText("Image preview")).not.toBeInTheDocument();
|
|
expect(svc.uploadCatalogImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("edit with no image change sends the stored imageUrl; Remove sends an empty string", async () => {
|
|
const user = userEvent.setup();
|
|
const rows: CatalogRow[] = [{ id: "5", code: "H", name: "Hamilton", status: "Y", imageUrl: "https://s3/h.png" }];
|
|
svc.listCatalogRows.mockResolvedValue(rows);
|
|
svc.updateCatalogRow.mockResolvedValue(rows[0]);
|
|
renderPage();
|
|
await screen.findByText("Hamilton");
|
|
|
|
await user.click(screen.getByRole("button", { name: "Edit H" }));
|
|
let dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).getByAltText("Image preview")).toHaveAttribute("src", "https://s3/h.png");
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
await waitFor(() =>
|
|
expect(svc.updateCatalogRow).toHaveBeenLastCalledWith(
|
|
"brand",
|
|
"5",
|
|
expect.objectContaining({ imageUrl: "https://s3/h.png" }),
|
|
),
|
|
);
|
|
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
|
|
|
await user.click(screen.getByRole("button", { name: "Edit H" }));
|
|
dialog = await screen.findByRole("dialog");
|
|
await user.click(within(dialog).getByRole("button", { name: "Remove image" }));
|
|
await user.click(within(dialog).getByRole("button", { name: "Save" }));
|
|
await waitFor(() =>
|
|
expect(svc.updateCatalogRow).toHaveBeenLastCalledWith("brand", "5", expect.objectContaining({ imageUrl: "" })),
|
|
);
|
|
expect(svc.uploadCatalogImage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows a thumbnail column for brand and no image picker for price types", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listCatalogRows
|
|
.mockResolvedValueOnce([
|
|
{ id: "5", code: "H", name: "Hamilton", status: "Y", imageUrl: "https://s3/h.png" },
|
|
{ id: "6", code: "B", name: "Babylon", status: "Y" },
|
|
])
|
|
.mockResolvedValueOnce(priceTypeRows);
|
|
renderPage();
|
|
await screen.findByText("Hamilton");
|
|
|
|
expect(screen.getByRole("columnheader", { name: "Image" })).toBeInTheDocument();
|
|
expect(screen.getByAltText("H logo")).toHaveAttribute("src", "https://s3/h.png");
|
|
expect(within(screen.getByText("Babylon").closest("tr")!).getByText("No image")).toBeInTheDocument();
|
|
|
|
await user.click(screen.getByRole("tab", { name: "Price types" }));
|
|
await screen.findByText("Retail");
|
|
expect(screen.queryByRole("columnheader", { name: "Image" })).not.toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Add row" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).queryByLabelText("Image")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
// unify-admin-listing-toolbar: "Code or name" search in the filter bar over the loaded rows.
|
|
describe("code or name search", () => {
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
});
|
|
|
|
it("filters the loaded rows by part of the code or name, ignoring case", async () => {
|
|
const user = userEvent.setup();
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
const bar = document.querySelector('[data-slot="filter-panel"]') as HTMLElement;
|
|
expect(within(bar).getByRole("group", { name: "Code or name" })).toBeInTheDocument();
|
|
|
|
await user.type(screen.getByRole("searchbox", { name: "Code or name" }), "adi");
|
|
await waitFor(() => expect(screen.queryByText("Nike")).not.toBeInTheDocument());
|
|
expect(screen.getByText("Adidas")).toBeInTheDocument();
|
|
});
|
|
|
|
it("says when nothing matches", async () => {
|
|
const user = userEvent.setup();
|
|
renderPage();
|
|
await screen.findByText("Nike");
|
|
await user.type(screen.getByRole("searchbox", { name: "Code or name" }), "zzz");
|
|
expect(await screen.findByText("No rows match your search")).toBeInTheDocument();
|
|
});
|
|
});
|