@@ -278,10 +278,12 @@ describe('MatTabGroup', () => {
278
278
expect ( tabs [ 3 ] . origin ) . toBeGreaterThanOrEqual ( 0 ) ;
279
279
280
280
// Add a new tab in the beginning and select it, expect an origin < than 0 (animate left)
281
- fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to left of index' } ) ;
282
281
fixture . componentInstance . selectedIndex = 0 ;
283
282
fixture . detectChanges ( ) ;
284
283
284
+ fixture . componentInstance . tabs . push ( { label : 'New tab' , content : 'to left of index' } ) ;
285
+ fixture . detectChanges ( ) ;
286
+
285
287
tabs = component . _tabs . toArray ( ) ;
286
288
expect ( tabs [ 0 ] . origin ) . toBeLessThan ( 0 ) ;
287
289
} ) ;
@@ -303,6 +305,47 @@ describe('MatTabGroup', () => {
303
305
expect ( component . selectedIndex ) . toBe ( numberOfTabs - 2 ) ;
304
306
} ) ;
305
307
308
+
309
+ it ( 'should maintain the selected tab if a new tab is added' , ( ) => {
310
+ fixture . detectChanges ( ) ;
311
+ const component : MatTabGroup =
312
+ fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
313
+
314
+ fixture . componentInstance . selectedIndex = 1 ;
315
+ fixture . detectChanges ( ) ;
316
+
317
+ // Add a new tab at the beginning.
318
+ fixture . componentInstance . tabs . unshift ( { label : 'New tab' , content : 'at the start' } ) ;
319
+ fixture . detectChanges ( ) ;
320
+
321
+ expect ( component . selectedIndex ) . toBe ( 2 ) ;
322
+ expect ( component . _tabs . toArray ( ) [ 2 ] . isActive ) . toBe ( true ) ;
323
+ } ) ;
324
+
325
+
326
+ it ( 'should maintain the selected tab if a tab is removed' , ( ) => {
327
+ // Add a couple of tabs so we have more to work with.
328
+ fixture . componentInstance . tabs . push (
329
+ { label : 'New tab' , content : 'with new content' } ,
330
+ { label : 'Another new tab' , content : 'with newer content' }
331
+ ) ;
332
+
333
+ // Select the second-to-last tab.
334
+ fixture . componentInstance . selectedIndex = 3 ;
335
+ fixture . detectChanges ( ) ;
336
+
337
+ const component : MatTabGroup =
338
+ fixture . debugElement . query ( By . css ( 'mat-tab-group' ) ) . componentInstance ;
339
+
340
+ // Remove a tab right before the selected one.
341
+ fixture . componentInstance . tabs . splice ( 2 , 1 ) ;
342
+ fixture . detectChanges ( ) ;
343
+
344
+ expect ( component . selectedIndex ) . toBe ( 1 ) ;
345
+ expect ( component . _tabs . toArray ( ) [ 1 ] . isActive ) . toBe ( true ) ;
346
+ } ) ;
347
+
348
+
306
349
} ) ;
307
350
308
351
describe ( 'async tabs' , ( ) => {
0 commit comments