|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +import {LEFT_ARROW, RIGHT_ARROW} from '@angular/cdk/keycodes'; |
9 | 10 | import {Platform} from '@angular/cdk/platform';
|
10 | 11 | import {
|
| 12 | + dispatchEvent, |
11 | 13 | dispatchMouseEvent,
|
12 | 14 | dispatchPointerEvent,
|
13 | 15 | dispatchTouchEvent,
|
@@ -392,7 +394,6 @@ describe('MDC-based MatSlider' , () => {
|
392 | 394 | it('should be able to set the min and max values when they are more precise ' +
|
393 | 395 | 'than the step', () => {
|
394 | 396 | sliderInstance.step = 10;
|
395 |
| - fixture.detectChanges(); |
396 | 397 | slideToValue(sliderInstance, 25, Thumb.END, platform.IOS);
|
397 | 398 | expect(inputInstance.value).toBe(25);
|
398 | 399 | slideToValue(sliderInstance, 75, Thumb.END, platform.IOS);
|
@@ -521,6 +522,130 @@ describe('MDC-based MatSlider' , () => {
|
521 | 522 | expect(endInputInstance.value).toBe(99);
|
522 | 523 | });
|
523 | 524 | });
|
| 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 | + }); |
524 | 649 | });
|
525 | 650 |
|
526 | 651 |
|
@@ -600,6 +725,25 @@ class SliderWithValue {}
|
600 | 725 | })
|
601 | 726 | class RangeSliderWithValue {}
|
602 | 727 |
|
| 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 | + |
603 | 747 | /** The pointer event types used by the MDC Slider. */
|
604 | 748 | const enum PointerEventType {
|
605 | 749 | POINTER_DOWN = 'pointerdown',
|
@@ -650,6 +794,22 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb, is
|
650 | 794 | dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, endX, endY, isIOS);
|
651 | 795 | }
|
652 | 796 |
|
| 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 | + |
653 | 813 | /** Dispatch a pointerdown or pointerup event if supported, otherwise dispatch the touch event. */
|
654 | 814 | function dispatchPointerOrTouchEvent(
|
655 | 815 | node: Node, type: PointerEventType, x: number, y: number, isIOS: boolean) {
|
|
0 commit comments