- 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.
188 lines
5.2 KiB
TypeScript
188 lines
5.2 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { MenuController } from "./menu.controller";
|
|
import { MenuService } from "./menu.service";
|
|
|
|
describe("MenuController", () => {
|
|
let controller: MenuController;
|
|
let service: MenuService;
|
|
|
|
const mockMenuItems = [
|
|
{
|
|
id: "menu-item-1",
|
|
menuType: "HEADER",
|
|
label: "محصولات مکمل سگ",
|
|
href: "/category/dog-supplements",
|
|
parentId: null,
|
|
order: 1,
|
|
isActive: true,
|
|
isExternal: false,
|
|
icon: "dog-icon",
|
|
badge: "جدید",
|
|
children: [],
|
|
},
|
|
{
|
|
id: "menu-item-2",
|
|
menuType: "HEADER",
|
|
label: "مشاوره آنلاین هوشمند",
|
|
href: "/smart-advisor",
|
|
parentId: null,
|
|
order: 2,
|
|
isActive: true,
|
|
isExternal: false,
|
|
icon: "sparkles",
|
|
badge: null,
|
|
children: [],
|
|
},
|
|
];
|
|
|
|
const mockMenuService = {
|
|
findByType: jest.fn(),
|
|
findAllAdmin: jest.fn(),
|
|
create: jest.fn(),
|
|
reorder: jest.fn(),
|
|
update: jest.fn(),
|
|
remove: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [MenuController],
|
|
providers: [
|
|
{
|
|
provide: MenuService,
|
|
useValue: mockMenuService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<MenuController>(MenuController);
|
|
service = module.get<MenuService>(MenuService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("findByType (Query param)", () => {
|
|
it("should return menu items for given type", async () => {
|
|
mockMenuService.findByType.mockResolvedValue(mockMenuItems);
|
|
|
|
const result = await controller.findByType("HEADER");
|
|
|
|
expect(service.findByType).toHaveBeenCalledWith("HEADER");
|
|
expect(result).toEqual(mockMenuItems);
|
|
expect(result).toHaveLength(2);
|
|
});
|
|
});
|
|
|
|
describe("findByTypeParam (Path param)", () => {
|
|
it("should return menu items for given path param type", async () => {
|
|
mockMenuService.findByType.mockResolvedValue([mockMenuItems[0]]);
|
|
|
|
const result = await controller.findByTypeParam("FOOTER_COL1");
|
|
|
|
expect(service.findByType).toHaveBeenCalledWith("FOOTER_COL1");
|
|
expect(result).toEqual([mockMenuItems[0]]);
|
|
});
|
|
});
|
|
|
|
describe("findAllAdmin", () => {
|
|
it("should return all menu items when type query is provided", async () => {
|
|
mockMenuService.findAllAdmin.mockResolvedValue(mockMenuItems);
|
|
|
|
const result = await controller.findAllAdmin("HEADER");
|
|
|
|
expect(service.findAllAdmin).toHaveBeenCalledWith("HEADER");
|
|
expect(result).toEqual(mockMenuItems);
|
|
});
|
|
|
|
it("should return all menu items when type query is undefined", async () => {
|
|
mockMenuService.findAllAdmin.mockResolvedValue(mockMenuItems);
|
|
|
|
const result = await controller.findAllAdmin(undefined);
|
|
|
|
expect(service.findAllAdmin).toHaveBeenCalledWith(undefined);
|
|
expect(result).toEqual(mockMenuItems);
|
|
});
|
|
});
|
|
|
|
describe("create", () => {
|
|
it("should create a new menu item", async () => {
|
|
const createBody = {
|
|
menuType: "HEADER",
|
|
label: "درباره کانینا آلمان",
|
|
href: "/about-us",
|
|
parentId: undefined,
|
|
order: 3,
|
|
isActive: true,
|
|
isExternal: false,
|
|
icon: "info",
|
|
badge: "معرفی",
|
|
};
|
|
|
|
const createdItem = { id: "menu-item-3", ...createBody };
|
|
mockMenuService.create.mockResolvedValue(createdItem);
|
|
|
|
const result = await controller.create(createBody);
|
|
|
|
expect(service.create).toHaveBeenCalledWith(createBody);
|
|
expect(result).toEqual(createdItem);
|
|
});
|
|
});
|
|
|
|
describe("reorder", () => {
|
|
it("should reorder menu items", async () => {
|
|
const reorderBody = {
|
|
items: [
|
|
{ id: "menu-item-1", order: 2 },
|
|
{ id: "menu-item-2", order: 1 },
|
|
],
|
|
};
|
|
|
|
mockMenuService.reorder.mockResolvedValue({ success: true, count: 2 });
|
|
|
|
const result = await controller.reorder(reorderBody);
|
|
|
|
expect(service.reorder).toHaveBeenCalledWith(reorderBody.items);
|
|
expect(result).toEqual({ success: true, count: 2 });
|
|
});
|
|
});
|
|
|
|
describe("update", () => {
|
|
it("should update an existing menu item", async () => {
|
|
const updateBody = {
|
|
label: "محصولات تقویتی و درمانی سگ",
|
|
order: 5,
|
|
isActive: true,
|
|
};
|
|
|
|
const updatedItem = { ...mockMenuItems[0], ...updateBody };
|
|
mockMenuService.update.mockResolvedValue(updatedItem);
|
|
|
|
const result = await controller.update("menu-item-1", updateBody);
|
|
|
|
expect(service.update).toHaveBeenCalledWith("menu-item-1", updateBody);
|
|
expect(result).toEqual(updatedItem);
|
|
});
|
|
});
|
|
|
|
describe("remove", () => {
|
|
it("should remove a menu item by ID", async () => {
|
|
mockMenuService.remove.mockResolvedValue({
|
|
success: true,
|
|
id: "menu-item-1",
|
|
});
|
|
|
|
const result = await controller.remove("menu-item-1");
|
|
|
|
expect(service.remove).toHaveBeenCalledWith("menu-item-1");
|
|
expect(result).toEqual({ success: true, id: "menu-item-1" });
|
|
});
|
|
});
|
|
});
|