import { Test, TestingModule } from "@nestjs/testing"; import { TestimonialsController } from "./testimonials.controller"; import { TestimonialsService } from "./testimonials.service"; import { Prisma } from "@prisma/client"; describe("TestimonialsController", () => { let controller: TestimonialsController; let service: TestimonialsService; const mockTestimonialsService = { findAll: jest.fn(), findHomepage: jest.fn(), create: jest.fn(), update: jest.fn(), remove: jest.fn(), }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [TestimonialsController], providers: [ { provide: TestimonialsService, useValue: mockTestimonialsService, }, ], }).compile(); controller = module.get(TestimonialsController); service = module.get(TestimonialsService); }); afterEach(() => { jest.clearAllMocks(); }); it("should be defined", () => { expect(controller).toBeDefined(); expect(service).toBeDefined(); }); describe("findAll", () => { it("should return all testimonials", async () => { const mockList = [ { id: "testi-1", authorName: "فرهاد پیروز", roleTitle: "سرپرست سگ ژرمن شپرد", content: "مکمل مفاصل کانین هیدروکس واقعا معجزه کرد برای پای سگم.", rating: 5, isActive: true, showOnHome: true, order: 1, }, ]; mockTestimonialsService.findAll.mockResolvedValue(mockList); const result = await controller.findAll(); expect(service.findAll).toHaveBeenCalledTimes(1); expect(result).toEqual(mockList); }); }); describe("findHomepage", () => { it("should return only active homepage testimonials", async () => { const mockHomeList = [ { id: "testi-1", authorName: "فرهاد پیروز", showOnHome: true, isActive: true, }, ]; mockTestimonialsService.findHomepage.mockResolvedValue(mockHomeList); const result = await controller.findHomepage(); expect(service.findHomepage).toHaveBeenCalledTimes(1); expect(result).toEqual(mockHomeList); }); }); describe("create", () => { it("should create a new testimonial via service.create", async () => { const createInput: Prisma.TestimonialCreateInput = { authorName: "مونا اسدی", roleTitle: "سرپرست گربه پرشین", content: "پودر بیوتین باعث براق شدن و قطع ریزش موی گربه‌ام شد.", rating: 5, isActive: true, showOnHome: true, order: 2, }; const created = { id: "testi-2", ...createInput, createdAt: new Date(), updatedAt: new Date(), }; mockTestimonialsService.create.mockResolvedValue(created); const result = await controller.create(createInput); expect(service.create).toHaveBeenCalledWith(createInput); expect(result).toEqual(created); }); }); describe("update", () => { it("should update a testimonial by id via service.update", async () => { const testId = "testi-1"; const updateInput: Prisma.TestimonialUpdateInput = { content: "متن ویرایش شده رضایت‌نامه مشتری", rating: 5, }; const updated = { id: testId, authorName: "فرهاد پیروز", content: "متن ویرایش شده رضایت‌نامه مشتری", rating: 5, updatedAt: new Date(), }; mockTestimonialsService.update.mockResolvedValue(updated); const result = await controller.update(testId, updateInput); expect(service.update).toHaveBeenCalledWith(testId, updateInput); expect(result).toEqual(updated); }); }); describe("remove", () => { it("should delete a testimonial by id via service.remove", async () => { const testId = "testi-del-1"; const deleted = { id: testId, authorName: "حذف شده" }; mockTestimonialsService.remove.mockResolvedValue(deleted); const result = await controller.remove(testId); expect(service.remove).toHaveBeenCalledWith(testId); expect(result).toEqual(deleted); }); }); });