@@ -646,6 +646,88 @@ describe('MDC-based MatSlider' , () => {
646
646
expect ( endInputInstance . value ) . toBe ( 99.7 ) ;
647
647
} ) ;
648
648
} ) ;
649
+
650
+ describe ( 'slider with custom thumb label formatting' , ( ) => {
651
+ let fixture : ComponentFixture < DiscreteSliderWithDisplayWith > ;
652
+ let sliderInstance : MatSlider ;
653
+ let valueIndicatorTextElement : Element ;
654
+
655
+ beforeEach ( ( ) => {
656
+ fixture = createComponent ( DiscreteSliderWithDisplayWith ) ;
657
+ fixture . detectChanges ( ) ;
658
+ const sliderDebugElement = fixture . debugElement . query ( By . directive ( MatSlider ) ) ! ;
659
+ const sliderNativeElement = sliderDebugElement . nativeElement ;
660
+ sliderInstance = sliderDebugElement . componentInstance ;
661
+ valueIndicatorTextElement =
662
+ sliderNativeElement . querySelector ( '.mdc-slider__value-indicator-text' ) ! ;
663
+ } ) ;
664
+
665
+ it ( 'should invoke the passed-in `displayWith` function with the value' , ( ) => {
666
+ spyOn ( fixture . componentInstance , 'displayWith' ) . and . callThrough ( ) ;
667
+ sliderInstance . _setValue ( 1337 , Thumb . END ) ;
668
+ fixture . whenStable ( ) . then ( ( ) => {
669
+ expect ( fixture . componentInstance . displayWith ) . toHaveBeenCalledWith ( 1337 ) ;
670
+ } ) ;
671
+ } ) ;
672
+
673
+ it ( 'should format the thumb label based on the passed-in `displayWith` function' , ( ) => {
674
+ sliderInstance . _setValue ( 200000 , Thumb . END ) ;
675
+ fixture . whenStable ( ) . then ( ( ) => {
676
+ expect ( valueIndicatorTextElement . textContent ) . toBe ( '200k' ) ;
677
+ } ) ;
678
+ } ) ;
679
+ } ) ;
680
+
681
+ describe ( 'range slider with custom thumb label formatting' , ( ) => {
682
+ let fixture : ComponentFixture < DiscreteRangeSliderWithDisplayWith > ;
683
+ let sliderInstance : MatSlider ;
684
+ let startValueIndicatorTextElement : Element ;
685
+ let endValueIndicatorTextElement : Element ;
686
+
687
+ beforeEach ( ( ) => {
688
+ fixture = createComponent ( DiscreteRangeSliderWithDisplayWith ) ;
689
+ fixture . detectChanges ( ) ;
690
+ const sliderDebugElement = fixture . debugElement . query ( By . directive ( MatSlider ) ) ! ;
691
+ sliderInstance = sliderDebugElement . componentInstance ;
692
+
693
+ const startThumbElement = sliderInstance . _getThumbElement ( Thumb . START ) ;
694
+ const endThumbElement = sliderInstance . _getThumbElement ( Thumb . END ) ;
695
+ startValueIndicatorTextElement =
696
+ startThumbElement . querySelector ( '.mdc-slider__value-indicator-text' ) ! ;
697
+ endValueIndicatorTextElement =
698
+ endThumbElement . querySelector ( '.mdc-slider__value-indicator-text' ) ! ;
699
+ } ) ;
700
+
701
+ it ( 'should invoke the passed-in `displayWith` function with the start value' , ( ) => {
702
+ spyOn ( fixture . componentInstance , 'displayWith' ) . and . callThrough ( ) ;
703
+ sliderInstance . _setValue ( 1337 , Thumb . START ) ;
704
+ fixture . whenStable ( ) . then ( ( ) => {
705
+ expect ( fixture . componentInstance . displayWith ) . toHaveBeenCalledWith ( 1337 ) ;
706
+ } ) ;
707
+ } ) ;
708
+
709
+ it ( 'should invoke the passed-in `displayWith` function with the end value' , ( ) => {
710
+ spyOn ( fixture . componentInstance , 'displayWith' ) . and . callThrough ( ) ;
711
+ sliderInstance . _setValue ( 5996 , Thumb . END ) ;
712
+ fixture . whenStable ( ) . then ( ( ) => {
713
+ expect ( fixture . componentInstance . displayWith ) . toHaveBeenCalledWith ( 5996 ) ;
714
+ } ) ;
715
+ } ) ;
716
+
717
+ it ( 'should format the start thumb label based on the passed-in `displayWith` function' , ( ) => {
718
+ sliderInstance . _setValue ( 200000 , Thumb . START ) ;
719
+ fixture . whenStable ( ) . then ( ( ) => {
720
+ expect ( startValueIndicatorTextElement . textContent ) . toBe ( '200k' ) ;
721
+ } ) ;
722
+ } ) ;
723
+
724
+ it ( 'should format the end thumb label based on the passed-in `displayWith` function' , ( ) => {
725
+ sliderInstance . _setValue ( 700000 , Thumb . END ) ;
726
+ fixture . whenStable ( ) . then ( ( ) => {
727
+ expect ( endValueIndicatorTextElement . textContent ) . toBe ( '700k' ) ;
728
+ } ) ;
729
+ } ) ;
730
+ } ) ;
649
731
} ) ;
650
732
651
733
@@ -744,6 +826,35 @@ class SliderWithStep {}
744
826
} )
745
827
class RangeSliderWithStep { }
746
828
829
+ @Component ( {
830
+ template : `
831
+ <mat-slider [displayWith]="displayWith" min="1" max="1000000" discrete>
832
+ <input matSliderThumb>
833
+ </mat-slider>
834
+ ` ,
835
+ } )
836
+ class DiscreteSliderWithDisplayWith {
837
+ displayWith ( v : number ) {
838
+ if ( v >= 1000 ) { return `$${ v / 1000 } k` ; }
839
+ return `$${ v } ` ;
840
+ }
841
+ }
842
+
843
+ @Component ( {
844
+ template : `
845
+ <mat-slider [displayWith]="displayWith" min="1" max="1000000" discrete>
846
+ <input matSliderStartThumb>
847
+ <input matSliderEndThumb>
848
+ </mat-slider>
849
+ ` ,
850
+ } )
851
+ class DiscreteRangeSliderWithDisplayWith {
852
+ displayWith ( v : number ) {
853
+ if ( v >= 1000 ) { return `$${ v / 1000 } k` ; }
854
+ return `$${ v } ` ;
855
+ }
856
+ }
857
+
747
858
/** The pointer event types used by the MDC Slider. */
748
859
const enum PointerEventType {
749
860
POINTER_DOWN = 'pointerdown' ,
0 commit comments