mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 01:20:24 +03:00
Added test for toDateRange helper function
This commit is contained in:
parent
e5185f2099
commit
5bd89efc09
3 changed files with 42 additions and 16 deletions
|
@ -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<DateInterval | undefined>(
|
||||
initialIntervalIsRange ? initialDateRange : undefined,
|
||||
);
|
||||
const [activeDateRange, setActiveDateRange] = useState(initialIntervalIsRange ? undefined : initialDateRange);
|
||||
|
||||
const updateDateRange = (dateRange: DateRange) => {
|
||||
|
|
|
@ -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<DateInterval, string | undefined> = {
|
||||
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<DateInterval, string | undefined> = {
|
|||
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],
|
||||
])();
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue