1- import { getStartTime , getEndTime } from 'src/timeMachine/selectors/index'
1+ import {
2+ getStartTime ,
3+ getEndTime ,
4+ handleCustomTime ,
5+ } from 'src/timeMachine/selectors/index'
26import moment from 'moment'
37
48import {
@@ -10,48 +14,178 @@ import {
1014const custom = 'custom' as 'custom'
1115
1216describe ( 'TimeMachine.Selectors.Index' , ( ) => {
13- const thirty = moment ( )
14- . subtract ( 30 , 'days' )
15- . subtract ( moment ( ) . isDST ( ) ? 1 : 0 , 'hours' ) // added to account for DST
16- . valueOf ( )
17- it ( `getStartTime should return ${ thirty } when lower is now() - 30d` , ( ) => {
18- expect ( getStartTime ( pastThirtyDaysTimeRange ) ) . toBeGreaterThanOrEqual ( thirty )
19- } )
17+ describe ( 'getStartTime and getEndTime' , ( ) => {
18+ const thirty = moment ( )
19+ . subtract ( 30 , 'days' )
20+ . subtract ( moment ( ) . isDST ( ) ? 1 : 0 , 'hours' ) // added to account for DST
21+ . valueOf ( )
22+ it ( `getStartTime should return ${ thirty } when lower is now() - 30d` , ( ) => {
23+ expect ( getStartTime ( pastThirtyDaysTimeRange ) ) . toBeGreaterThanOrEqual (
24+ thirty
25+ )
26+ } )
2027
21- const hour = moment ( ) . subtract ( 1 , 'hours' ) . valueOf ( )
22- it ( `getStartTime should return ${ hour } when lower is now() - 1h` , ( ) => {
23- expect ( getStartTime ( pastHourTimeRange ) ) . toBeGreaterThanOrEqual ( hour )
24- } )
28+ const hour = moment ( ) . subtract ( 1 , 'hours' ) . valueOf ( )
29+ it ( `getStartTime should return ${ hour } when lower is now() - 1h` , ( ) => {
30+ expect ( getStartTime ( pastHourTimeRange ) ) . toBeGreaterThanOrEqual ( hour )
31+ } )
2532
26- const fifteen = moment ( ) . subtract ( 15 , 'minutes' ) . valueOf ( )
27- it ( `getStartTime should return ${ hour } when lower is now() - 1h` , ( ) => {
28- expect ( getStartTime ( pastFifteenMinTimeRange ) ) . toBeGreaterThanOrEqual (
29- fifteen
30- )
31- } )
33+ const fifteen = moment ( ) . subtract ( 15 , 'minutes' ) . valueOf ( )
34+ it ( `getStartTime should return ${ hour } when lower is now() - 1h` , ( ) => {
35+ expect ( getStartTime ( pastFifteenMinTimeRange ) ) . toBeGreaterThanOrEqual (
36+ fifteen
37+ )
38+ } )
3239
33- const date = '2019-01-01'
34- const newYears = new Date ( date ) . valueOf ( )
35- it ( `getStartTime should return ${ newYears } when lower is ${ date } ` , ( ) => {
36- const timeRange = {
37- type : custom ,
38- lower : date ,
39- upper : date ,
40- }
41- expect ( getStartTime ( timeRange ) ) . toEqual ( newYears )
42- } )
40+ const date = '2019-01-01'
41+ const newYears = new Date ( date ) . valueOf ( )
42+ it ( `getStartTime should return ${ newYears } when lower is ${ date } ` , ( ) => {
43+ const timeRange = {
44+ type : custom ,
45+ lower : date ,
46+ upper : date ,
47+ }
48+ expect ( getStartTime ( timeRange ) ) . toEqual ( newYears )
49+ } )
50+
51+ it ( `getEndTime should return ${ newYears } when lower is ${ date } ` , ( ) => {
52+ const timeRange = {
53+ type : custom ,
54+ lower : date ,
55+ upper : date ,
56+ }
57+ expect ( getEndTime ( timeRange ) ) . toEqual ( newYears )
58+ } )
4359
44- it ( `getEndTime should return ${ newYears } when lower is ${ date } ` , ( ) => {
45- const timeRange = {
46- type : custom ,
47- lower : date ,
48- upper : date ,
49- }
50- expect ( getEndTime ( timeRange ) ) . toEqual ( newYears )
60+ const now = new Date ( ) . valueOf ( )
61+ it ( `getEndTime should return ${ now } when upper is null and lower includes now()` , ( ) => {
62+ expect ( getEndTime ( pastThirtyDaysTimeRange ) ) . toBeGreaterThanOrEqual ( now )
63+ } )
5164 } )
5265
53- const now = new Date ( ) . valueOf ( )
54- it ( `getEndTime should return ${ now } when upper is null and lower includes now()` , ( ) => {
55- expect ( getEndTime ( pastThirtyDaysTimeRange ) ) . toBeGreaterThanOrEqual ( now )
66+ describe ( 'handleCustomTime' , ( ) => {
67+ const now = new Date ( )
68+ const threeDays = 1000 * 60 * 60 * 24 * 3
69+ const twentyFourHours = 1000 * 60 * 60 * 24
70+ const fifteenMinutes = 1000 * 60 * 15
71+ const oneMillisecond = 1
72+
73+ it ( 'can parse just "now"' , ( ) => {
74+ expect ( handleCustomTime ( 'now()' , now ) ) . toEqual ( now . getTime ( ) )
75+ } )
76+
77+ it ( 'can parse now() and a duration' , ( ) => {
78+ expect ( handleCustomTime ( 'now() - 3d' , now ) ) . toEqual (
79+ now . getTime ( ) - threeDays
80+ )
81+ expect ( handleCustomTime ( 'now() + 3d' , now ) ) . toEqual (
82+ now . getTime ( ) + threeDays
83+ )
84+
85+ expect ( handleCustomTime ( 'now() - 24h' , now ) ) . toEqual (
86+ now . getTime ( ) - twentyFourHours
87+ )
88+ expect ( handleCustomTime ( 'now() + 24h' , now ) ) . toEqual (
89+ now . getTime ( ) + twentyFourHours
90+ )
91+
92+ expect ( handleCustomTime ( 'now() - 15m' , now ) ) . toEqual (
93+ now . getTime ( ) - fifteenMinutes
94+ )
95+ expect ( handleCustomTime ( 'now() + 15m' , now ) ) . toEqual (
96+ now . getTime ( ) + fifteenMinutes
97+ )
98+
99+ expect ( handleCustomTime ( 'now() - 1ms' , now ) ) . toEqual (
100+ now . getTime ( ) - oneMillisecond
101+ )
102+ expect ( handleCustomTime ( 'now() + 1ms' , now ) ) . toEqual (
103+ now . getTime ( ) + oneMillisecond
104+ )
105+ } )
106+
107+ it ( 'can parse a duration without now()' , ( ) => {
108+ expect ( handleCustomTime ( '-3d' , now ) ) . toEqual ( now . getTime ( ) - threeDays )
109+ expect ( handleCustomTime ( '+3d' , now ) ) . toEqual ( now . getTime ( ) + threeDays )
110+
111+ expect ( handleCustomTime ( '-24h' , now ) ) . toEqual (
112+ now . getTime ( ) - twentyFourHours
113+ )
114+ expect ( handleCustomTime ( '+24h' , now ) ) . toEqual (
115+ now . getTime ( ) + twentyFourHours
116+ )
117+
118+ expect ( handleCustomTime ( '-15m' , now ) ) . toEqual (
119+ now . getTime ( ) - fifteenMinutes
120+ )
121+ expect ( handleCustomTime ( '+15m' , now ) ) . toEqual (
122+ now . getTime ( ) + fifteenMinutes
123+ )
124+
125+ expect ( handleCustomTime ( '-1ms' , now ) ) . toEqual (
126+ now . getTime ( ) - oneMillisecond
127+ )
128+ expect ( handleCustomTime ( '+1ms' , now ) ) . toEqual (
129+ now . getTime ( ) + oneMillisecond
130+ )
131+ } )
132+
133+ it ( 'can ignore spaces around now and the sign correctly' , ( ) => {
134+ expect ( handleCustomTime ( ' now()' , now ) ) . toEqual ( now . getTime ( ) )
135+ expect ( handleCustomTime ( 'now() ' , now ) ) . toEqual ( now . getTime ( ) )
136+ expect ( handleCustomTime ( ' now() ' , now ) ) . toEqual ( now . getTime ( ) )
137+
138+ expect ( handleCustomTime ( ' now() - 3d' , now ) ) . toEqual (
139+ now . getTime ( ) - threeDays
140+ )
141+ expect ( handleCustomTime ( 'now()+ 3d ' , now ) ) . toEqual (
142+ now . getTime ( ) + threeDays
143+ )
144+
145+ expect ( handleCustomTime ( '- 24h' , now ) ) . toEqual (
146+ now . getTime ( ) - twentyFourHours
147+ )
148+ expect ( handleCustomTime ( ' + 24h' , now ) ) . toEqual (
149+ now . getTime ( ) + twentyFourHours
150+ )
151+
152+ expect ( handleCustomTime ( ' 15m' , now ) ) . toEqual (
153+ now . getTime ( ) + fifteenMinutes
154+ )
155+ expect ( handleCustomTime ( '15m ' , now ) ) . toEqual (
156+ now . getTime ( ) + fifteenMinutes
157+ )
158+ expect ( handleCustomTime ( ' 15m ' , now ) ) . toEqual (
159+ now . getTime ( ) + fifteenMinutes
160+ )
161+ } )
162+
163+ it ( 'can parse a JavaScript timestamp as a string' , ( ) => {
164+ let specificTimeString : string = '2022-10-14T20:33:01.433Z'
165+ expect ( handleCustomTime ( specificTimeString , now ) ) . toEqual (
166+ new Date ( specificTimeString ) . getTime ( )
167+ )
168+
169+ specificTimeString = '2022/07/04'
170+ expect ( handleCustomTime ( specificTimeString , now ) ) . toEqual (
171+ new Date ( specificTimeString ) . getTime ( )
172+ )
173+
174+ specificTimeString = '2022-10-06 00:00'
175+ expect ( handleCustomTime ( specificTimeString , now ) ) . toEqual (
176+ new Date ( specificTimeString ) . getTime ( )
177+ )
178+ } )
179+
180+ it ( 'rejects numbers as invalid time' , ( ) => {
181+ let numberString = '1666029415'
182+ expect ( ( ) => handleCustomTime ( numberString , now ) ) . toThrowError ( )
183+
184+ numberString = '0'
185+ expect ( ( ) => handleCustomTime ( numberString , now ) ) . toThrowError ( )
186+
187+ numberString = '-4'
188+ expect ( ( ) => handleCustomTime ( numberString , now ) ) . toThrowError ( )
189+ } )
56190 } )
57191} )
0 commit comments