@@ -4,6 +4,7 @@ import { act, render } from '@testing-library/react';
44import { getMonthStart } from '@wojtekmaj/date-utils' ;
55
66import Calendar from './Calendar' ;
7+ import { LooseValue } from './shared/types' ;
78
89const { format } = new Intl . DateTimeFormat ( 'en-US' , {
910 day : 'numeric' ,
@@ -707,6 +708,85 @@ describe('Calendar', () => {
707708
708709 expect ( onActiveStartDateChange ) . not . toHaveBeenCalled ( ) ;
709710 } ) ;
711+
712+ it ( 'returns to start of range when range is completed' , ( ) => {
713+ const onActiveStartDateChange = vi . fn ( ) ;
714+
715+ const initialRange : LooseValue = [ new Date ( 2017 , 0 , 10 ) , null ] ;
716+
717+ const { container } = render (
718+ < Calendar
719+ view = "month"
720+ selectRange
721+ onActiveStartDateChange = { onActiveStartDateChange }
722+ defaultValue = { initialRange }
723+ /> ,
724+ ) ;
725+
726+ const nextMonthButton = container . querySelector (
727+ 'button.react-calendar__navigation__next-button' ,
728+ ) as HTMLButtonElement ;
729+
730+ act ( ( ) => {
731+ nextMonthButton . click ( ) ;
732+ } ) ;
733+ // Disregard the call on the above button click
734+ onActiveStartDateChange . mockClear ( ) ;
735+
736+ // First day not in the previous month
737+ const firstDayTile = container . querySelector (
738+ '.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])' ,
739+ ) as HTMLButtonElement ;
740+
741+ act ( ( ) => {
742+ firstDayTile . click ( ) ;
743+ } ) ;
744+
745+ expect ( onActiveStartDateChange ) . toHaveBeenCalledWith (
746+ expect . objectContaining ( {
747+ action : 'onChange' ,
748+ activeStartDate : new Date ( 2017 , 0 , 1 ) ,
749+ view : 'month' ,
750+ } ) ,
751+ ) ;
752+ } ) ;
753+
754+ it ( 'does not change activeStartDate on range completion when goToRangeStartOnSelect is set to false' , ( ) => {
755+ const onActiveStartDateChange = vi . fn ( ) ;
756+
757+ const initialRange : LooseValue = new Date ( 2017 , 0 , 10 ) ;
758+
759+ const { container } = render (
760+ < Calendar
761+ view = "month"
762+ selectRange
763+ onActiveStartDateChange = { onActiveStartDateChange }
764+ defaultValue = { initialRange }
765+ goToRangeStartOnSelect = { false }
766+ /> ,
767+ ) ;
768+
769+ const nextMonthButton = container . querySelector (
770+ 'button.react-calendar__navigation__next-button' ,
771+ ) as HTMLButtonElement ;
772+
773+ act ( ( ) => {
774+ nextMonthButton . click ( ) ;
775+ } ) ;
776+ // Disregard the call on the above button click
777+ onActiveStartDateChange . mockClear ( ) ;
778+
779+ // First day not in the previous month
780+ const firstDayTile = container . querySelector (
781+ '.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])' ,
782+ ) as HTMLButtonElement ;
783+
784+ act ( ( ) => {
785+ firstDayTile . click ( ) ;
786+ } ) ;
787+
788+ expect ( onActiveStartDateChange ) . not . toHaveBeenCalled ( ) ;
789+ } ) ;
710790 } ) ;
711791
712792 describe ( 'handles view change properly' , ( ) => {
0 commit comments