All materials
batches.test.ts
tsbatches.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest'
// Mock Prisma
vi.mock('../src/lib/db', () => ({
prisma: {
batch: {
findMany: vi.fn(),
findUnique: vi.fn(),
create: vi.fn(),
update: vi.fn(),
},
},
}))
import { prisma } from '../src/lib/db'
describe('Batches API', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should return all batches', async () => {
const mockBatches = [
{ id: 1, variete: 'W320', stage: 'reception', poids_brut: 1200 },
{ id: 2, variete: 'W240', stage: 'shelling', poids_brut: 950 },
]
;(prisma.batch.findMany as ReturnType<typeof vi.fn>).mockResolvedValue(mockBatches)
const result = await prisma.batch.findMany({
include: { cooperative: true },
orderBy: { date_reception: 'desc' },
})
expect(result).toHaveLength(2)
expect(result[0].variete).toBe('W320')
})
it('should find a batch by id', async () => {
const mockBatch = { id: 1, variete: 'W320', stage: 'grading', poids_brut: 1200, net_weight: 276 }
;(prisma.batch.findUnique as ReturnType<typeof vi.fn>).mockResolvedValue(mockBatch)
const result = await prisma.batch.findUnique({
where: { id: 1 },
include: { cooperative: true, quality_checks: true, shipment_items: { include: { shipment: true } } },
})
expect(result).toBeDefined()
expect(result?.net_weight).toBe(276)
})
it('should create a new batch', async () => {
const newBatch = {
id: 3,
cooperative_id: 1,
date_reception: new Date('2025-12-01'),
poids_brut: 1500,
variete: 'W180',
stage: 'reception',
}
;(prisma.batch.create as ReturnType<typeof vi.fn>).mockResolvedValue(newBatch)
const result = await prisma.batch.create({
data: {
cooperative_id: 1,
date_reception: new Date('2025-12-01'),
poids_brut: 1500,
variete: 'W180',
},
})
expect(result.variete).toBe('W180')
expect(result.poids_brut).toBe(1500)
})
it('should update batch stage', async () => {
const updatedBatch = { id: 1, stage: 'shelling', net_weight: 276 }
;(prisma.batch.update as ReturnType<typeof vi.fn>).mockResolvedValue(updatedBatch)
const result = await prisma.batch.update({
where: { id: 1 },
data: { stage: 'shelling', net_weight: 276 },
})
expect(result.stage).toBe('shelling')
})
})