feat(design): migrate the Incidents list to TableCard with server-side views, sort, filters and paging

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 09:33:44 +08:00
co-authored by Claude Haiku 4.5
parent 9ba708ecb7
commit a64b951922
15 changed files with 576 additions and 547 deletions
@@ -1,53 +0,0 @@
import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { IncidentList } from '@/components/incidents/incident-list'
const mockIncidents = [
{
id: 'inc-001',
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' },
reporter: { name: 'John Doe' },
},
{
id: 'inc-002',
reference_no: 'SCW1-202607-0002',
incident_type: 'injury',
status: 'triaged',
severity: 3,
reported_at: '2026-07-10T10:00:00Z',
sites: { name: 'SCW1' },
zones: { name: 'Loading Bay' },
reporter: { name: 'Jane Smith' },
},
]
describe('IncidentList', () => {
it('renders all incident rows', () => {
render(<IncidentList incidents={mockIncidents as any} basePath="/hse" />)
expect(screen.getByText('SCW1-202607-0001')).toBeTruthy()
expect(screen.getByText('SCW1-202607-0002')).toBeTruthy()
})
it('shows incident type labels', () => {
render(<IncidentList incidents={mockIncidents as any} basePath="/hse" />)
expect(screen.getByText('Near Miss')).toBeTruthy()
expect(screen.getByText('Injury / Medical')).toBeTruthy()
})
it('shows status badges', () => {
render(<IncidentList incidents={mockIncidents as any} basePath="/hse" />)
expect(screen.getByText('REPORTED')).toBeTruthy()
expect(screen.getByText('TRIAGED')).toBeTruthy()
})
it('renders empty state when no incidents', () => {
render(<IncidentList incidents={[]} basePath="/hse" />)
expect(screen.getByText(/no incidents/i)).toBeTruthy()
})
})
@@ -0,0 +1,107 @@
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')
})
})
+7 -1
View File
@@ -1,6 +1,6 @@
import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { Button } from '@/components/ui/button'
import { Button, buttonClass } from '@/components/ui/button'
import { StatusTag, PriorityTag, statusHue } from '@/components/ui/status-tag'
import { Checkbox } from '@/components/ui/checkbox'
import { FilterChip } from '@/components/ui/filter-chip'
@@ -35,6 +35,12 @@ describe('Button', () => {
expect(b.className).toContain('h-7')
})
it('buttonClass gives links the same look as buttons', () => {
expect(buttonClass()).toContain('bg-ink-2')
expect(buttonClass('secondary')).toContain('shadow-secondary')
expect(buttonClass('primary', { block: true })).toContain('w-full')
})
it('mobile and block make a full-width 48px button', () => {
render(<Button block mobile>Submit</Button>)
const b = screen.getByRole('button', { name: 'Submit' })
+45
View File
@@ -0,0 +1,45 @@
import { describe, it, expect } from 'vitest'
import { parseListParams, nextSort, withParams, PAGE_SIZE, OPEN_STATUSES } from '@/lib/incidents/list-params'
describe('parseListParams', () => {
it('applies defaults', () => {
expect(parseListParams({})).toEqual({
page: 1, q: '', status: '', type: '', siteId: '', truckId: '', view: 'all', sort: 'reported_at', dir: 'desc',
})
})
it('reads every param and rejects bad values', () => {
const p = parseListParams({ page: '3', q: 'fork', status: 'closed', type: 'injury', site_id: 's1', truck_id: 't1', view: 'open', sort: 'status', dir: 'asc' })
expect(p).toMatchObject({ page: 3, q: 'fork', status: 'closed', siteId: 's1', truckId: 't1', view: 'open', sort: 'status', dir: 'asc' })
expect(parseListParams({ page: '-4' }).page).toBe(1)
expect(parseListParams({ page: 'x' }).page).toBe(1)
expect(parseListParams({ view: 'bogus' }).view).toBe('all')
expect(parseListParams({ sort: 'drop table' }).sort).toBe('reported_at')
expect(parseListParams({ dir: 'sideways' }).dir).toBe('desc')
})
it('exposes the page size and the open statuses', () => {
expect(PAGE_SIZE).toBe(25)
expect(OPEN_STATUSES).toEqual(['reported', 'triaged', 'investigating', 'capa_pending', 'verification'])
})
})
describe('nextSort', () => {
it('flips the direction on the same column', () => {
expect(nextSort({ key: 'reference_no', dir: 'asc' }, 'reference_no')).toEqual({ key: 'reference_no', dir: 'desc' })
expect(nextSort({ key: 'reference_no', dir: 'desc' }, 'reference_no')).toEqual({ key: 'reference_no', dir: 'asc' })
})
it('starts a new column ascending, but the date column newest first', () => {
expect(nextSort({ key: 'reported_at', dir: 'desc' }, 'status')).toEqual({ key: 'status', dir: 'asc' })
expect(nextSort({ key: 'status', dir: 'asc' }, 'reported_at')).toEqual({ key: 'reported_at', dir: 'desc' })
})
})
describe('withParams', () => {
it('sets and removes params and resets the page', () => {
expect(withParams('?status=closed&page=3', { q: 'fork' })).toBe('?status=closed&q=fork')
expect(withParams('?status=closed', { status: null })).toBe('')
expect(withParams('?page=2', { page: '4' })).toBe('?page=4')
})
})