65 lines
2.5 KiB
TypeScript
65 lines
2.5 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 filter', () => {
|
|
it('defaults the site to the preselected truck site and filters trucks', () => {
|
|
renderForm()
|
|
expect(selectWith(en.ReportForm.sitePlaceholder).value).toBe('site-a')
|
|
expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'AAA 1', 'FREE 3'])
|
|
})
|
|
|
|
it('changing the site refilters and clears a truck that no longer fits', () => {
|
|
renderForm()
|
|
expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('t1')
|
|
fireEvent.change(selectWith(en.ReportForm.sitePlaceholder), { target: { value: 'site-b' } })
|
|
expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'BBB 2', 'FREE 3'])
|
|
expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('')
|
|
})
|
|
|
|
it('uses the QR zone site when a zone token is present', () => {
|
|
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([en.ReportForm.truckPlaceholder, 'BBB 2', 'FREE 3'])
|
|
})
|
|
})
|