From 3999d14bab09ed6690b6fa946b8107a503f37438 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 25 Jun 2021 19:52:50 +0200 Subject: [PATCH] Created abstraction function to parse dates --- src/utils/helpers/date.ts | 4 +++- test/short-urls/ShortUrlForm.test.tsx | 7 ++++--- test/short-urls/helpers/ShortUrlsRow.test.tsx | 5 +++-- test/utils/dates/types/index.test.ts | 12 +++++------- test/utils/helpers/date.test.ts | 14 +++++++------- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/utils/helpers/date.ts b/src/utils/helpers/date.ts index ee4d6369..956e59d0 100644 --- a/src/utils/helpers/date.ts +++ b/src/utils/helpers/date.ts @@ -1,4 +1,4 @@ -import { format, formatISO } from 'date-fns'; +import { format, formatISO, parse } from 'date-fns'; import { OptionalString } from '../utils'; type DateOrString = Date | string; @@ -19,3 +19,5 @@ export const formatDate = (format = 'yyyy-MM-dd') => (date?: NullableDate) => fo export const formatIsoDate = (date?: NullableDate) => formatDateFromFormat(date, undefined); export const formatInternational = formatDate(); + +export const parseDate = (date: string, format: string) => parse(date, format, new Date()); diff --git a/test/short-urls/ShortUrlForm.test.tsx b/test/short-urls/ShortUrlForm.test.tsx index 5e9b2b07..380d1309 100644 --- a/test/short-urls/ShortUrlForm.test.tsx +++ b/test/short-urls/ShortUrlForm.test.tsx @@ -1,5 +1,5 @@ import { shallow, ShallowWrapper } from 'enzyme'; -import { formatISO, parse } from 'date-fns'; +import { formatISO } from 'date-fns'; import { identity } from 'ramda'; import { Mock } from 'ts-mockery'; import { Input } from 'reactstrap'; @@ -8,6 +8,7 @@ import DateInput from '../../src/utils/DateInput'; import { ShortUrlData } from '../../src/short-urls/data'; import { ReachableServer, SelectedServer } from '../../src/servers/data'; import { SimpleCard } from '../../src/utils/SimpleCard'; +import { parseDate } from '../../src/utils/helpers/date'; describe('', () => { let wrapper: ShallowWrapper; @@ -34,8 +35,8 @@ describe('', () => { it('saves short URL with data set in form controls', () => { const wrapper = createWrapper(); - const validSince = parse('2017-01-01', 'yyyy-MM-dd', new Date()); - const validUntil = parse('2017-01-06', 'yyyy-MM-dd', new Date()); + const validSince = parseDate('2017-01-01', 'yyyy-MM-dd'); + const validUntil = parseDate('2017-01-06', 'yyyy-MM-dd'); wrapper.find(Input).first().simulate('change', { target: { value: 'https://long-domain.com/foo/bar' } }); wrapper.find('TagsSelector').simulate('change', [ 'tag_foo', 'tag_bar' ]); diff --git a/test/short-urls/helpers/ShortUrlsRow.test.tsx b/test/short-urls/helpers/ShortUrlsRow.test.tsx index adf48129..beae06cc 100644 --- a/test/short-urls/helpers/ShortUrlsRow.test.tsx +++ b/test/short-urls/helpers/ShortUrlsRow.test.tsx @@ -2,7 +2,7 @@ import { shallow, ShallowWrapper } from 'enzyme'; import { assoc, toString } from 'ramda'; import { Mock } from 'ts-mockery'; import { ExternalLink } from 'react-external-link'; -import { formatISO, parse } from 'date-fns'; +import { formatISO } from 'date-fns'; import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow'; import Tag from '../../../src/tags/helpers/Tag'; import ColorGenerator from '../../../src/utils/services/ColorGenerator'; @@ -11,6 +11,7 @@ import { ShortUrl } from '../../../src/short-urls/data'; import { ReachableServer } from '../../../src/servers/data'; import { CopyToClipboardIcon } from '../../../src/utils/CopyToClipboardIcon'; import { Time } from '../../../src/utils/Time'; +import { parseDate } from '../../../src/utils/helpers/date'; describe('', () => { let wrapper: ShallowWrapper; @@ -27,7 +28,7 @@ describe('', () => { shortCode: 'abc123', shortUrl: 'http://doma.in/abc123', longUrl: 'http://foo.com/bar', - dateCreated: formatISO(parse('2018-05-23 18:30:41', 'yyyy-MM-dd HH:mm:ss', new Date())), + dateCreated: formatISO(parseDate('2018-05-23 18:30:41', 'yyyy-MM-dd HH:mm:ss')), tags: [ 'nodejs', 'reactjs' ], visitsCount: 45, domain: null, diff --git a/test/utils/dates/types/index.test.ts b/test/utils/dates/types/index.test.ts index 3c2127e3..7f4b6c1d 100644 --- a/test/utils/dates/types/index.test.ts +++ b/test/utils/dates/types/index.test.ts @@ -1,4 +1,4 @@ -import { format, parse, subDays } from 'date-fns'; +import { format, subDays } from 'date-fns'; import { DateInterval, dateRangeIsEmpty, @@ -6,6 +6,7 @@ import { rangeIsInterval, rangeOrIntervalToString, } from '../../../../src/utils/dates/types'; +import { parseDate } from '../../../../src/utils/helpers/date'; describe('date-types', () => { describe('dateRangeIsEmpty', () => { @@ -58,13 +59,10 @@ describe('date-types', () => { [{ startDate: undefined, endDate: undefined }, undefined ], [{ startDate: undefined, endDate: null }, undefined ], [{ startDate: null, endDate: undefined }, undefined ], - [{ 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: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Since 2020-01-01' ], + [{ endDate: parseDate('2020-01-01', 'yyyy-MM-dd') }, 'Until 2020-01-01' ], [ - { - startDate: parse('2020-01-01', 'yyyy-MM-dd', new Date()), - endDate: parse('2021-02-02', 'yyyy-MM-dd', new Date()), - }, + { startDate: parseDate('2020-01-01', 'yyyy-MM-dd'), endDate: parseDate('2021-02-02', 'yyyy-MM-dd') }, '2020-01-01 - 2021-02-02', ], ])('proper result is returned', (range, expectedValue) => { diff --git a/test/utils/helpers/date.test.ts b/test/utils/helpers/date.test.ts index c3684617..ac0444a7 100644 --- a/test/utils/helpers/date.test.ts +++ b/test/utils/helpers/date.test.ts @@ -1,12 +1,12 @@ -import { formatISO, parse } from 'date-fns'; -import { formatDate, formatIsoDate } from '../../../src/utils/helpers/date'; +import { formatISO } from 'date-fns'; +import { formatDate, formatIsoDate, parseDate } from '../../../src/utils/helpers/date'; describe('date', () => { describe('formatDate', () => { it.each([ - [ 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' ], + [ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 'dd/MM/yyyy', '05/03/2020' ], + [ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 'yyyy-MM', '2020-03' ], + [ parseDate('2020-03-05 10:00:10', 'yyyy-MM-dd HH:mm:ss'), 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 ], @@ -19,8 +19,8 @@ describe('date', () => { describe('formatIsoDate', () => { it.each([ [ - 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())), + 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')), ], [ '2020-03-05 10:00:10', '2020-03-05 10:00:10' ], [ 'foo', 'foo' ],