import { toDateRange } from '@/lib/dateRange'; /** * The three shapes the DatePicker emits, and what a page must do with them. * * The bug: the Deliveries page ran every emitted value through * `dayjs(d).format('YYYY-MM-DD')`. For a range preset that is * `dayjs({ from, to })` and for All Dates it is `dayjs('all')` — both Invalid * Date — so the filter compared every row against the string "Invalid Date". */ describe('toDateRange', () => { it('turns a single day into a one-day range', () => { expect(toDateRange('2026-09-22')).toEqual({ from: '2026-09-22', to: '2026-09-22' }); }); it('passes a range preset straight through — This Month, Last 7 Days', () => { expect(toDateRange({ from: '2026-09-01', to: '2026-09-22' })).toEqual({ from: '2026-09-01', to: '2026-09-22' }); }); it('turns All Dates into no bounds, not into a date', () => { expect(toDateRange('all')).toEqual({ from: '', to: '' }); }); it('treats a cleared picker as no bounds', () => { expect(toDateRange(null)).toEqual({ from: '', to: '' }); expect(toDateRange(undefined)).toEqual({ from: '', to: '' }); expect(toDateRange('')).toEqual({ from: '', to: '' }); }); it('keeps a half-open range open on the missing side', () => { // An empty bound means unbounded there, which is what the API layer's // inRange already expects — not "filter everything out". expect(toDateRange({ from: '2026-09-01' })).toEqual({ from: '2026-09-01', to: '' }); expect(toDateRange({ to: '2026-09-22' })).toEqual({ from: '', to: '2026-09-22' }); expect(toDateRange({})).toEqual({ from: '', to: '' }); }); it('never produces "Invalid Date" — the actual defect, in one assertion', () => { const values = ['all', null, undefined, {}, { from: '2026-09-01', to: '2026-09-22' }, '2026-09-22']; values.forEach((v) => { const { from, to } = toDateRange(v); expect(from).not.toMatch(/Invalid/); expect(to).not.toMatch(/Invalid/); }); }); it('is empty, not a crash, for a shape nobody should pass', () => { expect(toDateRange(20260922)).toEqual({ from: '', to: '' }); expect(toDateRange(true)).toEqual({ from: '', to: '' }); }); }); describe('toReportRange — DatePicker value → report { from, to }', () => { const { toReportRange } = require('@/lib/dateRange'); const dayjsLib = require('dayjs'); const todayStr = dayjsLib().format('YYYY-MM-DD'); const past = (n) => dayjsLib().subtract(n, 'day').format('YYYY-MM-DD'); const future = (n) => dayjsLib().add(n, 'day').format('YYYY-MM-DD'); it('turns a single day into a one-day range', () => { expect(toReportRange(past(3))).toEqual({ from: past(3), to: past(3) }); }); it('keeps a valid range as it is', () => { expect(toReportRange({ from: past(7), to: past(1) })).toEqual({ from: past(7), to: past(1) }); }); it('puts an inverted range in order', () => { expect(toReportRange({ from: past(1), to: past(7) })).toEqual({ from: past(7), to: past(1) }); }); it('never goes past today', () => { expect(toReportRange(future(2))).toEqual({ from: todayStr, to: todayStr }); expect(toReportRange({ from: past(2), to: future(5) })).toEqual({ from: past(2), to: todayStr }); }); it('falls back to today for "all" or nothing', () => { expect(toReportRange('all')).toEqual({ from: todayStr, to: todayStr }); expect(toReportRange(undefined)).toEqual({ from: todayStr, to: todayStr }); }); });