-
-
+
)
diff --git a/tests/components/capa/capa-owner-notes-form.test.tsx b/tests/components/capa/capa-owner-notes-form.test.tsx
new file mode 100644
index 0000000..ad77a22
--- /dev/null
+++ b/tests/components/capa/capa-owner-notes-form.test.tsx
@@ -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(
)
+ 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(
)
+ fireEvent.click(screen.getByRole('button', { name: 'Save Notes' }))
+ expect((await screen.findByRole('alert')).textContent).toBe('Failed to save. Please try again.')
+ })
+})
diff --git a/tests/components/capa/verify-form.test.tsx b/tests/components/capa/verify-form.test.tsx
new file mode 100644
index 0000000..182ab24
--- /dev/null
+++ b/tests/components/capa/verify-form.test.tsx
@@ -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(
)
+ 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()
+ })
+})