1
1
import { Component } from '@angular/core' ;
2
2
import { By , HAMMER_GESTURE_CONFIG } from '@angular/platform-browser' ;
3
3
import {
4
- async , ComponentFixture , TestBed , fakeAsync , tick ,
5
- flushMicrotasks
4
+ ComponentFixture ,
5
+ TestBed ,
6
+ fakeAsync ,
7
+ tick ,
8
+ flushMicrotasks ,
6
9
} from '@angular/core/testing' ;
7
10
import { NgModel , FormsModule , ReactiveFormsModule , FormControl } from '@angular/forms' ;
8
11
import { MatSlideToggle , MatSlideToggleChange , MatSlideToggleModule } from './index' ;
9
12
import { TestGestureConfig } from '../slider/test-gesture-config' ;
10
13
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
11
14
import { defaultRippleAnimationConfig } from '@angular/material/core' ;
15
+ import { MutationObserverFactory } from '@angular/cdk/observers' ;
12
16
13
17
describe ( 'MatSlideToggle without forms' , ( ) => {
14
18
let gestureConfig : TestGestureConfig ;
19
+ let mutationObserverCallbacks : Function [ ] ;
20
+ let flushMutationObserver = ( ) => mutationObserverCallbacks . forEach ( callback => callback ( ) ) ;
21
+
22
+ beforeEach ( fakeAsync ( ( ) => {
23
+ mutationObserverCallbacks = [ ] ;
15
24
16
- beforeEach ( async ( ( ) => {
17
25
TestBed . configureTestingModule ( {
18
26
imports : [ MatSlideToggleModule ] ,
19
27
declarations : [
@@ -22,7 +30,16 @@ describe('MatSlideToggle without forms', () => {
22
30
SlideToggleWithoutLabel
23
31
] ,
24
32
providers : [
25
- { provide : HAMMER_GESTURE_CONFIG , useFactory : ( ) => gestureConfig = new TestGestureConfig ( ) }
33
+ { provide : HAMMER_GESTURE_CONFIG , useFactory : ( ) => gestureConfig = new TestGestureConfig ( ) } ,
34
+ {
35
+ provide : MutationObserverFactory ,
36
+ useValue : {
37
+ create : ( callback : Function ) => {
38
+ mutationObserverCallbacks . push ( callback ) ;
39
+ return { observe : ( ) => { } , disconnect : ( ) => { } } ;
40
+ }
41
+ }
42
+ }
26
43
]
27
44
} ) ;
28
45
@@ -39,7 +56,7 @@ describe('MatSlideToggle without forms', () => {
39
56
let inputElement : HTMLInputElement ;
40
57
41
58
// This initialization is async() because it needs to wait for ngModel to set the initial value.
42
- beforeEach ( async ( ( ) => {
59
+ beforeEach ( fakeAsync ( ( ) => {
43
60
fixture = TestBed . createComponent ( SlideToggleBasic ) ;
44
61
45
62
// Enable jasmine spies on event functions, which may trigger at initialization
@@ -129,7 +146,7 @@ describe('MatSlideToggle without forms', () => {
129
146
expect ( testComponent . onSlideChange ) . toHaveBeenCalledTimes ( 1 ) ;
130
147
} ) ;
131
148
132
- it ( 'should not trigger the change event by changing the native value' , async ( ( ) => {
149
+ it ( 'should not trigger the change event by changing the native value' , fakeAsync ( ( ) => {
133
150
expect ( inputElement . checked ) . toBe ( false ) ;
134
151
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-checked' ) ;
135
152
@@ -138,15 +155,12 @@ describe('MatSlideToggle without forms', () => {
138
155
139
156
expect ( inputElement . checked ) . toBe ( true ) ;
140
157
expect ( slideToggleElement . classList ) . toContain ( 'mat-checked' ) ;
158
+ tick ( ) ;
141
159
142
- // The change event shouldn't fire because the value change was not caused
143
- // by any interaction. Use whenStable to ensure an event isn't fired asynchronously.
144
- fixture . whenStable ( ) . then ( ( ) => {
145
- expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
146
- } ) ;
160
+ expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
147
161
} ) ) ;
148
162
149
- it ( 'should not trigger the change event on initialization' , async ( ( ) => {
163
+ it ( 'should not trigger the change event on initialization' , fakeAsync ( ( ) => {
150
164
expect ( inputElement . checked ) . toBe ( false ) ;
151
165
expect ( slideToggleElement . classList ) . not . toContain ( 'mat-checked' ) ;
152
166
@@ -155,12 +169,9 @@ describe('MatSlideToggle without forms', () => {
155
169
156
170
expect ( inputElement . checked ) . toBe ( true ) ;
157
171
expect ( slideToggleElement . classList ) . toContain ( 'mat-checked' ) ;
172
+ tick ( ) ;
158
173
159
- // The change event shouldn't fire, because the native input element is not focused.
160
- // Use whenStable to ensure an event isn't fired asynchronously.
161
- fixture . whenStable ( ) . then ( ( ) => {
162
- expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
163
- } ) ;
174
+ expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
164
175
} ) ) ;
165
176
166
177
it ( 'should add a suffix to the inputs id' , ( ) => {
@@ -235,16 +246,15 @@ describe('MatSlideToggle without forms', () => {
235
246
expect ( inputElement . hasAttribute ( 'aria-labelledby' ) ) . toBeFalsy ( ) ;
236
247
} ) ;
237
248
238
- it ( 'should emit the new values properly' , async ( ( ) => {
249
+ it ( 'should emit the new values properly' , fakeAsync ( ( ) => {
239
250
labelElement . click ( ) ;
240
251
fixture . detectChanges ( ) ;
252
+ tick ( ) ;
241
253
242
- fixture . whenStable ( ) . then ( ( ) => {
243
- // We're checking the arguments type / emitted value to be a boolean, because sometimes the
244
- // emitted value can be a DOM Event, which is not valid.
245
- // See angular/angular#4059
246
- expect ( testComponent . lastEvent . checked ) . toBe ( true ) ;
247
- } ) ;
254
+ // We're checking the arguments type / emitted value to be a boolean, because sometimes the
255
+ // emitted value can be a DOM Event, which is not valid.
256
+ // See angular/angular#4059
257
+ expect ( testComponent . lastEvent . checked ) . toBe ( true ) ;
248
258
} ) ) ;
249
259
250
260
it ( 'should support subscription on the change observable' , ( ) => {
@@ -329,7 +339,7 @@ describe('MatSlideToggle without forms', () => {
329
339
} ) ;
330
340
331
341
describe ( 'custom template' , ( ) => {
332
- it ( 'should not trigger the change event on initialization' , async ( ( ) => {
342
+ it ( 'should not trigger the change event on initialization' , fakeAsync ( ( ) => {
333
343
const fixture = TestBed . createComponent ( SlideToggleBasic ) ;
334
344
335
345
fixture . componentInstance . slideChecked = true ;
@@ -338,7 +348,7 @@ describe('MatSlideToggle without forms', () => {
338
348
expect ( fixture . componentInstance . lastEvent ) . toBeFalsy ( ) ;
339
349
} ) ) ;
340
350
341
- it ( 'should be able to set the tabindex via the native attribute' , async ( ( ) => {
351
+ it ( 'should be able to set the tabindex via the native attribute' , fakeAsync ( ( ) => {
342
352
const fixture = TestBed . createComponent ( SlideToggleWithTabindexAttr ) ;
343
353
344
354
fixture . detectChanges ( ) ;
@@ -360,7 +370,7 @@ describe('MatSlideToggle without forms', () => {
360
370
let slideThumbContainer : HTMLElement ;
361
371
let inputElement : HTMLInputElement ;
362
372
363
- beforeEach ( async ( ( ) => {
373
+ beforeEach ( fakeAsync ( ( ) => {
364
374
fixture = TestBed . createComponent ( SlideToggleBasic ) ;
365
375
fixture . detectChanges ( ) ;
366
376
@@ -545,42 +555,34 @@ describe('MatSlideToggle without forms', () => {
545
555
. toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
546
556
} ) ;
547
557
548
- it ( 'should not remove margin if initial label is set through binding' , async ( ( ) => {
558
+ it ( 'should not remove margin if initial label is set through binding' , fakeAsync ( ( ) => {
549
559
testComponent . label = 'Some content' ;
550
560
fixture . detectChanges ( ) ;
551
561
552
562
expect ( slideToggleBarElement . classList )
553
563
. not . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
554
564
} ) ) ;
555
565
556
- it ( 'should re-add margin if label is added asynchronously' , async ( ( ) => {
566
+ it ( 'should re-add margin if label is added asynchronously' , fakeAsync ( ( ) => {
557
567
fixture . detectChanges ( ) ;
558
568
559
569
expect ( slideToggleBarElement . classList )
560
570
. toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
561
571
562
572
testComponent . label = 'Some content' ;
563
573
fixture . detectChanges ( ) ;
574
+ flushMutationObserver ( ) ;
575
+ fixture . detectChanges ( ) ;
564
576
565
- // Wait for the MutationObserver to detect the content change and for the cdkObserveContent
566
- // to emit the change event to the slide-toggle.
567
- setTimeout ( ( ) => {
568
- // The MutationObserver from the cdkObserveContent directive detected the content change
569
- // and notified the slide-toggle component. The slide-toggle then marks the component as
570
- // dirty by calling `markForCheck()`. This needs to be reflected by the component template
571
- // then.
572
- fixture . detectChanges ( ) ;
573
-
574
- expect ( slideToggleElement . classList )
575
- . not . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
576
- } , 1 ) ;
577
+ expect ( slideToggleElement . classList )
578
+ . not . toContain ( 'mat-slide-toggle-bar-no-side-margin' ) ;
577
579
} ) ) ;
578
580
} ) ;
579
581
} ) ;
580
582
581
583
describe ( 'MatSlideToggle with forms' , ( ) => {
582
584
583
- beforeEach ( async ( ( ) => {
585
+ beforeEach ( fakeAsync ( ( ) => {
584
586
TestBed . configureTestingModule ( {
585
587
imports : [ MatSlideToggleModule , FormsModule , ReactiveFormsModule ] ,
586
588
declarations : [
@@ -605,7 +607,7 @@ describe('MatSlideToggle with forms', () => {
605
607
let labelElement : HTMLLabelElement ;
606
608
607
609
// This initialization is async() because it needs to wait for ngModel to set the initial value.
608
- beforeEach ( async ( ( ) => {
610
+ beforeEach ( fakeAsync ( ( ) => {
609
611
fixture = TestBed . createComponent ( SlideToggleWithModel ) ;
610
612
fixture . detectChanges ( ) ;
611
613
@@ -723,6 +725,7 @@ describe('MatSlideToggle with forms', () => {
723
725
724
726
labelElement . click ( ) ;
725
727
fixture . detectChanges ( ) ;
728
+ tick ( ) ;
726
729
727
730
expect ( slideToggle . checked )
728
731
. toBe ( false , 'Expected slide-toggle to be no longer checked after label click.' ) ;
@@ -785,7 +788,7 @@ describe('MatSlideToggle with forms', () => {
785
788
let inputElement : HTMLInputElement ;
786
789
787
790
// This initialization is async() because it needs to wait for ngModel to set the initial value.
788
- beforeEach ( async ( ( ) => {
791
+ beforeEach ( fakeAsync ( ( ) => {
789
792
fixture = TestBed . createComponent ( SlideToggleWithForm ) ;
790
793
fixture . detectChanges ( ) ;
791
794
0 commit comments