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
130 lines
7.0 KiB
TypeScript
130 lines
7.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
// add-storefront-menus Task 7: one menu card (specs/admin-portal-menus-ui "Each Menu Is Edited As A Two-Level List").
|
|
|
|
const menus = vi.hoisted(() => ({ getMenu: vi.fn(), saveMenu: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminMenus", () => menus);
|
|
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { MenuEditor } from "./MenuEditor";
|
|
|
|
const OPTIONS = [
|
|
{ label: "Collection: Pregnancy", url: "/collections/pregnancy" },
|
|
{ label: "Page: Contact us", url: "/contact" },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
menus.getMenu.mockReset();
|
|
menus.saveMenu.mockReset();
|
|
menus.getMenu.mockResolvedValue({ handle: "main", items: [] });
|
|
menus.saveMenu.mockResolvedValue({ handle: "main", items: [] });
|
|
});
|
|
|
|
const card = () => screen.getByRole("region", { name: "Main menu" });
|
|
const item = (n: number) => within(card()).getByRole("group", { name: `Item ${n}` });
|
|
const child = (n: number, m: number) => within(card()).getByRole("group", { name: `Item ${n} link ${m}` });
|
|
|
|
describe("MenuEditor", () => {
|
|
it("loads a menu and saves it back unchanged", async () => {
|
|
menus.getMenu.mockResolvedValue({
|
|
handle: "main",
|
|
items: [{ label: "Shop by age", children: [{ label: "Pregnancy", url: "/collections/pregnancy" }] }, { label: "Gift cards", url: "/gift-cards" }],
|
|
});
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
|
|
expect(await within(card()).findByDisplayValue("Shop by age")).toBeInTheDocument();
|
|
expect(within(child(1, 1)).getByLabelText("Link")).toHaveValue("/collections/pregnancy");
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() => expect(menus.saveMenu).toHaveBeenCalledTimes(1));
|
|
expect(menus.saveMenu).toHaveBeenCalledWith("main", {
|
|
items: [{ label: "Shop by age", children: [{ label: "Pregnancy", url: "/collections/pregnancy" }] }, { label: "Gift cards", url: "/gift-cards" }],
|
|
});
|
|
expect(await within(card()).findByRole("status")).toHaveTextContent("Menu saved");
|
|
});
|
|
|
|
it("adds an item with a link picked from a collection", async () => {
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
await waitFor(() => expect(menus.getMenu).toHaveBeenCalledWith("main"));
|
|
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Add item" }));
|
|
await userEvent.type(within(item(1)).getByLabelText("Label"), "Shop by age");
|
|
await userEvent.click(within(item(1)).getByRole("button", { name: "Add link" }));
|
|
await userEvent.type(within(child(1, 1)).getByLabelText("Label"), "Pregnancy");
|
|
await userEvent.selectOptions(within(child(1, 1)).getByLabelText("Pick a collection or page"), "/collections/pregnancy");
|
|
expect(within(child(1, 1)).getByLabelText("Link")).toHaveValue("/collections/pregnancy");
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
|
|
await waitFor(() => expect(menus.saveMenu).toHaveBeenCalledTimes(1));
|
|
expect(menus.saveMenu).toHaveBeenCalledWith("main", {
|
|
items: [{ label: "Shop by age", children: [{ label: "Pregnancy", url: "/collections/pregnancy" }] }],
|
|
});
|
|
});
|
|
|
|
it("moves and removes items and links", async () => {
|
|
menus.getMenu.mockResolvedValue({
|
|
handle: "main",
|
|
items: [
|
|
{ label: "A", url: "/a", children: [{ label: "A1", url: "/a1" }, { label: "A2", url: "/a2" }] },
|
|
{ label: "B", url: "/b" },
|
|
],
|
|
});
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
await within(card()).findByDisplayValue("A");
|
|
|
|
await userEvent.click(within(item(2)).getByRole("button", { name: "Move item up" }));
|
|
expect(within(item(1)).getAllByLabelText("Label")[0]).toHaveValue("B");
|
|
await userEvent.click(within(child(2, 2)).getByRole("button", { name: "Move link up" }));
|
|
expect(within(child(2, 1)).getByLabelText("Label")).toHaveValue("A2");
|
|
await userEvent.click(within(child(2, 1)).getByRole("button", { name: "Remove link" }));
|
|
expect(within(child(2, 1)).getByLabelText("Label")).toHaveValue("A1");
|
|
await userEvent.click(within(item(1)).getByRole("button", { name: "Remove item" }));
|
|
expect(within(card()).getAllByRole("group", { name: /^Item \d+$/ })).toHaveLength(1);
|
|
});
|
|
|
|
it("shows a rejected link under its field and keeps the input", async () => {
|
|
menus.getMenu.mockResolvedValue({ handle: "main", items: [{ label: "Age", children: [{ label: "Bad", url: "javascript:alert(1)" }] }] });
|
|
menus.saveMenu.mockRejectedValue(new ApiError("items[0].children[0].url: must start with a single / or https://", 422));
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
await within(card()).findByDisplayValue("Age");
|
|
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
|
|
expect(await within(child(1, 1)).findByText("must start with a single / or https://")).toBeInTheDocument();
|
|
expect(within(child(1, 1)).getByLabelText("Link")).toHaveValue("javascript:alert(1)");
|
|
expect(within(card()).queryByRole("status")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("puts an error it has no field for in the summary, and clears field errors on move", async () => {
|
|
menus.getMenu.mockResolvedValue({ handle: "main", items: [{ label: "A", url: "/a" }, { label: "B", url: "/b" }] });
|
|
menus.saveMenu.mockRejectedValueOnce(new ApiError("items: at most 20 items", 422));
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
await within(card()).findByDisplayValue("A");
|
|
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
expect(await within(card()).findByText("at most 20 items")).toBeInTheDocument();
|
|
|
|
menus.saveMenu.mockRejectedValueOnce(new ApiError("items[1].label: is required, at most 60 characters", 422));
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
expect(await within(item(2)).findByText("is required, at most 60 characters")).toBeInTheDocument();
|
|
await userEvent.click(within(item(2)).getByRole("button", { name: "Move item up" }));
|
|
expect(within(card()).queryByText("is required, at most 60 characters")).not.toBeInTheDocument();
|
|
|
|
menus.saveMenu.mockRejectedValueOnce(new ApiError("items[0].children[0]: must not be null", 422));
|
|
await userEvent.click(within(card()).getByRole("button", { name: "Save" }));
|
|
expect(await within(card()).findByRole("alert")).toHaveTextContent("items[0].children[0]: must not be null");
|
|
});
|
|
|
|
// Task 7 review: a failed read must never show an editable empty menu (saving it would wipe the live menu).
|
|
it("shows an error and no Save when the menu cannot be read", async () => {
|
|
menus.getMenu.mockRejectedValue(new ApiError("down", 500));
|
|
render(<MenuEditor handle="main" options={OPTIONS} />);
|
|
|
|
expect(await within(card()).findByRole("alert")).toBeInTheDocument();
|
|
expect(within(card()).queryByRole("button", { name: "Save" })).toBeNull();
|
|
expect(within(card()).queryByRole("button", { name: "Add item" })).toBeNull();
|
|
});
|
|
});
|