Skip to content

Commit e5cc439

Browse files
feat(material/chips): add unit tests for custom aria-describedby
1 parent 2f06a67 commit e5cc439

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/dev-app/chips/chips-demo.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ <h4>Form Field</h4>
124124
[matChipInputAddOnBlur]="addOnBlur"
125125
(matChipInputTokenEnd)="add($event)" />
126126
</mat-chip-list>
127+
<mat-hint><code>&lt;mat-hint&gt;</code> is supported too!</mat-hint>
128+
<mat-error><code>&lt;mat-error&gt;</code> is supported too!</mat-error>
127129
</mat-form-field>
128130

129131

src/material/chips/chip-list.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,8 @@ describe('MatChipList', () => {
749749
let nativeChips: HTMLElement[];
750750

751751
describe('single selection', () => {
752+
let chipListEl: HTMLElement;
753+
752754
beforeEach(() => {
753755
fixture = createComponent(BasicChipList);
754756
fixture.detectChanges();
@@ -757,6 +759,7 @@ describe('MatChipList', () => {
757759
.queryAll(By.css('mat-chip'))
758760
.map(chip => chip.nativeElement);
759761
chips = fixture.componentInstance.chips;
762+
chipListEl = fixture.debugElement.query(By.css('mat-chip-list'))!.nativeElement;
760763
});
761764

762765
it('should take an initial view value with reactive forms', () => {
@@ -949,6 +952,22 @@ describe('MatChipList', () => {
949952

950953
expect(formField.classList).not.toContain('mat-focused');
951954
}));
955+
956+
it('should set `aria-describedby` to the id of the mat-hint', fakeAsync(() => {
957+
expect(chipListEl.getAttribute('aria-describedby')).toBeNull();
958+
959+
fixture.componentInstance.hint = 'test';
960+
fixture.detectChanges();
961+
const hint = fixture.debugElement.query(By.css('.mat-hint')).nativeElement;
962+
expect(chipListEl.getAttribute('aria-describedby')).toBe(hint.getAttribute('id'));
963+
expect(chipListEl.getAttribute('aria-describedby')).toMatch(/^mat-hint-\d+$/);
964+
}));
965+
966+
it('should support user binding to `aria-describedby`', fakeAsync(() => {
967+
fixture.componentInstance.ariaDescribedBy = 'test';
968+
fixture.detectChanges();
969+
expect(chipListEl.getAttribute('aria-describedby')).toBe('test');
970+
}));
952971
});
953972

954973
describe('multiple selection', () => {
@@ -1586,11 +1605,13 @@ class FormFieldChipList {
15861605
template: `
15871606
<mat-form-field>
15881607
<mat-chip-list placeholder="Food" [formControl]="control"
1608+
[aria-describedby]="ariaDescribedBy"
15891609
[tabIndex]="tabIndexOverride" [selectable]="selectable">
15901610
<mat-chip *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
15911611
{{ food.viewValue }}
15921612
</mat-chip>
15931613
</mat-chip-list>
1614+
<mat-hint *ngIf="hint">{{ hint }}</mat-hint>
15941615
</mat-form-field>
15951616
`,
15961617
})
@@ -1605,7 +1626,9 @@ class BasicChipList {
16051626
{value: 'pasta-6', viewValue: 'Pasta'},
16061627
{value: 'sushi-7', viewValue: 'Sushi'},
16071628
];
1629+
ariaDescribedBy: string = '';
16081630
control = new FormControl();
1631+
hint: string;
16091632
tabIndexOverride: number;
16101633
selectable: boolean;
16111634

src/material/chips/chip-list.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,16 @@ export class MatChipList
197197
* Implemented as part of MatFormFieldControl.
198198
* @docs-private
199199
*/
200-
@Input('aria-describedby') userAriaDescribedBy: string;
200+
@Input('aria-describedby')
201+
get userAriaDescribedBy(): string {
202+
return this._userAriaDescribedBy;
203+
}
204+
set userAriaDescribedBy(userAriaDescribedBy: string) {
205+
this._userAriaDescribedBy = userAriaDescribedBy;
206+
this.stateChanges.next();
207+
}
208+
209+
private _userAriaDescribedBy: string;
201210

202211
/** An object used to control when error messages are shown. */
203212
@Input() override errorStateMatcher: ErrorStateMatcher;

tools/public_api_guard/material/chips.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
256256
_uid: string;
257257
protected _updateFocusForDestroyedChips(): void;
258258
protected _updateTabIndex(): void;
259-
userAriaDescribedBy: string;
259+
get userAriaDescribedBy(): string;
260+
set userAriaDescribedBy(userAriaDescribedBy: string);
260261
_userTabIndex: number | null;
261262
get value(): any;
262263
set value(value: any);

0 commit comments

Comments
 (0)