Skip to content

Commit 4bbbf18

Browse files
authored
test(material-experimental/mdc-slider): add input handler tests (#22481)
1 parent 1a4f999 commit 4bbbf18

File tree

2 files changed

+147
-2
lines changed

2 files changed

+147
-2
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ describe('MDC-based MatSlider' , () => {
5757
});
5858

5959
it('should update the end thumb value on slide', async () => {
60-
console.log('value:', await getSliderValue(slider, Thumb.END));
6160
await slideToValue(slider, 55, Thumb.END);
62-
console.log('value:', await getSliderValue(slider, Thumb.END));
6361
expect(await getSliderValue(slider, Thumb.END)).toBe(55);
6462
});
6563

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

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,153 @@ describe('MDC-based MatSlider' , () => {
10201020
});
10211021
});
10221022

1023+
describe('slider with input event', () => {
1024+
let sliderInstance: MatSlider;
1025+
let sliderElement: HTMLElement;
1026+
let testComponent: SliderWithChangeHandler;
1027+
1028+
beforeEach(waitForAsync(() => {
1029+
const fixture = createComponent(SliderWithChangeHandler);
1030+
fixture.detectChanges();
1031+
1032+
testComponent = fixture.debugElement.componentInstance;
1033+
1034+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1035+
sliderInstance = sliderDebugElement.componentInstance;
1036+
sliderElement = sliderInstance._elementRef.nativeElement;
1037+
}));
1038+
1039+
it('should emit an input event while sliding', () => {
1040+
const dispatchSliderEvent = (type: PointerEventType, value: number) => {
1041+
const {x, y} = getCoordsForValue(sliderInstance, value);
1042+
dispatchPointerOrTouchEvent(sliderElement, type, x, y, platform.IOS);
1043+
};
1044+
1045+
expect(testComponent.onChange).not.toHaveBeenCalled();
1046+
expect(testComponent.onInput).not.toHaveBeenCalled();
1047+
1048+
// pointer down on current value (should not trigger input event)
1049+
dispatchSliderEvent(PointerEventType.POINTER_DOWN, 0);
1050+
1051+
// value changes (should trigger input)
1052+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 10);
1053+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 25);
1054+
1055+
// a new value has been committed (should trigger change event)
1056+
dispatchSliderEvent(PointerEventType.POINTER_UP, 25);
1057+
1058+
expect(testComponent.onInput).toHaveBeenCalledTimes(2);
1059+
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
1060+
});
1061+
1062+
it('should emit an input event when clicking', () => {
1063+
expect(testComponent.onChange).not.toHaveBeenCalled();
1064+
expect(testComponent.onInput).not.toHaveBeenCalled();
1065+
setValueByClick(sliderInstance, 75, platform.IOS);
1066+
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
1067+
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
1068+
});
1069+
});
1070+
1071+
describe('range slider with input event', () => {
1072+
let sliderInstance: MatSlider;
1073+
let sliderElement: HTMLElement;
1074+
let testComponent: RangeSliderWithChangeHandler;
1075+
1076+
beforeEach(waitForAsync(() => {
1077+
const fixture = createComponent(RangeSliderWithChangeHandler);
1078+
fixture.detectChanges();
1079+
1080+
testComponent = fixture.debugElement.componentInstance;
1081+
1082+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1083+
sliderInstance = sliderDebugElement.componentInstance;
1084+
sliderElement = sliderInstance._elementRef.nativeElement;
1085+
}));
1086+
1087+
it('should emit an input event while sliding the start thumb', () => {
1088+
const dispatchSliderEvent = (type: PointerEventType, value: number) => {
1089+
const {x, y} = getCoordsForValue(sliderInstance, value);
1090+
dispatchPointerOrTouchEvent(sliderElement, type, x, y, platform.IOS);
1091+
};
1092+
1093+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1094+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1095+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1096+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1097+
1098+
// pointer down on current start thumb value (should not trigger input event)
1099+
dispatchSliderEvent(PointerEventType.POINTER_DOWN, 0);
1100+
1101+
// value changes (should trigger input)
1102+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 10);
1103+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 25);
1104+
1105+
// a new value has been committed (should trigger change event)
1106+
dispatchSliderEvent(PointerEventType.POINTER_UP, 25);
1107+
1108+
expect(testComponent.onStartThumbChange).toHaveBeenCalledTimes(1);
1109+
expect(testComponent.onStartThumbInput).toHaveBeenCalledTimes(2);
1110+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1111+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1112+
});
1113+
1114+
it('should emit an input event while sliding the end thumb', () => {
1115+
const dispatchSliderEvent = (type: PointerEventType, value: number) => {
1116+
const {x, y} = getCoordsForValue(sliderInstance, value);
1117+
dispatchPointerOrTouchEvent(sliderElement, type, x, y, platform.IOS);
1118+
};
1119+
1120+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1121+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1122+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1123+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1124+
1125+
// pointer down on current end thumb value (should not trigger input event)
1126+
dispatchSliderEvent(PointerEventType.POINTER_DOWN, 100);
1127+
1128+
// value changes (should trigger input)
1129+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 90);
1130+
dispatchSliderEvent(PointerEventType.POINTER_MOVE, 55);
1131+
1132+
// a new value has been committed (should trigger change event)
1133+
dispatchSliderEvent(PointerEventType.POINTER_UP, 55);
1134+
1135+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1136+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1137+
expect(testComponent.onEndThumbChange).toHaveBeenCalledTimes(1);
1138+
expect(testComponent.onEndThumbInput).toHaveBeenCalledTimes(2);
1139+
});
1140+
1141+
it('should emit an input event on the start thumb when clicking near it', () => {
1142+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1143+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1144+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1145+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1146+
1147+
setValueByClick(sliderInstance, 30, platform.IOS);
1148+
1149+
expect(testComponent.onStartThumbChange).toHaveBeenCalledTimes(1);
1150+
expect(testComponent.onStartThumbInput).toHaveBeenCalledTimes(1);
1151+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1152+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1153+
});
1154+
1155+
it('should emit an input event on the end thumb when clicking near it', () => {
1156+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1157+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1158+
expect(testComponent.onEndThumbChange).not.toHaveBeenCalled();
1159+
expect(testComponent.onEndThumbInput).not.toHaveBeenCalled();
1160+
1161+
setValueByClick(sliderInstance, 55, platform.IOS);
1162+
1163+
expect(testComponent.onStartThumbChange).not.toHaveBeenCalled();
1164+
expect(testComponent.onStartThumbInput).not.toHaveBeenCalled();
1165+
expect(testComponent.onEndThumbChange).toHaveBeenCalledTimes(1);
1166+
expect(testComponent.onEndThumbInput).toHaveBeenCalledTimes(1);
1167+
});
1168+
});
1169+
10231170
describe('slider with ngModel', () => {
10241171
let fixture: ComponentFixture<SliderWithNgModel>;
10251172
let testComponent: SliderWithNgModel;

0 commit comments

Comments
 (0)