@@ -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,15 @@ 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 ( ) ;
284
+ flush ( ) ;
282
285
283
286
expect ( document . activeElement ) . toBe ( buttonElement ) ;
284
- } ) ;
287
+ } ) ) ;
285
288
286
289
it ( 'should focus on underlying element when the host is focused' , fakeAsync ( ( ) => {
287
290
expect ( document . activeElement ) . not . toBe ( buttonElement ) ;
@@ -308,7 +311,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
308
311
expect ( document . activeElement ) . not . toBe ( buttonElement ) ;
309
312
} ) ) ) ;
310
313
311
- it ( 'should set a element class if labelPosition is set to before' , ( ) => {
314
+ it ( 'should set a element class if labelPosition is set to before' , fakeAsync ( ( ) => {
312
315
const formField = slideToggleElement . querySelector ( '.mdc-form-field' ) ! ;
313
316
314
317
expect ( formField . classList ) . not . toContain ( 'mdc-form-field--align-end' ) ;
@@ -317,9 +320,9 @@ describe('MDC-based MatSlideToggle without forms', () => {
317
320
fixture . detectChanges ( ) ;
318
321
319
322
expect ( formField . classList ) . toContain ( 'mdc-form-field--align-end' ) ;
320
- } ) ;
323
+ } ) ) ;
321
324
322
- it ( 'should show ripples' , ( ) => {
325
+ it ( 'should show ripples' , fakeAsync ( ( ) => {
323
326
const rippleSelector = '.mat-ripple-element' ;
324
327
const switchElement = slideToggleElement . querySelector ( '.mdc-switch' ) ! ;
325
328
@@ -329,9 +332,10 @@ describe('MDC-based MatSlideToggle without forms', () => {
329
332
dispatchFakeEvent ( switchElement , 'mouseup' ) ;
330
333
331
334
expect ( slideToggleElement . querySelectorAll ( rippleSelector ) . length ) . toBe ( 1 ) ;
332
- } ) ;
335
+ flush ( ) ;
336
+ } ) ) ;
333
337
334
- it ( 'should not show ripples when disableRipple is set' , ( ) => {
338
+ it ( 'should not show ripples when disableRipple is set' , fakeAsync ( ( ) => {
335
339
const switchElement = slideToggleElement . querySelector ( '.mdc-switch' ) ! ;
336
340
const rippleSelector = '.mat-ripple-element' ;
337
341
testComponent . disableRipple = true ;
@@ -343,12 +347,13 @@ describe('MDC-based MatSlideToggle without forms', () => {
343
347
dispatchFakeEvent ( switchElement , 'mouseup' ) ;
344
348
345
349
expect ( slideToggleElement . querySelectorAll ( rippleSelector ) . length ) . toBe ( 0 ) ;
346
- } ) ;
350
+ flush ( ) ;
351
+ } ) ) ;
347
352
348
- it ( 'should have a focus indicator' , ( ) => {
353
+ it ( 'should have a focus indicator' , fakeAsync ( ( ) => {
349
354
const rippleElement = slideToggleElement . querySelector ( '.mat-mdc-slide-toggle-ripple' ) ! ;
350
355
expect ( rippleElement . classList ) . toContain ( 'mat-mdc-focus-indicator' ) ;
351
- } ) ;
356
+ } ) ) ;
352
357
} ) ;
353
358
354
359
describe ( 'custom template' , ( ) => {
@@ -374,21 +379,21 @@ describe('MDC-based MatSlideToggle without forms', () => {
374
379
. toBe ( 5 ) ;
375
380
} ) ) ;
376
381
377
- it ( 'should add the disabled class if disabled through attribute' , ( ) => {
382
+ it ( 'should add the disabled class if disabled through attribute' , fakeAsync ( ( ) => {
378
383
const fixture = TestBed . createComponent ( SlideToggleCheckedAndDisabledAttr ) ;
379
384
fixture . detectChanges ( ) ;
380
385
381
386
const switchEl = fixture . nativeElement . querySelector ( '.mdc-switch' ) ;
382
387
expect ( switchEl . classList ) . toContain ( 'mdc-switch--disabled' ) ;
383
- } ) ;
388
+ } ) ) ;
384
389
385
- it ( 'should add the checked class if checked through attribute' , ( ) => {
390
+ it ( 'should add the checked class if checked through attribute' , fakeAsync ( ( ) => {
386
391
const fixture = TestBed . createComponent ( SlideToggleCheckedAndDisabledAttr ) ;
387
392
fixture . detectChanges ( ) ;
388
393
389
394
const switchEl = fixture . nativeElement . querySelector ( '.mdc-switch' ) ;
390
395
expect ( switchEl . classList ) . toContain ( 'mdc-switch--checked' ) ;
391
- } ) ;
396
+ } ) ) ;
392
397
393
398
it ( 'should set the tabindex of the host element to -1' , fakeAsync ( ( ) => {
394
399
const fixture = TestBed . createComponent ( SlideToggleWithTabindexAttr ) ;
@@ -452,7 +457,7 @@ describe('MDC-based MatSlideToggle without forms', () => {
452
457
expect ( testComponent . dragTriggered ) . toBe ( 0 ) ;
453
458
} ) ) ;
454
459
455
- it ( 'should be able to change the default color' , ( ) => {
460
+ it ( 'should be able to change the default color' , fakeAsync ( ( ) => {
456
461
TestBed
457
462
. resetTestingModule ( )
458
463
. configureTestingModule ( {
@@ -466,16 +471,16 @@ describe('MDC-based MatSlideToggle without forms', () => {
466
471
fixture . detectChanges ( ) ;
467
472
const slideToggle = fixture . nativeElement . querySelector ( '.mat-mdc-slide-toggle' ) ;
468
473
expect ( slideToggle . classList ) . toContain ( 'mat-warn' ) ;
469
- } ) ;
474
+ } ) ) ;
470
475
471
- it ( 'should clear static aria attributes from the host node' , ( ) => {
476
+ it ( 'should clear static aria attributes from the host node' , fakeAsync ( ( ) => {
472
477
const fixture = TestBed . createComponent ( SlideToggleWithStaticAriaAttributes ) ;
473
478
fixture . detectChanges ( ) ;
474
479
475
480
const host : HTMLElement = fixture . nativeElement . querySelector ( 'mat-slide-toggle' ) ;
476
481
expect ( host . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
477
482
expect ( host . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
478
- } ) ;
483
+ } ) ) ;
479
484
} ) ;
480
485
481
486
describe ( 'MDC-based MatSlideToggle with forms' , ( ) => {
@@ -519,10 +524,10 @@ describe('MDC-based MatSlideToggle with forms', () => {
519
524
labelElement = fixture . debugElement . query ( By . css ( 'label' ) ) ! . nativeElement ;
520
525
} ) ) ;
521
526
522
- it ( 'should be initially set to ng-pristine' , ( ) => {
527
+ it ( 'should be initially set to ng-pristine' , fakeAsync ( ( ) => {
523
528
expect ( slideToggleElement . classList ) . toContain ( 'ng-pristine' ) ;
524
529
expect ( slideToggleElement . classList ) . not . toContain ( 'ng-dirty' ) ;
525
- } ) ;
530
+ } ) ) ;
526
531
527
532
it ( 'should update the model programmatically' , fakeAsync ( ( ) => {
528
533
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-mdc-slide-toggle-checked' ) ;
@@ -549,6 +554,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
549
554
550
555
dispatchFakeEvent ( buttonElement , 'focus' ) ;
551
556
buttonElement . click ( ) ;
557
+ flush ( ) ;
552
558
553
559
expect ( slideToggleModel . valid ) . toBe ( true ) ;
554
560
expect ( slideToggleModel . pristine ) . toBe ( false ) ;
@@ -682,16 +688,16 @@ describe('MDC-based MatSlideToggle with forms', () => {
682
688
let slideToggle : MatSlideToggle ;
683
689
let buttonElement : HTMLButtonElement ;
684
690
685
- beforeEach ( ( ) => {
691
+ beforeEach ( fakeAsync ( ( ) => {
686
692
fixture = TestBed . createComponent ( SlideToggleWithFormControl ) ;
687
693
fixture . detectChanges ( ) ;
688
694
689
695
testComponent = fixture . debugElement . componentInstance ;
690
696
slideToggle = fixture . debugElement . query ( By . directive ( MatSlideToggle ) ) ! . componentInstance ;
691
697
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
692
- } ) ;
698
+ } ) ) ;
693
699
694
- it ( 'should toggle the disabled state' , ( ) => {
700
+ it ( 'should toggle the disabled state' , fakeAsync ( ( ) => {
695
701
expect ( slideToggle . disabled ) . toBe ( false ) ;
696
702
expect ( buttonElement . disabled ) . toBe ( false ) ;
697
703
@@ -706,7 +712,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
706
712
707
713
expect ( slideToggle . disabled ) . toBe ( false ) ;
708
714
expect ( buttonElement . disabled ) . toBe ( false ) ;
709
- } ) ;
715
+ } ) ) ;
710
716
} ) ;
711
717
712
718
describe ( 'with form element' , ( ) => {
@@ -723,16 +729,17 @@ describe('MDC-based MatSlideToggle with forms', () => {
723
729
buttonElement = fixture . debugElement . query ( By . css ( 'button' ) ) ! . nativeElement ;
724
730
} ) ) ;
725
731
726
- it ( 'should not submit the form when clicked' , ( ) => {
732
+ it ( 'should not submit the form when clicked' , fakeAsync ( ( ) => {
727
733
expect ( testComponent . isSubmitted ) . toBe ( false ) ;
728
734
729
735
buttonElement . click ( ) ;
730
736
fixture . detectChanges ( ) ;
737
+ flush ( ) ;
731
738
732
739
expect ( testComponent . isSubmitted ) . toBe ( false ) ;
733
- } ) ;
740
+ } ) ) ;
734
741
735
- it ( 'should have proper invalid state if unchecked' , ( ) => {
742
+ it ( 'should have proper invalid state if unchecked' , fakeAsync ( ( ) => {
736
743
testComponent . isRequired = true ;
737
744
fixture . detectChanges ( ) ;
738
745
@@ -745,6 +752,7 @@ describe('MDC-based MatSlideToggle with forms', () => {
745
752
// should become valid.
746
753
buttonElement . click ( ) ;
747
754
fixture . detectChanges ( ) ;
755
+ flush ( ) ;
748
756
749
757
expect ( slideToggleEl . classList ) . not . toContain ( 'ng-invalid' ) ;
750
758
expect ( slideToggleEl . classList ) . toContain ( 'ng-valid' ) ;
@@ -753,14 +761,15 @@ describe('MDC-based MatSlideToggle with forms', () => {
753
761
// should become invalid.
754
762
buttonElement . click ( ) ;
755
763
fixture . detectChanges ( ) ;
764
+ flush ( ) ;
756
765
757
766
expect ( slideToggleEl . classList ) . toContain ( 'ng-invalid' ) ;
758
767
expect ( slideToggleEl . classList ) . not . toContain ( 'ng-valid' ) ;
759
- } ) ;
768
+ } ) ) ;
760
769
} ) ;
761
770
762
771
describe ( 'with model and change event' , ( ) => {
763
- it ( 'should report changes to NgModel before emitting change event' , ( ) => {
772
+ it ( 'should report changes to NgModel before emitting change event' , fakeAsync ( ( ) => {
764
773
const fixture = TestBed . createComponent ( SlideToggleWithModelAndChangeEvent ) ;
765
774
fixture . detectChanges ( ) ;
766
775
@@ -773,9 +782,10 @@ describe('MDC-based MatSlideToggle with forms', () => {
773
782
} ) ;
774
783
775
784
labelEl . click ( ) ;
785
+ flush ( ) ;
776
786
777
787
expect ( fixture . componentInstance . onChange ) . toHaveBeenCalledTimes ( 1 ) ;
778
- } ) ;
788
+ } ) ) ;
779
789
} ) ;
780
790
} ) ;
781
791
0 commit comments