58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { ProductsService } from './products.service';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
describe('ProductsService', () => {
|
|
let service: ProductsService;
|
|
let prisma: PrismaService;
|
|
|
|
const mockPrisma = {
|
|
product: {
|
|
findMany: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
findFirst: jest.fn(),
|
|
count: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
ProductsService,
|
|
{ provide: PrismaService, useValue: mockPrisma },
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<ProductsService>(ProductsService);
|
|
prisma = module.get<PrismaService>(PrismaService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should be defined', () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe('findAll', () => {
|
|
it('should query products with correct filters', async () => {
|
|
mockPrisma.product.findMany.mockResolvedValue([{ id: 'prod-1' }]);
|
|
mockPrisma.product.count.mockResolvedValue(1);
|
|
const filters = { category: 'joints', petType: 'سگ', query: 'can' };
|
|
const result = await service.findAll(filters);
|
|
|
|
expect(result.data).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe('findOne', () => {
|
|
it('should find product by id', async () => {
|
|
const prod = { id: 'prod-1' };
|
|
mockPrisma.product.findFirst.mockResolvedValue(prod);
|
|
const result = await service.findOne('prod-1');
|
|
expect(result).toEqual(prod);
|
|
});
|
|
});
|
|
});
|