@@ -442,6 +442,45 @@ describe('MatTabGroup', () => {
442442 fixture . detectChanges ( ) ;
443443 expect ( tabList . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
444444 } ) ) ;
445+
446+ it ( 'should set IDs on individual tabs and use them to label the tab bodies' , ( ) => {
447+ fixture . detectChanges ( ) ;
448+ const tabs = Array . from < HTMLElement > ( fixture . nativeElement . querySelectorAll ( '.mat-mdc-tab' ) ) ;
449+ const bodies = Array . from < HTMLElement > (
450+ fixture . nativeElement . querySelectorAll ( 'mat-tab-body' ) ,
451+ ) ;
452+
453+ expect ( tabs . length ) . toBe ( 3 ) ;
454+ expect ( bodies . length ) . toBe ( 3 ) ;
455+ expect ( tabs . every ( tab => ! ! tab . getAttribute ( 'id' ) ) )
456+ . withContext ( 'All tabs should have IDs' )
457+ . toBe ( true ) ;
458+ expect (
459+ bodies . every ( ( body , index ) => {
460+ const attr = body . getAttribute ( 'aria-labelledby' ) ;
461+ return ! ! attr && tabs [ index ] . getAttribute ( 'id' ) === attr ;
462+ } ) ,
463+ )
464+ . withContext ( 'All tab bodies should be labelled' )
465+ . toBe ( true ) ;
466+ } ) ;
467+
468+ it ( 'should be able to set a custom ID for a tab' , ( ) => {
469+ fixture . detectChanges ( ) ;
470+ const tab = fixture . nativeElement . querySelectorAll ( '.mat-mdc-tab' ) [ 1 ] as HTMLElement ;
471+ const body = fixture . nativeElement . querySelectorAll ( 'mat-tab-body' ) [ 1 ] as HTMLElement ;
472+
473+ expect ( tab . getAttribute ( 'id' ) ) . toBeTruthy ( ) ;
474+ expect ( tab . getAttribute ( 'id' ) ) . not . toBe ( 'foo' ) ;
475+ expect ( body . getAttribute ( 'aria-labelledby' ) ) . toBeTruthy ( ) ;
476+ expect ( body . getAttribute ( 'aria-labelledby' ) ) . toBe ( tab . getAttribute ( 'id' ) ) ;
477+
478+ fixture . componentInstance . secondTabId = 'foo' ;
479+ fixture . changeDetectorRef . markForCheck ( ) ;
480+ fixture . detectChanges ( ) ;
481+ expect ( tab . getAttribute ( 'id' ) ) . toBe ( 'foo' ) ;
482+ expect ( body . getAttribute ( 'aria-labelledby' ) ) . toBe ( 'foo' ) ;
483+ } ) ;
445484 } ) ;
446485
447486 describe ( 'aria labelling' , ( ) => {
@@ -1236,7 +1275,7 @@ describe('MatTabGroup labels aligned with a config', () => {
12361275 <ng-template mat-tab-label>Tab One</ng-template>
12371276 Tab one content
12381277 </mat-tab>
1239- <mat-tab>
1278+ <mat-tab [id]="secondTabId" >
12401279 <ng-template mat-tab-label>Tab Two</ng-template>
12411280 <span>Tab </span><span>two</span><span>content</span>
12421281 </mat-tab>
@@ -1259,6 +1298,7 @@ class SimpleTabsTestApp {
12591298 headerPosition : MatTabHeaderPosition = 'above' ;
12601299 ariaLabel : string ;
12611300 ariaLabelledby : string ;
1301+ secondTabId : string | null = null ;
12621302 handleFocus ( event : any ) {
12631303 this . focusEvent = event ;
12641304 }
0 commit comments