Skip to content

Commit ffc267c

Browse files
zelliottmmalerba
authored andcommitted
feat(material/*) Focus indicator unit tests (#18150)
* Add focus indicators to mat-chip. Use a dynamically added element as the ripple target * _document should be an optional param to avoid breaking change. * Added unit tests for focus indicator, improved focus indicator color on particular components. * Some minor style changes. * Cleaned up focus indicator unit tests. * Some more minor unit test formatting changes, undo coloring changes for now. * Fixed formatting. * Fixed final linter error.
1 parent 4e98c5b commit ffc267c

File tree

17 files changed

+116
-0
lines changed

17 files changed

+116
-0
lines changed

src/material/button-toggle/button-toggle.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,14 @@ describe('MatButtonToggle without forms', () => {
880880
}).not.toThrow();
881881
});
882882

883+
it('should have a focus indicator', () => {
884+
const fixture = TestBed.createComponent(ButtonTogglesInsideButtonToggleGroup);
885+
const buttonNativeElements = [...fixture.debugElement.nativeElement.querySelectorAll('button')];
886+
887+
expect(buttonNativeElements.every(element => element.classList.contains('mat-focus-indicator')))
888+
.toBe(true);
889+
});
890+
883891
});
884892

885893
@Component({

src/material/button/button.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ describe('MatButton', () => {
270270
);
271271
});
272272
});
273+
274+
it('should have a focus indicator', () => {
275+
const fixture = TestBed.createComponent(TestApp);
276+
const buttonNativeElement = fixture.debugElement.nativeElement.querySelector('button');
277+
278+
expect(buttonNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
279+
});
273280
});
274281

275282
/** Test component that contains an MatButton. */

src/material/checkbox/checkbox.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ describe('MatCheckbox', () => {
673673
expect(checkboxNativeElement.classList).toContain('mat-checkbox-indeterminate');
674674
}));
675675
});
676+
677+
it('should have a focus indicator', () => {
678+
const checkboxRippleNativeElement =
679+
checkboxNativeElement.querySelector('.mat-checkbox-ripple')!;
680+
681+
expect(checkboxRippleNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
682+
});
676683
});
677684

678685
describe('with change event and no initial value', () => {

src/material/chips/chip.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ describe('MatChip', () => {
411411
});
412412

413413
});
414+
415+
it('should have a focus indicator', () => {
416+
expect(chipNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
417+
});
414418
});
415419
});
416420

src/material/core/option/option.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ describe('MatOption component', () => {
190190

191191
});
192192

193+
it('should have a focus indicator', () => {
194+
const fixture = TestBed.createComponent(BasicOption);
195+
const optionNativeElement = fixture.debugElement.query(By.directive(MatOption))!.nativeElement;
196+
197+
expect(optionNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
198+
});
199+
193200
});
194201

195202
@Component({

src/material/datepicker/calendar-body.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ describe('MatCalendarBody', () => {
106106
expect(cellEls[1].classList).toContain('even');
107107
});
108108

109+
it('should have a focus indicator', () => {
110+
expect(cellEls.every(element => element.classList.contains('mat-focus-indicator')))
111+
.toBe(true);
112+
});
113+
109114
});
110115

111116
});

src/material/expansion/expansion.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@ describe('MatExpansionPanel', () => {
450450
});
451451

452452
});
453+
454+
it('should have a focus indicator', () => {
455+
const fixture = TestBed.createComponent(PanelWithContent);
456+
const headerNativeElement =
457+
fixture.debugElement.query(By.directive(MatExpansionPanelHeader))!.nativeElement;
458+
459+
expect(headerNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
460+
});
453461
});
454462

455463

src/material/list/list.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ describe('MatList', () => {
2929
fixture.detectChanges();
3030
expect(listItem.nativeElement.classList.length).toBe(2);
3131
expect(listItem.nativeElement.classList).toContain('mat-list-item');
32+
33+
// This spec also ensures the focus indicator is present.
3234
expect(listItem.nativeElement.classList).toContain('mat-focus-indicator');
3335
});
3436

src/material/list/selection-list.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,13 @@ describe('MatSelectionList without forms', () => {
660660
expect(option.classList).toContain('mat-2-line');
661661
});
662662

663+
it('should have a focus indicator', () => {
664+
const optionNativeElements = listOptions.map(option => option.nativeElement);
665+
666+
expect(optionNativeElements
667+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
668+
});
669+
663670
});
664671

665672
describe('with list option selected', () => {

src/material/menu/menu.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,17 @@ describe('MatMenu', () => {
21482148

21492149
});
21502150

2151+
it('should have a focus indicator', () => {
2152+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
2153+
fixture.detectChanges();
2154+
fixture.componentInstance.trigger.openMenu();
2155+
fixture.detectChanges();
2156+
const menuItemNativeElements =
2157+
Array.from(overlayContainerElement.querySelectorAll('.mat-menu-item'));
2158+
2159+
expect(menuItemNativeElements
2160+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
2161+
});
21512162
});
21522163

21532164
describe('MatMenu default overrides', () => {

src/material/radio/radio.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,14 @@ describe('MatRadio', () => {
387387
expect(radioNativeElements[2].classList).toContain('mat-warn');
388388
});
389389

390+
it('should have a focus indicator', () => {
391+
const radioRippleNativeElements =
392+
radioNativeElements.map(element => element.querySelector('.mat-radio-ripple')!);
393+
394+
expect(radioRippleNativeElements
395+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
396+
});
397+
390398
});
391399

392400
describe('group with ngModel', () => {

src/material/slide-toggle/slide-toggle.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ describe('MatSlideToggle without forms', () => {
357357

358358
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(0);
359359
});
360+
361+
it('should have a focus indicator', () => {
362+
const slideToggleRippleNativeElement =
363+
slideToggleElement.querySelector('.mat-slide-toggle-ripple')!;
364+
365+
expect(slideToggleRippleNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
366+
});
360367
});
361368

362369
describe('custom template', () => {

src/material/slider/slider.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ describe('MatSlider', () => {
216216
expect(event.defaultPrevented).toBe(true);
217217
});
218218

219+
it('should have a focus indicator', () => {
220+
expect(sliderNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
221+
});
222+
219223
});
220224

221225
describe('disabled slider', () => {

src/material/sort/sort.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ describe('MatSort', () => {
409409
expect(sortHeaderElement.querySelector('.mat-sort-header-arrow')).toBeTruthy();
410410
}));
411411

412+
it('should have a focus indicator', () => {
413+
const headerNativeElement =
414+
fixture.debugElement.query(By.directive(MatSortHeader))!.nativeElement;
415+
const buttonNativeElement = headerNativeElement.querySelector('.mat-sort-header-button');
416+
417+
expect(buttonNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
418+
});
419+
412420
});
413421

414422
/**

src/material/stepper/stepper.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ describe('MatStepper', () => {
394394
expect(secondStepContentEl.getAttribute('tabindex')).toBe('0');
395395
});
396396

397+
it('should have a focus indicator', () => {
398+
const stepHeaderNativeElements =
399+
[...fixture.debugElement.nativeElement.querySelectorAll('.mat-vertical-stepper-header')];
400+
401+
expect(stepHeaderNativeElements
402+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
403+
});
404+
397405
});
398406

399407
describe('basic stepper when attempting to set the selected step too early', () => {

src/material/tabs/tab-group.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ describe('MatTabGroup', () => {
291291
subscription.unsubscribe();
292292
});
293293

294+
it('should have a focus indicator', () => {
295+
const tabLabelNativeElements =
296+
[...fixture.debugElement.nativeElement.querySelectorAll('.mat-tab-label')];
297+
298+
expect(tabLabelNativeElements.every(el => el.classList.contains('mat-focus-indicator')))
299+
.toBe(true);
300+
});
294301
});
295302

296303
describe('aria labelling', () => {

src/material/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ describe('MatTabNavBar', () => {
333333
expect(fixture.componentInstance.tabLinks.toArray().every(tabLink => tabLink.rippleDisabled))
334334
.toBe(true, 'Expected every tab link to have ripples disabled');
335335
});
336+
337+
it('should have a focus indicator', () => {
338+
const tabLinkNativeElements =
339+
[...fixture.debugElement.nativeElement.querySelectorAll('.mat-tab-link')];
340+
341+
expect(tabLinkNativeElements
342+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
343+
});
336344
});
337345
});
338346

0 commit comments

Comments
 (0)