fix(capa): handle network errors, keep form locked after submit, extend tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||
import { SubmitVerificationForm } from '@/components/capa/submit-verification-form'
|
||||
|
||||
const { refresh } = vi.hoisted(() => ({ refresh: vi.fn() }))
|
||||
vi.mock('next/navigation', () => ({
|
||||
useRouter: () => ({ refresh: vi.fn() }),
|
||||
useRouter: () => ({ refresh }),
|
||||
}))
|
||||
|
||||
const fetchMock = vi.fn()
|
||||
beforeEach(() => {
|
||||
refresh.mockClear()
|
||||
fetchMock.mockReset()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
})
|
||||
|
||||
function fill(container: HTMLElement, names: string[] = ['proof.jpg']) {
|
||||
fireEvent.change(screen.getByLabelText('Completion note'), { target: { value: 'Rack fixed' } })
|
||||
const input = container.querySelector('input[type="file"]') as HTMLInputElement
|
||||
const files = names.map(n => new File(['x'], n, { type: 'image/jpeg' }))
|
||||
fireEvent.change(input, { target: { files } })
|
||||
}
|
||||
|
||||
function button() {
|
||||
return screen.getByRole('button', { name: 'Submit for Verification' }) as HTMLButtonElement
|
||||
}
|
||||
@@ -24,4 +39,53 @@ describe('SubmitVerificationForm', () => {
|
||||
expect(button().disabled).toBe(false)
|
||||
expect(screen.getByText('proof.jpg')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('posts FormData on success, refreshes and stays disabled', async () => {
|
||||
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ ok: true }) })
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
fill(container)
|
||||
fireEvent.click(button())
|
||||
await waitFor(() => expect(refresh).toHaveBeenCalledTimes(1))
|
||||
const [url, init] = fetchMock.mock.calls[0]
|
||||
expect(url).toBe('/api/capa/abc/submit')
|
||||
expect(init.method).toBe('POST')
|
||||
const fd = init.body as FormData
|
||||
expect(fd.get('note')).toBe('Rack fixed')
|
||||
expect((fd.getAll('files') as File[]).map(f => f.name)).toEqual(['proof.jpg'])
|
||||
expect((screen.getByRole('button', { name: 'Submitting…' }) as HTMLButtonElement).disabled).toBe(true)
|
||||
})
|
||||
|
||||
it('shows server details and re-enables on 422', async () => {
|
||||
fetchMock.mockResolvedValue({ ok: false, json: async () => ({ error: 'bad', details: ['Note too short', 'Bad photo'] }) })
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
fill(container)
|
||||
fireEvent.click(button())
|
||||
expect((await screen.findByRole('alert')).textContent).toBe('Note too short. Bad photo')
|
||||
expect(button().disabled).toBe(false)
|
||||
expect(refresh).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows a network error and re-enables when fetch rejects', async () => {
|
||||
fetchMock.mockRejectedValue(new Error('offline'))
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
fill(container)
|
||||
fireEvent.click(button())
|
||||
expect((await screen.findByRole('alert')).textContent).toBe('Network error. Please try again.')
|
||||
expect(button().disabled).toBe(false)
|
||||
})
|
||||
|
||||
it('disables submit again after removing the only file', () => {
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
fill(container)
|
||||
expect(button().disabled).toBe(false)
|
||||
fireEvent.click(screen.getByLabelText('Remove proof.jpg'))
|
||||
expect(button().disabled).toBe(true)
|
||||
})
|
||||
|
||||
it('caps the list at 5 photos', () => {
|
||||
const { container } = render(<SubmitVerificationForm capaId="abc" />)
|
||||
fill(container, ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg'])
|
||||
expect(screen.getAllByText(/^\d\.jpg$/)).toHaveLength(5)
|
||||
expect(screen.queryByText('6.jpg')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user