Files
canina/backend/src/wiki/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

97 lines
2.7 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { WikiService } from "./wiki.service";
import { PrismaService } from "../prisma/prisma.service";
import { NotFoundException } from "@nestjs/common";
import { SortOrder } from "../common/dto/pagination.dto";
describe("WikiService", () => {
let service: WikiService;
const mockPrisma = {
scientificTerm: {
findMany: jest.fn(),
count: jest.fn(),
findUnique: 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("findAll", () => {
it("should paginate and filter terms with fallback to default sortBy", async () => {
const mockTerms = [
{
key: "glucosamine",
term: "گلوکزامین",
definition: "ترکیب محافظت از مفاصل",
wikiId: "W1",
},
];
mockPrisma.scientificTerm.findMany.mockResolvedValue(mockTerms);
mockPrisma.scientificTerm.count.mockResolvedValue(1);
const result = await service.findAll({
search: "مفصل",
page: 1,
limit: 10,
sortBy: "invalidField", // should fallback to 'term'
sortOrder: SortOrder.DESC,
});
expect(mockPrisma.scientificTerm.findMany).toHaveBeenCalledWith({
where: {
OR: [
{ term: { contains: "مفصل", mode: "insensitive" } },
{ definition: { contains: "مفصل", mode: "insensitive" } },
],
},
skip: 0,
take: 10,
orderBy: { term: "desc" },
});
expect(result.data).toEqual(mockTerms);
expect(result.meta.total).toBe(1);
expect(result.meta.lastPage).toBe(1);
});
});
describe("findOneByKey", () => {
it("should throw NotFoundException if scientific term key is not found", async () => {
mockPrisma.scientificTerm.findUnique.mockResolvedValue(null);
await expect(service.findOneByKey("unknown-term")).rejects.toThrow(
NotFoundException,
);
});
it("should return term when found by key", async () => {
const term = {
key: "calcium",
term: "کلسیم",
definition: "ماده معدنی ضروری برای استخوان‌ها",
wikiId: "W2",
};
mockPrisma.scientificTerm.findUnique.mockResolvedValue(term);
const result = await service.findOneByKey("calcium");
expect(result).toEqual(term);
});
});
});