@@ -865,6 +865,60 @@ describe('MatDateRangeInput', () => {
865865 expect ( endInput . errorStateMatcher ) . toBe ( matcher ) ;
866866 } ) ;
867867
868+
869+ it ( 'should only update model for input that changed' , fakeAsync ( ( ) => {
870+ const fixture = createComponent ( RangePickerNgModel ) ;
871+
872+ fixture . detectChanges ( ) ;
873+ tick ( ) ;
874+
875+ expect ( fixture . componentInstance . startDateModelChangeCount ) . toBe ( 0 ) ;
876+ expect ( fixture . componentInstance . endDateModelChangeCount ) . toBe ( 0 ) ;
877+
878+ fixture . componentInstance . rangePicker . open ( ) ;
879+ fixture . detectChanges ( ) ;
880+ tick ( ) ;
881+
882+ const fromDate = new Date ( 2020 , 0 , 1 ) ;
883+ const toDate = new Date ( 2020 , 0 , 2 ) ;
884+ fixture . componentInstance . rangePicker . select ( fromDate ) ;
885+ fixture . detectChanges ( ) ;
886+ tick ( ) ;
887+
888+ expect ( fixture . componentInstance . startDateModelChangeCount ) . toBe ( 1 , 'Start Date set once' ) ;
889+ expect ( fixture . componentInstance . endDateModelChangeCount ) . toBe ( 0 , 'End Date not set' ) ;
890+
891+ fixture . componentInstance . rangePicker . select ( toDate ) ;
892+ fixture . detectChanges ( ) ;
893+ tick ( ) ;
894+
895+ expect ( fixture . componentInstance . startDateModelChangeCount ) . toBe ( 1 ,
896+ 'Start Date unchanged (set once)' ) ;
897+ expect ( fixture . componentInstance . endDateModelChangeCount ) . toBe ( 1 , 'End Date set once' ) ;
898+
899+ fixture . componentInstance . rangePicker . open ( ) ;
900+ fixture . detectChanges ( ) ;
901+ tick ( ) ;
902+
903+ const fromDate2 = new Date ( 2021 , 0 , 1 ) ;
904+ const toDate2 = new Date ( 2021 , 0 , 2 ) ;
905+ fixture . componentInstance . rangePicker . select ( fromDate2 ) ;
906+ fixture . detectChanges ( ) ;
907+ tick ( ) ;
908+
909+ expect ( fixture . componentInstance . startDateModelChangeCount ) . toBe ( 2 , 'Start Date set twice' ) ;
910+ expect ( fixture . componentInstance . endDateModelChangeCount ) . toBe ( 2 ,
911+ 'End Date set twice (nulled)' ) ;
912+
913+ fixture . componentInstance . rangePicker . select ( toDate2 ) ;
914+ fixture . detectChanges ( ) ;
915+ tick ( ) ;
916+
917+ expect ( fixture . componentInstance . startDateModelChangeCount ) . toBe ( 2 ,
918+ 'Start Date unchanged (set twice)' ) ;
919+ expect ( fixture . componentInstance . endDateModelChangeCount ) . toBe ( 3 , 'End date set three times' ) ;
920+ } ) ) ;
921+
868922} ) ;
869923
870924@Component ( {
@@ -959,8 +1013,24 @@ class RangePickerNgModel {
9591013 @ViewChild ( MatStartDate , { read : ElementRef } ) startInput : ElementRef < HTMLInputElement > ;
9601014 @ViewChild ( MatEndDate , { read : ElementRef } ) endInput : ElementRef < HTMLInputElement > ;
9611015 @ViewChild ( MatDateRangePicker ) rangePicker : MatDateRangePicker < Date > ;
962- start : Date | null = null ;
963- end : Date | null = null ;
1016+ private _start : Date | null = null ;
1017+ get start ( ) : Date | null {
1018+ return this . _start ;
1019+ }
1020+ set start ( aStart : Date | null ) {
1021+ this . startDateModelChangeCount ++ ;
1022+ this . _start = aStart ;
1023+ }
1024+ private _end : Date | null = null ;
1025+ get end ( ) : Date | null {
1026+ return this . _end ;
1027+ }
1028+ set end ( anEnd : Date | null ) {
1029+ this . endDateModelChangeCount ++ ;
1030+ this . _end = anEnd ;
1031+ }
1032+ startDateModelChangeCount = 0 ;
1033+ endDateModelChangeCount = 0 ;
9641034}
9651035
9661036
0 commit comments