Skip to content

Commit a257872

Browse files
committed
add more tests
1 parent 3e47349 commit a257872

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,38 @@ describe('MatChipInput', () => {
155155
expect(inputNativeElement.classList).toContain('mat-mdc-chip-input');
156156
expect(inputNativeElement.classList).toContain('mdc-text-field__input');
157157
});
158+
159+
it('should set `aria-describedby` to the id of the mat-hint', () => {
160+
expect(inputNativeElement.getAttribute('aria-describedby')).toBeNull();
161+
162+
fixture.componentInstance.hint = 'test';
163+
fixture.changeDetectorRef.markForCheck();
164+
fixture.detectChanges();
165+
const hint = fixture.debugElement.query(By.css('mat-hint')).nativeElement;
166+
167+
expect(inputNativeElement.getAttribute('aria-describedby')).toBe(hint.getAttribute('id'));
168+
expect(inputNativeElement.getAttribute('aria-describedby')).toMatch(/^mat-mdc-hint-\w+\d+$/);
169+
});
170+
171+
it('should support user binding to `aria-describedby`', () => {
172+
inputNativeElement.setAttribute('aria-describedby', 'test');
173+
fixture.changeDetectorRef.markForCheck();
174+
fixture.detectChanges();
175+
176+
expect(inputNativeElement.getAttribute('aria-describedby')).toBe('test');
177+
});
178+
179+
it('should preserve aria-describedby set directly in the DOM', fakeAsync(() => {
180+
inputNativeElement.setAttribute('aria-describedby', 'custom');
181+
fixture.componentInstance.hint = 'test';
182+
fixture.changeDetectorRef.markForCheck();
183+
fixture.detectChanges();
184+
const hint = fixture.debugElement.query(By.css('mat-hint')).nativeElement;
185+
186+
expect(inputNativeElement.getAttribute('aria-describedby')).toBe(
187+
`${hint.getAttribute('id')} custom`,
188+
);
189+
}));
158190
});
159191

160192
describe('[addOnBlur]', () => {
@@ -289,7 +321,7 @@ describe('MatChipInput', () => {
289321

290322
@Component({
291323
template: `
292-
<mat-form-field>
324+
<mat-form-field [hintLabel]="hint">
293325
<mat-chip-grid #chipGrid [required]="required">
294326
<mat-chip-row>Hello</mat-chip-row>
295327
<input
@@ -309,6 +341,7 @@ class TestChipInput {
309341
placeholder = '';
310342
required = false;
311343
disabledInteractive = false;
344+
hint: string;
312345

313346
add(_: MatChipInputEvent) {}
314347
}

src/material/datepicker/date-range-input.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('MatDateRangeInput', () => {
169169
expect(rangeInput.getAttribute('aria-labelledby')).toBe(labelId);
170170
});
171171

172-
it('should point the range input aria-labelledby to the form field hint element', () => {
172+
it('should point the range input aria-describedby to the form field hint element', () => {
173173
const fixture = createComponent(StandardRangePicker);
174174
fixture.detectChanges();
175175
const labelId = fixture.nativeElement.querySelector('.mat-mdc-form-field-hint').id;
@@ -179,6 +179,18 @@ describe('MatDateRangeInput', () => {
179179
expect(rangeInput.getAttribute('aria-describedby')).toBe(labelId);
180180
});
181181

182+
it('should preserve aria-describedby set directly in the DOM', fakeAsync(() => {
183+
const fixture = createComponent(StandardRangePicker);
184+
const rangeInput = fixture.nativeElement.querySelector('.mat-date-range-input');
185+
186+
rangeInput.setAttribute('aria-describedby', 'custom');
187+
fixture.changeDetectorRef.markForCheck();
188+
fixture.detectChanges();
189+
const hint = fixture.nativeElement.querySelector('.mat-mdc-form-field-hint');
190+
191+
expect(rangeInput.getAttribute('aria-describedby')).toBe(`${hint.getAttribute('id')} custom`);
192+
}));
193+
182194
it('should not set aria-labelledby if the form field does not have a label', () => {
183195
const fixture = createComponent(RangePickerNoLabel);
184196
fixture.detectChanges();

src/material/datepicker/date-range-input.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ export class MatDateRangeInput<D>
289289
* @docs-private
290290
*/
291291
get describedByIds(): string[] {
292-
return this._ariaDescribedBy?.split(' ') || [];
292+
const element = this._elementRef.nativeElement;
293+
const existingDescribedBy = element.getAttribute('aria-describedby');
294+
295+
return existingDescribedBy?.split(' ') || [];
293296
}
294297

295298
/**

src/material/select/select.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ describe('MatSelect', () => {
226226
expect(select.getAttribute('aria-describedby')).toBe('test');
227227
});
228228

229+
it('should preserve aria-describedby set directly in the DOM', fakeAsync(() => {
230+
select.setAttribute('aria-describedby', 'custom');
231+
fixture.componentInstance.hint = 'test';
232+
fixture.changeDetectorRef.markForCheck();
233+
fixture.detectChanges();
234+
const hint = fixture.debugElement.query(By.css('mat-hint')).nativeElement;
235+
236+
expect(select.getAttribute('aria-describedby')).toBe(`${hint.getAttribute('id')} custom`);
237+
}));
238+
229239
it('should be able to override the tabindex', () => {
230240
fixture.componentInstance.tabIndexOverride = 3;
231241
fixture.changeDetectorRef.markForCheck();

0 commit comments

Comments
 (0)