test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,278 @@
|
||||
import { fireEvent,render,screen,waitFor } from "@testing-library/react";
|
||||
import { toast } from "react-hot-toast";
|
||||
import { MemoryRouter } from "react-router-dom";
|
||||
import { beforeEach,describe,expect,it,vi } from "vitest";
|
||||
import api from "../services/api";
|
||||
import MenuManager from "./MenuManager";
|
||||
|
||||
vi.mock("../services/api", () => ({
|
||||
default: {
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
put: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("react-hot-toast", () => ({
|
||||
toast: {
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
describe("MenuManager Page Component", () => {
|
||||
const mockMenuItems = [
|
||||
{
|
||||
id: "menu-1",
|
||||
menuType: "HEADER",
|
||||
label: "فروشگاه تخصصی",
|
||||
href: "/shop",
|
||||
parentId: null,
|
||||
order: 1,
|
||||
isActive: true,
|
||||
isExternal: false,
|
||||
icon: "🛒",
|
||||
badge: "جدید",
|
||||
children: [
|
||||
{
|
||||
id: "menu-1-sub",
|
||||
menuType: "HEADER",
|
||||
label: "مکملهای سگ",
|
||||
href: "/shop/dog-supplements",
|
||||
parentId: "menu-1",
|
||||
order: 1,
|
||||
isActive: true,
|
||||
isExternal: false,
|
||||
icon: null,
|
||||
badge: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: "menu-2",
|
||||
menuType: "HEADER",
|
||||
label: "مقالات و دانشنامه",
|
||||
href: "/wiki",
|
||||
parentId: null,
|
||||
order: 2,
|
||||
isActive: false,
|
||||
isExternal: false,
|
||||
icon: "📚",
|
||||
badge: null,
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
id: "menu-3",
|
||||
menuType: "FOOTER_COL1",
|
||||
label: "درباره کنینا",
|
||||
href: "/about-us",
|
||||
parentId: null,
|
||||
order: 1,
|
||||
isActive: true,
|
||||
isExternal: false,
|
||||
icon: null,
|
||||
badge: null,
|
||||
children: [],
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(api.get).mockResolvedValue({ data: mockMenuItems });
|
||||
});
|
||||
|
||||
const renderComponent = (initialEntries = ["/menu"]) =>
|
||||
render(
|
||||
<MemoryRouter initialEntries={initialEntries}>
|
||||
<MenuManager />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
it("renders menu tabs and current items for HEADER by default", async () => {
|
||||
renderComponent();
|
||||
|
||||
expect(screen.getByText("مدیریت منو")).toBeInTheDocument();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
expect(screen.getByText("مقالات و دانشنامه")).toBeInTheDocument();
|
||||
expect(screen.getByText("مکملهای سگ")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
expect(screen.getByText("/shop")).toBeInTheDocument();
|
||||
expect(screen.getByText("جدید")).toBeInTheDocument();
|
||||
expect(screen.getByText("🛒")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("switches tabs and displays items corresponding to selected tab", async () => {
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const footerTabBtn = screen.getByRole("button", { name: "🔗 فوتر — ستون ۱" });
|
||||
fireEvent.click(footerTabBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("درباره کنینا")).toBeInTheDocument();
|
||||
expect(screen.getByText("/about-us")).toBeInTheDocument();
|
||||
expect(screen.queryByText("فروشگاه تخصصی")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("shows empty state when a menu category has no items", async () => {
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const footerCol3Tab = screen.getByRole("button", { name: "🔗 فوتر — ستون ۳" });
|
||||
fireEvent.click(footerCol3Tab);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("هیچ آیتمی در این منو وجود ندارد")).toBeInTheDocument();
|
||||
expect(screen.getByText("اولین آیتم را اضافه کنید")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("toggles item active status successfully", async () => {
|
||||
vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true } });
|
||||
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const hideButtons = screen.getAllByTitle("مخفی کردن");
|
||||
fireEvent.click(hideButtons[0]);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.put).toHaveBeenCalledWith("/menu/menu-1", { isActive: false });
|
||||
expect(toast.success).toHaveBeenCalledWith("مخفی شد");
|
||||
});
|
||||
});
|
||||
|
||||
it("creates a new menu item via modal submission", async () => {
|
||||
vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true } });
|
||||
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const createButton = screen.getByRole("button", { name: "آیتم جدید" });
|
||||
fireEvent.click(createButton);
|
||||
|
||||
expect(screen.getByText("افزودن آیتم جدید")).toBeInTheDocument();
|
||||
|
||||
const titleInput = screen.getByPlaceholderText("مثال: فروشگاه تخصصی");
|
||||
const hrefInput = screen.getByPlaceholderText("مثال: /shop یا https://...");
|
||||
|
||||
fireEvent.change(titleInput, { target: { value: "تماس با ما" } });
|
||||
fireEvent.change(hrefInput, { target: { value: "/contact" } });
|
||||
|
||||
const saveButton = screen.getByRole("button", { name: "ذخیره" });
|
||||
fireEvent.click(saveButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith(
|
||||
"/menu",
|
||||
expect.objectContaining({
|
||||
menuType: "HEADER",
|
||||
label: "تماس با ما",
|
||||
href: "/contact",
|
||||
parentId: null,
|
||||
isActive: true,
|
||||
})
|
||||
);
|
||||
expect(toast.success).toHaveBeenCalledWith("آیتم جدید اضافه شد");
|
||||
});
|
||||
});
|
||||
|
||||
it("validates required fields before submitting form", async () => {
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const createButton = screen.getByRole("button", { name: "آیتم جدید" });
|
||||
fireEvent.click(createButton);
|
||||
|
||||
const form = document.querySelector("#menuForm");
|
||||
expect(form).toBeInTheDocument();
|
||||
fireEvent.submit(form!);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(toast.error).toHaveBeenCalledWith("عنوان و لینک اجباری هستند");
|
||||
expect(api.post).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("edits an existing menu item via modal", async () => {
|
||||
vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true } });
|
||||
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const editBtn = document.querySelector(".text-blue-600.hover\\:bg-blue-50");
|
||||
expect(editBtn).toBeInTheDocument();
|
||||
fireEvent.click(editBtn!);
|
||||
|
||||
expect(screen.getByText("ویرایش آیتم منو")).toBeInTheDocument();
|
||||
|
||||
const titleInput = screen.getByPlaceholderText("مثال: فروشگاه تخصصی");
|
||||
expect(titleInput).toHaveValue("فروشگاه تخصصی");
|
||||
|
||||
fireEvent.change(titleInput, { target: { value: "فروشگاه محصولات اورجینال" } });
|
||||
|
||||
const saveButton = screen.getByRole("button", { name: "ذخیره" });
|
||||
fireEvent.click(saveButton);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.put).toHaveBeenCalledWith(
|
||||
"/menu/menu-1",
|
||||
expect.objectContaining({
|
||||
label: "فروشگاه محصولات اورجینال",
|
||||
href: "/shop",
|
||||
})
|
||||
);
|
||||
expect(toast.success).toHaveBeenCalledWith("آیتم منو بروزرسانی شد");
|
||||
});
|
||||
});
|
||||
|
||||
it("deletes a menu item after modal confirmation", async () => {
|
||||
vi.mocked(api.delete).mockResolvedValueOnce({ data: { success: true } });
|
||||
|
||||
renderComponent();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("فروشگاه تخصصی")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
const deleteBtn = document.querySelector(".text-red-500.hover\\:bg-red-50");
|
||||
expect(deleteBtn).toBeInTheDocument();
|
||||
fireEvent.click(deleteBtn!);
|
||||
|
||||
expect(screen.getByText("حذف آیتم منو")).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText("آیا از حذف این آیتم اطمینان دارید؟ آیتمهای زیرمجموعه نیز حذف خواهند شد.")
|
||||
).toBeInTheDocument();
|
||||
|
||||
const confirmBtn = screen.getByRole("button", { name: "تایید" });
|
||||
fireEvent.click(confirmBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.delete).toHaveBeenCalledWith("/menu/menu-1");
|
||||
expect(toast.success).toHaveBeenCalledWith("آیتم حذف شد");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user