Files
canina/backend/src/admin/pets.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

147 lines
3.9 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { PetsService } from "./pets.service";
import { PrismaService } from "../prisma/prisma.service";
import { NotFoundException } from "@nestjs/common";
describe("PetsService", () => {
let service: PetsService;
const mockPrisma = {
pet: {
findMany: jest.fn(),
count: jest.fn(),
findUnique: jest.fn(),
delete: jest.fn(),
},
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PetsService,
{ provide: PrismaService, useValue: mockPrisma },
],
}).compile();
service = module.get<PetsService>(PetsService);
jest.clearAllMocks();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("getPets", () => {
it("should return pets with default pagination when no query provided", async () => {
const mockPets = [
{
id: "pet-1",
name: "Bella",
user: { firstName: "Ali", lastName: "Rezaei" },
medicalConditions: [],
reminders: [],
healthLogs: [],
prescriptions: [],
},
];
mockPrisma.pet.findMany.mockResolvedValue(mockPets);
mockPrisma.pet.count.mockResolvedValue(1);
const result = await service.getPets();
expect(mockPrisma.pet.findMany).toHaveBeenCalledWith({
where: {},
skip: 0,
take: 10,
orderBy: { createdAt: "desc" },
include: {
user: {
select: {
firstName: true,
lastName: true,
email: true,
mobile: true,
},
},
medicalConditions: true,
reminders: true,
healthLogs: {
orderBy: { createdAt: "desc" },
take: 10,
},
prescriptions: {
orderBy: { createdAt: "desc" },
take: 5,
},
},
});
expect(mockPrisma.pet.count).toHaveBeenCalledWith({ where: {} });
expect(result).toEqual({
data: mockPets,
meta: {
total: 1,
page: 1,
limit: 10,
lastPage: 1,
},
});
});
it("should filter pets by search term and handle custom pagination", async () => {
const mockPets = [{ id: "pet-2", name: "Max" }];
mockPrisma.pet.findMany.mockResolvedValue(mockPets);
mockPrisma.pet.count.mockResolvedValue(25);
const result = await service.getPets({
page: 3,
limit: 10,
search: "max",
});
expect(mockPrisma.pet.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { name: { contains: "max", mode: "insensitive" } },
skip: 20,
take: 10,
}),
);
expect(mockPrisma.pet.count).toHaveBeenCalledWith({
where: { name: { contains: "max", mode: "insensitive" } },
});
expect(result.meta).toEqual({
total: 25,
page: 3,
limit: 10,
lastPage: 3,
});
});
});
describe("deletePet", () => {
it("should throw NotFoundException if pet not found", async () => {
mockPrisma.pet.findUnique.mockResolvedValue(null);
await expect(service.deletePet("pet-999")).rejects.toThrow(
NotFoundException,
);
expect(mockPrisma.pet.delete).not.toHaveBeenCalled();
});
it("should delete and return pet if found", async () => {
const mockPet = { id: "pet-1", name: "Bella" };
mockPrisma.pet.findUnique.mockResolvedValue(mockPet);
mockPrisma.pet.delete.mockResolvedValue(mockPet);
const result = await service.deletePet("pet-1");
expect(mockPrisma.pet.findUnique).toHaveBeenCalledWith({
where: { id: "pet-1" },
});
expect(mockPrisma.pet.delete).toHaveBeenCalledWith({
where: { id: "pet-1" },
});
expect(result).toEqual(mockPet);
});
});
});