Files
ims/tests/components/incidents/report-form-trucks.test.tsx
2026-10-15 13:52:46 +08:00

65 lines
2.3 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent, within } from '@testing-library/react'
import { ReportForm } from '@/components/incidents/report-form'
import { I18nProvider } from '@/lib/i18n/context'
import en from '@/messages/en.json'
vi.mock('next/navigation', () => ({ useRouter: () => ({ push: vi.fn(), refresh: vi.fn() }) }))
const sites = [
{ id: 'site-a', name: 'Site A', zones: [{ id: 'z1', name: 'Dock', qr_code_token: 'tok-a' }] },
{ id: 'site-b', name: 'Site B', zones: [{ id: 'z2', name: 'Yard', qr_code_token: 'tok-b' }] },
]
const trucks = [
{ id: 't1', truck_no: 'AAA 1', carrier: null, site_id: 'site-a' },
{ id: 't2', truck_no: 'BBB 2', carrier: null, site_id: 'site-b' },
{ id: 't3', truck_no: 'FREE 3', carrier: null, site_id: null },
]
function renderForm(props: Partial<React.ComponentProps<typeof ReportForm>> = {}) {
return render(
<I18nProvider messages={en}>
<ReportForm
zoneToken={null}
zoneName={null}
siteName={null}
sites={sites}
defaultSiteId=""
trucks={trucks}
initialTruckId="t1"
{...props}
/>
</I18nProvider>,
)
}
const selectWith = (text: string) =>
screen.getAllByRole('combobox').find(s => within(s).queryByText(text)) as HTMLSelectElement
const truckNos = () =>
Array.from(selectWith(en.ReportForm.truckPlaceholder).options).map(o => o.textContent)
describe('ReportForm truck list', () => {
const all = [en.ReportForm.truckPlaceholder, 'AAA 1', 'BBB 2', 'FREE 3']
it('shows every truck regardless of site', () => {
renderForm()
expect(truckNos()).toEqual(all)
})
it('keeps the chosen truck when the site changes', () => {
renderForm()
fireEvent.change(selectWith(en.ReportForm.sitePlaceholder), { target: { value: 'site-b' } })
expect(truckNos()).toEqual(all)
expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('t1')
})
it('shows every truck with a QR zone token', () => {
renderForm({ zoneToken: 'tok-b', zoneName: 'Yard', siteName: 'Site B', initialTruckId: null })
fireEvent.change(screen.getAllByRole('combobox').find(s => within(s).queryByText(en.IncidentType.transport))!, {
target: { value: 'transport' },
})
expect(truckNos()).toEqual(all)
})
})