From c3e30a3d33c55a5ba4a2b4daf7deb3faefa6584f Mon Sep 17 00:00:00 2001 From: ShafinNigamana Date: Fri, 12 Jun 2026 01:57:41 +0530 Subject: [PATCH] \test(dateRange): add empty-fallback test and robust parsing" --- utils/dateRange.empty-fallback.test.ts | 40 ++++++++++++++++++++++++++ utils/dateRange.ts | 9 ++++-- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 utils/dateRange.empty-fallback.test.ts diff --git a/utils/dateRange.empty-fallback.test.ts b/utils/dateRange.empty-fallback.test.ts new file mode 100644 index 000000000..306529283 --- /dev/null +++ b/utils/dateRange.empty-fallback.test.ts @@ -0,0 +1,40 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { describe, expect, it } from 'vitest'; +import { formatDateRange, DEFAULT_DATE_RANGE } from './dateRange'; + +describe('formatDateRange - Edge Cases & Empty/Missing Inputs Verification', () => { + it('1. falls back to default date range when year is completely omitted', () => { + expect(formatDateRange()).toEqual(DEFAULT_DATE_RANGE); + }); + + it('2. falls back to default date range when year is undefined', () => { + expect(formatDateRange(undefined)).toEqual(DEFAULT_DATE_RANGE); + }); + + it('3. falls back to default date range when year is null', () => { + expect(formatDateRange(null as any)).toEqual(DEFAULT_DATE_RANGE); + }); + + it('4. falls back to default date range when year is an empty or whitespace string', () => { + expect(formatDateRange('')).toEqual(DEFAULT_DATE_RANGE); + expect(formatDateRange(' ')).toEqual(DEFAULT_DATE_RANGE); + }); + + it('5. processes numerical years correctly without throwing', () => { + expect(formatDateRange(2024)).toEqual({ + from: '2024-01-01T00:00:00Z', + to: '2024-12-31T23:59:59Z', + }); + + expect(formatDateRange(24)).toEqual({ + from: '2024-01-01T00:00:00Z', + to: '2024-12-31T23:59:59Z', + }); + }); + + it('6. falls back to default date range when year resolves to invalid numeric bounds or NaN', () => { + expect(formatDateRange(NaN as any)).toEqual(DEFAULT_DATE_RANGE); + expect(formatDateRange(false as any)).toEqual(DEFAULT_DATE_RANGE); + expect(formatDateRange({} as any)).toEqual(DEFAULT_DATE_RANGE); + }); +}); diff --git a/utils/dateRange.ts b/utils/dateRange.ts index 6d47168a0..d3a17a27a 100644 --- a/utils/dateRange.ts +++ b/utils/dateRange.ts @@ -28,12 +28,15 @@ export interface DateRange { * formatDateRange("") // returns DEFAULT_DATE_RANGE * formatDateRange(undefined) // returns DEFAULT_DATE_RANGE */ -export function formatDateRange(year?: string): DateRange { - if (!year || year.trim() === '') { +export function formatDateRange(year?: string | number | null): DateRange { + if (year === undefined || year === null) { return { ...DEFAULT_DATE_RANGE }; } - const trimmedYear = year.trim(); + const trimmedYear = String(year).trim(); + if (trimmedYear === '') { + return { ...DEFAULT_DATE_RANGE }; + } // Handle partial years (1 or 2 digits): assume 20xx // For 2-digit: '24' -> 2024