@@ -2922,37 +2922,67 @@ describe("DatePicker", () => {
29222922 expect ( instance ! . state . monthSelectedIn ) . toEqual ( 0 ) ;
29232923 } ) ;
29242924
2925- it ( "should set monthSelectedIn to 0 when selectsRange is true " , ( ) => {
2925+ it ( "should set monthSelectedIn to 0 when calendar opens with selectsRange " , ( ) => {
29262926 let instance : DatePicker | null = null ;
2927- const { rerender } = render (
2927+ const { container } = render (
29282928 < DatePicker
29292929 ref = { ( node ) => {
29302930 instance = node ;
29312931 } }
29322932 monthsShown = { 2 }
2933- inline
2933+ selectsRange
29342934 /> ,
29352935 ) ;
29362936 expect ( instance ) . toBeTruthy ( ) ;
2937+
2938+ // Manually set monthSelectedIn to 1 (simulating previous user interaction)
29372939 act ( ( ) => {
29382940 (
29392941 instance ! . state as unknown as { monthSelectedIn : number }
29402942 ) . monthSelectedIn = 1 ;
29412943 } ) ;
29422944 expect ( instance ! . state . monthSelectedIn ) . toEqual ( 1 ) ;
29432945
2944- rerender (
2946+ // Open the calendar by clicking the input
2947+ const input = container . querySelector ( "input" ) ;
2948+ expect ( input ) . not . toBeNull ( ) ;
2949+ fireEvent . click ( input ! ) ;
2950+
2951+ // monthSelectedIn should be reset to 0 when the calendar opens
2952+ expect ( instance ! . state . monthSelectedIn ) . toEqual ( 0 ) ;
2953+ } ) ;
2954+
2955+ it ( "should NOT reset monthSelectedIn when selecting a date from second calendar with selectsRange" , ( ) => {
2956+ let instance : DatePicker | null = null ;
2957+ const onChange = jest . fn ( ) ;
2958+ const { container } = render (
29452959 < DatePicker
29462960 ref = { ( node ) => {
29472961 instance = node ;
29482962 } }
29492963 monthsShown = { 2 }
2950- inline
29512964 selectsRange
2965+ inline
2966+ onChange = { onChange }
29522967 /> ,
29532968 ) ;
2969+ expect ( instance ) . toBeTruthy ( ) ;
29542970
2955- expect ( instance ! . state . monthSelectedIn ) . toEqual ( 0 ) ;
2971+ // Find a day in the second month container
2972+ const secondMonthContainer = container . querySelectorAll (
2973+ ".react-datepicker__month-container" ,
2974+ ) [ 1 ] ;
2975+ expect ( secondMonthContainer ) . toBeTruthy ( ) ;
2976+ const secondMonthDays = secondMonthContainer ?. querySelectorAll (
2977+ ".react-datepicker__day:not(.react-datepicker__day--outside-month)" ,
2978+ ) ;
2979+ expect ( secondMonthDays ?. length ) . toBeGreaterThan ( 0 ) ;
2980+
2981+ // Click a day in the second month
2982+ fireEvent . click ( secondMonthDays ! [ 10 ] ! ) ;
2983+
2984+ // monthSelectedIn should be 1 (second calendar), not reset to 0
2985+ expect ( instance ! . state . monthSelectedIn ) . toEqual ( 1 ) ;
29562986 } ) ;
29572987
29582988 it ( "should disable non-jumping if prop focusSelectedMonth is true" , ( ) => {
0 commit comments