2021-12-22 22:08:28 +03:00
|
|
|
import { addDays, formatISO, subDays } from 'date-fns';
|
2023-08-04 12:16:01 +03:00
|
|
|
import { formatIsoDate, isBeforeOrEqual, isBetween, parseDate } from '../../../../src/utils/dates/helpers/date';
|
2020-08-22 10:48:55 +03:00
|
|
|
|
|
|
|
describe('date', () => {
|
2021-12-22 22:08:28 +03:00
|
|
|
const now = new Date();
|
|
|
|
|
2020-08-22 10:48:55 +03:00
|
|
|
describe('formatIsoDate', () => {
|
|
|
|
it.each([
|
2021-06-24 21:13:06 +03:00
|
|
|
[
|
2021-06-25 20:52:50 +03:00
|
|
|
parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'),
|
|
|
|
formatISO(parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss')),
|
2021-06-24 21:13:06 +03:00
|
|
|
],
|
2022-03-26 14:17:42 +03:00
|
|
|
['2020-03-05 10:00:10', '2020-03-05 10:00:10'],
|
|
|
|
['foo', 'foo'],
|
|
|
|
[undefined, undefined],
|
|
|
|
[null, null],
|
2020-08-22 10:48:55 +03:00
|
|
|
])('formats date as expected', (date, expected) => {
|
|
|
|
expect(formatIsoDate(date)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
2021-12-22 22:08:28 +03:00
|
|
|
|
|
|
|
describe('isBetween', () => {
|
2021-12-25 21:58:40 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[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],
|
2021-12-22 22:08:28 +03:00
|
|
|
])('returns true when a date is between provided range', (date, start, end, expectedResult) => {
|
|
|
|
expect(isBetween(date, start, end)).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isBeforeOrEqual', () => {
|
2021-12-25 21:58:40 +03:00
|
|
|
it.each([
|
2022-03-26 14:17:42 +03:00
|
|
|
[now, now, true],
|
|
|
|
[now, addDays(now, 1), true],
|
|
|
|
[now, subDays(now, 1), false],
|
2021-12-22 22:08:28 +03:00
|
|
|
])('returns true when the date before or equal to provided one', (date, dateToCompare, expectedResult) => {
|
|
|
|
expect(isBeforeOrEqual(date, dateToCompare)).toEqual(expectedResult);
|
|
|
|
});
|
|
|
|
});
|
2020-08-22 10:48:55 +03:00
|
|
|
});
|