@@ -252,6 +252,80 @@ describe('CalendarInterval', () => {
252
252
} ) ;
253
253
} ) ;
254
254
255
+ describe ( 'getPrevious' , ( ) => {
256
+ it ( 'returns 1st of previous month when month start is 1' , ( ) => {
257
+ clock = sinon . useFakeTimers ( moment ( '2024-07-15 21:10:01' ) . valueOf ( ) ) ;
258
+ chai . expect ( service . getPrevious ( 1 ) ) . to . deep . equal ( {
259
+ start : moment ( '2024-06-01 00:00:00' ) . valueOf ( ) ,
260
+ end : moment ( '2024-06-30 23:59:59:999' ) . valueOf ( ) ,
261
+ } ) ;
262
+ clock . restore ( ) ;
263
+ } ) ;
264
+
265
+ it ( 'returns 1st of previous month when reference date is provided' , ( ) => {
266
+ const referenceDate = '2024-07-15' ;
267
+ chai . expect ( service . getPrevious ( 1 , referenceDate ) ) . to . deep . equal ( {
268
+ start : moment ( '2024-06-01 00:00:00' ) . valueOf ( ) ,
269
+ end : moment ( '2024-06-30 23:59:59:999' ) . valueOf ( ) ,
270
+ } ) ;
271
+ } ) ;
272
+
273
+ it ( 'returns 1st of previous month when reference date provided is in the future' , ( ) => {
274
+ const referenceDate = '2025-07-15' ;
275
+ chai . expect ( service . getPrevious ( 1 , referenceDate ) ) . to . deep . equal ( {
276
+ start : moment ( '2025-06-01 00:00:00' ) . valueOf ( ) ,
277
+ end : moment ( '2025-06-30 23:59:59:999' ) . valueOf ( ) ,
278
+ } ) ;
279
+ } ) ;
280
+
281
+ it ( 'returns n-th of the previous month when month start is n <= current date' , ( ) => {
282
+ clock = sinon . useFakeTimers ( moment ( '2018-03-20 21:10:01' ) . valueOf ( ) ) ;
283
+ chai . expect ( service . getPrevious ( 15 ) ) . to . deep . equal ( {
284
+ start : moment ( '2018-02-15 00:00:00' ) . valueOf ( ) ,
285
+ end : moment ( '2018-03-14 23:59:59:999' ) . valueOf ( ) ,
286
+ } ) ;
287
+ clock . restore ( ) ;
288
+ } ) ;
289
+
290
+ it ( 'returns n-th of two months ago when month start is n > current date' , ( ) => {
291
+ clock = sinon . useFakeTimers ( moment ( '2018-03-10 21:10:01' ) . valueOf ( ) ) ;
292
+ chai . expect ( service . getPrevious ( 20 ) ) . to . deep . equal ( {
293
+ start : moment ( '2018-01-20 00:00:00' ) . valueOf ( ) ,
294
+ end : moment ( '2018-02-19 23:59:59:999' ) . valueOf ( ) ,
295
+ } ) ;
296
+ clock . restore ( ) ;
297
+ } ) ;
298
+
299
+ it ( 'returns correct previous period when incorrect start date values' , ( ) => {
300
+ clock = sinon . useFakeTimers ( moment ( '2018-02-10' ) . valueOf ( ) ) ;
301
+ [ null , undefined , - 1 , 'date' , false , 0 , 35 ] . forEach ( ( invalidStart ) => {
302
+ chai . expect ( service . getPrevious ( invalidStart ) ) . to . deep . equal ( {
303
+ start : moment ( '2018-01-01 00:00:00' ) . valueOf ( ) ,
304
+ end : moment ( '2018-01-31 23:59:59:999' ) . valueOf ( ) ,
305
+ } ) ;
306
+ } ) ;
307
+ clock . restore ( ) ;
308
+ } ) ;
309
+
310
+ it ( 'returns correct previous period when start date same as current date' , ( ) => {
311
+ clock = sinon . useFakeTimers ( moment ( '2018-04-15' ) . valueOf ( ) ) ;
312
+ chai . expect ( service . getPrevious ( 15 ) ) . to . deep . equal ( {
313
+ start : moment ( '2018-03-15 00:00:00' ) . valueOf ( ) ,
314
+ end : moment ( '2018-04-14 23:59:59:999' ) . valueOf ( ) ,
315
+ } ) ;
316
+ clock . restore ( ) ;
317
+ } ) ;
318
+
319
+ it ( 'returns correct start date in previous month if month has 30 days' , ( ) => {
320
+ clock = sinon . useFakeTimers ( moment ( '2018-05-15' ) . valueOf ( ) ) ;
321
+ chai . expect ( service . getPrevious ( 31 ) ) . to . deep . equal ( {
322
+ start : moment ( '2018-03-31 00:00:00' ) . valueOf ( ) ,
323
+ end : moment ( '2018-04-30 23:59:59:999' ) . valueOf ( ) ,
324
+ } ) ;
325
+ clock . restore ( ) ;
326
+ } ) ;
327
+ } ) ;
328
+
255
329
describe ( 'getInterval' , ( ) => {
256
330
it ( 'returns 1st of current month when month start is not set or incorrect' , ( ) => {
257
331
let timestamp ;
0 commit comments