108 lines
4.7 KiB
TypeScript
108 lines
4.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||
import { render, screen, fireEvent, within, act } from '@testing-library/react'
|
||
|
||
const push = vi.fn()
|
||
const replace = vi.fn()
|
||
let search = ''
|
||
vi.mock('next/navigation', () => ({
|
||
useRouter: () => ({ push, replace }),
|
||
usePathname: () => '/hse/incidents',
|
||
useSearchParams: () => new URLSearchParams(search),
|
||
}))
|
||
|
||
import { IncidentTable } from '@/components/incidents/incident-table'
|
||
import type { Incident } from '@/lib/incidents/types'
|
||
|
||
const incidents: Incident[] = [
|
||
{ id: 'a', reference_no: 'SCW1-202607-0001', incident_type: 'near_miss', status: 'reported', severity: null,
|
||
reported_at: '2026-07-10T09:00:00Z', sites: { name: 'SCW1' }, zones: { name: 'Dock A' }, trucks: null, reporter: { name: 'John' } },
|
||
{ id: 'b', reference_no: 'SCW1-202607-0002', incident_type: 'injury', status: 'capa_pending', severity: 3,
|
||
reported_at: '2026-07-11T10:00:00Z', sites: { name: 'SCW1' }, zones: { name: 'Loading Bay' }, trucks: { truck_no: 'WXY1234' }, reporter: { name: 'Jane' } },
|
||
]
|
||
|
||
const props = {
|
||
incidents,
|
||
basePath: '/hse',
|
||
total: 248,
|
||
counts: { all: 248, open: 37, closed: 211 },
|
||
params: { page: 1, q: '', status: '', type: '', siteId: '', truckId: '', view: 'all' as const, sort: 'reported_at' as const, dir: 'desc' as const },
|
||
sites: [{ id: 's1', name: 'SCW1' }],
|
||
trucks: [{ id: 't1', truck_no: 'WXY1234' }],
|
||
}
|
||
|
||
beforeEach(() => {
|
||
push.mockClear()
|
||
replace.mockClear()
|
||
search = ''
|
||
})
|
||
afterEach(() => vi.useRealTimers())
|
||
|
||
describe('IncidentTable', () => {
|
||
it('renders rows with reference, label pills and type', () => {
|
||
render(<IncidentTable {...props} />)
|
||
expect(screen.getAllByText('SCW1-202607-0001').length).toBeGreaterThan(0)
|
||
expect(screen.getAllByText('Reported').some(el => el.className.includes('bg-st-amber-bg'))).toBe(true)
|
||
expect(screen.getAllByText('CAPA pending').some(el => el.className.includes('bg-st-orange-bg'))).toBe(true)
|
||
expect(screen.getAllByText('Near Miss').length).toBeGreaterThan(0)
|
||
expect(screen.getAllByText('Injury / Medical').length).toBeGreaterThan(0)
|
||
})
|
||
|
||
it('shows saved views with counts and switches view through the URL', () => {
|
||
search = '?status=closed&page=2'
|
||
render(<IncidentTable {...props} />)
|
||
expect(screen.getByText('248')).toBeTruthy()
|
||
expect(screen.getByText('37')).toBeTruthy()
|
||
fireEvent.click(screen.getByRole('button', { name: /Open/ }))
|
||
expect(replace).toHaveBeenCalledWith('/hse/incidents?status=closed&view=open')
|
||
})
|
||
|
||
it('opens an incident from the row and from the eye action', () => {
|
||
render(<IncidentTable {...props} />)
|
||
const row = screen.getAllByText('SCW1-202607-0002')[0].closest('tr')!
|
||
fireEvent.click(row)
|
||
expect(push).toHaveBeenCalledWith('/hse/incidents/b')
|
||
expect(within(row).getByRole('link', { name: 'Open incident' }).getAttribute('href')).toBe('/hse/incidents/b')
|
||
})
|
||
|
||
it('sorts server-side by writing sort and dir to the URL', () => {
|
||
render(<IncidentTable {...props} />)
|
||
fireEvent.click(screen.getByRole('button', { name: /Reference/ }))
|
||
expect(replace).toHaveBeenCalledWith('/hse/incidents?sort=reference_no&dir=asc')
|
||
expect(screen.getByRole('columnheader', { name: /Reported/ }).getAttribute('aria-sort')).toBe('descending')
|
||
})
|
||
|
||
it('changes a filter chip through the URL and clears it', () => {
|
||
render(<IncidentTable {...props} />)
|
||
fireEvent.change(screen.getByLabelText('Status'), { target: { value: 'closed' } })
|
||
expect(replace).toHaveBeenCalledWith('/hse/incidents?status=closed')
|
||
})
|
||
|
||
it('debounces search into the q param', () => {
|
||
vi.useFakeTimers()
|
||
render(<IncidentTable {...props} />)
|
||
fireEvent.change(screen.getByRole('searchbox', { name: 'Search incidents' }), { target: { value: 'forklift' } })
|
||
expect(replace).not.toHaveBeenCalled()
|
||
act(() => { vi.advanceTimersByTime(300) })
|
||
expect(replace).toHaveBeenCalledWith('/hse/incidents?q=forklift')
|
||
})
|
||
|
||
it('shows the footer count and pages through the URL', () => {
|
||
render(<IncidentTable {...props} />)
|
||
expect(screen.getByText('Showing 1–2 of 248')).toBeTruthy()
|
||
fireEvent.click(screen.getByRole('button', { name: 'Next page' }))
|
||
expect(replace).toHaveBeenCalledWith('/hse/incidents?page=2')
|
||
})
|
||
|
||
it('renders the stacked mobile list', () => {
|
||
const { container } = render(<IncidentTable {...props} />)
|
||
expect(container.querySelector('ul.md\\:hidden')).not.toBeNull()
|
||
})
|
||
|
||
it('shows the empty state with a report action', () => {
|
||
render(<IncidentTable {...props} incidents={[]} total={0} counts={{ all: 0, open: 0, closed: 0 }} />)
|
||
expect(screen.getByText('No incidents found')).toBeTruthy()
|
||
fireEvent.click(screen.getByRole('button', { name: 'Report incident' }))
|
||
expect(push).toHaveBeenCalledWith('/report')
|
||
})
|
||
})
|