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
78 lines
3.9 KiB
TypeScript
78 lines
3.9 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: the /menus page shows both menus, each saved on its own.
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const menus = vi.hoisted(() => ({ getMenu: vi.fn(), saveMenu: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminMenus", () => menus);
|
|
const collections = vi.hoisted(() => ({ listCollections: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminCollections", () => collections);
|
|
const pages = vi.hoisted(() => ({ listPages: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminPages", () => pages);
|
|
|
|
import MenusPage from "./page";
|
|
|
|
beforeEach(() => {
|
|
auth.hasModule.mockReturnValue(true);
|
|
menus.getMenu.mockReset();
|
|
menus.saveMenu.mockReset();
|
|
menus.getMenu.mockImplementation(async (handle: string) => ({ handle, items: [{ label: handle === "main" ? "Shop by age" : "Help", url: "/x" }] }));
|
|
menus.saveMenu.mockImplementation(async (handle: string) => ({ handle, items: [] }));
|
|
collections.listCollections.mockResolvedValue([{ id: 1, slug: "pregnancy", title: "Pregnancy", type: "MANUAL", status: "Y", productCount: 0 }]);
|
|
pages.listPages.mockResolvedValue([{ id: 2, slug: "contact", title: "Contact us", status: "Y" }]);
|
|
});
|
|
|
|
describe("/menus", () => {
|
|
it("shows both menus and saves each on its own", async () => {
|
|
render(<MenusPage />);
|
|
|
|
const main = await screen.findByRole("region", { name: "Main menu" });
|
|
const footer = screen.getByRole("region", { name: "Footer menu" });
|
|
expect(await within(main).findByDisplayValue("Shop by age")).toBeInTheDocument();
|
|
expect(await within(footer).findByDisplayValue("Help")).toBeInTheDocument();
|
|
|
|
await userEvent.click(within(footer).getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(menus.saveMenu).toHaveBeenCalledTimes(1));
|
|
expect(menus.saveMenu.mock.calls[0][0]).toBe("footer");
|
|
});
|
|
|
|
it("offers collections and pages in the link picker", async () => {
|
|
render(<MenusPage />);
|
|
const main = await screen.findByRole("region", { name: "Main menu" });
|
|
await within(main).findByDisplayValue("Shop by age");
|
|
|
|
const picker = within(main).getAllByLabelText("Pick a collection or page")[0];
|
|
await waitFor(() => expect(within(picker).getByRole("option", { name: "Collection: Pregnancy" })).toHaveValue("/collections/pregnancy"));
|
|
expect(within(picker).getByRole("option", { name: "Page: Contact us" })).toHaveValue("/contact");
|
|
});
|
|
|
|
// Task 7 review: one failed list keeps the other in the picker and both menus usable.
|
|
it("keeps the pages in the picker when collections fail to load", async () => {
|
|
collections.listCollections.mockRejectedValue(new Error("down"));
|
|
render(<MenusPage />);
|
|
const main = await screen.findByRole("region", { name: "Main menu" });
|
|
await within(main).findByDisplayValue("Shop by age");
|
|
expect(screen.getByRole("region", { name: "Footer menu" })).toBeInTheDocument();
|
|
|
|
const picker = within(main).getAllByLabelText("Pick a collection or page")[0];
|
|
await waitFor(() => expect(within(picker).getByRole("option", { name: "Page: Contact us" })).toBeInTheDocument());
|
|
expect(within(picker).queryByRole("option", { name: "Collection: Pregnancy" })).toBeNull();
|
|
});
|
|
|
|
// Task 7 review: without BO_MENUS the page shows neither menu and reads nothing.
|
|
it("shows no menu without the BO_MENUS module", async () => {
|
|
auth.hasModule.mockReturnValue(false);
|
|
render(<MenusPage />);
|
|
|
|
await waitFor(() => expect(screen.queryByRole("region", { name: "Main menu" })).toBeNull());
|
|
expect(screen.queryByRole("region", { name: "Footer menu" })).toBeNull();
|
|
expect(menus.getMenu).not.toHaveBeenCalled();
|
|
});
|
|
});
|