-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
slider.spec.ts
1793 lines (1368 loc) · 60.5 KB
/
slider.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {BidiModule} from '@angular/cdk/bidi';
import {
BACKSPACE,
DOWN_ARROW,
END,
HOME,
LEFT_ARROW,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
UP_ARROW,
A,
} from '@angular/cdk/keycodes';
import {
createMouseEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
createKeyboardEvent,
} from '@angular/cdk/testing/private';
import {Component, DebugElement, Type, ViewChild} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, TestBed} from '@angular/core/testing';
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {MatSlider, MatSliderModule} from './index';
describe('MatSlider', () => {
function createComponent<T>(component: Type<T>): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [MatSliderModule, ReactiveFormsModule, FormsModule, BidiModule],
declarations: [component],
}).compileComponents();
return TestBed.createComponent<T>(component);
}
describe('standard slider', () => {
let fixture: ComponentFixture<StandardSlider>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(StandardSlider);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should set the default values', () => {
expect(sliderInstance.value).toBe(0);
expect(sliderInstance.min).toBe(0);
expect(sliderInstance.max).toBe(100);
});
it('should update the value on mousedown', () => {
expect(sliderInstance.value).toBe(0);
dispatchMousedownEventSequence(sliderNativeElement, 0.19);
expect(sliderInstance.value).toBe(19);
});
it('should not update when pressing the right mouse button', () => {
expect(sliderInstance.value).toBe(0);
dispatchMousedownEventSequence(sliderNativeElement, 0.19, 1);
expect(sliderInstance.value).toBe(0);
});
it('should update the value on a slide', () => {
expect(sliderInstance.value).toBe(0);
dispatchSlideEventSequence(sliderNativeElement, 0, 0.89);
expect(sliderInstance.value).toBe(89);
});
it('should set the value as min when sliding before the track', () => {
expect(sliderInstance.value).toBe(0);
dispatchSlideEventSequence(sliderNativeElement, 0, -1.33);
expect(sliderInstance.value).toBe(0);
});
it('should set the value as max when sliding past the track', () => {
expect(sliderInstance.value).toBe(0);
dispatchSlideEventSequence(sliderNativeElement, 0, 1.75);
expect(sliderInstance.value).toBe(100);
});
it('should update the track fill on mousedown', () => {
expect(trackFillElement.style.transform).toContain('scale3d(0, 1, 1)');
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
fixture.detectChanges();
expect(trackFillElement.style.transform).toContain('scale3d(0.39, 1, 1)');
});
it('should hide the fill element at zero percent', () => {
expect(trackFillElement.style.display).toBe('none');
dispatchMousedownEventSequence(sliderNativeElement, 0.39);
fixture.detectChanges();
expect(trackFillElement.style.display).toBeFalsy();
});
it('should update the track fill on slide', () => {
expect(trackFillElement.style.transform).toContain('scale3d(0, 1, 1)');
dispatchSlideEventSequence(sliderNativeElement, 0, 0.86);
fixture.detectChanges();
expect(trackFillElement.style.transform).toContain('scale3d(0.86, 1, 1)');
});
it('should add and remove the mat-slider-sliding class when sliding', () => {
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
dispatchSlideStartEvent(sliderNativeElement, 0);
fixture.detectChanges();
expect(sliderNativeElement.classList).toContain('mat-slider-sliding');
dispatchSlideEndEvent(sliderNativeElement, 0.34);
fixture.detectChanges();
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
});
it('should not interrupt sliding by pressing a key', () => {
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
dispatchSlideStartEvent(sliderNativeElement, 0);
fixture.detectChanges();
expect(sliderNativeElement.classList).toContain('mat-slider-sliding');
// Any key code will do here. Use A since it isn't associated with other actions.
dispatchKeyboardEvent(sliderNativeElement, 'keydown', A);
fixture.detectChanges();
dispatchKeyboardEvent(sliderNativeElement, 'keyup', A);
fixture.detectChanges();
expect(sliderNativeElement.classList).toContain('mat-slider-sliding');
dispatchSlideEndEvent(sliderNativeElement, 0.34);
fixture.detectChanges();
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
});
it('should stop dragging if the page loses focus', () => {
const classlist = sliderNativeElement.classList;
expect(classlist).not.toContain('mat-slider-sliding');
dispatchSlideStartEvent(sliderNativeElement, 0);
fixture.detectChanges();
expect(classlist).toContain('mat-slider-sliding');
dispatchSlideEvent(sliderNativeElement, 0.34);
fixture.detectChanges();
expect(classlist).toContain('mat-slider-sliding');
dispatchFakeEvent(window, 'blur');
fixture.detectChanges();
expect(classlist).not.toContain('mat-slider-sliding');
});
it('should reset active state upon blur', () => {
sliderInstance._isActive = true;
dispatchFakeEvent(sliderNativeElement, 'blur');
fixture.detectChanges();
expect(sliderInstance._isActive).toBe(false);
});
it('should reset thumb gap when blurred on min value', () => {
sliderInstance._isActive = true;
sliderInstance.value = 0;
fixture.detectChanges();
expect(sliderInstance._getThumbGap()).toBe(10);
dispatchFakeEvent(sliderNativeElement, 'blur');
fixture.detectChanges();
expect(sliderInstance._getThumbGap()).toBe(7);
});
it('should have thumb gap when at min value', () => {
expect(trackFillElement.style.transform).toContain('translateX(-7px)');
});
it('should not have thumb gap when not at min value', () => {
dispatchMousedownEventSequence(sliderNativeElement, 1);
fixture.detectChanges();
// Some browsers use '0' and some use '0px', so leave off the closing paren.
expect(trackFillElement.style.transform).toContain('translateX(0');
});
it('should have aria-orientation horizontal', () => {
expect(sliderNativeElement.getAttribute('aria-orientation')).toEqual('horizontal');
});
it('should slide to the max value when the steps do not divide evenly into it', () => {
sliderInstance.min = 5;
sliderInstance.max = 100;
sliderInstance.step = 15;
dispatchSlideEventSequence(sliderNativeElement, 0, 1);
fixture.detectChanges();
expect(sliderInstance.value).toBe(100);
});
it('should prevent the default action of the `selectstart` event', () => {
const event = dispatchFakeEvent(sliderNativeElement, 'selectstart');
fixture.detectChanges();
expect(event.defaultPrevented).toBe(true);
});
it('should have a focus indicator', () => {
expect(sliderNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
});
});
describe('disabled slider', () => {
let fixture: ComponentFixture<StandardSlider>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(DisabledSlider);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should be disabled', () => {
expect(sliderInstance.disabled).toBeTruthy();
});
it('should not change the value on mousedown when disabled', () => {
expect(sliderInstance.value).toBe(0);
dispatchMousedownEventSequence(sliderNativeElement, 0.63);
expect(sliderInstance.value).toBe(0);
});
it('should not change the value on slide when disabled', () => {
expect(sliderInstance.value).toBe(0);
dispatchSlideEventSequence(sliderNativeElement, 0, 0.5);
expect(sliderInstance.value).toBe(0);
});
it('should not emit change when disabled', () => {
const onChangeSpy = jasmine.createSpy('slider onChange');
sliderInstance.change.subscribe(onChangeSpy);
dispatchSlideEventSequence(sliderNativeElement, 0, 0.5);
expect(onChangeSpy).toHaveBeenCalledTimes(0);
});
it('should not add the mat-slider-active class on mousedown when disabled', () => {
expect(sliderNativeElement.classList).not.toContain('mat-slider-active');
dispatchMousedownEventSequence(sliderNativeElement, 0.43);
fixture.detectChanges();
expect(sliderNativeElement.classList).not.toContain('mat-slider-active');
});
it('should not add the mat-slider-sliding class on slide when disabled', () => {
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
dispatchSlideStartEvent(sliderNativeElement, 0.46);
fixture.detectChanges();
expect(sliderNativeElement.classList).not.toContain('mat-slider-sliding');
});
it ('should leave thumb gap', () => {
expect(trackFillElement.style.transform).toContain('translateX(-7px)');
});
it('should disable tabbing to the slider', () => {
expect(sliderNativeElement.tabIndex).toBe(-1);
});
});
describe('slider with set min and max', () => {
let fixture: ComponentFixture<SliderWithMinAndMax>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
let ticksContainerElement: HTMLElement;
let ticksElement: HTMLElement;
let testComponent: SliderWithMinAndMax;
beforeEach(() => {
fixture = createComponent(SliderWithMinAndMax);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
testComponent = fixture.debugElement.componentInstance;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
ticksContainerElement =
<HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks-container');
ticksElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks');
});
it('should set the default values from the attributes', () => {
expect(sliderInstance.value).toBe(4);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});
it('should set the correct value on mousedown', () => {
dispatchMousedownEventSequence(sliderNativeElement, 0.09);
fixture.detectChanges();
// Computed by multiplying the difference between the min and the max by the percentage from
// the mousedown and adding that to the minimum.
const value = Math.round(4 + (0.09 * (6 - 4)));
expect(sliderInstance.value).toBe(value);
});
it('should set the correct value on slide', () => {
dispatchSlideEventSequence(sliderNativeElement, 0, 0.62);
fixture.detectChanges();
// Computed by multiplying the difference between the min and the max by the percentage from
// the mousedown and adding that to the minimum.
const value = Math.round(4 + (0.62 * (6 - 4)));
expect(sliderInstance.value).toBe(value);
});
it('should snap the fill to the nearest value on mousedown', () => {
dispatchMousedownEventSequence(sliderNativeElement, 0.68);
fixture.detectChanges();
// The closest snap is halfway on the slider.
expect(trackFillElement.style.transform).toContain('scale3d(0.5, 1, 1)');
});
it('should snap the fill to the nearest value on slide', () => {
dispatchSlideEventSequence(sliderNativeElement, 0, 0.74);
fixture.detectChanges();
// The closest snap is at the halfway point on the slider.
expect(trackFillElement.style.transform).toContain('scale3d(0.5, 1, 1)');
});
it('should adjust fill and ticks on mouse enter when min changes', () => {
testComponent.min = -2;
fixture.detectChanges();
dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();
expect(trackFillElement.style.transform).toContain('scale3d(0.75, 1, 1)');
expect(ticksElement.style.backgroundSize).toBe('75% 2px');
// Make sure it cuts off the last half tick interval.
expect(ticksElement.style.transform).toContain('translateX(37.5%)');
expect(ticksContainerElement.style.transform).toBe('translateX(-37.5%)');
});
it('should adjust fill and ticks on mouse enter when max changes', () => {
testComponent.min = -2;
fixture.detectChanges();
testComponent.max = 10;
fixture.detectChanges();
dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();
expect(trackFillElement.style.transform).toContain('scale3d(0.5, 1, 1)');
expect(ticksElement.style.backgroundSize).toBe('50% 2px');
// Make sure it cuts off the last half tick interval.
expect(ticksElement.style.transform).toContain('translateX(25%)');
expect(ticksContainerElement.style.transform).toBe('translateX(-25%)');
});
it('should be able to set the min and max values when they are more precise ' +
'than the step', () => {
// Note that we assign min/max with more decimals than the
// step to ensure that the value doesn't get rounded up.
testComponent.step = 0.5;
testComponent.min = 10.15;
testComponent.max = 50.15;
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0.5, 0);
fixture.detectChanges();
expect(sliderInstance.value).toBe(10.15);
expect(sliderInstance.percent).toBe(0);
dispatchSlideEventSequence(sliderNativeElement, 0.5, 1);
fixture.detectChanges();
expect(sliderInstance.value).toBe(50.15);
expect(sliderInstance.percent).toBe(1);
});
});
describe('slider with set value', () => {
let fixture: ComponentFixture<SliderWithValue>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
beforeEach(() => {
fixture = createComponent(SliderWithValue);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
});
it('should set the default value from the attribute', () => {
expect(sliderInstance.value).toBe(26);
});
it('should set the correct value on mousedown', () => {
dispatchMousedownEventSequence(sliderNativeElement, 0.92);
fixture.detectChanges();
// On a slider with default max and min the value should be approximately equal to the
// percentage clicked. This should be the case regardless of what the original set value was.
expect(sliderInstance.value).toBe(92);
});
it('should set the correct value on slide', () => {
dispatchSlideEventSequence(sliderNativeElement, 0, 0.32);
fixture.detectChanges();
expect(sliderInstance.value).toBe(32);
});
});
describe('slider with set step', () => {
let fixture: ComponentFixture<SliderWithStep>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithStep);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should set the correct step value on mousedown', () => {
expect(sliderInstance.value).toBe(0);
dispatchMousedownEventSequence(sliderNativeElement, 0.13);
fixture.detectChanges();
expect(sliderInstance.value).toBe(25);
});
it('should snap the fill to a step on mousedown', () => {
dispatchMousedownEventSequence(sliderNativeElement, 0.66);
fixture.detectChanges();
// The closest step is at 75% of the slider.
expect(trackFillElement.style.transform).toContain('scale3d(0.75, 1, 1)');
});
it('should set the correct step value on slide', () => {
dispatchSlideEventSequence(sliderNativeElement, 0, 0.07);
fixture.detectChanges();
expect(sliderInstance.value).toBe(0);
});
it('should snap the thumb and fill to a step on slide', () => {
dispatchSlideEventSequence(sliderNativeElement, 0, 0.88);
fixture.detectChanges();
// The closest snap is at the end of the slider.
expect(trackFillElement.style.transform).toContain('scale3d(1, 1, 1)');
});
it('should not add decimals to the value if it is a whole number', () => {
fixture.componentInstance.step = 0.1;
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0, 1);
expect(sliderDebugElement.componentInstance.displayValue).toBe(100);
});
it('should truncate long decimal values when using a decimal step', () => {
fixture.componentInstance.step = 0.1;
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333);
expect(sliderInstance.value).toBe(33);
});
it('should truncate long decimal values when using a decimal step and the arrow keys', () => {
fixture.componentInstance.step = 0.1;
fixture.detectChanges();
for (let i = 0; i < 3; i++) {
dispatchKeyboardEvent(sliderNativeElement, 'keydown', UP_ARROW);
}
expect(sliderInstance.value).toBe(0.3);
});
it('should set the truncated value to the aria-valuetext', () => {
fixture.componentInstance.step = 0.1;
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333);
fixture.detectChanges();
expect(sliderNativeElement.getAttribute('aria-valuetext')).toBe('33');
});
it('should be able to override the aria-valuetext', () => {
fixture.componentInstance.step = 0.1;
fixture.componentInstance.valueText = 'custom';
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333);
fixture.detectChanges();
expect(sliderNativeElement.getAttribute('aria-valuetext')).toBe('custom');
});
it('should be able to clear aria-valuetext', () => {
fixture.componentInstance.valueText = '';
fixture.detectChanges();
dispatchSlideEventSequence(sliderNativeElement, 0, 0.333333);
fixture.detectChanges();
expect(sliderNativeElement.getAttribute('aria-valuetext')).toBeFalsy();
});
});
describe('slider with auto ticks', () => {
let fixture: ComponentFixture<SliderWithAutoTickInterval>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let ticksContainerElement: HTMLElement;
let ticksElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithAutoTickInterval);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
ticksContainerElement =
<HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks-container');
ticksElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks');
});
it('should set the correct tick separation on mouse enter', () => {
dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();
// Ticks should be 30px apart (therefore 30% for a 100px long slider).
expect(ticksElement.style.backgroundSize).toBe('30% 2px');
// Make sure it cuts off the last half tick interval.
expect(ticksElement.style.transform).toContain('translateX(15%)');
expect(ticksContainerElement.style.transform).toBe('translateX(-15%)');
});
});
describe('slider with set tick interval', () => {
let fixture: ComponentFixture<SliderWithSetTickInterval>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let ticksContainerElement: HTMLElement;
let ticksElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithSetTickInterval);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
ticksContainerElement =
<HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks-container');
ticksElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-ticks');
});
it('should set the correct tick separation on mouse enter', () => {
dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();
// Ticks should be every 18 values (tickInterval of 6 * step size of 3). On a slider 100px
// long with 100 values, this is 18%.
expect(ticksElement.style.backgroundSize).toBe('18% 2px');
// Make sure it cuts off the last half tick interval.
expect(ticksElement.style.transform).toContain('translateX(9%)');
expect(ticksContainerElement.style.transform).toBe('translateX(-9%)');
});
it('should be able to reset the tick interval after it has been set', () => {
expect(sliderNativeElement.classList)
.toContain('mat-slider-has-ticks', 'Expected element to have ticks initially.');
fixture.componentInstance.tickInterval = 0;
fixture.detectChanges();
expect(sliderNativeElement.classList)
.not.toContain('mat-slider-has-ticks', 'Expected element not to have ticks after reset.');
});
});
describe('slider with thumb label', () => {
let fixture: ComponentFixture<SliderWithThumbLabel>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let thumbLabelTextElement: Element;
beforeEach(() => {
fixture = createComponent(SliderWithThumbLabel);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
thumbLabelTextElement = sliderNativeElement.querySelector('.mat-slider-thumb-label-text')!;
});
it('should add the thumb label class to the slider container', () => {
expect(sliderNativeElement.classList).toContain('mat-slider-thumb-label-showing');
});
it('should update the thumb label text on mousedown', () => {
expect(thumbLabelTextElement.textContent).toBe('0');
dispatchMousedownEventSequence(sliderNativeElement, 0.13);
fixture.detectChanges();
// The thumb label text is set to the slider's value. These should always be the same.
expect(thumbLabelTextElement.textContent).toBe('13');
});
it('should update the thumb label text on slide', () => {
expect(thumbLabelTextElement.textContent).toBe('0');
dispatchSlideEventSequence(sliderNativeElement, 0, 0.56);
fixture.detectChanges();
// The thumb label text is set to the slider's value. These should always be the same.
expect(thumbLabelTextElement.textContent).toBe(`${sliderInstance.value}`);
});
});
describe('slider with custom thumb label formatting', () => {
let fixture: ComponentFixture<SliderWithCustomThumbLabelFormatting>;
let sliderInstance: MatSlider;
let thumbLabelTextElement: Element;
beforeEach(() => {
fixture = createComponent(SliderWithCustomThumbLabelFormatting);
fixture.detectChanges();
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
const sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
thumbLabelTextElement = sliderNativeElement.querySelector('.mat-slider-thumb-label-text')!;
});
it('should invoke the passed-in `displayWith` function with the value', () => {
spyOn(fixture.componentInstance, 'displayWith').and.callThrough();
sliderInstance.value = 1337;
fixture.detectChanges();
expect(fixture.componentInstance.displayWith).toHaveBeenCalledWith(1337);
});
it('should format the thumb label based on the passed-in `displayWith` function', () => {
sliderInstance.value = 200000;
fixture.detectChanges();
expect(thumbLabelTextElement.textContent).toBe('200k');
});
});
describe('slider with value property binding', () => {
let fixture: ComponentFixture<SliderWithOneWayBinding>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let testComponent: SliderWithOneWayBinding;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithOneWayBinding);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should initialize based on bound value', () => {
expect(sliderInstance.value).toBe(50);
expect(trackFillElement.style.transform).toContain('scale3d(0.5, 1, 1)');
});
it('should update when bound value changes', () => {
testComponent.val = 75;
fixture.detectChanges();
expect(sliderInstance.value).toBe(75);
expect(trackFillElement.style.transform).toContain('scale3d(0.75, 1, 1)');
});
});
describe('slider with set min and max and a value smaller than min', () => {
let fixture: ComponentFixture<SliderWithValueSmallerThanMin>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithValueSmallerThanMin);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should set the value smaller than the min value', () => {
expect(sliderInstance.value).toBe(3);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});
it('should set the fill to the min value', () => {
expect(trackFillElement.style.transform).toContain('scale3d(0, 1, 1)');
});
});
describe('slider with set min and max and a value greater than max', () => {
let fixture: ComponentFixture<SliderWithValueSmallerThanMin>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MatSlider;
let trackFillElement: HTMLElement;
beforeEach(() => {
fixture = createComponent(SliderWithValueGreaterThanMax);
fixture.detectChanges();
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.mat-slider-track-fill');
});
it('should set the value greater than the max value', () => {
expect(sliderInstance.value).toBe(7);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});
it('should set the fill to the max value', () => {
expect(trackFillElement.style.transform).toContain('scale3d(1, 1, 1)');
});
});
describe('slider with change handler', () => {
let fixture: ComponentFixture<SliderWithChangeHandler>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let testComponent: SliderWithChangeHandler;
beforeEach(() => {
fixture = createComponent(SliderWithChangeHandler);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
spyOn(testComponent, 'onChange');
spyOn(testComponent, 'onInput');
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
});
it('should emit change on mouseup', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
fixture.detectChanges();
dispatchSlideEndEvent(sliderNativeElement, 0.2);
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
});
it('should emit change on slide', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchSlideEventSequence(sliderNativeElement, 0, 0.4);
fixture.detectChanges();
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
});
it('should not emit multiple changes for the same value', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchMousedownEventSequence(sliderNativeElement, 0.6);
dispatchSlideEventSequence(sliderNativeElement, 0.6, 0.6);
fixture.detectChanges();
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
});
it('should dispatch events when changing back to previously emitted value after ' +
'programmatically setting value', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
expect(testComponent.onInput).not.toHaveBeenCalled();
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
fixture.detectChanges();
expect(testComponent.onChange).not.toHaveBeenCalled();
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
dispatchSlideEndEvent(sliderNativeElement, 0.2);
fixture.detectChanges();
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
testComponent.slider.value = 0;
fixture.detectChanges();
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
dispatchMousedownEventSequence(sliderNativeElement, 0.2);
fixture.detectChanges();
dispatchSlideEndEvent(sliderNativeElement, 0.2);
expect(testComponent.onChange).toHaveBeenCalledTimes(2);
expect(testComponent.onInput).toHaveBeenCalledTimes(2);
});
});
describe('slider with input event', () => {
let fixture: ComponentFixture<SliderWithChangeHandler>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let testComponent: SliderWithChangeHandler;
beforeEach(() => {
fixture = createComponent(SliderWithChangeHandler);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
spyOn(testComponent, 'onInput');
spyOn(testComponent, 'onChange');
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
});
it('should emit an input event while sliding', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchMouseenterEvent(sliderNativeElement);
dispatchSlideStartEvent(sliderNativeElement, 0);
dispatchSlideEvent(sliderNativeElement, 0.5);
dispatchSlideEvent(sliderNativeElement, 1);
dispatchSlideEndEvent(sliderNativeElement, 1);
fixture.detectChanges();
// The input event should fire twice, because the slider changed two times.
expect(testComponent.onInput).toHaveBeenCalledTimes(2);
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
});
it('should emit an input event when clicking', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchMousedownEventSequence(sliderNativeElement, 0.75);
fixture.detectChanges();
dispatchSlideEndEvent(sliderNativeElement, 0.75);
// The `onInput` event should be emitted once due to a single click.
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
});
});
describe('keyboard support', () => {
let fixture: ComponentFixture<SliderWithChangeHandler>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let testComponent: SliderWithChangeHandler;
let sliderInstance: MatSlider;
beforeEach(() => {
fixture = createComponent(SliderWithChangeHandler);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
spyOn(testComponent, 'onInput');
spyOn(testComponent, 'onChange');
sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider))!;
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.injector.get<MatSlider>(MatSlider);
});
it('should increment slider by 1 on up arrow pressed', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchKeyboardEvent(sliderNativeElement, 'keydown', UP_ARROW);
fixture.detectChanges();
// The `onInput` event should be emitted once due to a single keyboard press.
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
expect(sliderInstance.value).toBe(1);
});
it('should increment slider by 1 on right arrow pressed', () => {
expect(testComponent.onChange).not.toHaveBeenCalled();
dispatchKeyboardEvent(sliderNativeElement, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
// The `onInput` event should be emitted once due to a single keyboard press.
expect(testComponent.onInput).toHaveBeenCalledTimes(1);
expect(testComponent.onChange).toHaveBeenCalledTimes(1);
expect(sliderInstance.value).toBe(1);
});
it('should decrement slider by 1 on down arrow pressed', () => {
sliderInstance.value = 100;
expect(testComponent.onChange).not.toHaveBeenCalled();