Files
ims/tests/components/capa/capa-form.test.tsx

83 lines
3.7 KiB
TypeScript

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 { CapaForm } from '@/components/capa/capa-form'
const users = [{ id: 'u1', name: 'Tan Mei Ling', department: 'Facilities' }]
function stubFetch(postResponse: { ok: boolean; body?: unknown } = { ok: true }) {
const fn = vi.fn(async (url: string, init?: RequestInit) => {
void init
if (url === '/api/users') return { ok: true, json: async () => users }
return { ok: postResponse.ok, json: async () => postResponse.body ?? {} }
})
vi.stubGlobal('fetch', fn)
return fn
}
const postCall = (fn: ReturnType<typeof stubFetch>) => fn.mock.calls.find(c => c[0] === '/api/capa')
beforeEach(() => {
push.mockClear()
refresh.mockClear()
})
describe('CapaForm', () => {
it('uses the shared labelled fields', async () => {
stubFetch()
render(<CapaForm incidentId="inc1" />)
expect(screen.getByLabelText('Action Description *').className).toContain('rounded-md')
expect(screen.getByLabelText('Due Date *').className).toContain('shadow-input')
expect(await screen.findByRole('option', { name: /Tan Mei Ling/ })).toBeTruthy()
})
it('rejects an empty submission without calling the API', async () => {
const fn = stubFetch()
const { container } = render(<CapaForm incidentId="inc1" />)
await screen.findByRole('option', { name: /Tan Mei Ling/ })
fireEvent.submit(container.querySelector('form')!)
expect((await screen.findByRole('alert')).textContent).toBe('Description, owner, and due date are required')
expect(postCall(fn)).toBeUndefined()
})
it('creates the action with the owner department and returns to the incident', async () => {
const fn = stubFetch()
const { container } = render(<CapaForm incidentId="inc1" />)
await screen.findByRole('option', { name: /Tan Mei Ling/ })
fireEvent.change(screen.getByLabelText('Action Description *'), { target: { value: 'Re-coat the ramp' } })
fireEvent.change(screen.getByLabelText('Root Cause Reference'), { target: { value: 'Why #2' } })
fireEvent.change(screen.getByLabelText('Assigned To *'), { target: { value: 'u1' } })
fireEvent.change(screen.getByLabelText('Due Date *'), { target: { value: '2026-11-01' } })
fireEvent.change(screen.getByLabelText('Priority'), { target: { value: 'high' } })
fireEvent.submit(container.querySelector('form')!)
await waitFor(() => expect(push).toHaveBeenCalledWith('/hse/incidents/inc1'))
expect(JSON.parse(postCall(fn)![1]!.body as string)).toEqual({
incident_id: 'inc1',
description: 'Re-coat the ramp',
owner_user_id: 'u1',
department: 'Facilities',
due_date: '2026-11-01',
priority: 'high',
root_cause_ref: 'Why #2',
})
})
it('shows the API error in a red banner and stays on the page', async () => {
stubFetch({ ok: false, body: { error: 'Owner inactive' } })
const { container } = render(<CapaForm incidentId="inc1" />)
await screen.findByRole('option', { name: /Tan Mei Ling/ })
fireEvent.change(screen.getByLabelText('Action Description *'), { target: { value: 'x' } })
fireEvent.change(screen.getByLabelText('Assigned To *'), { target: { value: 'u1' } })
fireEvent.change(screen.getByLabelText('Due Date *'), { target: { value: '2026-11-01' } })
fireEvent.submit(container.querySelector('form')!)
const alert = await screen.findByRole('alert')
expect(alert.textContent).toBe('Owner inactive')
expect(alert.className).toContain('bg-st-red-bg')
expect(push).not.toHaveBeenCalled()
})
})