@@ -1471,6 +1471,52 @@ describe('MDC-based MatAutocomplete', () => {
14711471 . toEqual ( 'listbox' , 'Expected role of the panel to be listbox.' ) ;
14721472 } ) ;
14731473
1474+ it ( 'should point the aria-labelledby of the panel to the field label' , ( ) => {
1475+ fixture . componentInstance . trigger . openPanel ( ) ;
1476+ fixture . detectChanges ( ) ;
1477+
1478+ const panel =
1479+ fixture . debugElement . query ( By . css ( '.mat-mdc-autocomplete-panel' ) ) ! . nativeElement ;
1480+ const labelId = fixture . nativeElement . querySelector ( 'label' ) . id ;
1481+ expect ( panel . getAttribute ( 'aria-labelledby' ) ) . toBe ( labelId ) ;
1482+ expect ( panel . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
1483+ } ) ;
1484+
1485+ it ( 'should add a custom aria-labelledby to the panel' , ( ) => {
1486+ fixture . componentInstance . ariaLabelledby = 'myLabelId' ;
1487+ fixture . componentInstance . trigger . openPanel ( ) ;
1488+ fixture . detectChanges ( ) ;
1489+
1490+ const panel =
1491+ fixture . debugElement . query ( By . css ( '.mat-mdc-autocomplete-panel' ) ) ! . nativeElement ;
1492+ const labelId = fixture . nativeElement . querySelector ( 'label' ) . id ;
1493+ expect ( panel . getAttribute ( 'aria-labelledby' ) ) . toBe ( `${ labelId } myLabelId` ) ;
1494+ expect ( panel . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
1495+ } ) ;
1496+
1497+ it ( 'should clear aria-labelledby from the panel if an aria-label is set' , ( ) => {
1498+ fixture . componentInstance . ariaLabel = 'My label' ;
1499+ fixture . componentInstance . trigger . openPanel ( ) ;
1500+ fixture . detectChanges ( ) ;
1501+
1502+ const panel =
1503+ fixture . debugElement . query ( By . css ( '.mat-mdc-autocomplete-panel' ) ) ! . nativeElement ;
1504+ expect ( panel . getAttribute ( 'aria-label' ) ) . toBe ( 'My label' ) ;
1505+ expect ( panel . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
1506+ } ) ;
1507+
1508+ it ( 'should support setting a custom aria-label' , ( ) => {
1509+ fixture . componentInstance . ariaLabel = 'Custom Label' ;
1510+ fixture . componentInstance . trigger . openPanel ( ) ;
1511+ fixture . detectChanges ( ) ;
1512+
1513+ const panel =
1514+ fixture . debugElement . query ( By . css ( '.mat-mdc-autocomplete-panel' ) ) ! . nativeElement ;
1515+
1516+ expect ( panel . getAttribute ( 'aria-label' ) ) . toEqual ( 'Custom Label' ) ;
1517+ expect ( panel . hasAttribute ( 'aria-labelledby' ) ) . toBe ( false ) ;
1518+ } ) ;
1519+
14741520 it ( 'should set aria-autocomplete to list' , ( ) => {
14751521 expect ( input . getAttribute ( 'aria-autocomplete' ) )
14761522 . toEqual ( 'list' , 'Expected aria-autocomplete attribute to equal list.' ) ;
@@ -2682,6 +2728,7 @@ describe('MDC-based MatAutocomplete', () => {
26822728
26832729const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
26842730 <mat-form-field [floatLabel]="floatLabel" [style.width.px]="width">
2731+ <mat-label>State</mat-label>
26852732 <input
26862733 matInput
26872734 placeholder="State"
@@ -2692,7 +2739,8 @@ const SIMPLE_AUTOCOMPLETE_TEMPLATE = `
26922739 </mat-form-field>
26932740
26942741 <mat-autocomplete [class]="panelClass" #auto="matAutocomplete" [displayWith]="displayFn"
2695- [disableRipple]="disableRipple" (opened)="openedSpy()" (closed)="closedSpy()">
2742+ [disableRipple]="disableRipple" [aria-label]="ariaLabel" [aria-labelledby]="ariaLabelledby"
2743+ (opened)="openedSpy()" (closed)="closedSpy()">
26962744 <mat-option
26972745 *ngFor="let state of filteredStates"
26982746 [value]="state"
@@ -2712,6 +2760,8 @@ class SimpleAutocomplete implements OnDestroy {
27122760 width : number ;
27132761 disableRipple = false ;
27142762 autocompleteDisabled = false ;
2763+ ariaLabel : string ;
2764+ ariaLabelledby : string ;
27152765 panelClass = 'class-one class-two' ;
27162766 openedSpy = jasmine . createSpy ( 'autocomplete opened spy' ) ;
27172767 closedSpy = jasmine . createSpy ( 'autocomplete closed spy' ) ;
0 commit comments