All materials
cooperatives.test.ts
tscooperatives.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest'
vi.mock('../src/lib/db', () => ({
prisma: {
cooperative: {
findMany: vi.fn(),
findUnique: vi.fn(),
},
},
}))
import { prisma } from '../src/lib/db'
describe('Cooperatives API', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should return all cooperatives', async () => {
const mockCoops = [
{ id: 1, nom: 'Cooperative Agricole de Korhogo', village: 'Korhogo' },
{ id: 2, nom: 'Union des Femmes de Boundiali', village: 'Boundiali' },
]
;(prisma.cooperative.findMany as ReturnType<typeof vi.fn>).mockResolvedValue(mockCoops)
const result = await prisma.cooperative.findMany({
include: { batches: { select: { id: true, stage: true } } },
orderBy: { nom: 'asc' },
})
expect(result).toHaveLength(2)
expect(result[0].nom).toContain('Korhogo')
})
it('should find a cooperative by id', async () => {
const mockCoop = {
id: 1,
nom: 'Cooperative Agricole de Korhogo',
village: 'Korhogo',
contact_name: 'Sekou Coulibaly',
batches: [{ id: 1, stage: 'reception' }],
}
;(prisma.cooperative.findUnique as ReturnType<typeof vi.fn>).mockResolvedValue(mockCoop)
const result = await prisma.cooperative.findUnique({
where: { id: 1 },
include: { batches: { orderBy: { date_reception: 'desc' } } },
})
expect(result).toBeDefined()
expect(result?.contact_name).toBe('Sekou Coulibaly')
})
})