@@ -282,14 +282,17 @@ describe('MatTabGroup', () => {
282
282
expect ( tabs [ 3 ] . origin ) . toBeGreaterThanOrEqual ( 0 ) ;
283
283
284
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
285
fixture . componentInstance . selectedIndex = 0 ;
287
286
fixture . detectChanges ( ) ;
288
287
tick ( ) ;
289
288
289
+ fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to left of index' } ) ;
290
+ fixture . detectChanges ( ) ;
291
+ tick ( ) ;
292
+
290
293
tabs = component . _tabs . toArray ( ) ;
291
294
expect ( tabs [ 0 ] . origin ) . toBeLessThan ( 0 ) ;
292
- } ) ) ;
295
+ } ) ) ;
293
296
294
297
295
298
it ( 'should update selected index if the last tab removed while selected' , fakeAsync ( ( ) => {
@@ -309,6 +312,47 @@ describe('MatTabGroup', () => {
309
312
expect ( component . selectedIndex ) . toBe ( numberOfTabs - 2 ) ;
310
313
} ) ) ;
311
314
315
+
316
+ it ( 'should maintain the selected tab if a new tab is added' , ( ) => {
317
+ fixture . detectChanges ( ) ;
318
+ const component : MatTabGroup =
319
+ fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
320
+
321
+ fixture . componentInstance . selectedIndex = 1 ;
322
+ fixture . detectChanges ( ) ;
323
+
324
+ // Add a new tab at the beginning.
325
+ fixture . componentInstance . tabs . unshift ( { label : 'New tab' , content : 'at the start' } ) ;
326
+ fixture . detectChanges ( ) ;
327
+
328
+ expect ( component . selectedIndex ) . toBe ( 2 ) ;
329
+ expect ( component . _tabs . toArray ( ) [ 2 ] . isActive ) . toBe ( true ) ;
330
+ } ) ;
331
+
332
+
333
+ it ( 'should maintain the selected tab if a tab is removed' , ( ) => {
334
+ // Add a couple of tabs so we have more to work with.
335
+ fixture . componentInstance . tabs . push (
336
+ { label : 'New tab' , content : 'with new content' } ,
337
+ { label : 'Another new tab' , content : 'with newer content' }
338
+ ) ;
339
+
340
+ // Select the second-to-last tab.
341
+ fixture . componentInstance . selectedIndex = 3 ;
342
+ fixture . detectChanges ( ) ;
343
+
344
+ const component : MatTabGroup =
345
+ fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
346
+
347
+ // Remove a tab right before the selected one.
348
+ fixture . componentInstance . tabs . splice ( 2 , 1 ) ;
349
+ fixture . detectChanges ( ) ;
350
+
351
+ expect ( component . selectedIndex ) . toBe ( 1 ) ;
352
+ expect ( component . _tabs . toArray ( ) [ 1 ] . isActive ) . toBe ( true ) ;
353
+ } ) ;
354
+
355
+
312
356
} ) ;
313
357
314
358
describe ( 'async tabs' , ( ) => {
0 commit comments