Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(material/chips): add unit tests for custom aria-describedby #24657

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions src/material-experimental/mdc-chips/chip-listbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,12 @@ describe('MDC-based MatChipListbox', () => {

expect(chipArray[4].focus).not.toHaveBeenCalled();
});

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

describe('multiple selection', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/material-experimental/mdc-chips/chip-listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export const MAT_CHIP_LISTBOX_CONTROL_VALUE_ACCESSOR: any = {
'class': 'mdc-evolution-chip-set mat-mdc-chip-listbox',
'[attr.role]': 'role',
'[tabIndex]': 'empty ? -1 : tabIndex',
// TODO: replace this binding with use of AriaDescriber
'[attr.aria-describedby]': '_ariaDescribedby || null',
'[attr.aria-required]': 'role ? required : null',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-multiselectable]': 'multiple',
Expand Down
6 changes: 6 additions & 0 deletions src/material-experimental/mdc-chips/chip-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ describe('MDC-based MatChipSet', () => {
fixture.detectChanges();
expect(chipSetNativeElement.getAttribute('role')).toBe('list');
});

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

Expand Down
12 changes: 12 additions & 0 deletions src/material-experimental/mdc-chips/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const _MatChipSetMixinBase = mixinTabIndex(MatChipSetBase);
'class': 'mat-mdc-chip-set mdc-evolution-chip-set',
'(keydown)': '_handleKeydown($event)',
'[attr.role]': 'role',
'[attr.aria-describedby]': 'userAriaDescribedBy || null',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down Expand Up @@ -85,6 +86,17 @@ export class MatChipSet
return this._getChipStream(chip => chip.destroyed);
}

@Input('aria-describedby')
get userAriaDescribedBy(): string {
return this._userAriaDescribedBy;
}

set userAriaDescribedBy(userAriaDescribedBy: string) {
this._userAriaDescribedBy = userAriaDescribedBy;
}

private _userAriaDescribedBy: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the getter/setter needed here? Also I lack a little on context for this change I feel like. I thought we only needed the custom input when a component was a FormFieldControl where we also added additional described by ids automatically, using .setDescribedByIds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewseguin Can you explain a bit more about what changes you were looking for?

@devversion Yes, the getters/setters seem unnecessary. I was copying from the mat-chips code (which in turn was copied from mat-select, IIRC). Will wait to see what Andrew says though.


/** Whether the chip set is disabled. */
@Input()
get disabled(): boolean {
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