Files
flashcards/src/app/cards/[id]/edit/page.test.tsx
T
devcodetools 67569e2d16 feat(tags): gestion des tags dans le formulaire carte (max 3, suggestions, normalisation)
- schéma zod : tags normalisés (trim + minuscules + dédupe), 3 max par carte
- couche générique : findTags (MySQL + Mongo), service tags()
- API GET /api/tags + cardsApi.tags() pour les suggestions
- formulaire : saisie + ajout (Entrée/bouton), suggestions des tags existants,
  chips supprimables, compteur n/3, blocage au-delà de 3
- tests : schémas, repositories, service, route, api-client, formulaire
2026-09-22 10:18:13 +02:00

60 lines
1.8 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import EditCardPage from './page'
vi.mock('@/lib/api-client', () => {
class ApiClientErrorMock extends Error {
status: number
constructor(status: number, name: string, message: string) {
super(message)
this.name = name
this.status = status
}
}
return {
ApiClientError: ApiClientErrorMock,
cardsApi: { get: vi.fn(), tags: vi.fn().mockResolvedValue([]) },
}
})
const notFoundMock = vi.fn()
vi.mock('next/navigation', () => ({
useRouter: () => ({ push: vi.fn(), refresh: vi.fn() }),
notFound: () => notFoundMock(),
}))
import { ApiClientError, cardsApi } from '@/lib/api-client'
const CARD = {
id: '1',
question: 'Cest quoi un commit ?',
response: 'Un snapshot',
theme: 'git',
tags: [],
createdAt: new Date('2026-01-01T08:00:00Z'),
updatedAt: new Date('2026-01-01T08:00:00Z'),
}
describe('EditCardPage', () => {
it('pré-remplit le formulaire depuis la carte (F-06)', async () => {
vi.mocked(cardsApi.get).mockResolvedValue(CARD)
const element = await EditCardPage({ params: Promise.resolve({ id: '1' }) })
render(element)
expect(screen.getByLabelText('Question')).toHaveValue('Cest quoi un commit ?')
expect(screen.getByLabelText('R\u00e9ponse')).toHaveValue('Un snapshot')
expect(screen.getByRole('button', { name: 'Enregistrer' })).toBeInTheDocument()
})
it('déclenche notFound si la carte est inconnue', async () => {
notFoundMock.mockImplementation(() => {
throw new Error('not-found')
})
vi.mocked(cardsApi.get).mockRejectedValue(
new ApiClientError(404, 'NotFoundError', 'Carte introuvable'),
)
await expect(EditCardPage({ params: Promise.resolve({ id: 'x' }) })).rejects.toThrow(
'not-found',
)
})
})