@@ -113,16 +113,18 @@ describe("when the first month and the last month are the same", () => {
113113 describe ( "when using a max date" , ( ) => {
114114 const month = new Date ( 2023 , 4 , 1 ) ;
115115 const maxDate = new Date ( 2023 , 4 , 15 ) ;
116+ const dateLib = new DateLib ( { weekStartsOn : 1 } ) ;
117+ const expectedLastDay = dateLib . addDays ( dateLib . startOfWeek ( maxDate ) , 6 ) ;
118+ const expectedLength =
119+ dateLib . differenceInCalendarDays (
120+ expectedLastDay ,
121+ dateLib . startOfWeek ( month ) ,
122+ ) + 1 ;
116123
117- it ( "the last day should be the max date" , ( ) => {
118- const dates = getDates (
119- [ month ] ,
120- maxDate ,
121- { } ,
122- new DateLib ( { weekStartsOn : 1 } ) ,
123- ) ;
124- expect ( dates ) . toHaveLength ( 15 ) ;
125- expect ( dates [ dates . length - 1 ] ) . toEqual ( maxDate ) ;
124+ it ( "the last day should be the end of that week" , ( ) => {
125+ const dates = getDates ( [ month ] , maxDate , { } , dateLib ) ;
126+ expect ( dates ) . toHaveLength ( expectedLength ) ;
127+ expect ( dates [ dates . length - 1 ] ) . toEqual ( expectedLastDay ) ;
126128 } ) ;
127129 } ) ;
128130 describe ( "when using ISO weeks" , ( ) => {
@@ -139,6 +141,50 @@ describe("when the first month and the last month are the same", () => {
139141 expect ( dates [ dates . length - 1 ] ) . toEqual ( new Date ( 2023 , 5 , 4 ) ) ;
140142 } ) ;
141143 } ) ;
144+
145+ describe ( "when using a custom week start with a max date" , ( ) => {
146+ const month = new Date ( 2023 , 4 , 1 ) ; // May 2023
147+ const maxDate = new Date ( 2023 , 4 , 15 ) ;
148+ const dateLib = new DateLib ( { weekStartsOn : 3 } ) ; // Wednesday start
149+
150+ const expectedLastDay = dateLib . addDays ( dateLib . startOfWeek ( maxDate ) , 6 ) ;
151+ const expectedLength =
152+ dateLib . differenceInCalendarDays (
153+ expectedLastDay ,
154+ dateLib . startOfWeek ( month ) ,
155+ ) + 1 ;
156+
157+ it ( "clamps to the end of the custom week" , ( ) => {
158+ const dates = getDates ( [ month ] , maxDate , { } , dateLib ) ;
159+ expect ( dates [ dates . length - 1 ] ) . toEqual ( expectedLastDay ) ;
160+ expect ( dates ) . toHaveLength ( expectedLength ) ;
161+ } ) ;
162+ } ) ;
163+
164+ describe ( "when using ISO weeks with a max date" , ( ) => {
165+ const month = new Date ( 2023 , 4 , 1 ) ; // May 2023
166+ const maxDate = new Date ( 2023 , 4 , 15 ) ;
167+ const expectedLastDay = defaultDateLib . addDays (
168+ defaultDateLib . startOfISOWeek ( maxDate ) ,
169+ 6 ,
170+ ) ;
171+ const expectedLength =
172+ defaultDateLib . differenceInCalendarDays (
173+ expectedLastDay ,
174+ defaultDateLib . startOfISOWeek ( month ) ,
175+ ) + 1 ;
176+
177+ it ( "clamps to the end of the ISO week" , ( ) => {
178+ const dates = getDates (
179+ [ month ] ,
180+ maxDate ,
181+ { ISOWeek : true } ,
182+ defaultDateLib ,
183+ ) ;
184+ expect ( dates [ dates . length - 1 ] ) . toEqual ( expectedLastDay ) ;
185+ expect ( dates ) . toHaveLength ( expectedLength ) ;
186+ } ) ;
187+ } ) ;
142188} ) ;
143189
144190describe ( "when the first month and the last month are different" , ( ) => {
@@ -161,16 +207,18 @@ describe("when the first month and the last month are different", () => {
161207 const firstMonth = new Date ( 2023 , 4 , 1 ) ;
162208 const lastMonth = new Date ( 2023 , 11 , 1 ) ;
163209 const maxDate = new Date ( 2023 , 5 , 15 ) ;
210+ const dateLib = new DateLib ( { weekStartsOn : 1 } ) ;
211+ const expectedLastDay = dateLib . addDays ( dateLib . startOfWeek ( maxDate ) , 6 ) ;
212+ const expectedLength =
213+ dateLib . differenceInCalendarDays (
214+ expectedLastDay ,
215+ dateLib . startOfWeek ( firstMonth ) ,
216+ ) + 1 ;
164217
165- it ( "the last day should be the max date" , ( ) => {
166- const dates = getDates (
167- [ firstMonth , lastMonth ] ,
168- maxDate ,
169- { } ,
170- new DateLib ( { weekStartsOn : 1 } ) ,
171- ) ;
172- expect ( dates ) . toHaveLength ( 46 ) ;
173- expect ( dates [ dates . length - 1 ] ) . toEqual ( maxDate ) ;
218+ it ( "the last day should be the end of that week" , ( ) => {
219+ const dates = getDates ( [ firstMonth , lastMonth ] , maxDate , { } , dateLib ) ;
220+ expect ( dates ) . toHaveLength ( expectedLength ) ;
221+ expect ( dates [ dates . length - 1 ] ) . toEqual ( expectedLastDay ) ;
174222 } ) ;
175223 } ) ;
176224 describe ( "when using ISO weeks" , ( ) => {
0 commit comments