test(api): couvrir les routes CRUD, random, thèmes et les helpers HTTP
Ajoute les tests des routes /api/cards, /api/cards/[id], /api/cards/random et /api/cards/themes, ainsi que les tests des helpers http (handleError, parseJsonBody) et de la factory getCardService. Corrige le formatage des fichiers sources embarqués non formatés par les commits précédents.
This commit is contained in:
1 parent
19a5a47b28
commit
1b8aac89f9
16 files changed
+369
-14
No files matched your search
@@ -0,0 +1,53 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { NextRequest } from 'next/server'
|
||||
import type { Card } from '@/domain/card'
|
||||
import { getCardService } from '@/services/card-service-factory'
|
||||
import { GET } from './route'
|
||||
|
||||
vi.mock('@/services/card-service-factory', () => ({
|
||||
getCardService: vi.fn(),
|
||||
}))
|
||||
|
||||
const CARD: Card = {
|
||||
id: '10000000-0000-4000-8000-000000000001',
|
||||
question: 'Question ?',
|
||||
response: 'Réponse',
|
||||
theme: 'javascript',
|
||||
tags: ['scope'],
|
||||
createdAt: new Date('2026-01-01T08:00:00Z'),
|
||||
updatedAt: new Date('2026-01-01T08:00:00Z'),
|
||||
}
|
||||
|
||||
const CARD_JSON = JSON.parse(JSON.stringify(CARD))
|
||||
|
||||
describe('GET /api/cards/random', () => {
|
||||
const service = vi.mocked(getCardService)
|
||||
|
||||
beforeEach(() => {
|
||||
service.mockReset()
|
||||
})
|
||||
|
||||
it('tire une carte au hasard sans thème', async () => {
|
||||
const serviceMock = { random: vi.fn().mockResolvedValue(CARD) }
|
||||
service.mockReturnValue(serviceMock as never)
|
||||
const res = await GET(new NextRequest('http://localhost/api/cards/random'))
|
||||
expect(res.status).toBe(200)
|
||||
expect(serviceMock.random).toHaveBeenCalledWith(undefined)
|
||||
expect(await res.json()).toEqual(CARD_JSON)
|
||||
})
|
||||
|
||||
it('tire une carte au hasard pour un thème donné', async () => {
|
||||
const serviceMock = { random: vi.fn().mockResolvedValue(CARD) }
|
||||
service.mockReturnValue(serviceMock as never)
|
||||
const res = await GET(new NextRequest('http://localhost/api/cards/random?theme=javascript'))
|
||||
expect(res.status).toBe(200)
|
||||
expect(serviceMock.random).toHaveBeenCalledWith('javascript')
|
||||
})
|
||||
|
||||
it('propage les erreurs du service', async () => {
|
||||
service.mockReturnValue({ random: vi.fn().mockRejectedValue(new Error('boom')) } as never)
|
||||
const res = await GET(new NextRequest('http://localhost/api/cards/random'))
|
||||
expect(res.status).toBe(500)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user