Skip to content

Commit f724e4d

Browse files
zelliottjelbourn
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 172ee1d commit f724e4d

File tree

18 files changed

+116
-1
lines changed

18 files changed

+116
-1
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
@@ -391,6 +391,10 @@ describe('MatChip', () => {
391391
});
392392

393393
});
394+
395+
it('should have a focus indicator', () => {
396+
expect(chipNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
397+
});
394398
});
395399
});
396400

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
@@ -404,6 +404,14 @@ describe('MatExpansionPanel', () => {
404404
});
405405

406406
});
407+
408+
it('should have a focus indicator', () => {
409+
const fixture = TestBed.createComponent(PanelWithContent);
410+
const headerNativeElement =
411+
fixture.debugElement.query(By.directive(MatExpansionPanelHeader))!.nativeElement;
412+
413+
expect(headerNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
414+
});
407415
});
408416

409417

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
@@ -650,6 +650,13 @@ describe('MatSelectionList without forms', () => {
650650
expect(option.classList).toContain('mat-2-line');
651651
});
652652

653+
it('should have a focus indicator', () => {
654+
const optionNativeElements = listOptions.map(option => option.nativeElement);
655+
656+
expect(optionNativeElements
657+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
658+
});
659+
653660
});
654661

655662
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
@@ -2116,6 +2116,17 @@ describe('MatMenu', () => {
21162116

21172117
});
21182118

2119+
it('should have a focus indicator', () => {
2120+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
2121+
fixture.detectChanges();
2122+
fixture.componentInstance.trigger.openMenu();
2123+
fixture.detectChanges();
2124+
const menuItemNativeElements =
2125+
Array.from(overlayContainerElement.querySelectorAll('.mat-menu-item'));
2126+
2127+
expect(menuItemNativeElements
2128+
.every(element => element.classList.contains('mat-focus-indicator'))).toBe(true);
2129+
});
21192130
});
21202131

21212132
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
@@ -342,6 +342,13 @@ describe('MatSlideToggle without forms', () => {
342342

343343
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(0);
344344
});
345+
346+
it('should have a focus indicator', () => {
347+
const slideToggleRippleNativeElement =
348+
slideToggleElement.querySelector('.mat-slide-toggle-ripple')!;
349+
350+
expect(slideToggleRippleNativeElement.classList.contains('mat-focus-indicator')).toBe(true);
351+
});
345352
});
346353

347354
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

src/material/tree/tree.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ $mat-node-height: 48px;
99
align-items: center;
1010
min-height: $mat-node-height;
1111
flex: 1;
12-
overflow: hidden;
1312
word-wrap: break-word;
1413
}
1514

0 commit comments

Comments
 (0)