Skip to content

Commit 0649866

Browse files
authored
test(material-experimental/mdc-slider): add ngModel unit tests (#22474)
1 parent 7ac106a commit 0649866

File tree

1 file changed

+152
-2
lines changed

1 file changed

+152
-2
lines changed

src/material-experimental/mdc-slider/slider.spec.ts

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import {
1313
dispatchTouchEvent,
1414
} from '@angular/cdk/testing/private';
1515
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';
1725
import {By} from '@angular/platform-browser';
1826
import {Thumb} from '@material/slider';
1927
import {MatSliderModule} from './module';
@@ -35,7 +43,7 @@ describe('MDC-based MatSlider' , () => {
3543

3644
function createComponent<T>(component: Type<T>): ComponentFixture<T> {
3745
TestBed.configureTestingModule({
38-
imports: [MatSliderModule],
46+
imports: [FormsModule, MatSliderModule],
3947
declarations: [component],
4048
}).compileComponents();
4149
return TestBed.createComponent<T>(component);
@@ -765,6 +773,122 @@ describe('MDC-based MatSlider' , () => {
765773
});
766774
});
767775

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+
768892
describe('slider with a two-way binding', () => {
769893
let fixture: ComponentFixture<SliderWithTwoWayBinding>;
770894
let testComponent: SliderWithTwoWayBinding;
@@ -980,6 +1104,32 @@ class RangeSliderWithOneWayBinding {
9801104
endValue = 75;
9811105
}
9821106

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+
9831133
@Component({
9841134
template: `
9851135
<mat-slider>

0 commit comments

Comments
 (0)