1- import { isValidDate , parseRelativeTime } from './time' ;
1+ import { isValidDate , normalizeDate , parseRelativeTime , userFriendlyDate } from './time' ;
22import moment from 'moment' ;
33
44moment . updateLocale ( 'en' , {
@@ -9,9 +9,12 @@ moment.updateLocale('en', {
99} ) ;
1010
1111it ( 'should test for valid date' , ( ) => {
12- expect ( isValidDate ( '2017-05-05' ) ) . toBe ( false ) ;
13- expect ( isValidDate ( '2017-05-05T15:23' ) ) . toBe ( false ) ;
14- expect ( isValidDate ( '2017-05-05 15:23' ) ) . toBe ( true ) ;
12+ expect ( isValidDate ( '2017-05-05' , 'YYYY-MM-DD HH:mm' ) ) . toBe ( false ) ;
13+ expect ( isValidDate ( '2017-05-05T15:23' , 'YYYY-MM-DD HH:mm' ) ) . toBe ( false ) ;
14+ expect ( isValidDate ( '2017-05-05 15:23' , 'YYYY-MM-DD HH:mm' ) ) . toBe ( true ) ;
15+
16+ expect ( isValidDate ( '2019-10-20T15:55:00Z' ) ) . toBe ( true ) ;
17+ expect ( isValidDate ( '2017-05-05 15:23' ) ) . toBe ( false ) ;
1518} ) ;
1619
1720// 2018-10-15 Monday
@@ -33,6 +36,27 @@ it('should parse', () => {
3336 expectSuccess ( parseRelativeTime ( 'now/y' , 'startOf' , moment ( '2019-10-20T15:55:15' ) ) ) . toEqual ( '2019-01-01 00:00:00' ) ;
3437} ) ;
3538
39+ it ( 'should convert to RFC3339 and back' , ( ) => {
40+ // can't put exact dates since moment doesn't allow overriding `.local()`'s timezone for unit tests
41+ const userDate = '2025-01-01 10:10' ;
42+ const rfcDate = moment ( userDate ) . utc ( ) . format ( ) ;
43+
44+ expect ( normalizeDate ( userDate ) ) . toBe ( rfcDate ) ;
45+ expect ( userFriendlyDate ( rfcDate ) ) . toBe ( userDate ) ;
46+ } ) ;
47+
48+ it ( 'should not modify relative ranges' , ( ) => {
49+ expect ( normalizeDate ( 'now-1d' ) ) . toBe ( 'now-1d' ) ;
50+ expect ( normalizeDate ( 'now-120s' ) ) . toBe ( 'now-120s' ) ;
51+ expect ( normalizeDate ( 'now-1d-1h' ) ) . toBe ( 'now-1d-1h' ) ;
52+ expect ( normalizeDate ( 'now/w' ) ) . toBe ( 'now/w' ) ;
53+ expect ( normalizeDate ( 'now/w' ) ) . toBe ( 'now/w' ) ;
54+ expect ( normalizeDate ( 'now-1w/w' ) ) . toBe ( 'now-1w/w' ) ;
55+ expect ( normalizeDate ( 'now-1y+1w/w' ) ) . toBe ( 'now-1y+1w/w' ) ;
56+ expect ( normalizeDate ( 'now/d+5h' ) ) . toBe ( 'now/d+5h' ) ;
57+ expect ( normalizeDate ( 'now/y' ) ) . toBe ( 'now/y' ) ;
58+ } ) ;
59+
3660const expectSuccess = ( value : ReturnType < typeof parseRelativeTime > ) => {
3761 if ( value . success ) {
3862 return expect ( value . value . format ( 'YYYY-MM-DD HH:mm:ss' ) ) ;
0 commit comments