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

95 lines
2.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;
let prisma: PrismaService;
const mockPrisma = {
pet: {
create: jest.fn(),
findMany: jest.fn(),
findFirst: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
count: jest.fn(),
},
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PetsService,
{ provide: PrismaService, useValue: mockPrisma },
],
}).compile();
service = module.get<PetsService>(PetsService);
prisma = module.get<PrismaService>(PrismaService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
it("should create pet", async () => {
const dto = { name: "Buddy", type: "Dog", weight: 12.5 };
mockPrisma.pet.create.mockResolvedValue({ id: "pet-id", ...dto });
const result = await service.create("user-id", dto);
expect(prisma.pet.create).toHaveBeenCalled();
expect(result.name).toBe("Buddy");
});
it("should find all pets by user", async () => {
mockPrisma.pet.findMany.mockResolvedValue([{ id: "pet-1" }]);
mockPrisma.pet.count.mockResolvedValue(1);
const result = await service.findAllByUser("user-id", {});
expect(prisma.pet.findMany).toHaveBeenCalled();
expect(result.data).toHaveLength(1);
});
describe("findOne", () => {
it("should throw NotFoundException if pet not found", async () => {
mockPrisma.pet.findFirst.mockResolvedValue(null);
await expect(service.findOne("pet-id", "user-id")).rejects.toThrow(
NotFoundException,
);
});
it("should return pet if found", async () => {
const pet = { id: "pet-id", userId: "user-id" };
mockPrisma.pet.findFirst.mockResolvedValue(pet);
const result = await service.findOne("pet-id", "user-id");
expect(result).toEqual(pet);
});
});
it("should update pet", async () => {
const pet = { id: "pet-id", userId: "user-id" };
mockPrisma.pet.findFirst.mockResolvedValue(pet);
const dto = { name: "Buddy New", type: "Dog" };
mockPrisma.pet.update.mockResolvedValue({ ...pet, ...dto });
const result = await service.update("pet-id", "user-id", dto);
expect(prisma.pet.update).toHaveBeenCalled();
expect(result.name).toBe("Buddy New");
});
it("should remove pet", async () => {
const pet = { id: "pet-id", userId: "user-id" };
mockPrisma.pet.findFirst.mockResolvedValue(pet);
mockPrisma.pet.delete.mockResolvedValue(pet);
const result = await service.remove("pet-id", "user-id");
expect(prisma.pet.delete).toHaveBeenCalled();
expect(result.id).toBe("pet-id");
});
});