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(); 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(); 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(); 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(); 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(); 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(); expect(await within(card()).findByRole("alert")).toBeInTheDocument(); expect(within(card()).queryByRole("button", { name: "Save" })).toBeNull(); expect(within(card()).queryByRole("button", { name: "Add item" })).toBeNull(); }); });