@@ -442,6 +442,45 @@ describe('MatTabGroup', () => {
442
442
fixture . detectChanges ( ) ;
443
443
expect ( tabList . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
444
444
} ) ) ;
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
+ } ) ;
445
484
} ) ;
446
485
447
486
describe ( 'aria labelling' , ( ) => {
@@ -1236,7 +1275,7 @@ describe('MatTabGroup labels aligned with a config', () => {
1236
1275
<ng-template mat-tab-label>Tab One</ng-template>
1237
1276
Tab one content
1238
1277
</mat-tab>
1239
- <mat-tab>
1278
+ <mat-tab [id]="secondTabId" >
1240
1279
<ng-template mat-tab-label>Tab Two</ng-template>
1241
1280
<span>Tab </span><span>two</span><span>content</span>
1242
1281
</mat-tab>
@@ -1259,6 +1298,7 @@ class SimpleTabsTestApp {
1259
1298
headerPosition : MatTabHeaderPosition = 'above' ;
1260
1299
ariaLabel : string ;
1261
1300
ariaLabelledby : string ;
1301
+ secondTabId : string | null = null ;
1262
1302
handleFocus ( event : any ) {
1263
1303
this . focusEvent = event ;
1264
1304
}
0 commit comments