@@ -13,7 +13,15 @@ import {
13
13
dispatchTouchEvent ,
14
14
} from '@angular/cdk/testing/private' ;
15
15
import { Component , QueryList , Type , ViewChild , ViewChildren } from '@angular/core' ;
16
- import { ComponentFixture , fakeAsync , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
16
+ import {
17
+ ComponentFixture ,
18
+ fakeAsync ,
19
+ flush ,
20
+ TestBed ,
21
+ tick ,
22
+ waitForAsync ,
23
+ } from '@angular/core/testing' ;
24
+ import { FormsModule } from '@angular/forms' ;
17
25
import { By } from '@angular/platform-browser' ;
18
26
import { Thumb } from '@material/slider' ;
19
27
import { MatSliderModule } from './module' ;
@@ -35,7 +43,7 @@ describe('MDC-based MatSlider' , () => {
35
43
36
44
function createComponent < T > ( component : Type < T > ) : ComponentFixture < T > {
37
45
TestBed . configureTestingModule ( {
38
- imports : [ MatSliderModule ] ,
46
+ imports : [ FormsModule , MatSliderModule ] ,
39
47
declarations : [ component ] ,
40
48
} ) . compileComponents ( ) ;
41
49
return TestBed . createComponent < T > ( component ) ;
@@ -765,6 +773,122 @@ describe('MDC-based MatSlider' , () => {
765
773
} ) ;
766
774
} ) ;
767
775
776
+ describe ( 'slider with ngModel' , ( ) => {
777
+ let fixture : ComponentFixture < SliderWithNgModel > ;
778
+ let testComponent : SliderWithNgModel ;
779
+ let inputInstance : MatSliderThumb ;
780
+
781
+ beforeEach ( waitForAsync ( ( ) => {
782
+ fixture = createComponent ( SliderWithNgModel ) ;
783
+ fixture . detectChanges ( ) ;
784
+ testComponent = fixture . debugElement . componentInstance ;
785
+ const sliderDebugElement = fixture . debugElement . query ( By . directive ( MatSlider ) ) ;
786
+ const sliderInstance = sliderDebugElement . componentInstance ;
787
+ inputInstance = sliderInstance . _getInput ( Thumb . END ) ;
788
+ } ) ) ;
789
+
790
+ it ( 'should update the model on mouseup' , ( ) => {
791
+ expect ( testComponent . val ) . toBe ( 0 ) ;
792
+ setValueByClick ( testComponent . slider , 76 , platform . IOS ) ;
793
+ fixture . detectChanges ( ) ;
794
+ expect ( testComponent . val ) . toBe ( 76 ) ;
795
+ } ) ;
796
+
797
+ it ( 'should update the model on slide' , ( ) => {
798
+ expect ( testComponent . val ) . toBe ( 0 ) ;
799
+ slideToValue ( testComponent . slider , 19 , Thumb . END , platform . IOS ) ;
800
+ fixture . detectChanges ( ) ;
801
+ expect ( testComponent . val ) . toBe ( 19 ) ;
802
+ } ) ;
803
+
804
+ it ( 'should be able to reset a slider by setting the model back to undefined' , fakeAsync ( ( ) => {
805
+ expect ( inputInstance . value ) . toBe ( 0 ) ;
806
+ testComponent . val = 5 ;
807
+ fixture . detectChanges ( ) ;
808
+ flush ( ) ;
809
+ expect ( inputInstance . value ) . toBe ( 5 ) ;
810
+
811
+ testComponent . val = undefined ;
812
+ fixture . detectChanges ( ) ;
813
+ flush ( ) ;
814
+ expect ( inputInstance . value ) . toBe ( 0 ) ;
815
+ } ) ) ;
816
+ } ) ;
817
+
818
+ describe ( 'slider with ngModel' , ( ) => {
819
+ let fixture : ComponentFixture < RangeSliderWithNgModel > ;
820
+ let testComponent : RangeSliderWithNgModel ;
821
+
822
+ let startInputInstance : MatSliderThumb ;
823
+ let endInputInstance : MatSliderThumb ;
824
+
825
+ beforeEach ( waitForAsync ( ( ) => {
826
+ fixture = createComponent ( RangeSliderWithNgModel ) ;
827
+ fixture . detectChanges ( ) ;
828
+ testComponent = fixture . debugElement . componentInstance ;
829
+ const sliderDebugElement = fixture . debugElement . query ( By . directive ( MatSlider ) ) ;
830
+ const sliderInstance = sliderDebugElement . componentInstance ;
831
+ startInputInstance = sliderInstance . _getInput ( Thumb . START ) ;
832
+ endInputInstance = sliderInstance . _getInput ( Thumb . END ) ;
833
+ } ) ) ;
834
+
835
+ it ( 'should update the start thumb model on mouseup' , ( ) => {
836
+ expect ( testComponent . startVal ) . toBe ( 0 ) ;
837
+ setValueByClick ( testComponent . slider , 25 , platform . IOS ) ;
838
+ fixture . detectChanges ( ) ;
839
+ expect ( testComponent . startVal ) . toBe ( 25 ) ;
840
+ } ) ;
841
+
842
+ it ( 'should update the end thumb model on mouseup' , ( ) => {
843
+ expect ( testComponent . endVal ) . toBe ( 100 ) ;
844
+ setValueByClick ( testComponent . slider , 75 , platform . IOS ) ;
845
+ fixture . detectChanges ( ) ;
846
+ expect ( testComponent . endVal ) . toBe ( 75 ) ;
847
+ } ) ;
848
+
849
+ it ( 'should update the start thumb model on slide' , ( ) => {
850
+ expect ( testComponent . startVal ) . toBe ( 0 ) ;
851
+ slideToValue ( testComponent . slider , 19 , Thumb . START , platform . IOS ) ;
852
+ fixture . detectChanges ( ) ;
853
+ expect ( testComponent . startVal ) . toBe ( 19 ) ;
854
+ } ) ;
855
+
856
+ it ( 'should update the end thumb model on slide' , ( ) => {
857
+ expect ( testComponent . endVal ) . toBe ( 100 ) ;
858
+ slideToValue ( testComponent . slider , 19 , Thumb . END , platform . IOS ) ;
859
+ fixture . detectChanges ( ) ;
860
+ expect ( testComponent . endVal ) . toBe ( 19 ) ;
861
+ } ) ;
862
+
863
+ it ( 'should be able to reset a slider by setting the start thumb model back to undefined' ,
864
+ fakeAsync ( ( ) => {
865
+ expect ( startInputInstance . value ) . toBe ( 0 ) ;
866
+ testComponent . startVal = 5 ;
867
+ fixture . detectChanges ( ) ;
868
+ flush ( ) ;
869
+ expect ( startInputInstance . value ) . toBe ( 5 ) ;
870
+
871
+ testComponent . startVal = undefined ;
872
+ fixture . detectChanges ( ) ;
873
+ flush ( ) ;
874
+ expect ( startInputInstance . value ) . toBe ( 0 ) ;
875
+ } ) ) ;
876
+
877
+ it ( 'should be able to reset a slider by setting the end thumb model back to undefined' ,
878
+ fakeAsync ( ( ) => {
879
+ expect ( endInputInstance . value ) . toBe ( 100 ) ;
880
+ testComponent . endVal = 5 ;
881
+ fixture . detectChanges ( ) ;
882
+ flush ( ) ;
883
+ expect ( endInputInstance . value ) . toBe ( 5 ) ;
884
+
885
+ testComponent . endVal = undefined ;
886
+ fixture . detectChanges ( ) ;
887
+ flush ( ) ;
888
+ expect ( endInputInstance . value ) . toBe ( 0 ) ;
889
+ } ) ) ;
890
+ } ) ;
891
+
768
892
describe ( 'slider with a two-way binding' , ( ) => {
769
893
let fixture : ComponentFixture < SliderWithTwoWayBinding > ;
770
894
let testComponent : SliderWithTwoWayBinding ;
@@ -980,6 +1104,32 @@ class RangeSliderWithOneWayBinding {
980
1104
endValue = 75 ;
981
1105
}
982
1106
1107
+ @Component ( {
1108
+ template : `
1109
+ <mat-slider>
1110
+ <input [(ngModel)]="val" matSliderThumb>
1111
+ </mat-slider>
1112
+ ` ,
1113
+ } )
1114
+ class SliderWithNgModel {
1115
+ @ViewChild ( MatSlider ) slider : MatSlider ;
1116
+ val : number | undefined = 0 ;
1117
+ }
1118
+
1119
+ @Component ( {
1120
+ template : `
1121
+ <mat-slider>
1122
+ <input [(ngModel)]="startVal" matSliderStartThumb>
1123
+ <input [(ngModel)]="endVal" matSliderEndThumb>
1124
+ </mat-slider>
1125
+ ` ,
1126
+ } )
1127
+ class RangeSliderWithNgModel {
1128
+ @ViewChild ( MatSlider ) slider : MatSlider ;
1129
+ startVal : number | undefined = 0 ;
1130
+ endVal : number | undefined = 100 ;
1131
+ }
1132
+
983
1133
@Component ( {
984
1134
template : `
985
1135
<mat-slider>
0 commit comments