11import * as moment from "moment" ;
2- import { durationInSecondsExcludingWeekends } from "./weekend" ;
2+ import { durationInSecondsExcludingWeekends } from "./weekend" ;
33
4- describe ( ' Weekend exclusion' , ( ) => {
5- it ( ' handles dates on the same weekday' , ( ) => {
4+ describe ( " Weekend exclusion" , ( ) => {
5+ it ( " handles dates on the same weekday" , ( ) => {
66 const beginning = moment ( "2019-04-03T08:34:50Z" ) ;
77 const end = moment ( "2019-04-03T13:34:50Z" ) ;
88
@@ -11,7 +11,7 @@ describe('Weekend exclusion', () => {
1111 expect ( duration ) . toEqual ( 18000 ) ;
1212 } ) ;
1313
14- it ( ' handles dates without weekends' , ( ) => {
14+ it ( " handles dates without weekends" , ( ) => {
1515 const beginning = moment ( "2019-04-03T08:34:50Z" ) ;
1616 const end = moment ( "2019-04-04T13:34:50Z" ) ;
1717
@@ -20,7 +20,7 @@ describe('Weekend exclusion', () => {
2020 expect ( duration ) . toEqual ( 104400 ) ;
2121 } ) ;
2222
23- it ( ' handles whole weekends' , ( ) => {
23+ it ( " handles whole weekends" , ( ) => {
2424 const beginning = moment . utc ( "2019-04-06T00:00:00Z" ) ;
2525 const end = moment . utc ( "2019-04-08T00:00:00Z" ) ;
2626
@@ -29,7 +29,7 @@ describe('Weekend exclusion', () => {
2929 expect ( duration ) . toEqual ( 0 ) ;
3030 } ) ;
3131
32- it ( ' handles partial weekends' , ( ) => {
32+ it ( " handles partial weekends" , ( ) => {
3333 const beginning = moment . utc ( "2019-04-06T08:00:00Z" ) ;
3434 const end = moment . utc ( "2019-04-07T08:00:00Z" ) ;
3535
@@ -38,7 +38,7 @@ describe('Weekend exclusion', () => {
3838 expect ( duration ) . toEqual ( 0 ) ;
3939 } ) ;
4040
41- it ( ' does not exclude weekends from the calculation if the provided range does not contain any weekend days' , ( ) => {
41+ it ( " does not exclude weekends from the calculation if the provided range does not contain any weekend days" , ( ) => {
4242 const beginning = moment ( "2019-04-03T08:34:50Z" ) ;
4343 const end = moment ( "2019-04-05T13:34:51Z" ) ;
4444
@@ -47,24 +47,54 @@ describe('Weekend exclusion', () => {
4747 expect ( duration ) . toEqual ( 190801 ) ;
4848 } ) ;
4949
50- it ( ' excludes weekends if the interval contains one or more whole weekends' , ( ) => {
50+ it ( " excludes weekends if the interval contains one or more whole weekends" , ( ) => {
5151 const beginning = moment ( "2019-04-03T08:34:50Z" ) ;
5252 const end = moment ( "2019-04-18T13:34:51Z" ) ;
5353
5454 const duration = durationInSecondsExcludingWeekends ( beginning , end ) ;
5555
56-
5756 // Without exclusion: 1314001 seconds
5857 // Excluded: two weekends, 96 hours, 172800 seconds
5958 expect ( duration ) . toEqual ( 1314001 - 2 * 48 * 60 * 60 ) ;
6059 } ) ;
6160
62- it ( ' excludes weekends if the interval contains one or more whole weekends and a partial weekend' , ( ) => {
61+ it ( " excludes weekends if the interval contains one or more whole weekends and a partial weekend" , ( ) => {
6362 const beginning = moment ( "2019-04-03T08:34:50Z" ) ;
6463 const end = moment ( "2019-04-21T13:34:51Z" ) ;
6564
6665 const duration = durationInSecondsExcludingWeekends ( beginning , end ) ;
6766
68- expect ( duration ) . toEqual ( 1573201 - ( 2 * 48 * 60 * 60 ) - ( 24 * 60 * 60 ) - ( 13 * 60 * 60 + 34 * 60 + 51 ) ) ;
69- } )
70- } ) ;
67+ expect ( duration ) . toEqual (
68+ 1573201 - 2 * 48 * 60 * 60 - 24 * 60 * 60 - ( 13 * 60 * 60 + 34 * 60 + 51 )
69+ ) ;
70+ } ) ;
71+
72+ it ( "accounts for business hours (09:00 - 17:00) only" , ( ) => {
73+ const usesCases = [
74+ {
75+ // Same day
76+ start : moment . utc ( "2019-04-01T01:00:00Z" ) ,
77+ end : moment . utc ( "2019-04-01T23:00:00Z" ) ,
78+ duration : 8 * 60 * 60
79+ } ,
80+ {
81+ // Spans over multiple days
82+ start : moment . utc ( "2019-04-01T01:00:00Z" ) ,
83+ end : moment . utc ( "2019-04-02T23:00:00Z" ) ,
84+ duration : 16 * 60 * 60
85+ } ,
86+ {
87+ // Spans over multiple weeks
88+ start : moment . utc ( "2019-04-01T01:00:00Z" ) ,
89+ end : moment . utc ( "2019-04-30T23:00:00Z" ) ,
90+ duration : 22 * 8 * 60 * 60
91+ }
92+ ] ;
93+
94+ usesCases . forEach ( usesCase => {
95+ expect (
96+ durationInSecondsExcludingWeekends ( usesCase . start , usesCase . end , true )
97+ ) . toEqual ( usesCase . duration ) ;
98+ } ) ;
99+ } ) ;
100+ } ) ;
0 commit comments