Skip to content

Commit 5903eb5

Browse files
committed
test(material-experimental/mdc-slider): add tests for sliders with set steps
1 parent aba710b commit 5903eb5

File tree

1 file changed

+164
-1
lines changed

1 file changed

+164
-1
lines changed

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

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

526649

@@ -600,6 +723,25 @@ class SliderWithValue {}
600723
})
601724
class RangeSliderWithValue {}
602725

726+
@Component({
727+
template: `
728+
<mat-slider step="25">
729+
<input matSliderThumb>
730+
</mat-slider>
731+
`,
732+
})
733+
class SliderWithStep {}
734+
735+
@Component({
736+
template: `
737+
<mat-slider step="25">
738+
<input matSliderStartThumb>
739+
<input matSliderEndThumb>
740+
</mat-slider>
741+
`,
742+
})
743+
class RangeSliderWithStep {}
744+
603745
/** The pointer event types used by the MDC Slider. */
604746
const enum PointerEventType {
605747
POINTER_DOWN = 'pointerdown',
@@ -614,6 +756,11 @@ const enum TouchEventType {
614756
TOUCH_MOVE = 'touchmove',
615757
}
616758

759+
const enum ArrowKey {
760+
LEFT = 37,
761+
RIGHT = 39,
762+
}
763+
617764
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
618765
function setValueByClick(slider: MatSlider, value: number, isIOS: boolean) {
619766
const {min, max} = slider;
@@ -650,6 +797,22 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb, is
650797
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, endX, endY, isIOS);
651798
}
652799

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

0 commit comments

Comments
 (0)