Files
ims/tests/lib/incidents/list-params.test.ts
T

46 lines
2.2 KiB
TypeScript

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')
})
})