@@ -8,7 +8,7 @@ import {MatTab, MatTabGroup, MatTabHeaderPosition, MatTabsModule} from './index'
8
8
9
9
10
10
describe ( 'MatTabGroup' , ( ) => {
11
- beforeEach ( async ( ( ) => {
11
+ beforeEach ( fakeAsync ( ( ) => {
12
12
TestBed . configureTestingModule ( {
13
13
imports : [ MatTabsModule , NoopAnimationsModule ] ,
14
14
declarations : [
@@ -59,21 +59,21 @@ describe('MatTabGroup', () => {
59
59
checkSelectedIndex ( 2 , fixture ) ;
60
60
} ) ;
61
61
62
- it ( 'should support two-way binding for selectedIndex' , async ( ( ) => {
62
+ it ( 'should support two-way binding for selectedIndex' , fakeAsync ( ( ) => {
63
63
let component = fixture . componentInstance ;
64
64
component . selectedIndex = 0 ;
65
65
66
66
fixture . detectChanges ( ) ;
67
67
68
68
let tabLabel = fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) [ 1 ] ;
69
69
tabLabel . nativeElement . click ( ) ;
70
-
71
70
fixture . detectChanges ( ) ;
72
- fixture . whenStable ( ) . then ( ( ) => {
73
- expect ( component . selectedIndex ) . toBe ( 1 ) ;
74
- } ) ;
71
+ tick ( ) ;
72
+
73
+ expect ( component . selectedIndex ) . toBe ( 1 ) ;
75
74
} ) ) ;
76
75
76
+ // Note: needs to be `async` in order to fail when we expect it to.
77
77
it ( 'should set to correct tab on fast change' , async ( ( ) => {
78
78
let component = fixture . componentInstance ;
79
79
component . selectedIndex = 0 ;
@@ -255,125 +255,125 @@ describe('MatTabGroup', () => {
255
255
describe ( 'dynamic binding tabs' , ( ) => {
256
256
let fixture : ComponentFixture < SimpleDynamicTabsTestApp > ;
257
257
258
- beforeEach ( async ( ( ) => {
258
+ beforeEach ( fakeAsync ( ( ) => {
259
259
fixture = TestBed . createComponent ( SimpleDynamicTabsTestApp ) ;
260
260
fixture . detectChanges ( ) ;
261
+ tick ( ) ;
262
+ fixture . detectChanges ( ) ;
261
263
} ) ) ;
262
264
263
- it ( 'should be able to add a new tab, select it, and have correct origin position' , ( ) => {
264
- fixture . detectChanges ( ) ;
265
- const component : MatTabGroup =
266
- fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
265
+ it ( 'should be able to add a new tab, select it, and have correct origin position' ,
266
+ fakeAsync ( ( ) => {
267
+ const component : MatTabGroup =
268
+ fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
267
269
268
- let tabs : MatTab [ ] = component . _tabs . toArray ( ) ;
269
- expect ( tabs [ 0 ] . origin ) . toBe ( null ) ;
270
- expect ( tabs [ 1 ] . origin ) . toBe ( 0 ) ;
271
- expect ( tabs [ 2 ] . origin ) . toBe ( null ) ;
270
+ let tabs : MatTab [ ] = component . _tabs . toArray ( ) ;
271
+ expect ( tabs [ 0 ] . origin ) . toBe ( null ) ;
272
+ expect ( tabs [ 1 ] . origin ) . toBe ( 0 ) ;
273
+ expect ( tabs [ 2 ] . origin ) . toBe ( null ) ;
272
274
273
- // Add a new tab on the right and select it, expect an origin >= than 0 (animate right)
274
- fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to right of index' } ) ;
275
- fixture . componentInstance . selectedIndex = 4 ;
276
- fixture . detectChanges ( ) ;
275
+ // Add a new tab on the right and select it, expect an origin >= than 0 (animate right)
276
+ fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to right of index' } ) ;
277
+ fixture . componentInstance . selectedIndex = 4 ;
278
+ fixture . detectChanges ( ) ;
279
+ tick ( ) ;
277
280
278
- tabs = component . _tabs . toArray ( ) ;
279
- expect ( tabs [ 3 ] . origin ) . toBeGreaterThanOrEqual ( 0 ) ;
281
+ tabs = component . _tabs . toArray ( ) ;
282
+ expect ( tabs [ 3 ] . origin ) . toBeGreaterThanOrEqual ( 0 ) ;
280
283
281
- // Add a new tab in the beginning and select it, expect an origin < than 0 (animate left)
282
- fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to left of index' } ) ;
283
- fixture . componentInstance . selectedIndex = 0 ;
284
- fixture . detectChanges ( ) ;
284
+ // Add a new tab in the beginning and select it, expect an origin < than 0 (animate left)
285
+ fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to left of index' } ) ;
286
+ fixture . componentInstance . selectedIndex = 0 ;
287
+ fixture . detectChanges ( ) ;
288
+ tick ( ) ;
285
289
286
- tabs = component . _tabs . toArray ( ) ;
287
- expect ( tabs [ 0 ] . origin ) . toBeLessThan ( 0 ) ;
288
- } ) ;
290
+ tabs = component . _tabs . toArray ( ) ;
291
+ expect ( tabs [ 0 ] . origin ) . toBeLessThan ( 0 ) ;
292
+ } ) ) ;
289
293
290
294
291
- it ( 'should update selected index if the last tab removed while selected' , ( ) => {
292
- fixture . detectChanges ( ) ;
295
+ it ( 'should update selected index if the last tab removed while selected' , fakeAsync ( ( ) => {
293
296
const component : MatTabGroup =
294
297
fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
295
298
296
299
const numberOfTabs = component . _tabs . length ;
297
300
fixture . componentInstance . selectedIndex = numberOfTabs - 1 ;
298
301
fixture . detectChanges ( ) ;
302
+ tick ( ) ;
299
303
300
304
// Remove last tab while last tab is selected, expect next tab over to be selected
301
305
fixture . componentInstance . tabs . pop ( ) ;
302
306
fixture . detectChanges ( ) ;
307
+ tick ( ) ;
303
308
304
309
expect ( component . selectedIndex ) . toBe ( numberOfTabs - 2 ) ;
305
- } ) ;
310
+ } ) ) ;
306
311
307
312
} ) ;
308
313
309
314
describe ( 'async tabs' , ( ) => {
310
315
let fixture : ComponentFixture < AsyncTabsTestApp > ;
311
316
312
- it ( 'should show tabs when they are available' , async ( ( ) => {
317
+ it ( 'should show tabs when they are available' , fakeAsync ( ( ) => {
313
318
fixture = TestBed . createComponent ( AsyncTabsTestApp ) ;
314
319
315
- let labels = fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) ;
316
-
317
- expect ( labels . length ) . toBe ( 0 ) ;
320
+ expect ( fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) . length ) . toBe ( 0 ) ;
318
321
319
322
fixture . detectChanges ( ) ;
323
+ tick ( ) ;
324
+ fixture . detectChanges ( ) ;
325
+ tick ( ) ;
320
326
321
- fixture . whenStable ( ) . then ( ( ) => {
322
- fixture . detectChanges ( ) ;
323
- labels = fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) ;
324
- expect ( labels . length ) . toBe ( 2 ) ;
325
- } ) ;
327
+ expect ( fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) . length ) . toBe ( 2 ) ;
326
328
} ) ) ;
327
329
} ) ;
328
330
329
331
describe ( 'with simple api' , ( ) => {
330
332
let fixture : ComponentFixture < TabGroupWithSimpleApi > ;
331
333
let tabGroup : MatTabGroup ;
332
334
333
- beforeEach ( ( ) => {
335
+ beforeEach ( fakeAsync ( ( ) => {
334
336
fixture = TestBed . createComponent ( TabGroupWithSimpleApi ) ;
335
337
fixture . detectChanges ( ) ;
338
+ tick ( ) ;
336
339
337
340
tabGroup =
338
341
fixture . debugElement . query ( By . directive ( MatTabGroup ) ) . componentInstance as MatTabGroup ;
339
- } ) ;
342
+ } ) ) ;
340
343
341
- it ( 'should support a tab-group with the simple api' , async ( ( ) => {
344
+ it ( 'should support a tab-group with the simple api' , fakeAsync ( ( ) => {
342
345
expect ( getSelectedLabel ( fixture ) . textContent ) . toMatch ( 'Junk food' ) ;
343
346
expect ( getSelectedContent ( fixture ) . textContent ) . toMatch ( 'Pizza, fries' ) ;
344
347
345
348
tabGroup . selectedIndex = 2 ;
346
349
fixture . detectChanges ( ) ;
347
- // Use whenStable to wait for async observables and change detection to run in content.
348
- fixture . whenStable ( ) . then ( ( ) => {
350
+ tick ( ) ;
349
351
350
- expect ( getSelectedLabel ( fixture ) . textContent ) . toMatch ( 'Fruit' ) ;
351
- expect ( getSelectedContent ( fixture ) . textContent ) . toMatch ( 'Apples, grapes' ) ;
352
+ expect ( getSelectedLabel ( fixture ) . textContent ) . toMatch ( 'Fruit' ) ;
353
+ expect ( getSelectedContent ( fixture ) . textContent ) . toMatch ( 'Apples, grapes' ) ;
352
354
353
- fixture . componentInstance . otherLabel = 'Chips' ;
354
- fixture . componentInstance . otherContent = 'Salt, vinegar' ;
355
- fixture . detectChanges ( ) ;
355
+ fixture . componentInstance . otherLabel = 'Chips' ;
356
+ fixture . componentInstance . otherContent = 'Salt, vinegar' ;
357
+ fixture . detectChanges ( ) ;
356
358
357
- expect ( getSelectedLabel ( fixture ) . textContent ) . toMatch ( 'Chips' ) ;
358
- expect ( getSelectedContent ( fixture ) . textContent ) . toMatch ( 'Salt, vinegar' ) ;
359
- } ) ;
359
+ expect ( getSelectedLabel ( fixture ) . textContent ) . toMatch ( 'Chips' ) ;
360
+ expect ( getSelectedContent ( fixture ) . textContent ) . toMatch ( 'Salt, vinegar' ) ;
360
361
} ) ) ;
361
362
362
363
it ( 'should support @ViewChild in the tab content' , ( ) => {
363
364
expect ( fixture . componentInstance . legumes ) . toBeTruthy ( ) ;
364
365
} ) ;
365
366
366
- it ( 'should only have the active tab in the DOM' , async ( ( ) => {
367
+ it ( 'should only have the active tab in the DOM' , fakeAsync ( ( ) => {
367
368
expect ( fixture . nativeElement . textContent ) . toContain ( 'Pizza, fries' ) ;
368
369
expect ( fixture . nativeElement . textContent ) . not . toContain ( 'Peanuts' ) ;
369
370
370
371
tabGroup . selectedIndex = 3 ;
371
372
fixture . detectChanges ( ) ;
372
- // Use whenStable to wait for async observables and change detection to run in content.
373
- fixture . whenStable ( ) . then ( ( ) => {
374
- expect ( fixture . nativeElement . textContent ) . not . toContain ( 'Pizza, fries' ) ;
375
- expect ( fixture . nativeElement . textContent ) . toContain ( 'Peanuts' ) ;
376
- } ) ;
373
+ tick ( ) ;
374
+
375
+ expect ( fixture . nativeElement . textContent ) . not . toContain ( 'Pizza, fries' ) ;
376
+ expect ( fixture . nativeElement . textContent ) . toContain ( 'Peanuts' ) ;
377
377
} ) ) ;
378
378
379
379
it ( 'should support setting the header position' , ( ) => {
@@ -389,20 +389,20 @@ describe('MatTabGroup', () => {
389
389
} ) ;
390
390
391
391
describe ( 'lazy loaded tabs' , ( ) => {
392
- it ( 'should lazy load the second tab' , async ( ) => {
393
- let fixture = TestBed . createComponent ( TemplateTabs ) ;
392
+ it ( 'should lazy load the second tab' , fakeAsync ( ( ) => {
393
+ const fixture = TestBed . createComponent ( TemplateTabs ) ;
394
394
fixture . detectChanges ( ) ;
395
+ tick ( ) ;
395
396
396
- let secondLabel = fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) [ 1 ] ;
397
+ const secondLabel = fixture . debugElement . queryAll ( By . css ( '.mat-tab-label' ) ) [ 1 ] ;
397
398
secondLabel . nativeElement . click ( ) ;
398
399
fixture . detectChanges ( ) ;
400
+ tick ( ) ;
401
+ fixture . detectChanges ( ) ;
399
402
400
- fixture . whenStable ( ) . then ( ( ) => {
401
- fixture . detectChanges ( ) ;
402
- let child = fixture . debugElement . query ( By . css ( '.child' ) ) ;
403
- expect ( child . nativeElement ) . toBeDefined ( ) ;
404
- } ) ;
405
- } ) ;
403
+ const child = fixture . debugElement . query ( By . css ( '.child' ) ) ;
404
+ expect ( child . nativeElement ) . toBeDefined ( ) ;
405
+ } ) ) ;
406
406
} ) ;
407
407
408
408
/**
@@ -436,7 +436,7 @@ describe('MatTabGroup', () => {
436
436
437
437
438
438
describe ( 'nested MatTabGroup with enabled animations' , ( ) => {
439
- beforeEach ( async ( ( ) => {
439
+ beforeEach ( fakeAsync ( ( ) => {
440
440
TestBed . configureTestingModule ( {
441
441
imports : [ MatTabsModule , BrowserAnimationsModule ] ,
442
442
declarations : [ NestedTabs ]
@@ -445,10 +445,11 @@ describe('nested MatTabGroup with enabled animations', () => {
445
445
TestBed . compileComponents ( ) ;
446
446
} ) ) ;
447
447
448
- it ( 'should not throw when creating a component with nested tab groups' , async ( ( ) => {
448
+ it ( 'should not throw when creating a component with nested tab groups' , fakeAsync ( ( ) => {
449
449
expect ( ( ) => {
450
450
let fixture = TestBed . createComponent ( NestedTabs ) ;
451
451
fixture . detectChanges ( ) ;
452
+ tick ( ) ;
452
453
} ) . not . toThrow ( ) ;
453
454
} ) ) ;
454
455
} ) ;
@@ -594,7 +595,7 @@ class AsyncTabsTestApp {
594
595
ngOnInit ( ) {
595
596
// Use ngOnInit because there is some issue with scheduling the async task in the constructor.
596
597
this . tabs = Observable . create ( ( observer : any ) => {
597
- requestAnimationFrame ( ( ) => observer . next ( this . _tabs ) ) ;
598
+ setTimeout ( ( ) => observer . next ( this . _tabs ) ) ;
598
599
} ) ;
599
600
}
600
601
}
0 commit comments