Skip to content

Commit b3b1fc6

Browse files
authored
test(material-experimental/mdc-slider): add tests for sliders with se… (#22214)
* test(material-experimental/mdc-slider): add tests for sliders with set steps
1 parent c14f31e commit b3b1fc6

File tree

1 file changed

+161
-1
lines changed

1 file changed

+161
-1
lines changed

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

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes';
910
import {Platform} from '@angular/cdk/platform';
1011
import {
12+
dispatchEvent,
1113
dispatchMouseEvent,
1214
dispatchPointerEvent,
1315
dispatchTouchEvent,
@@ -392,7 +394,6 @@ describe('MDC-based MatSlider' , () => {
392394
it('should be able to set the min and max values when they are more precise ' +
393395
'than the step', () => {
394396
sliderInstance.step = 10;
395-
fixture.detectChanges();
396397
slideToValue(sliderInstance, 25, Thumb.END, platform.IOS);
397398
expect(inputInstance.value).toBe(25);
398399
slideToValue(sliderInstance, 75, Thumb.END, platform.IOS);
@@ -521,6 +522,130 @@ describe('MDC-based MatSlider' , () => {
521522
expect(endInputInstance.value).toBe(99);
522523
});
523524
});
525+
526+
describe('slider with set step', () => {
527+
let sliderInstance: MatSlider;
528+
let inputInstance: MatSliderThumb;
529+
530+
beforeEach(waitForAsync(() => {
531+
const fixture = createComponent(SliderWithStep);
532+
fixture.detectChanges();
533+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
534+
sliderInstance = sliderDebugElement.componentInstance;
535+
inputInstance = sliderInstance._getInput(Thumb.END);
536+
}));
537+
538+
it('should set the correct step value on mousedown', () => {
539+
expect(inputInstance.value).toBe(0);
540+
setValueByClick(sliderInstance, 13, platform.IOS);
541+
expect(inputInstance.value).toBe(25);
542+
});
543+
544+
it('should set the correct step value on slide', () => {
545+
slideToValue(sliderInstance, 12, Thumb.END, platform.IOS);
546+
expect(inputInstance.value).toBe(0);
547+
});
548+
549+
it('should not add decimals to the value if it is a whole number', () => {
550+
sliderInstance.step = 0.1;
551+
slideToValue(sliderInstance, 100, Thumb.END, platform.IOS);
552+
expect(inputInstance.value).toBe(100);
553+
});
554+
555+
it('should truncate long decimal values when using a decimal step', () => {
556+
// TODO(wagnermaciel): Uncomment this test once b/182504575 is resolved.
557+
// sliderInstance.step = 0.1;
558+
// slideToValue(sliderInstance, 33.3333, Thumb.END, platform.IOS);
559+
// expect(inputInstance.value).toBe(33);
560+
});
561+
562+
it('should truncate long decimal values when using a decimal step and the arrow keys', () => {
563+
sliderInstance.step = 0.1;
564+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.END);
565+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.END);
566+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.END);
567+
expect(inputInstance.value).toBe(0.3);
568+
});
569+
});
570+
571+
describe('range slider with set step', () => {
572+
let sliderInstance: MatSlider;
573+
let startInputInstance: MatSliderThumb;
574+
let endInputInstance: MatSliderThumb;
575+
576+
beforeEach(waitForAsync(() => {
577+
const fixture = createComponent(RangeSliderWithStep);
578+
fixture.detectChanges();
579+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
580+
sliderInstance = sliderDebugElement.componentInstance;
581+
startInputInstance = sliderInstance._getInput(Thumb.START);
582+
endInputInstance = sliderInstance._getInput(Thumb.END);
583+
}));
584+
585+
it('should set the correct step value on mousedown behind the start thumb', () => {
586+
sliderInstance._setValue(50, Thumb.START);
587+
setValueByClick(sliderInstance, 13, platform.IOS);
588+
expect(startInputInstance.value).toBe(25);
589+
});
590+
591+
it('should set the correct step value on mousedown in front of the end thumb', () => {
592+
sliderInstance._setValue(50, Thumb.END);
593+
setValueByClick(sliderInstance, 63, platform.IOS);
594+
expect(endInputInstance.value).toBe(75);
595+
});
596+
597+
it('should set the correct start thumb step value on slide', () => {
598+
slideToValue(sliderInstance, 26, Thumb.START, platform.IOS);
599+
expect(startInputInstance.value).toBe(25);
600+
});
601+
602+
it('should set the correct end thumb step value on slide', () => {
603+
slideToValue(sliderInstance, 45, Thumb.END, platform.IOS);
604+
expect(endInputInstance.value).toBe(50);
605+
});
606+
607+
it('should not add decimals to the end value if it is a whole number', () => {
608+
sliderInstance.step = 0.1;
609+
slideToValue(sliderInstance, 100, Thumb.END, platform.IOS);
610+
expect(endInputInstance.value).toBe(100);
611+
});
612+
613+
it('should not add decimals to the start value if it is a whole number', () => {
614+
sliderInstance.step = 0.1;
615+
slideToValue(sliderInstance, 100, Thumb.END, platform.IOS);
616+
expect(endInputInstance.value).toBe(100);
617+
});
618+
619+
it('should truncate long decimal start values when using a decimal step', () => {
620+
// TODO(wagnermaciel): Uncomment this test once b/182504575 is resolved.
621+
// sliderInstance.step = 0.1;
622+
// slideToValue(sliderInstance, 33.3333, Thumb.START, platform.IOS);
623+
// expect(startInputInstance.value).toBe(33);
624+
});
625+
626+
it('should truncate long decimal end values when using a decimal step', () => {
627+
// TODO(wagnermaciel): Uncomment this test once b/182504575 is resolved.
628+
// sliderInstance.step = 0.1;
629+
// slideToValue(sliderInstance, 66.6666, Thumb.END, platform.IOS);
630+
// expect(endInputInstance.value).toBe(66);
631+
});
632+
633+
it('should truncate long decimal start values when using a decimal step arrow keys', () => {
634+
sliderInstance.step = 0.1;
635+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.START);
636+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.START);
637+
changeValueUsingArrowKeys(sliderInstance, RIGHT_ARROW, Thumb.START);
638+
expect(startInputInstance.value).toBe(0.3);
639+
});
640+
641+
it('should truncate long decimal end values when using a decimal step arrow keys', () => {
642+
sliderInstance.step = 0.1;
643+
changeValueUsingArrowKeys(sliderInstance, LEFT_ARROW, Thumb.END);
644+
changeValueUsingArrowKeys(sliderInstance, LEFT_ARROW, Thumb.END);
645+
changeValueUsingArrowKeys(sliderInstance, LEFT_ARROW, Thumb.END);
646+
expect(endInputInstance.value).toBe(99.7);
647+
});
648+
});
524649
});
525650

526651

@@ -600,6 +725,25 @@ class SliderWithValue {}
600725
})
601726
class RangeSliderWithValue {}
602727

728+
@Component({
729+
template: `
730+
<mat-slider step="25">
731+
<input matSliderThumb>
732+
</mat-slider>
733+
`,
734+
})
735+
class SliderWithStep {}
736+
737+
@Component({
738+
template: `
739+
<mat-slider step="25">
740+
<input matSliderStartThumb>
741+
<input matSliderEndThumb>
742+
</mat-slider>
743+
`,
744+
})
745+
class RangeSliderWithStep {}
746+
603747
/** The pointer event types used by the MDC Slider. */
604748
const enum PointerEventType {
605749
POINTER_DOWN = 'pointerdown',
@@ -650,6 +794,22 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb, is
650794
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, endX, endY, isIOS);
651795
}
652796

797+
/**
798+
* Mimics changing the slider value using arrow keys.
799+
*
800+
* Dispatching keydown events on inputs do not trigger value changes. Thus, to mimic this behavior,
801+
* we manually change the slider inputs value and then dispatch a change event (which is what the
802+
* MDC Foundation is listening for & how it handles these updates).
803+
*/
804+
function changeValueUsingArrowKeys(slider: MatSlider, arrow: number, thumbPosition: Thumb) {
805+
const input = slider._getInput(thumbPosition);
806+
const value = arrow === RIGHT_ARROW
807+
? input.value + slider.step
808+
: input.value - slider.step;
809+
input._hostElement.value = value.toString();
810+
dispatchEvent(input._hostElement, new Event('change'));
811+
}
812+
653813
/** Dispatch a pointerdown or pointerup event if supported, otherwise dispatch the touch event. */
654814
function dispatchPointerOrTouchEvent(
655815
node: Node, type: PointerEventType, x: number, y: number, isIOS: boolean) {

0 commit comments

Comments
 (0)