feat: API route GET /api/sync/history (TDD)

This commit is contained in:
2026-06-08 21:04:45 +02:00
parent 16fece8ca9
commit e9d4f9dc20
2 changed files with 93 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
import { GET } from '@/app/api/sync/history/route'
import { NextRequest } from 'next/server'
jest.mock('@/lib/prisma', () => ({
prisma: {
syncRun: {
findMany: jest.fn(),
},
},
}))
import { prisma } from '@/lib/prisma'
const mockFindMany = prisma.syncRun.findMany as jest.Mock
describe('GET /api/sync/history', () => {
beforeEach(() => jest.clearAllMocks())
it('retourne une liste vide si aucun SyncRun', async () => {
mockFindMany.mockResolvedValue([])
const req = new NextRequest('http://localhost/api/sync/history')
const res = await GET(req)
expect(res.status).toBe(200)
expect(await res.json()).toEqual([])
})
it('retourne les syncs formatées', async () => {
const fakeRun = {
id: 'clid1',
createdAt: new Date('2026-06-08T10:00:00Z'),
status: 'success',
created: 3,
updated: 1,
deactivated: 0,
errors: 0,
log: JSON.stringify([{ ref: 'REF-1', action: 'create', status: 'success', message: '✓ REF-1 créé' }]),
user: { name: 'Mathieu' },
}
mockFindMany.mockResolvedValue([fakeRun])
const req = new NextRequest('http://localhost/api/sync/history')
const res = await GET(req)
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toHaveLength(1)
expect(data[0].id).toBe('clid1')
expect(data[0].userName).toBe('Mathieu')
expect(data[0].status).toBe('success')
expect(data[0].created).toBe(3)
expect(data[0].log).toHaveLength(1)
expect(data[0].log[0].ref).toBe('REF-1')
expect(data[0].createdAt).toBe('2026-06-08T10:00:00.000Z')
})
it('retourne 500 si Prisma échoue', async () => {
mockFindMany.mockRejectedValue(new Error('DB error'))
const req = new NextRequest('http://localhost/api/sync/history')
const res = await GET(req)
expect(res.status).toBe(500)
})
})