Files
ims/tests/lib/capa/list.test.ts
T

110 lines
5.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
EMPTY_FILTERS, PAGE_SIZE, STATUS_ORDER, VIEWS,
daysLate, distinct, filterCapas, nextSort, paginate, sortCapas, viewCounts,
type CapaRow,
} from '@/lib/capa/list'
const row = (over: Partial<CapaRow>): CapaRow => ({
id: 'x', incident_id: 'i', description: 'Fit guard rail', department: 'Facilities', due_date: '2026-10-10',
priority: 'med', status: 'open', incident_ref: 'SC1-202607-0001', owner_name: 'Tan Mei Ling', ...over,
})
const rows: CapaRow[] = [
row({ id: '1', description: 'Anti-slip coating', due_date: '2026-09-12', priority: 'high', status: 'overdue', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
row({ id: '2', description: 'Forklift refresher', due_date: '2026-10-20', priority: 'low', status: 'in_progress', owner_name: 'Ravi Kumar', department: 'Operations', incident_ref: 'SC3-202607-0002' }),
row({ id: '3', description: 'Guard rail repair', due_date: '2026-10-01', priority: 'med', status: 'pending_verification', owner_name: 'Tan Mei Ling', department: 'Facilities' }),
row({ id: '4', description: 'Signage', due_date: '2026-08-30', priority: 'low', status: 'closed', owner_name: null, department: 'Operations' }),
]
describe('constants', () => {
it('orders statuses for the board and defines the saved views', () => {
expect(STATUS_ORDER).toEqual(['open', 'in_progress', 'overdue', 'reopened', 'pending_verification', 'verified', 'closed'])
expect(VIEWS.map(v => v.key)).toEqual(['all', 'active', 'overdue', 'verification', 'done'])
expect(PAGE_SIZE).toBe(25)
})
})
describe('filterCapas', () => {
it('returns everything for the empty filters', () => {
expect(filterCapas(rows, EMPTY_FILTERS)).toHaveLength(4)
})
it('searches description, incident reference and owner, case-insensitively', () => {
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'GUARD' }).map(r => r.id)).toEqual(['3'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'sc3-' }).map(r => r.id)).toEqual(['2'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, q: 'ravi' }).map(r => r.id)).toEqual(['2'])
})
it('filters by owner, department and priority', () => {
expect(filterCapas(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling' }).map(r => r.id)).toEqual(['1', '3'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, department: 'Operations' }).map(r => r.id)).toEqual(['2', '4'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, priority: 'low' }).map(r => r.id)).toEqual(['2', '4'])
})
it('filters by saved view', () => {
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'overdue' }).map(r => r.id)).toEqual(['1'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'active' }).map(r => r.id)).toEqual(['2'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'verification' }).map(r => r.id)).toEqual(['3'])
expect(filterCapas(rows, { ...EMPTY_FILTERS, view: 'done' }).map(r => r.id)).toEqual(['4'])
})
it('combines filters', () => {
expect(filterCapas(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling', view: 'overdue' }).map(r => r.id)).toEqual(['1'])
})
})
describe('viewCounts', () => {
it('counts each view with the other filters applied', () => {
expect(viewCounts(rows, EMPTY_FILTERS)).toEqual({ all: 4, active: 1, overdue: 1, verification: 1, done: 1 })
expect(viewCounts(rows, { ...EMPTY_FILTERS, owner: 'Tan Mei Ling', view: 'done' })).toEqual({ all: 2, active: 0, overdue: 1, verification: 1, done: 0 })
})
})
describe('sortCapas', () => {
it('sorts by due date both ways without mutating the input', () => {
const copy = [...rows]
expect(sortCapas(rows, { key: 'due_date', dir: 'asc' }).map(r => r.id)).toEqual(['4', '1', '3', '2'])
expect(sortCapas(rows, { key: 'due_date', dir: 'desc' }).map(r => r.id)).toEqual(['2', '3', '1', '4'])
expect(rows).toEqual(copy)
})
it('sorts priority high first and status in board order', () => {
expect(sortCapas(rows, { key: 'priority', dir: 'asc' }).map(r => r.id)).toEqual(['1', '3', '2', '4'])
expect(sortCapas(rows, { key: 'status', dir: 'asc' }).map(r => r.id)).toEqual(['2', '1', '3', '4'])
})
})
describe('nextSort', () => {
it('flips the direction on the same column and starts a new one ascending', () => {
expect(nextSort({ key: 'due_date', dir: 'asc' }, 'due_date')).toEqual({ key: 'due_date', dir: 'desc' })
expect(nextSort({ key: 'due_date', dir: 'desc' }, 'priority')).toEqual({ key: 'priority', dir: 'asc' })
})
})
describe('paginate', () => {
const many = Array.from({ length: 27 }, (_, i) => i + 1)
it('slices a page and reports the range', () => {
expect(paginate(many, 1, 25)).toMatchObject({ from: 1, to: 25, total: 27, page: 1, pageCount: 2 })
expect(paginate(many, 2, 25)).toMatchObject({ from: 26, to: 27, items: [26, 27], page: 2 })
})
it('clamps an out-of-range page and handles empty input', () => {
expect(paginate(many, 9, 25).page).toBe(2)
expect(paginate(many, 0, 25).page).toBe(1)
expect(paginate([], 1, 25)).toMatchObject({ from: 0, to: 0, total: 0, page: 1, pageCount: 1, items: [] })
})
})
describe('distinct and daysLate', () => {
it('returns sorted unique non-empty values', () => {
expect(distinct(['b', 'a', 'b', null, undefined, ''])).toEqual(['a', 'b'])
})
it('counts whole days past the due date, never negative', () => {
expect(daysLate('2026-09-12', '2026-09-27')).toBe(15)
expect(daysLate('2026-10-10', '2026-09-27')).toBe(0)
})
})