From 5bd89efc09d56e39d1ff5531e83d85fe977e8329 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 3 Dec 2022 12:45:25 +0100 Subject: [PATCH] Added test for toDateRange helper function --- src/utils/dates/DateRangeSelector.tsx | 4 ++- src/utils/helpers/dateIntervals.ts | 30 +++++++++++-------- .../dateIntervals.test.ts} | 24 +++++++++++++-- 3 files changed, 42 insertions(+), 16 deletions(-) rename test/utils/{dates/types/index.test.ts => helpers/dateIntervals.test.ts} (80%) diff --git a/src/utils/dates/DateRangeSelector.tsx b/src/utils/dates/DateRangeSelector.tsx index 72096c82..652d3c11 100644 --- a/src/utils/dates/DateRangeSelector.tsx +++ b/src/utils/dates/DateRangeSelector.tsx @@ -25,7 +25,9 @@ export const DateRangeSelector = ( { onDatesChange, initialDateRange, defaultText, disabled, updatable = false }: DateRangeSelectorProps, ) => { const initialIntervalIsRange = rangeIsInterval(initialDateRange); - const [activeInterval, setActiveInterval] = useState(initialIntervalIsRange ? initialDateRange : undefined); + const [activeInterval, setActiveInterval] = useState( + initialIntervalIsRange ? initialDateRange : undefined, + ); const [activeDateRange, setActiveDateRange] = useState(initialIntervalIsRange ? undefined : initialDateRange); const updateDateRange = (dateRange: DateRange) => { diff --git a/src/utils/helpers/dateIntervals.ts b/src/utils/helpers/dateIntervals.ts index 9e0f0f00..f81addb8 100644 --- a/src/utils/helpers/dateIntervals.ts +++ b/src/utils/helpers/dateIntervals.ts @@ -7,14 +7,8 @@ export interface DateRange { endDate?: Date | null; } -export type DateInterval = 'all' | 'today' | 'yesterday' | 'last7Days' | 'last30Days' | 'last90Days' | 'last180Days' | 'last365Days'; - -export const dateRangeIsEmpty = (dateRange?: DateRange): boolean => dateRange === undefined - || isEmpty(filter(Boolean, dateRange as any)); - -export const rangeIsInterval = (range?: DateRange | DateInterval): range is DateInterval => typeof range === 'string'; - -const INTERVAL_TO_STRING_MAP: Record = { +const ALL = 'all'; +const INTERVAL_TO_STRING_MAP = { today: 'Today', yesterday: 'Yesterday', last7Days: 'Last 7 days', @@ -22,10 +16,20 @@ const INTERVAL_TO_STRING_MAP: Record = { last90Days: 'Last 90 days', last180Days: 'Last 180 days', last365Days: 'Last 365 days', - all: undefined, + [ALL]: undefined, }; -export const DATE_INTERVALS = Object.keys(INTERVAL_TO_STRING_MAP).filter((value) => value !== 'all') as DateInterval[]; +export type DateInterval = keyof typeof INTERVAL_TO_STRING_MAP; + +const INTERVALS = Object.keys(INTERVAL_TO_STRING_MAP) as DateInterval[]; + +export const dateRangeIsEmpty = (dateRange?: DateRange): boolean => dateRange === undefined + || isEmpty(filter(Boolean, dateRange as any)); + +export const rangeIsInterval = (range?: DateRange | DateInterval): range is DateInterval => + typeof range === 'string' && INTERVALS.includes(range); + +export const DATE_INTERVALS = INTERVALS.filter((value) => value !== ALL) as DateInterval[]; export const datesToDateRange = (startDate?: string, endDate?: string): DateRange => ({ startDate: dateOrNull(startDate), @@ -49,7 +53,7 @@ const dateRangeToString = (range?: DateRange): string | undefined => { }; export const rangeOrIntervalToString = (range?: DateRange | DateInterval): string | undefined => { - if (!range || range === 'all') { + if (!range || range === ALL) { return undefined; } @@ -64,7 +68,7 @@ const startOfDaysAgo = (daysAgo: number) => startOfDay(subDays(new Date(), daysA const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(new Date()) }); export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => { - if (!dateInterval || dateInterval === 'all') { + if (!dateInterval || dateInterval === ALL) { return {}; } @@ -99,7 +103,7 @@ export const dateToMatchingInterval = (date: DateOrString): DateInterval => { [() => isBeforeOrEqual(startOfDaysAgo(90), theDate), () => 'last90Days'], [() => isBeforeOrEqual(startOfDaysAgo(180), theDate), () => 'last180Days'], [() => isBeforeOrEqual(startOfDaysAgo(365), theDate), () => 'last365Days'], - [T, () => 'all'], + [T, () => ALL], ])(); }; diff --git a/test/utils/dates/types/index.test.ts b/test/utils/helpers/dateIntervals.test.ts similarity index 80% rename from test/utils/dates/types/index.test.ts rename to test/utils/helpers/dateIntervals.test.ts index 34dd22e5..37ebe6ff 100644 --- a/test/utils/dates/types/index.test.ts +++ b/test/utils/helpers/dateIntervals.test.ts @@ -6,8 +6,9 @@ import { intervalToDateRange, rangeIsInterval, rangeOrIntervalToString, -} from '../../../../src/utils/helpers/dateIntervals'; -import { parseDate } from '../../../../src/utils/helpers/date'; + toDateRange, +} from '../../../src/utils/helpers/dateIntervals'; +import { parseDate } from '../../../src/utils/helpers/date'; describe('date-types', () => { const now = () => new Date(); @@ -116,4 +117,23 @@ describe('date-types', () => { expect(dateToMatchingInterval(date)).toEqual(expectedInterval); }); }); + + describe('toDateRange', () => { + it.each([ + ['today' as DateInterval, intervalToDateRange('today')], + ['yesterday' as DateInterval, intervalToDateRange('yesterday')], + ['last7Days' as DateInterval, intervalToDateRange('last7Days')], + ['last30Days' as DateInterval, intervalToDateRange('last30Days')], + ['last90Days' as DateInterval, intervalToDateRange('last90Days')], + ['last180Days' as DateInterval, intervalToDateRange('last180Days')], + ['last365Days' as DateInterval, intervalToDateRange('last365Days')], + ['all' as DateInterval, intervalToDateRange('all')], + [{}, {}], + [{ startDate: now() }, { startDate: now() }], + [{ endDate: now() }, { endDate: now() }], + [{ startDate: daysBack(10), endDate: now() }, { startDate: daysBack(10), endDate: now() }], + ])('returns properly parsed interval or range', (rangeOrInterval, expectedResult) => { + expect(toDateRange(rangeOrInterval)).toEqual(expectedResult); + }); + }); });