@@ -173,3 +173,88 @@ describe('dateTimeString parsing tests', () => {
173173 testDateTimeStringThrows ( '2012-04-20 12:13:14.321Overflow' ) ;
174174 } ) ;
175175} ) ;
176+
177+ describe ( 'makeDateWrapper' , ( ) => {
178+ it ( 'should use default values if not given arguments' , ( ) => {
179+ const expectedDate = new Date ( 2022 , 0 , 1 , 0 , 0 , 0 , 0 ) ;
180+
181+ expect (
182+ DateUtils . makeDateWrapper ( 'Asia/Dubai' , 2022 ) . valueOf ( )
183+ ) . toStrictEqual ( expectedDate . valueOf ( ) . toString ( ) ) ;
184+ } ) ;
185+ } ) ;
186+
187+ describe ( 'parseDateValues' , ( ) => {
188+ it ( 'should return null if any value is invalid' , ( ) => {
189+ expect (
190+ DateUtils . parseDateValues (
191+ 'test' ,
192+ 'test' ,
193+ 'test' ,
194+ 'test' ,
195+ 'test' ,
196+ 'test' ,
197+ 'test'
198+ )
199+ ) . toBe ( null ) ;
200+ } ) ;
201+ } ) ;
202+
203+ describe ( 'parseDateRange' , ( ) => {
204+ const MS_PER_DAY = 1000 * 60 * 60 * 24 ;
205+
206+ function dateDiffInMillisseconds ( a : Date , b : Date ) {
207+ // Discard the time and time-zone information.
208+ const utc1 = Date . UTC ( a . getFullYear ( ) , a . getMonth ( ) , a . getDate ( ) ) ;
209+ const utc2 = Date . UTC ( b . getFullYear ( ) , b . getMonth ( ) , b . getDate ( ) ) ;
210+
211+ return Math . floor ( utc2 - utc1 ) ;
212+ }
213+
214+ it ( 'should throw an error if the text is empty' , ( ) => {
215+ expect ( ( ) => DateUtils . parseDateRange ( '' , 'America/New_York' ) ) . toThrowError (
216+ 'Cannot parse date range from empty string'
217+ ) ;
218+ } ) ;
219+
220+ it ( 'should return a range of null values if text is "null"' , ( ) => {
221+ expect ( DateUtils . parseDateRange ( 'null' , 'America/New_York' ) ) . toEqual ( [
222+ null ,
223+ null ,
224+ ] ) ;
225+ } ) ;
226+
227+ it ( 'should return a range from today to tomorrow if text is "today"' , ( ) => {
228+ const range = DateUtils . parseDateRange ( 'today' , 'America/New_York' ) ;
229+ const start = range [ 0 ] ;
230+ const end = range [ 1 ] ;
231+ if ( start && end ) {
232+ const startDate = start ?. asDate ( ) ;
233+ const endDate = end ?. asDate ( ) ;
234+ expect ( dateDiffInMillisseconds ( startDate , endDate ) ) . toBe ( MS_PER_DAY ) ;
235+ }
236+ } ) ;
237+
238+ it ( 'should return null as the end range if text is "now"' , ( ) => {
239+ const range = DateUtils . parseDateRange ( 'now' , 'America/New_York' ) ;
240+ expect ( range [ 1 ] ) . toBeNull ( ) ;
241+ } ) ;
242+
243+ it ( 'should throw an error if a value in text is invalid' , ( ) => {
244+ expect ( ( ) =>
245+ DateUtils . parseDateRange ( '9999-99-99' , 'America/New_York' )
246+ ) . toThrowError ( / U n a b l e t o e x t r a c t d a t e v a l u e s f r o m / i) ;
247+ } ) ;
248+ } ) ;
249+
250+ describe ( 'getJsDate' , ( ) => {
251+ it ( 'returns a date object given that input is a number' , ( ) => {
252+ const expectedDate = new Date ( 10000 ) ;
253+ expect ( DateUtils . getJsDate ( 10000 ) ) . toEqual ( expectedDate ) ;
254+ } ) ;
255+
256+ it ( 'returns a date object given a DateWrapper' , ( ) => {
257+ const dateWrapper = DateUtils . makeDateWrapper ( 'America/New_York' , 2022 ) ;
258+ expect ( DateUtils . getJsDate ( dateWrapper ) ) . toEqual ( dateWrapper . asDate ( ) ) ;
259+ } ) ;
260+ } ) ;
0 commit comments