Skip to content

Commit

Permalink
feat(material/chips): add unit tests for custom aria-describedby
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertAKARobin committed Apr 27, 2022
1 parent 317011e commit 313431e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/dev-app/chips/chips-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ <h4>Form Field</h4>
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)" />
</mat-chip-list>
<mat-hint><code>&lt;mat-hint&gt;</code> is supported too!</mat-hint>
<mat-error><code>&lt;mat-error&gt;</code> is supported too!</mat-error>
</mat-form-field>


Expand Down
23 changes: 23 additions & 0 deletions src/material/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,8 @@ describe('MatChipList', () => {
let nativeChips: HTMLElement[];

describe('single selection', () => {
let chipListEl: HTMLElement;

beforeEach(() => {
fixture = createComponent(BasicChipList);
fixture.detectChanges();
Expand All @@ -757,6 +759,7 @@ describe('MatChipList', () => {
.queryAll(By.css('mat-chip'))
.map(chip => chip.nativeElement);
chips = fixture.componentInstance.chips;
chipListEl = fixture.debugElement.query(By.css('mat-chip-list'))!.nativeElement;
});

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

expect(formField.classList).not.toContain('mat-focused');
}));

it('should set `aria-describedby` to the id of the mat-hint', fakeAsync(() => {
expect(chipListEl.getAttribute('aria-describedby')).toBeNull();

fixture.componentInstance.hint = 'test';
fixture.detectChanges();
const hint = fixture.debugElement.query(By.css('.mat-hint')).nativeElement;
expect(chipListEl.getAttribute('aria-describedby')).toBe(hint.getAttribute('id'));
expect(chipListEl.getAttribute('aria-describedby')).toMatch(/^mat-hint-\d+$/);
}));

it('should support user binding to `aria-describedby`', fakeAsync(() => {
fixture.componentInstance.ariaDescribedBy = 'test';
fixture.detectChanges();
expect(chipListEl.getAttribute('aria-describedby')).toBe('test');
}));
});

describe('multiple selection', () => {
Expand Down Expand Up @@ -1586,11 +1605,13 @@ class FormFieldChipList {
template: `
<mat-form-field>
<mat-chip-list placeholder="Food" [formControl]="control"
[aria-describedby]="ariaDescribedBy"
[tabIndex]="tabIndexOverride" [selectable]="selectable">
<mat-chip *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled">
{{ food.viewValue }}
</mat-chip>
</mat-chip-list>
<mat-hint *ngIf="hint">{{ hint }}</mat-hint>
</mat-form-field>
`,
})
Expand All @@ -1605,7 +1626,9 @@ class BasicChipList {
{value: 'pasta-6', viewValue: 'Pasta'},
{value: 'sushi-7', viewValue: 'Sushi'},
];
ariaDescribedBy: string = '';
control = new FormControl<string | null>(null);
hint: string;
tabIndexOverride: number;
selectable: boolean;

Expand Down
11 changes: 10 additions & 1 deletion src/material/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@ export class MatChipList
* Implemented as part of MatFormFieldControl.
* @docs-private
*/
@Input('aria-describedby') userAriaDescribedBy: string;
@Input('aria-describedby')
get userAriaDescribedBy(): string {
return this._userAriaDescribedBy;
}
set userAriaDescribedBy(userAriaDescribedBy: string) {
this._userAriaDescribedBy = userAriaDescribedBy;
this.stateChanges.next();
}

private _userAriaDescribedBy: string;

/** An object used to control when error messages are shown. */
@Input() override errorStateMatcher: ErrorStateMatcher;
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/material/chips.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
_uid: string;
protected _updateFocusForDestroyedChips(): void;
protected _updateTabIndex(): void;
userAriaDescribedBy: string;
get userAriaDescribedBy(): string;
set userAriaDescribedBy(userAriaDescribedBy: string);
_userTabIndex: number | null;
get value(): any;
set value(value: any);
Expand Down

0 comments on commit 313431e

Please sign in to comment.