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', () => {

0 commit comments

Comments
 (0)