95 lines
2.9 KiB
TypeScript
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');
|
|
});
|
|
});
|