import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '@testing-library/react' const push = vi.fn() const refresh = vi.fn() vi.mock('next/navigation', () => ({ useRouter: () => ({ push, refresh }) })) import { VerifyForm } from '@/components/capa/verify-form' const ok = () => vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }) beforeEach(() => { push.mockClear() refresh.mockClear() vi.stubGlobal('fetch', ok()) }) describe('VerifyForm', () => { it('offers two verdict cards and no submit button until one is chosen', () => { render() expect(screen.getByRole('radio', { name: /Verified, effective/ })).toBeTruthy() expect(screen.getByRole('radio', { name: /Reopen, ineffective/ })).toBeTruthy() expect(screen.queryByRole('button', { name: /Confirm verification|Reopen action/ })).toBeNull() }) it('marks the chosen card and shows the matching submit label', () => { render() fireEvent.click(screen.getByRole('radio', { name: /Verified, effective/ })) expect(screen.getByRole('radio', { name: /Verified, effective/ }).getAttribute('aria-checked')).toBe('true') expect(screen.getByRole('radio', { name: /Reopen, ineffective/ }).getAttribute('aria-checked')).toBe('false') expect(screen.getByRole('button', { name: 'Confirm verification' })).toBeTruthy() fireEvent.click(screen.getByRole('radio', { name: /Reopen, ineffective/ })) expect(screen.getByRole('button', { name: 'Reopen action' })).toBeTruthy() }) it('posts a verified verdict and returns to the CAPA list', async () => { render() fireEvent.click(screen.getByRole('radio', { name: /Verified, effective/ })) fireEvent.click(screen.getByRole('button', { name: 'Confirm verification' })) await waitFor(() => expect(push).toHaveBeenCalledWith('/hse/capa')) expect(fetch).toHaveBeenCalledWith('/api/capa/c1/verify', expect.objectContaining({ method: 'POST', body: JSON.stringify({ verdict: 'verified', reopen_reason: null }), })) }) it('requires a reason to reopen and does not call the API without one', () => { const { container } = render() fireEvent.click(screen.getByRole('radio', { name: /Reopen, ineffective/ })) fireEvent.submit(container.querySelector('form')!) expect(screen.getByRole('alert').textContent).toBe('Reopen reason is required') expect(fetch).not.toHaveBeenCalled() }) it('posts the reason when reopening', async () => { const { container } = render() fireEvent.click(screen.getByRole('radio', { name: /Reopen, ineffective/ })) fireEvent.change(screen.getByLabelText(/Why is the action ineffective/), { target: { value: 'Coating peeled' } }) fireEvent.submit(container.querySelector('form')!) await waitFor(() => expect(push).toHaveBeenCalledWith('/hse/capa')) expect(fetch).toHaveBeenCalledWith('/api/capa/c1/verify', expect.objectContaining({ body: JSON.stringify({ verdict: 'reopened', reopen_reason: 'Coating peeled' }), })) }) it('shows the API error in a red banner and stays on the page', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, json: async () => ({ error: 'Not allowed' }) })) render() fireEvent.click(screen.getByRole('radio', { name: /Verified, effective/ })) fireEvent.click(screen.getByRole('button', { name: 'Confirm verification' })) const alert = await screen.findByRole('alert') expect(alert.textContent).toBe('Not allowed') expect(alert.className).toContain('bg-st-red-bg') expect(push).not.toHaveBeenCalled() }) })