Files
canina/backend/src/admin/wiki.service.spec.ts
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

170 lines
5.1 KiB
TypeScript

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>(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);
});
});
});