import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import ObjectiveValidationCard from '../../../src/renderer/src/components/ObjectiveValidationCard'
const objective = {
subject: 'Facturation freelance',
professionalGoal: 'Facturer mes clients sans erreur',
duration: '2 semaines'
}
describe('', () => {
it('should display the 3 pillars', () => {
render(
)
expect(screen.getByText('Facturation freelance')).toBeInTheDocument()
expect(screen.getByText('Facturer mes clients sans erreur')).toBeInTheDocument()
expect(screen.getByText('2 semaines')).toBeInTheDocument()
})
it('should switch a row to edit mode and back without affecting other rows', () => {
render(
)
fireEvent.click(screen.getByRole('button', { name: 'Modifier Sujet' }))
const input = screen.getByLabelText('Sujet')
expect(input.value).toBe('Facturation freelance')
fireEvent.change(input, { target: { value: 'Facturation freelance avancee' } })
fireEvent.click(screen.getByRole('button', { name: 'Valider Sujet' }))
expect(screen.getByText('Facturation freelance avancee')).toBeInTheDocument()
expect(screen.queryByLabelText('Sujet')).not.toBeInTheDocument()
})
it('should close the previous field when opening another one', () => {
render(
)
fireEvent.click(screen.getByRole('button', { name: 'Modifier Sujet' }))
expect(screen.getByLabelText('Sujet')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button', { name: 'Modifier Duree' }))
expect(screen.queryByLabelText('Sujet')).not.toBeInTheDocument()
expect(screen.getByLabelText('Duree')).toBeInTheDocument()
})
it('should not allow validating an empty field', () => {
render(
)
fireEvent.click(screen.getByRole('button', { name: 'Modifier Sujet' }))
fireEvent.change(screen.getByLabelText('Sujet'), { target: { value: '' } })
expect(screen.getByRole('button', { name: 'Valider Sujet' })).toBeDisabled()
})
it('should call onConfirm with the current draft and show the locked confirmed state', async () => {
const onConfirm = vi.fn().mockResolvedValue(undefined)
render(
)
fireEvent.click(
screen.getByRole('button', { name: "C'est exactement ca, confirmer l'objectif" })
)
expect(onConfirm).toHaveBeenCalledWith(objective)
await waitFor(() => expect(screen.getByText(/Objectif enregistre/)).toBeInTheDocument())
expect(screen.queryByRole('button', { name: 'Modifier Sujet' })).not.toBeInTheDocument()
})
it('should call onRestart when the secondary button is clicked', () => {
const onRestart = vi.fn()
render(
)
fireEvent.click(screen.getByRole('button', { name: 'Recommencer depuis le debut' }))
expect(onRestart).toHaveBeenCalledOnce()
})
})