import { Test, TestingModule } from "@nestjs/testing"; import { FaqService } from "./faq.service"; import { PrismaService } from "../prisma/prisma.service"; import { NotFoundException } from "@nestjs/common"; describe("FaqService", () => { let service: FaqService; const mockPrisma = { fAQ: { findMany: jest.fn(), findUnique: jest.fn(), create: jest.fn(), update: jest.fn(), delete: jest.fn(), }, }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ FaqService, { provide: PrismaService, useValue: mockPrisma, }, ], }).compile(); service = module.get(FaqService); jest.clearAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); describe("findActive", () => { it("should return only active faqs ordered by order asc", async () => { const activeFaqs = [ { id: "1", question: "Q1", answer: "A1", isActive: true, order: 0 }, ]; mockPrisma.fAQ.findMany.mockResolvedValue(activeFaqs); const result = await service.findActive(); expect(mockPrisma.fAQ.findMany).toHaveBeenCalledWith({ where: { isActive: true }, orderBy: { order: "asc" }, }); expect(result).toEqual(activeFaqs); }); }); describe("create", () => { it("should create new FAQ", async () => { const dto = { question: "New Q", answer: "New A", order: 1, isActive: true, }; mockPrisma.fAQ.create.mockResolvedValue({ id: "faq-1", ...dto }); const result = await service.create(dto); expect(mockPrisma.fAQ.create).toHaveBeenCalledWith({ data: dto }); expect(result.id).toBe("faq-1"); }); }); describe("update", () => { it("should throw NotFoundException if FAQ not found", async () => { mockPrisma.fAQ.findUnique.mockResolvedValue(null); await expect( service.update("invalid-id", { question: "updated" }), ).rejects.toThrow(NotFoundException); }); it("should update FAQ if found", async () => { mockPrisma.fAQ.findUnique.mockResolvedValue({ id: "faq-1", question: "Old", }); mockPrisma.fAQ.update.mockResolvedValue({ id: "faq-1", question: "Updated", }); const result = await service.update("faq-1", { question: "Updated" }); expect(result.question).toBe("Updated"); }); }); describe("remove", () => { it("should throw NotFoundException if FAQ not found", async () => { mockPrisma.fAQ.findUnique.mockResolvedValue(null); await expect(service.remove("invalid-id")).rejects.toThrow( NotFoundException, ); }); it("should delete FAQ if found", async () => { mockPrisma.fAQ.findUnique.mockResolvedValue({ id: "faq-1" }); mockPrisma.fAQ.delete.mockResolvedValue({ id: "faq-1" }); const result = await service.remove("faq-1"); expect(result.id).toBe("faq-1"); }); }); describe("reorder", () => { it("should update orders for each item", async () => { mockPrisma.fAQ.update.mockResolvedValue({}); const items = [ { id: "1", order: 2 }, { id: "2", order: 1 }, ]; const result = await service.reorder(items); expect(mockPrisma.fAQ.update).toHaveBeenCalledTimes(2); expect(result).toEqual({ success: true }); }); }); });