import { Test, TestingModule } from "@nestjs/testing"; import { TestimonialsService } from "./testimonials.service"; import { PrismaService } from "../prisma/prisma.service"; import { NotFoundException } from "@nestjs/common"; describe("TestimonialsService", () => { let service: TestimonialsService; const mockPrisma = { testimonial: { findMany: jest.fn(), findUnique: jest.fn(), create: jest.fn(), update: jest.fn(), delete: jest.fn(), }, }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ TestimonialsService, { provide: PrismaService, useValue: mockPrisma, }, ], }).compile(); service = module.get(TestimonialsService); jest.clearAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); describe("findHomepage", () => { it("should query only active testimonials flagged for showOnHome ordered by order", async () => { const activeItems = [ { id: "t1", authorName: "دکتر علوی", order: 1, isActive: true, showOnHome: true, }, ]; mockPrisma.testimonial.findMany.mockResolvedValue(activeItems); const result = await service.findHomepage(); expect(mockPrisma.testimonial.findMany).toHaveBeenCalledWith({ where: { isActive: true, showOnHome: true }, orderBy: { order: "asc" }, }); expect(result).toEqual(activeItems); }); }); describe("update", () => { it("should throw NotFoundException if testimonial does not exist", async () => { mockPrisma.testimonial.findUnique.mockResolvedValue(null); await expect( service.update("invalid-id", { authorName: "تغییر نام" }), ).rejects.toThrow(NotFoundException); }); it("should update and return testimonial when exists", async () => { mockPrisma.testimonial.findUnique.mockResolvedValue({ id: "t1" }); mockPrisma.testimonial.update.mockResolvedValue({ id: "t1", authorName: "تغییر نام", }); const result = await service.update("t1", { authorName: "تغییر نام" }); expect(result.authorName).toBe("تغییر نام"); }); }); describe("remove", () => { it("should throw NotFoundException if testimonial does not exist on remove", async () => { mockPrisma.testimonial.findUnique.mockResolvedValue(null); await expect(service.remove("missing-id")).rejects.toThrow( NotFoundException, ); }); it("should delete and return testimonial when found", async () => { mockPrisma.testimonial.findUnique.mockResolvedValue({ id: "t1" }); mockPrisma.testimonial.delete.mockResolvedValue({ id: "t1" }); const result = await service.remove("t1"); expect(result.id).toBe("t1"); }); }); });