@@ -63,7 +63,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
63
63
labelElement = fixture . debugElement . query ( By . css ( 'label' ) ) ! . nativeElement ;
64
64
} ) ) ;
65
65
66
- it ( 'should apply class based on color attribute' , ( ) => {
66
+ it ( 'should apply class based on color attribute' , fakeAsync ( ( ) => {
67
67
testComponent . slideColor = 'primary' ;
68
68
fixture . detectChanges ( ) ;
69
69
@@ -73,39 +73,40 @@ describe('MDC-based MatSlideToggle without forms', () => {
73
73
fixture . detectChanges ( ) ;
74
74
75
75
expect ( slideToggleElement . classList ) . toContain ( 'mat-accent' ) ;
76
- } ) ;
76
+ } ) ) ;
77
77
78
- it ( 'should correctly update the disabled property' , ( ) => {
78
+ it ( 'should correctly update the disabled property' , fakeAsync ( ( ) => {
79
79
expect ( buttonElement . disabled ) . toBeFalsy ( ) ;
80
80
81
81
testComponent . isDisabled = true ;
82
82
fixture . detectChanges ( ) ;
83
83
84
84
expect ( buttonElement . disabled ) . toBeTruthy ( ) ;
85
- } ) ;
85
+ } ) ) ;
86
86
87
- it ( 'should correctly update the checked property' , ( ) => {
87
+ it ( 'should correctly update the checked property' , fakeAsync ( ( ) => {
88
88
expect ( slideToggle . checked ) . toBeFalsy ( ) ;
89
89
expect ( buttonElement . getAttribute ( 'aria-checked' ) ) . toBe ( 'false' ) ;
90
90
91
91
testComponent . slideChecked = true ;
92
92
fixture . detectChanges ( ) ;
93
93
94
94
expect ( buttonElement . getAttribute ( 'aria-checked' ) ) . toBe ( 'true' ) ;
95
- } ) ;
95
+ } ) ) ;
96
96
97
- it ( 'should set the toggle to checked on click' , ( ) => {
97
+ it ( 'should set the toggle to checked on click' , fakeAsync ( ( ) => {
98
98
expect ( slideToggle . checked ) . toBe ( false ) ;
99
99
expect ( buttonElement . getAttribute ( 'aria-checked' ) ) . toBe ( 'false' ) ;
100
100
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
101
101
102
102
labelElement . click ( ) ;
103
103
fixture . detectChanges ( ) ;
104
+ flush ( ) ;
104
105
105
106
expect ( slideToggleElement . classList ) . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
106
107
expect ( slideToggle . checked ) . toBe ( true ) ;
107
108
expect ( buttonElement . getAttribute ( 'aria-checked' ) ) . toBe ( 'true' ) ;
108
- } ) ;
109
+ } ) ) ;
109
110
110
111
it ( 'should not trigger the click event multiple times' , fakeAsync ( ( ) => {
111
112
// By default, when clicking on a label element, a generated click will be dispatched
@@ -125,15 +126,16 @@ describe('MDC-based MatSlideToggle without forms', () => {
125
126
expect ( testComponent . onSlideClick ) . toHaveBeenCalledTimes ( 1 ) ;
126
127
} ) ) ;
127
128
128
- it ( 'should trigger the change event properly' , ( ) => {
129
+ it ( 'should trigger the change event properly' , fakeAsync ( ( ) => {
129
130
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
130
131
131
132
labelElement . click ( ) ;
132
133
fixture . detectChanges ( ) ;
134
+ flush ( ) ;
133
135
134
136
expect ( slideToggleElement . classList ) . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
135
137
expect ( testComponent . onSlideChange ) . toHaveBeenCalledTimes ( 1 ) ;
136
- } ) ;
138
+ } ) ) ;
137
139
138
140
it ( 'should not trigger the change event by changing the native value' , fakeAsync ( ( ) => {
139
141
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
@@ -147,7 +149,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
147
149
expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
148
150
} ) ) ;
149
151
150
- it ( 'should add a suffix to the element id' , ( ) => {
152
+ it ( 'should add a suffix to the element id' , fakeAsync ( ( ) => {
151
153
testComponent . slideId = 'myId' ;
152
154
fixture . detectChanges ( ) ;
153
155
@@ -165,9 +167,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
165
167
166
168
// Once the id binding is set to null, the id property should auto-generate a unique id.
167
169
expect ( buttonElement . id ) . toMatch ( / m a t - m d c - s l i d e - t o g g l e - \d + - b u t t o n / ) ;
168
- } ) ;
170
+ } ) ) ;
169
171
170
- it ( 'should forward the tabIndex to the underlying element' , ( ) => {
172
+ it ( 'should forward the tabIndex to the underlying element' , fakeAsync ( ( ) => {
171
173
fixture . detectChanges ( ) ;
172
174
173
175
expect ( buttonElement . tabIndex ) . toBe ( 0 ) ;
@@ -176,9 +178,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
176
178
fixture . detectChanges ( ) ;
177
179
178
180
expect ( buttonElement . tabIndex ) . toBe ( 4 ) ;
179
- } ) ;
181
+ } ) ) ;
180
182
181
- it ( 'should forward the specified name to the element' , ( ) => {
183
+ it ( 'should forward the specified name to the element' , fakeAsync ( ( ) => {
182
184
testComponent . slideName = 'myName' ;
183
185
fixture . detectChanges ( ) ;
184
186
@@ -193,9 +195,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
193
195
fixture . detectChanges ( ) ;
194
196
195
197
expect ( buttonElement . name ) . toBe ( '' ) ;
196
- } ) ;
198
+ } ) ) ;
197
199
198
- it ( 'should forward the aria-label attribute to the element' , ( ) => {
200
+ it ( 'should forward the aria-label attribute to the element' , fakeAsync ( ( ) => {
199
201
testComponent . slideLabel = 'ariaLabel' ;
200
202
fixture . detectChanges ( ) ;
201
203
@@ -205,9 +207,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
205
207
fixture . detectChanges ( ) ;
206
208
207
209
expect ( buttonElement . hasAttribute ( 'aria-label' ) ) . toBeFalsy ( ) ;
208
- } ) ;
210
+ } ) ) ;
209
211
210
- it ( 'should forward the aria-labelledby attribute to the element' , ( ) => {
212
+ it ( 'should forward the aria-labelledby attribute to the element' , fakeAsync ( ( ) => {
211
213
testComponent . slideLabelledBy = 'ariaLabelledBy' ;
212
214
fixture . detectChanges ( ) ;
213
215
@@ -219,9 +221,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
219
221
// We fall back to pointing to the label if a value isn't provided.
220
222
expect ( buttonElement . getAttribute ( 'aria-labelledby' ) )
221
223
. toMatch ( / m a t - m d c - s l i d e - t o g g l e - l a b e l - \d + / ) ;
222
- } ) ;
224
+ } ) ) ;
223
225
224
- it ( 'should forward the aria-describedby attribute to the element' , ( ) => {
226
+ it ( 'should forward the aria-describedby attribute to the element' , fakeAsync ( ( ) => {
225
227
testComponent . slideAriaDescribedBy = 'some-element' ;
226
228
fixture . detectChanges ( ) ;
227
229
@@ -231,13 +233,13 @@ describe('MDC-based MatSlideToggle without forms', () => {
231
233
fixture . detectChanges ( ) ;
232
234
233
235
expect ( buttonElement . hasAttribute ( 'aria-describedby' ) ) . toBe ( false ) ;
234
- } ) ;
236
+ } ) ) ;
235
237
236
- it ( 'should set the `for` attribute to the id of the element' , ( ) => {
238
+ it ( 'should set the `for` attribute to the id of the element' , fakeAsync ( ( ) => {
237
239
expect ( labelElement . getAttribute ( 'for' ) ) . toBeTruthy ( ) ;
238
240
expect ( buttonElement . getAttribute ( 'id' ) ) . toBeTruthy ( ) ;
239
241
expect ( labelElement . getAttribute ( 'for' ) ) . toBe ( buttonElement . getAttribute ( 'id' ) ) ;
240
- } ) ;
242
+ } ) ) ;
241
243
242
244
it ( 'should emit the new values properly' , fakeAsync ( ( ) => {
243
245
labelElement . click ( ) ;
@@ -274,14 +276,14 @@ describe('MDC-based MatSlideToggle without forms', () => {
274
276
expect ( buttonElement . getAttribute ( 'aria-required' ) ) . toBe ( 'false' ) ;
275
277
} ) ;
276
278
277
- it ( 'should focus on underlying element when focus() is called' , ( ) => {
279
+ it ( 'should focus on underlying element when focus() is called' , fakeAsync ( ( ) => {
278
280
expect ( document . activeElement ) . not . toBe ( buttonElement ) ;
279
281
280
282
slideToggle . focus ( ) ;
281
283
fixture . detectChanges ( ) ;
282
284
283
285
expect ( document . activeElement ) . toBe ( buttonElement ) ;
284
- } ) ;
286
+ } ) ) ;
285
287
286
288
it ( 'should focus on underlying element when the host is focused' , fakeAsync ( ( ) => {
287
289
expect ( document . activeElement ) . not . toBe ( buttonElement ) ;
@@ -308,7 +310,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
308
310
expect ( document . activeElement ) . not . toBe ( buttonElement ) ;
309
311
} ) ) ) ;
310
312
311
- it ( 'should set a element class if labelPosition is set to before' , ( ) => {
313
+ it ( 'should set a element class if labelPosition is set to before' , fakeAsync ( ( ) => {
312
314
const formField = slideToggleElement . querySelector ( '.mdc-form-field' ) ! ;
313
315
314
316
expect ( formField . classList ) . not . toContain ( 'mdc-form-field--align-end' ) ;
@@ -317,9 +319,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
317
319
fixture . detectChanges ( ) ;
318
320
319
321
expect ( formField . classList ) . toContain ( 'mdc-form-field--align-end' ) ;
320
- } ) ;
322
+ } ) ) ;
321
323
322
- it ( 'should show ripples' , ( ) => {
324
+ it ( 'should show ripples' , fakeAsync ( ( ) => {
323
325
const rippleSelector = '.mat-ripple-element' ;
324
326
const switchElement = slideToggleElement . querySelector ( '.mdc-switch' ) ! ;
325
327
@@ -329,9 +331,10 @@ describe('MDC-based MatSlideToggle without forms', () => {
329
331
dispatchFakeEvent ( switchElement , 'mouseup' ) ;
330
332
331
333
expect ( slideToggleElement . querySelectorAll ( rippleSelector ) . length ) . toBe ( 1 ) ;
332
- } ) ;
334
+ flush ( ) ;
335
+ } ) ) ;
333
336
334
- it ( 'should not show ripples when disableRipple is set' , ( ) => {
337
+ it ( 'should not show ripples when disableRipple is set' , fakeAsync ( ( ) => {
335
338
const switchElement = slideToggleElement . querySelector ( '.mdc-switch' ) ! ;
336
339
const rippleSelector = '.mat-ripple-element' ;
337
340
testComponent . disableRipple = true ;
@@ -343,12 +346,13 @@ describe('MDC-based MatSlideToggle without forms', () => {
343
346
dispatchFakeEvent ( switchElement , 'mouseup' ) ;
344
347
345
348
expect ( slideToggleElement . querySelectorAll ( rippleSelector ) . length ) . toBe ( 0 ) ;
346
- } ) ;
349
+ flush ( ) ;
350
+ } ) ) ;
347
351
348
- it ( 'should have a focus indicator' , ( ) => {
352
+ it ( 'should have a focus indicator' , fakeAsync ( ( ) => {
349
353
const rippleElement = slideToggleElement . querySelector ( '.mat-mdc-slide-toggle-ripple' ) ! ;
350
354
expect ( rippleElement . classList ) . toContain ( 'mat-mdc-focus-indicator' ) ;
351
- } ) ;
355
+ } ) ) ;
352
356
} ) ;
353
357
354
358
describe ( 'custom template' , ( ) => {
@@ -374,21 +378,21 @@ describe('MDC-based MatSlideToggle without forms', () => {
374
378
. toBe ( 5 ) ;
375
379
} ) ) ;
376
380
377
- it ( 'should add the disabled class if disabled through attribute' , ( ) => {
381
+ it ( 'should add the disabled class if disabled through attribute' , fakeAsync ( ( ) => {
378
382
const fixture = TestBed . createComponent ( SlideToggleCheckedAndDisabledAttr ) ;
379
383
fixture . detectChanges ( ) ;
380
384
381
385
const switchEl = fixture . nativeElement . querySelector ( '.mdc-switch' ) ;
382
386
expect ( switchEl . classList ) . toContain ( 'mdc-switch--disabled' ) ;
383
- } ) ;
387
+ } ) ) ;
384
388
385
- it ( 'should add the checked class if checked through attribute' , ( ) => {
389
+ it ( 'should add the checked class if checked through attribute' , fakeAsync ( ( ) => {
386
390
const fixture = TestBed . createComponent ( SlideToggleCheckedAndDisabledAttr ) ;
387
391
fixture . detectChanges ( ) ;
388
392
389
393
const switchEl = fixture . nativeElement . querySelector ( '.mdc-switch' ) ;
390
394
expect ( switchEl . classList ) . toContain ( 'mdc-switch--checked' ) ;
391
- } ) ;
395
+ } ) ) ;
392
396
393
397
it ( 'should set the tabindex of the host element to -1' , fakeAsync ( ( ) => {
394
398
const fixture = TestBed . createComponent ( SlideToggleWithTabindexAttr ) ;
@@ -452,7 +456,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
452
456
expect ( testComponent . dragTriggered ) . toBe ( 0 ) ;
453
457
} ) ) ;
454
458
455
- it ( 'should be able to change the default color' , ( ) => {
459
+ it ( 'should be able to change the default color' , fakeAsync ( ( ) => {
456
460
TestBed
457
461
. resetTestingModule ( )
458
462
. configureTestingModule ( {
@@ -466,16 +470,16 @@ describe('MDC-based MatSlideToggle without forms', () => {
466
470
fixture . detectChanges ( ) ;
467
471
const slideToggle = fixture . nativeElement . querySelector ( '.mat-mdc-slide-toggle' ) ;
468
472
expect ( slideToggle . classList ) . toContain ( 'mat-warn' ) ;
469
- } ) ;
473
+ } ) ) ;
470
474
471
- it ( 'should clear static aria attributes from the host node' , ( ) => {
475
+ it ( 'should clear static aria attributes from the host node' , fakeAsync ( ( ) => {
472
476
const fixture = TestBed . createComponent ( SlideToggleWithStaticAriaAttributes ) ;
473
477
fixture . detectChanges ( ) ;
474
478
475
479
const host : HTMLElement = fixture . nativeElement . querySelector ( 'mat-slide-toggle' ) ;
476
480
expect ( host . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
477
481
expect ( host . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
478
- } ) ;
482
+ } ) ) ;
479
483
} ) ;
480
484
481
485
describe ( 'MDC-based MatSlideToggle with forms' , ( ) => {
@@ -519,10 +523,10 @@ describe('MDC-based MatSlideToggle with forms', () => {
519
523
labelElement = fixture . debugElement . query ( By . css ( 'label' ) ) ! . nativeElement ;
520
524
} ) ) ;
521
525
522
- it ( 'should be initially set to ng-pristine' , ( ) => {
526
+ it ( 'should be initially set to ng-pristine' , fakeAsync ( ( ) => {
523
527
expect ( slideToggleElement . classList ) . toContain ( 'ng-pristine' ) ;
524
528
expect ( slideToggleElement . classList ) . not . toContain ( 'ng-dirty' ) ;
525
- } ) ;
529
+ } ) ) ;
526
530
527
531
it ( 'should update the model programmatically' , fakeAsync ( ( ) => {
528
532
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
@@ -549,6 +553,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
549
553
550
554
dispatchFakeEvent ( buttonElement , 'focus' ) ;
551
555
buttonElement . click ( ) ;
556
+ flush ( ) ;
552
557
553
558
expect ( slideToggleModel . valid ) . toBe ( true ) ;
554
559
expect ( slideToggleModel . pristine ) . toBe ( false ) ;
@@ -682,16 +687,16 @@ describe('MDC-based MatSlideToggle with forms', () => {
682
687
let slideToggle : MatSlideToggle ;
683
688
let buttonElement : HTMLButtonElement ;
684
689
685
- beforeEach ( ( ) => {
690
+ beforeEach ( fakeAsync ( ( ) => {
686
691
fixture = TestBed . createComponent ( SlideToggleWithFormControl ) ;
687
692
fixture . detectChanges ( ) ;
688
693
689
694
testComponent = fixture . debugElement . componentInstance ;
690
695
slideToggle = fixture . debugElement . query ( By . directive ( MatSlideToggle ) ) ! . componentInstance ;
691
696
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
692
- } ) ;
697
+ } ) ) ;
693
698
694
- it ( 'should toggle the disabled state' , ( ) => {
699
+ it ( 'should toggle the disabled state' , fakeAsync ( ( ) => {
695
700
expect ( slideToggle . disabled ) . toBe ( false ) ;
696
701
expect ( buttonElement . disabled ) . toBe ( false ) ;
697
702
@@ -706,7 +711,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
706
711
707
712
expect ( slideToggle . disabled ) . toBe ( false ) ;
708
713
expect ( buttonElement . disabled ) . toBe ( false ) ;
709
- } ) ;
714
+ } ) ) ;
710
715
} ) ;
711
716
712
717
describe ( 'with form element' , ( ) => {
@@ -723,16 +728,17 @@ describe('MDC-based MatSlideToggle with forms', () => {
723
728
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
724
729
} ) ) ;
725
730
726
- it ( 'should not submit the form when clicked' , ( ) => {
731
+ it ( 'should not submit the form when clicked' , fakeAsync ( ( ) => {
727
732
expect ( testComponent . isSubmitted ) . toBe ( false ) ;
728
733
729
734
buttonElement . click ( ) ;
730
735
fixture . detectChanges ( ) ;
736
+ flush ( ) ;
731
737
732
738
expect ( testComponent . isSubmitted ) . toBe ( false ) ;
733
- } ) ;
739
+ } ) ) ;
734
740
735
- it ( 'should have proper invalid state if unchecked' , ( ) => {
741
+ it ( 'should have proper invalid state if unchecked' , fakeAsync ( ( ) => {
736
742
testComponent . isRequired = true ;
737
743
fixture . detectChanges ( ) ;
738
744
@@ -745,6 +751,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
745
751
// should become valid.
746
752
buttonElement . click ( ) ;
747
753
fixture . detectChanges ( ) ;
754
+ flush ( ) ;
748
755
749
756
expect ( slideToggleEl . classList ) . not . toContain ( 'ng-invalid' ) ;
750
757
expect ( slideToggleEl . classList ) . toContain ( 'ng-valid' ) ;
@@ -753,14 +760,15 @@ describe('MDC-based MatSlideToggle with forms', () => {
753
760
// should become invalid.
754
761
buttonElement . click ( ) ;
755
762
fixture . detectChanges ( ) ;
763
+ flush ( ) ;
756
764
757
765
expect ( slideToggleEl . classList ) . toContain ( 'ng-invalid' ) ;
758
766
expect ( slideToggleEl . classList ) . not . toContain ( 'ng-valid' ) ;
759
- } ) ;
767
+ } ) ) ;
760
768
} ) ;
761
769
762
770
describe ( 'with model and change event' , ( ) => {
763
- it ( 'should report changes to NgModel before emitting change event' , ( ) => {
771
+ it ( 'should report changes to NgModel before emitting change event' , fakeAsync ( ( ) => {
764
772
const fixture = TestBed . createComponent ( SlideToggleWithModelAndChangeEvent ) ;
765
773
fixture . detectChanges ( ) ;
766
774
@@ -773,9 +781,10 @@ describe('MDC-based MatSlideToggle with forms', () => {
773
781
} ) ;
774
782
775
783
labelEl . click ( ) ;
784
+ flush ( ) ;
776
785
777
786
expect ( fixture . componentInstance . onChange ) . toHaveBeenCalledTimes ( 1 ) ;
778
- } ) ;
787
+ } ) ) ;
779
788
} ) ;
780
789
} ) ;
781
790
0 commit comments