Files
doormilxpress_astryx/tests/lib/dateRange.test.js

55 lines
2.2 KiB
JavaScript

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