mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 22:31:52 +00:00
Replaced most of the usages of moment with date-fns
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { shallow, ShallowWrapper } from 'enzyme';
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import moment from 'moment';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import DateInput, { DateInputProps } from '../../src/utils/DateInput';
|
||||
|
||||
@@ -30,7 +29,7 @@ describe('<DateInput />', () => {
|
||||
});
|
||||
|
||||
it('does not show calendar icon when input is clearable', () => {
|
||||
wrapped = createComponent({ isClearable: true, selected: moment() });
|
||||
wrapped = createComponent({ isClearable: true, selected: new Date() });
|
||||
expect(wrapped.find(FontAwesomeIcon)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { shallow, ShallowWrapper } from 'enzyme';
|
||||
import { DropdownItem } from 'reactstrap';
|
||||
import moment from 'moment';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import { DateRangeSelector, DateRangeSelectorProps } from '../../../src/utils/dates/DateRangeSelector';
|
||||
import { DateInterval } from '../../../src/utils/dates/types';
|
||||
@@ -40,7 +39,7 @@ describe('<DateRangeSelector />', () => {
|
||||
[ 'last90Days' as DateInterval, 0, 1 ],
|
||||
[ 'last180days' as DateInterval, 0, 1 ],
|
||||
[ 'last365Days' as DateInterval, 0, 1 ],
|
||||
[{ startDate: moment() }, 0, 0 ],
|
||||
[{ startDate: new Date() }, 0, 0 ],
|
||||
])('sets proper element as active based on provided date range', (
|
||||
initialDateRange,
|
||||
expectedActiveItems,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import moment from 'moment';
|
||||
import { format, parse, subDays } from 'date-fns';
|
||||
import {
|
||||
DateInterval,
|
||||
dateRangeIsEmpty,
|
||||
@@ -20,9 +20,9 @@ describe('date-types', () => {
|
||||
[{ startDate: undefined, endDate: undefined }, true ],
|
||||
[{ startDate: undefined, endDate: null }, true ],
|
||||
[{ startDate: null, endDate: undefined }, true ],
|
||||
[{ startDate: moment() }, false ],
|
||||
[{ endDate: moment() }, false ],
|
||||
[{ startDate: moment(), endDate: moment() }, false ],
|
||||
[{ startDate: new Date() }, false ],
|
||||
[{ endDate: new Date() }, false ],
|
||||
[{ startDate: new Date(), endDate: new Date() }, false ],
|
||||
])('proper result is returned', (dateRange, expectedResult) => {
|
||||
expect(dateRangeIsEmpty(dateRange)).toEqual(expectedResult);
|
||||
});
|
||||
@@ -58,31 +58,39 @@ describe('date-types', () => {
|
||||
[{ startDate: undefined, endDate: undefined }, undefined ],
|
||||
[{ startDate: undefined, endDate: null }, undefined ],
|
||||
[{ startDate: null, endDate: undefined }, undefined ],
|
||||
[{ startDate: moment('2020-01-01') }, 'Since 2020-01-01' ],
|
||||
[{ endDate: moment('2020-01-01') }, 'Until 2020-01-01' ],
|
||||
[{ startDate: moment('2020-01-01'), endDate: moment('2021-02-02') }, '2020-01-01 - 2021-02-02' ],
|
||||
[{ startDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()) }, 'Since 2020-01-01' ],
|
||||
[{ endDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()) }, 'Until 2020-01-01' ],
|
||||
[
|
||||
{
|
||||
startDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()),
|
||||
endDate: parse('2021-02-02', 'yyyy-MM-dd', new Date()),
|
||||
},
|
||||
'2020-01-01 - 2021-02-02',
|
||||
],
|
||||
])('proper result is returned', (range, expectedValue) => {
|
||||
expect(rangeOrIntervalToString(range)).toEqual(expectedValue);
|
||||
});
|
||||
});
|
||||
|
||||
describe('intervalToDateRange', () => {
|
||||
const now = () => moment();
|
||||
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');
|
||||
|
||||
test.each([
|
||||
[ undefined, undefined, undefined ],
|
||||
[ 'today' as DateInterval, now(), now() ],
|
||||
[ 'yesterday' as DateInterval, now().subtract(1, 'day'), now().subtract(1, 'day') ],
|
||||
[ 'last7Days' as DateInterval, now().subtract(7, 'day'), now() ],
|
||||
[ 'last30Days' as DateInterval, now().subtract(30, 'day'), now() ],
|
||||
[ 'last90Days' as DateInterval, now().subtract(90, 'day'), now() ],
|
||||
[ 'last180days' as DateInterval, now().subtract(180, 'day'), now() ],
|
||||
[ 'last365Days' as DateInterval, now().subtract(365, 'day'), 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() ],
|
||||
])('proper result is returned', (interval, expectedStartDate, expectedEndDate) => {
|
||||
const { startDate, endDate } = intervalToDateRange(interval);
|
||||
|
||||
expect(expectedStartDate?.format('YYYY-MM-DD')).toEqual(startDate?.format('YYYY-MM-DD'));
|
||||
expect(expectedEndDate?.format('YYYY-MM-DD')).toEqual(endDate?.format('YYYY-MM-DD'));
|
||||
expect(formatted(expectedStartDate)).toEqual(formatted(startDate));
|
||||
expect(formatted(expectedEndDate)).toEqual(formatted(endDate));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import moment from 'moment';
|
||||
import { formatISO, parse } from 'date-fns';
|
||||
import { formatDate, formatIsoDate } from '../../../src/utils/helpers/date';
|
||||
|
||||
describe('date', () => {
|
||||
describe('formatDate', () => {
|
||||
it.each([
|
||||
[ moment('2020-03-05 10:00:10'), 'DD/MM/YYYY', '05/03/2020' ],
|
||||
[ moment('2020-03-05 10:00:10'), 'YYYY-MM', '2020-03' ],
|
||||
[ moment('2020-03-05 10:00:10'), undefined, '2020-03-05' ],
|
||||
[ '2020-03-05 10:00:10', 'DD-MM-YYYY', '2020-03-05 10:00:10' ],
|
||||
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), 'dd/MM/yyyy', '05/03/2020' ],
|
||||
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), 'yyyy-MM', '2020-03' ],
|
||||
[ parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()), undefined, '2020-03-05' ],
|
||||
[ '2020-03-05 10:00:10', 'dd-MM-yyyy', '2020-03-05 10:00:10' ],
|
||||
[ '2020-03-05 10:00:10', undefined, '2020-03-05 10:00:10' ],
|
||||
[ undefined, undefined, undefined ],
|
||||
[ null, undefined, null ],
|
||||
@@ -18,7 +18,10 @@ describe('date', () => {
|
||||
|
||||
describe('formatIsoDate', () => {
|
||||
it.each([
|
||||
[ moment('2020-03-05 10:00:10'), moment('2020-03-05 10:00:10').format() ],
|
||||
[
|
||||
parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date()),
|
||||
formatISO(parse('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss', new Date())),
|
||||
],
|
||||
[ '2020-03-05 10:00:10', '2020-03-05 10:00:10' ],
|
||||
[ 'foo', 'foo' ],
|
||||
[ undefined, undefined ],
|
||||
|
||||
Reference in New Issue
Block a user