File tree 3 files changed +52
-0
lines changed
material-experimental/mdc-tabs
3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -588,6 +588,26 @@ describe('MDC-based MatTabGroup', () => {
588
588
589
589
expect ( fixture . componentInstance . handleSelection ) . not . toHaveBeenCalled ( ) ;
590
590
} ) ) ;
591
+
592
+ it ( 'should update the newly-selected tab if the previously-selected tab is replaced' , fakeAsync ( ( ) => {
593
+ const component : MatTabGroup = fixture . debugElement . query (
594
+ By . css ( 'mat-tab-group' ) ,
595
+ ) ! . componentInstance ;
596
+
597
+ spyOn ( fixture . componentInstance , 'handleSelection' ) ;
598
+
599
+ fixture . componentInstance . tabs [ fixture . componentInstance . selectedIndex ] = {
600
+ label : 'New' ,
601
+ content : 'New' ,
602
+ } ;
603
+ fixture . detectChanges ( ) ;
604
+ tick ( ) ;
605
+
606
+ expect ( component . _tabs . get ( 1 ) ?. isActive ) . toBe ( true ) ;
607
+ expect ( fixture . componentInstance . handleSelection ) . toHaveBeenCalledWith (
608
+ jasmine . objectContaining ( { index : 1 } ) ,
609
+ ) ;
610
+ } ) ) ;
591
611
} ) ;
592
612
593
613
describe ( 'async tabs' , ( ) => {
Original file line number Diff line number Diff line change @@ -586,6 +586,26 @@ describe('MatTabGroup', () => {
586
586
587
587
expect ( fixture . componentInstance . handleSelection ) . not . toHaveBeenCalled ( ) ;
588
588
} ) ) ;
589
+
590
+ it ( 'should update the newly-selected tab if the previously-selected tab is replaced' , fakeAsync ( ( ) => {
591
+ const component : MatTabGroup = fixture . debugElement . query (
592
+ By . css ( 'mat-tab-group' ) ,
593
+ ) ! . componentInstance ;
594
+
595
+ spyOn ( fixture . componentInstance , 'handleSelection' ) ;
596
+
597
+ fixture . componentInstance . tabs [ fixture . componentInstance . selectedIndex ] = {
598
+ label : 'New' ,
599
+ content : 'New' ,
600
+ } ;
601
+ fixture . detectChanges ( ) ;
602
+ tick ( ) ;
603
+
604
+ expect ( component . _tabs . get ( 1 ) ?. isActive ) . toBe ( true ) ;
605
+ expect ( fixture . componentInstance . handleSelection ) . toHaveBeenCalledWith (
606
+ jasmine . objectContaining ( { index : 1 } ) ,
607
+ ) ;
608
+ } ) ) ;
589
609
} ) ;
590
610
591
611
describe ( 'async tabs' , ( ) => {
Original file line number Diff line number Diff line change @@ -305,16 +305,28 @@ export abstract class _MatTabGroupBase
305
305
// explicit change that selects a different tab.
306
306
if ( indexToSelect === this . _selectedIndex ) {
307
307
const tabs = this . _tabs . toArray ( ) ;
308
+ let selectedTab : MatTab | undefined ;
308
309
309
310
for ( let i = 0 ; i < tabs . length ; i ++ ) {
310
311
if ( tabs [ i ] . isActive ) {
311
312
// Assign both to the `_indexToSelect` and `_selectedIndex` so we don't fire a changed
312
313
// event, otherwise the consumer may end up in an infinite loop in some edge cases like
313
314
// adding a tab within the `selectedIndexChange` event.
314
315
this . _indexToSelect = this . _selectedIndex = i ;
316
+ selectedTab = tabs [ i ] ;
315
317
break ;
316
318
}
317
319
}
320
+
321
+ // If we haven't found an active tab and a tab exists at the selected index, it means
322
+ // that the active tab was swapped out. Since this won't be picked up by the rendering
323
+ // loop in `ngAfterContentChecked`, we need to sync it up manually.
324
+ if ( ! selectedTab && tabs [ indexToSelect ] ) {
325
+ Promise . resolve ( ) . then ( ( ) => {
326
+ tabs [ indexToSelect ] . isActive = true ;
327
+ this . selectedTabChange . emit ( this . _createChangeEvent ( indexToSelect ) ) ;
328
+ } ) ;
329
+ }
318
330
}
319
331
320
332
this . _changeDetectorRef . markForCheck ( ) ;
You can’t perform that action at this time.
0 commit comments