feat(capa): restyle the CAPA detail page, verification form, close card, owner actions and notes

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 11:20:57 +08:00
co-authored by Claude Haiku 4.5
parent 04437926c2
commit 69e510af74
7 changed files with 297 additions and 137 deletions
@@ -0,0 +1,29 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { CapaOwnerNotesForm } from '@/components/capa/capa-owner-notes-form'
beforeEach(() => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }))
})
describe('CapaOwnerNotesForm', () => {
it('shows the saved notes and saves edits with PATCH', async () => {
render(<CapaOwnerNotesForm capaId="c9" initialNotes="Ramp ground back" />)
const box = screen.getByLabelText('Remarks / Notes') as HTMLTextAreaElement
expect(box.value).toBe('Ramp ground back')
fireEvent.change(box, { target: { value: 'Ramp ground back and coated' } })
fireEvent.click(screen.getByRole('button', { name: 'Save Notes' }))
await waitFor(() => expect(screen.getByText('Saved')).toBeTruthy())
expect(fetch).toHaveBeenCalledWith('/api/capa/c9', expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({ owner_notes: 'Ramp ground back and coated' }),
}))
})
it('shows an error when the save fails', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, json: async () => ({}) }))
render(<CapaOwnerNotesForm capaId="c9" initialNotes={null} />)
fireEvent.click(screen.getByRole('button', { name: 'Save Notes' }))
expect((await screen.findByRole('alert')).textContent).toBe('Failed to save. Please try again.')
})
})
@@ -0,0 +1,76 @@
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(<VerifyForm capaId="c1" />)
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(<VerifyForm capaId="c1" />)
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(<VerifyForm capaId="c1" />)
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(<VerifyForm capaId="c1" />)
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(<VerifyForm capaId="c1" />)
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(<VerifyForm capaId="c1" />)
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()
})
})