Skip to content

Commit 081d833

Browse files
authored
feat(material/testing): expose whether harness elements are focused (#19704)
Currently we have a bunch of test harnesses that have methods for blurring and focusing the underlying element, but they don't expose whether it currently has focus. These changes add `isFocused` methods to all of the harnesses so they're consistent between each other. Fixes #19702.
1 parent 8ea3558 commit 081d833

41 files changed

Lines changed: 162 additions & 80 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/material-experimental/mdc-button/testing/button-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ export class MatButtonHarness extends ComponentHarness {
6262
async blur(): Promise<void> {
6363
return (await this.host()).blur();
6464
}
65+
66+
/** Whether the button is focused. */
67+
async isFocused(): Promise<boolean> {
68+
return (await this.host()).isFocused();
69+
}
6570
}

src/material-experimental/mdc-checkbox/testing/checkbox-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ export class MatCheckboxHarness extends ComponentHarness {
102102
return (await this._input()).blur();
103103
}
104104

105+
/** Whether the checkbox is focused. */
106+
async isFocused(): Promise<boolean> {
107+
return (await this._input()).isFocused();
108+
}
109+
105110
/**
106111
* Toggle the checked state of the checkbox and returns a void promise that indicates when the
107112
* action is complete.

src/material-experimental/mdc-menu/testing/menu-harness.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export class MatMenuHarness extends ComponentHarness {
5656
return (await this.host()).blur();
5757
}
5858

59+
/** Whether the menu is focused. */
60+
async isFocused(): Promise<boolean> {
61+
return (await this.host()).isFocused();
62+
}
63+
5964
async open(): Promise<void> {
6065
throw Error('not implemented');
6166
}
@@ -105,16 +110,23 @@ export class MatMenuItemHarness extends ComponentHarness {
105110
return (await this.host()).text();
106111
}
107112

108-
/** Focuses the menu and returns a void promise that indicates when the action is complete. */
113+
/**
114+
* Focuses the menu item and returns a void promise that indicates when the action is complete.
115+
*/
109116
async focus(): Promise<void> {
110117
return (await this.host()).focus();
111118
}
112119

113-
/** Blurs the menu and returns a void promise that indicates when the action is complete. */
120+
/** Blurs the menu item and returns a void promise that indicates when the action is complete. */
114121
async blur(): Promise<void> {
115122
return (await this.host()).blur();
116123
}
117124

125+
/** Whether the menu item is focused. */
126+
async isFocused(): Promise<boolean> {
127+
return (await this.host()).isFocused();
128+
}
129+
118130
async click(): Promise<void> {
119131
throw Error('not implemented');
120132
}

src/material-experimental/mdc-slide-toggle/testing/slide-toggle-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export class MatSlideToggleHarness extends ComponentHarness {
9090
return (await this._input()).blur();
9191
}
9292

93+
/** Whether the slide-toggle is focused. */
94+
async isFocused(): Promise<boolean> {
95+
return (await this._input()).isFocused();
96+
}
97+
9398
/**
9499
* Toggle the checked state of the slide-toggle and returns a void promise that indicates when the
95100
* action is complete.

src/material-experimental/mdc-slider/testing/slider-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ export class MatSliderHarness extends ComponentHarness {
126126
return (await this.host()).blur();
127127
}
128128

129+
/** Whether the slider is focused. */
130+
async isFocused(): Promise<boolean> {
131+
return (await this.host()).isFocused();
132+
}
133+
129134
/** Calculates the percentage of the given value. */
130135
private async _calculatePercentage(value: number) {
131136
const [min, max] = await Promise.all([this.getMinValue(), this.getMaxValue()]);

src/material/autocomplete/testing/autocomplete-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export class MatAutocompleteHarness extends ComponentHarness {
5656
return (await this.host()).blur();
5757
}
5858

59+
/** Whether the autocomplete input is focused. */
60+
async isFocused(): Promise<boolean> {
61+
return (await this.host()).isFocused();
62+
}
63+
5964
/** Enters text into the autocomplete. */
6065
async enterText(value: string): Promise<void> {
6166
return (await this.host()).sendKeys(value);

src/material/autocomplete/testing/shared.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ export function runHarnessTests(
6363

6464
it('should focus and blur an input', async () => {
6565
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
66-
expect(getActiveElementId()).not.toBe('plain');
66+
expect(await input.isFocused()).toBe(false);
6767
await input.focus();
68-
expect(getActiveElementId()).toBe('plain');
68+
expect(await input.isFocused()).toBe(true);
6969
await input.blur();
70-
expect(getActiveElementId()).not.toBe('plain');
70+
expect(await input.isFocused()).toBe(false);
7171
});
7272

7373
it('should be able to type in an input', async () => {
@@ -149,10 +149,6 @@ export function runHarnessTests(
149149
});
150150
}
151151

152-
function getActiveElementId() {
153-
return document.activeElement ? document.activeElement.id : '';
154-
}
155-
156152
@Component({
157153
template: `
158154
<mat-autocomplete #autocomplete="matAutocomplete">

src/material/button-toggle/testing/button-toggle-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export class MatButtonToggleHarness extends ComponentHarness {
8585
return (await this._button()).blur();
8686
}
8787

88+
/** Whether the toggle is focused. */
89+
async isFocused(): Promise<boolean> {
90+
return (await this._button()).isFocused();
91+
}
92+
8893
/** Toggle the checked state of the buttons toggle. */
8994
async toggle(): Promise<void> {
9095
return (await this._button()).click();

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ export function runHarnessTests(
8181

8282
it('should focus the button toggle', async () => {
8383
const toggle = await loader.getHarness(buttonToggleHarness.with({text: 'First'}));
84-
expect(getActiveElementTagName()).not.toBe('button');
84+
expect(await toggle.isFocused()).toBe(false);
8585
await toggle.focus();
86-
expect(getActiveElementTagName()).toBe('button');
86+
expect(await toggle.isFocused()).toBe(true);
8787
});
8888

8989
it('should blur the button toggle', async () => {
9090
const toggle = await loader.getHarness(buttonToggleHarness.with({text: 'First'}));
9191
await toggle.focus();
92-
expect(getActiveElementTagName()).toBe('button');
92+
expect(await toggle.isFocused()).toBe(true);
9393
await toggle.blur();
94-
expect(getActiveElementTagName()).not.toBe('button');
94+
expect(await toggle.isFocused()).toBe(false);
9595
});
9696

9797
it('should toggle the button value', async () => {
@@ -122,10 +122,6 @@ export function runHarnessTests(
122122
});
123123
}
124124

125-
function getActiveElementTagName() {
126-
return document.activeElement ? document.activeElement.tagName.toLowerCase() : '';
127-
}
128-
129125
@Component({
130126
template: `
131127
<mat-button-toggle

src/material/button/testing/button-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ export class MatButtonHarness extends ComponentHarness {
6262
async blur(): Promise<void> {
6363
return (await this.host()).blur();
6464
}
65+
66+
/** Whether the button is focused. */
67+
async isFocused(): Promise<boolean> {
68+
return (await this.host()).isFocused();
69+
}
6570
}

0 commit comments

Comments
 (0)