@@ -21,14 +21,15 @@ describe("MonthDropdown", () => {
2121
2222 function getMonthDropdown (
2323 overrideProps ?: Partial <
24- Pick < MonthDropdownProps , "dropdownMode" | "month" | "onChange" >
24+ Pick < MonthDropdownProps , "dropdownMode" | "month" | "onChange" | "date" >
2525 > &
26- Omit < MonthDropdownProps , "dropdownMode" | "month" | "onChange" > ,
26+ Omit < MonthDropdownProps , "dropdownMode" | "month" | "onChange" | "date" > ,
2727 ) {
2828 return render (
2929 < MonthDropdown
3030 dropdownMode = "scroll"
3131 month = { 11 }
32+ date = { new Date ( 2025 , 11 , 1 ) }
3233 onChange = { mockHandleChange }
3334 { ...overrideProps }
3435 /> ,
@@ -138,6 +139,7 @@ describe("MonthDropdown", () => {
138139 onChange = { onCancelSpy }
139140 month = { 11 }
140141 monthNames = { monthNames }
142+ date = { new Date ( 2025 , 11 , 1 ) }
141143 /> ,
142144 ) ;
143145 fireEvent . mouseDown ( document . body ) ;
@@ -291,6 +293,58 @@ describe("MonthDropdown", () => {
291293 const firstMonthOption = monthOptions [ 0 ] ;
292294 expect ( document . activeElement ) . toEqual ( firstMonthOption ) ;
293295 } ) ;
296+
297+ it ( "does not call onChange when clicking a month outside of minDate/maxDate range" , ( ) => {
298+ monthDropdown = getMonthDropdown ( {
299+ month : 3 ,
300+ date : new Date ( 2025 , 3 , 1 ) ,
301+ minDate : new Date ( 2025 , 0 , 1 ) ,
302+ maxDate : new Date ( 2025 , 5 , 30 ) ,
303+ } ) ;
304+ const monthReadView = safeQuerySelector (
305+ monthDropdown ,
306+ ".react-datepicker__month-read-view" ,
307+ ) ;
308+ fireEvent . click ( monthReadView ) ;
309+
310+ const monthOptions = safeQuerySelectorAll (
311+ monthDropdown ,
312+ ".react-datepicker__month-option" ,
313+ ) ;
314+
315+ // August (index 7) is outside the Jan-Jun 2025 range
316+ const disabledMonthOption = monthOptions [ 7 ] ! ;
317+ expect ( disabledMonthOption . getAttribute ( "aria-disabled" ) ) . toEqual ( "true" ) ;
318+
319+ fireEvent . click ( disabledMonthOption ) ;
320+ expect ( handleChangeResult ) . toBeNull ( ) ;
321+ } ) ;
322+
323+ it ( "calls onChange when clicking a month inside of minDate/maxDate range" , ( ) => {
324+ monthDropdown = getMonthDropdown ( {
325+ month : 3 ,
326+ date : new Date ( 2025 , 3 , 1 ) ,
327+ minDate : new Date ( 2025 , 0 , 1 ) ,
328+ maxDate : new Date ( 2025 , 5 , 30 ) ,
329+ } ) ;
330+ const monthReadView = safeQuerySelector (
331+ monthDropdown ,
332+ ".react-datepicker__month-read-view" ,
333+ ) ;
334+ fireEvent . click ( monthReadView ) ;
335+
336+ const monthOptions = safeQuerySelectorAll (
337+ monthDropdown ,
338+ ".react-datepicker__month-option" ,
339+ ) ;
340+
341+ // May (index 4) is inside the Jan-Jun 2025 range
342+ const enabledMonthOption = monthOptions [ 4 ] ! ;
343+ expect ( enabledMonthOption . getAttribute ( "aria-disabled" ) ) . toBeNull ( ) ;
344+
345+ fireEvent . click ( enabledMonthOption ) ;
346+ expect ( handleChangeResult ) . toEqual ( 4 ) ;
347+ } ) ;
294348 } ) ;
295349
296350 describe ( "select mode" , ( ) => {
@@ -381,5 +435,32 @@ describe("MonthDropdown", () => {
381435 } ) ;
382436 expect ( handleChangeResult ) . toEqual ( 9 ) ;
383437 } ) ;
438+
439+ it ( "disables options for months outside of minDate/maxDate range" , ( ) => {
440+ monthDropdown = getMonthDropdown ( {
441+ dropdownMode : "select" ,
442+ month : 3 ,
443+ date : new Date ( 2025 , 3 , 1 ) ,
444+ minDate : new Date ( 2025 , 0 , 1 ) ,
445+ maxDate : new Date ( 2025 , 5 , 30 ) ,
446+ } ) ;
447+ const options = Array . from (
448+ monthDropdown . querySelectorAll < HTMLOptionElement > ( "option" ) ,
449+ ) ;
450+ expect ( options . map ( ( o ) => o . disabled ) ) . toEqual ( [
451+ false , // Jan
452+ false , // Feb
453+ false , // Mar
454+ false , // Apr
455+ false , // May
456+ false , // Jun
457+ true , // Jul
458+ true , // Aug
459+ true , // Sep
460+ true , // Oct
461+ true , // Nov
462+ true , // Dec
463+ ] ) ;
464+ } ) ;
384465 } ) ;
385466} ) ;
0 commit comments