2021-12-22 22:08:28 +03:00
|
|
|
import { endOfDay, format, formatISO, startOfDay, subDays } from 'date-fns';
|
2020-12-15 00:58:15 +03:00
|
|
|
import {
|
|
|
|
DateInterval,
|
|
|
|
dateRangeIsEmpty,
|
2021-12-22 22:08:28 +03:00
|
|
|
dateToMatchingInterval,
|
2020-12-15 00:58:15 +03:00
|
|
|
intervalToDateRange,
|
|
|
|
rangeIsInterval,
|
|
|
|
rangeOrIntervalToString,
|
2022-12-03 14:45:25 +03:00
|
|
|
toDateRange,
|
|
|
|
} from '../../../src/utils/helpers/dateIntervals';
|
|
|
|
import { parseDate } from '../../../src/utils/helpers/date';
|
2020-12-15 00:58:15 +03:00
|
|
|
|
|
|
|
describe('date-types', () => {
|
2021-12-22 22:08:28 +03:00
|
|
|
const now = () => new Date();
|
|
|
|
const daysBack = (days: number) => subDays(new Date(), days);
|
|
|
|
|
2020-12-15 00:58:15 +03:00
|
|
|
describe('dateRangeIsEmpty', () => {
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, true],
|
|
|
|
[{}, true],
|
|
|
|
[{ startDate: null }, true],
|
|
|
|
[{ endDate: null }, true],
|
|
|
|
[{ startDate: null, endDate: null }, true],
|
|
|
|
[{ startDate: undefined }, true],
|
|
|
|
[{ endDate: undefined }, true],
|
|
|
|
[{ startDate: undefined, endDate: undefined }, true],
|
|
|
|
[{ startDate: undefined, endDate: null }, true],
|
|
|
|
[{ startDate: null, endDate: undefined }, true],
|
|
|
|
[{ startDate: new Date() }, false],
|
|
|
|
[{ endDate: new Date() }, false],
|
|
|
|
[{ startDate: new Date(), endDate: new Date() }, false],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('returns proper result', (dateRange, expectedResult) => {
|
2020-12-15 00:58:15 +03:00
|
|
|
expect(dateRangeIsEmpty(dateRange)).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rangeIsInterval', () => {
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, false],
|
|
|
|
[{}, false],
|
|
|
|
['today' as DateInterval, true],
|
|
|
|
['yesterday' as DateInterval, true],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('returns proper result', (range, expectedResult) => {
|
2020-12-15 00:58:15 +03:00
|
|
|
expect(rangeIsInterval(range)).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rangeOrIntervalToString', () => {
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, undefined],
|
|
|
|
['today' as DateInterval, 'Today'],
|
|
|
|
['yesterday' as DateInterval, 'Yesterday'],
|
|
|
|
['last7Days' as DateInterval, 'Last 7 days'],
|
|
|
|
['last30Days' as DateInterval, 'Last 30 days'],
|
|
|
|
['last90Days' as DateInterval, 'Last 90 days'],
|
|
|
|
['last180Days' as DateInterval, 'Last 180 days'],
|
|
|
|
['last365Days' as DateInterval, 'Last 365 days'],
|
|
|
|
[{}, undefined],
|
|
|
|
[{ startDate: null }, undefined],
|
|
|
|
[{ endDate: null }, undefined],
|
|
|
|
[{ startDate: null, endDate: null }, undefined],
|
|
|
|
[{ startDate: undefined }, undefined],
|
|
|
|
[{ endDate: undefined }, undefined],
|
|
|
|
[{ startDate: undefined, endDate: undefined }, undefined],
|
|
|
|
[{ startDate: undefined, endDate: null }, undefined],
|
|
|
|
[{ startDate: null, endDate: undefined }, undefined],
|
|
|
|
[{ startDate: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Since 2020-01-01'],
|
|
|
|
[{ endDate: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Until 2020-01-01'],
|
2021-06-24 21:13:06 +03:00
|
|
|
[
|
2021-06-25 20:52:50 +03:00
|
|
|
{ startDate: parseDate('2020-01-01', 'yyyy-MM-dd'), endDate: parseDate('2021-02-02', 'yyyy-MM-dd') },
|
2021-06-24 21:13:06 +03:00
|
|
|
'2020-01-01 - 2021-02-02',
|
|
|
|
],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('returns proper result', (range, expectedValue) => {
|
2020-12-15 00:58:15 +03:00
|
|
|
expect(rangeOrIntervalToString(range)).toEqual(expectedValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('intervalToDateRange', () => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const formatted = (date?: Date | null): string | undefined => (!date ? undefined : format(date, 'yyyy-MM-dd'));
|
2020-12-15 00:58:15 +03:00
|
|
|
|
2021-09-20 23:00:34 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[undefined, undefined, undefined],
|
|
|
|
['today' as DateInterval, now(), now()],
|
|
|
|
['yesterday' as DateInterval, daysBack(1), daysBack(1)],
|
|
|
|
['last7Days' as DateInterval, daysBack(7), now()],
|
|
|
|
['last30Days' as DateInterval, daysBack(30), now()],
|
|
|
|
['last90Days' as DateInterval, daysBack(90), now()],
|
|
|
|
['last180Days' as DateInterval, daysBack(180), now()],
|
|
|
|
['last365Days' as DateInterval, daysBack(365), now()],
|
2021-09-20 23:00:34 +03:00
|
|
|
])('returns proper result', (interval, expectedStartDate, expectedEndDate) => {
|
2020-12-15 00:58:15 +03:00
|
|
|
const { startDate, endDate } = intervalToDateRange(interval);
|
|
|
|
|
2021-06-24 21:13:06 +03:00
|
|
|
expect(formatted(expectedStartDate)).toEqual(formatted(startDate));
|
|
|
|
expect(formatted(expectedEndDate)).toEqual(formatted(endDate));
|
2020-12-15 00:58:15 +03:00
|
|
|
});
|
|
|
|
});
|
2021-12-22 22:08:28 +03:00
|
|
|
|
|
|
|
describe('dateToMatchingInterval', () => {
|
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[startOfDay(now()), 'today'],
|
|
|
|
[now(), 'today'],
|
|
|
|
[formatISO(now()), 'today'],
|
|
|
|
[daysBack(1), 'yesterday'],
|
|
|
|
[endOfDay(daysBack(1)), 'yesterday'],
|
|
|
|
[daysBack(2), 'last7Days'],
|
|
|
|
[daysBack(7), 'last7Days'],
|
|
|
|
[startOfDay(daysBack(7)), 'last7Days'],
|
|
|
|
[daysBack(18), 'last30Days'],
|
|
|
|
[daysBack(29), 'last30Days'],
|
|
|
|
[daysBack(58), 'last90Days'],
|
|
|
|
[startOfDay(daysBack(90)), 'last90Days'],
|
|
|
|
[daysBack(120), 'last180Days'],
|
|
|
|
[daysBack(250), 'last365Days'],
|
|
|
|
[daysBack(366), 'all'],
|
|
|
|
[formatISO(daysBack(500)), 'all'],
|
2021-12-22 22:08:28 +03:00
|
|
|
])('returns the first interval which contains provided date', (date, expectedInterval) => {
|
|
|
|
expect(dateToMatchingInterval(date)).toEqual(expectedInterval);
|
|
|
|
});
|
|
|
|
});
|
2022-12-03 14:45:25 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2020-12-15 00:58:15 +03:00
|
|
|
});
|