mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-19 04:56:17 +00:00
Added some helper function to deal with dates
This commit is contained in:
@@ -8,7 +8,7 @@ describe('<DateIntervalDropdownItems />', () => {
|
||||
const onChange = jest.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
wrapper = shallow(<DateIntervalDropdownItems allText="All" active="last180days" onChange={onChange} />);
|
||||
wrapper = shallow(<DateIntervalDropdownItems allText="All" active="last180Days" onChange={onChange} />);
|
||||
});
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { format, subDays } from 'date-fns';
|
||||
import { endOfDay, format, formatISO, startOfDay, subDays } from 'date-fns';
|
||||
import {
|
||||
DateInterval,
|
||||
dateRangeIsEmpty,
|
||||
dateToMatchingInterval,
|
||||
intervalToDateRange,
|
||||
rangeIsInterval,
|
||||
rangeOrIntervalToString,
|
||||
@@ -9,6 +10,9 @@ import {
|
||||
import { parseDate } from '../../../../src/utils/helpers/date';
|
||||
|
||||
describe('date-types', () => {
|
||||
const now = () => new Date();
|
||||
const daysBack = (days: number) => subDays(new Date(), days);
|
||||
|
||||
describe('dateRangeIsEmpty', () => {
|
||||
it.each([
|
||||
[ undefined, true ],
|
||||
@@ -48,7 +52,7 @@ describe('date-types', () => {
|
||||
[ 'last7Days' as DateInterval, 'Last 7 days' ],
|
||||
[ 'last30Days' as DateInterval, 'Last 30 days' ],
|
||||
[ 'last90Days' as DateInterval, 'Last 90 days' ],
|
||||
[ 'last180days' as DateInterval, 'Last 180 days' ],
|
||||
[ 'last180Days' as DateInterval, 'Last 180 days' ],
|
||||
[ 'last365Days' as DateInterval, 'Last 365 days' ],
|
||||
[{}, undefined ],
|
||||
[{ startDate: null }, undefined ],
|
||||
@@ -71,8 +75,6 @@ describe('date-types', () => {
|
||||
});
|
||||
|
||||
describe('intervalToDateRange', () => {
|
||||
const now = () => new Date();
|
||||
const daysBack = (days: number) => subDays(new Date(), days);
|
||||
const formatted = (date?: Date | null): string | undefined => !date ? undefined : format(date, 'yyyy-MM-dd');
|
||||
|
||||
it.each([
|
||||
@@ -82,7 +84,7 @@ describe('date-types', () => {
|
||||
[ 'last7Days' as DateInterval, daysBack(7), now() ],
|
||||
[ 'last30Days' as DateInterval, daysBack(30), now() ],
|
||||
[ 'last90Days' as DateInterval, daysBack(90), now() ],
|
||||
[ 'last180days' as DateInterval, daysBack(180), now() ],
|
||||
[ 'last180Days' as DateInterval, daysBack(180), now() ],
|
||||
[ 'last365Days' as DateInterval, daysBack(365), now() ],
|
||||
])('returns proper result', (interval, expectedStartDate, expectedEndDate) => {
|
||||
const { startDate, endDate } = intervalToDateRange(interval);
|
||||
@@ -91,4 +93,27 @@ describe('date-types', () => {
|
||||
expect(formatted(expectedEndDate)).toEqual(formatted(endDate));
|
||||
});
|
||||
});
|
||||
|
||||
describe('dateToMatchingInterval', () => {
|
||||
it.each([
|
||||
[ 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' ],
|
||||
])('returns the first interval which contains provided date', (date, expectedInterval) => {
|
||||
expect(dateToMatchingInterval(date)).toEqual(expectedInterval);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { formatISO } from 'date-fns';
|
||||
import { formatDate, formatIsoDate, parseDate } from '../../../src/utils/helpers/date';
|
||||
import { addDays, formatISO, subDays } from 'date-fns';
|
||||
import { formatDate, formatIsoDate, isBeforeOrEqual, isBetween, parseDate } from '../../../src/utils/helpers/date';
|
||||
|
||||
describe('date', () => {
|
||||
const now = new Date();
|
||||
|
||||
describe('formatDate', () => {
|
||||
it.each([
|
||||
[ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 'dd/MM/yyyy', '05/03/2020' ],
|
||||
@@ -30,4 +32,32 @@ describe('date', () => {
|
||||
expect(formatIsoDate(date)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isBetween', () => {
|
||||
test.each([
|
||||
[ now, undefined, undefined, true ],
|
||||
[ now, subDays(now, 1), undefined, true ],
|
||||
[ now, now, undefined, true ],
|
||||
[ now, undefined, addDays(now, 1), true ],
|
||||
[ now, undefined, now, true ],
|
||||
[ now, subDays(now, 1), addDays(now, 1), true ],
|
||||
[ now, now, now, true ],
|
||||
[ now, addDays(now, 1), undefined, false ],
|
||||
[ now, undefined, subDays(now, 1), false ],
|
||||
[ now, subDays(now, 3), subDays(now, 1), false ],
|
||||
[ now, addDays(now, 1), addDays(now, 3), false ],
|
||||
])('returns true when a date is between provided range', (date, start, end, expectedResult) => {
|
||||
expect(isBetween(date, start, end)).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isBeforeOrEqual', () => {
|
||||
test.each([
|
||||
[ now, now, true ],
|
||||
[ now, addDays(now, 1), true ],
|
||||
[ now, subDays(now, 1), false ],
|
||||
])('returns true when the date before or equal to provided one', (date, dateToCompare, expectedResult) => {
|
||||
expect(isBeforeOrEqual(date, dateToCompare)).toEqual(expectedResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user