import { Test, TestingModule } from "@nestjs/testing"; import { WikiService } from "./wiki.service"; import { PrismaService } from "../prisma/prisma.service"; import { NotFoundException } from "@nestjs/common"; describe("WikiService", () => { let service: WikiService; const mockPrisma = { scientificTerm: { findMany: jest.fn(), count: jest.fn(), findUnique: jest.fn(), create: jest.fn(), update: jest.fn(), delete: jest.fn(), }, }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ WikiService, { provide: PrismaService, useValue: mockPrisma }, ], }).compile(); service = module.get(WikiService); jest.clearAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); describe("getTerms", () => { it("should return terms with default pagination when no query provided", async () => { const mockTerms = [{ id: "1", key: "omega-3", term: "Omega 3" }]; mockPrisma.scientificTerm.findMany.mockResolvedValue(mockTerms); mockPrisma.scientificTerm.count.mockResolvedValue(1); const result = await service.getTerms(); expect(mockPrisma.scientificTerm.findMany).toHaveBeenCalledWith({ where: {}, skip: 0, take: 10, orderBy: { term: "asc" }, }); expect(mockPrisma.scientificTerm.count).toHaveBeenCalledWith({ where: {}, }); expect(result).toEqual({ data: mockTerms, meta: { total: 1, page: 1, limit: 10, lastPage: 1, }, }); }); it("should apply search filter and custom pagination", async () => { const mockTerms = [{ id: "2", key: "probiotics", term: "Probiotics" }]; mockPrisma.scientificTerm.findMany.mockResolvedValue(mockTerms); mockPrisma.scientificTerm.count.mockResolvedValue(15); const result = await service.getTerms({ page: "2", limit: "5", search: "bio", }); expect(mockPrisma.scientificTerm.findMany).toHaveBeenCalledWith({ where: { term: { contains: "bio", mode: "insensitive" } }, skip: 5, take: 5, orderBy: { term: "asc" }, }); expect(mockPrisma.scientificTerm.count).toHaveBeenCalledWith({ where: { term: { contains: "bio", mode: "insensitive" } }, }); expect(result.meta).toEqual({ total: 15, page: 2, limit: 5, lastPage: 3, }); }); }); describe("createTerm", () => { it("should create a new scientific term", async () => { const input = { key: "calcium", term: "Calcium", description: "Essential mineral", }; const created = { id: "term-1", ...input }; mockPrisma.scientificTerm.create.mockResolvedValue(created); const result = await service.createTerm(input as any); expect(mockPrisma.scientificTerm.create).toHaveBeenCalledWith({ data: input, }); expect(result).toEqual(created); }); }); describe("updateTerm", () => { it("should throw NotFoundException if term does not exist", async () => { mockPrisma.scientificTerm.findUnique.mockResolvedValue(null); await expect( service.updateTerm("non-existent", { term: "New" }), ).rejects.toThrow(NotFoundException); expect(mockPrisma.scientificTerm.update).not.toHaveBeenCalled(); }); it("should update term if exists", async () => { const existing = { id: "1", key: "calcium", term: "Calcium" }; const updateData = { term: "Calcium Carbonate" }; const updated = { ...existing, ...updateData }; mockPrisma.scientificTerm.findUnique.mockResolvedValue(existing); mockPrisma.scientificTerm.update.mockResolvedValue(updated); const result = await service.updateTerm("calcium", updateData); expect(mockPrisma.scientificTerm.findUnique).toHaveBeenCalledWith({ where: { key: "calcium" }, }); expect(mockPrisma.scientificTerm.update).toHaveBeenCalledWith({ where: { key: "calcium" }, data: updateData, }); expect(result).toEqual(updated); }); }); describe("deleteTerm", () => { it("should throw NotFoundException if term does not exist", async () => { mockPrisma.scientificTerm.findUnique.mockResolvedValue(null); await expect(service.deleteTerm("non-existent")).rejects.toThrow( NotFoundException, ); expect(mockPrisma.scientificTerm.delete).not.toHaveBeenCalled(); }); it("should delete term if exists", async () => { const existing = { id: "1", key: "calcium", term: "Calcium" }; mockPrisma.scientificTerm.findUnique.mockResolvedValue(existing); mockPrisma.scientificTerm.delete.mockResolvedValue(existing); const result = await service.deleteTerm("calcium"); expect(mockPrisma.scientificTerm.findUnique).toHaveBeenCalledWith({ where: { key: "calcium" }, }); expect(mockPrisma.scientificTerm.delete).toHaveBeenCalledWith({ where: { key: "calcium" }, }); expect(result).toEqual(existing); }); }); });