Skip to content

Commit e704996

Browse files
mmalerbajelbourn
authored andcommitted
feat(cdk/testing): add isFocused method to TestElement (#18183)
1 parent 321d3f9 commit e704996

File tree

9 files changed

+32
-0
lines changed

9 files changed

+32
-0
lines changed

src/cdk/testing/protractor/protractor-element.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,8 @@ export class ProtractorElement implements TestElement {
146146
Element.prototype.msMatchesSelector).call(arguments[0], arguments[1])
147147
`, this.element, selector);
148148
}
149+
150+
async isFocused(): Promise<boolean> {
151+
return this.element.equals(browser.driver.switchTo().activeElement());
152+
}
149153
}

src/cdk/testing/test-element.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ export interface TestElement {
111111

112112
/** Checks whether this element matches the given selector. */
113113
matchesSelector(selector: string): Promise<boolean>;
114+
115+
/** Checks whether the element is focused. */
116+
isFocused(): Promise<boolean>;
114117
}

src/cdk/testing/testbed/unit-test-element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,9 @@ export class UnitTestElement implements TestElement {
142142
return (elementPrototype['matches'] || elementPrototype['msMatchesSelector'])
143143
.call(this.element, selector);
144144
}
145+
146+
async isFocused(): Promise<boolean> {
147+
await this._stabilize();
148+
return document.activeElement === this.element;
149+
}
145150
}

src/cdk/testing/tests/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ng_test_library(
3939
"//src/cdk/testing",
4040
"//src/cdk/testing/private",
4141
"//src/cdk/testing/testbed",
42+
"@npm//@angular/platform-browser",
4243
],
4344
)
4445

src/cdk/testing/tests/protractor.e2e.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ describe('ProtractorHarnessEnvironment', () => {
398398
'\n(SubComponentHarness with host element matching selector: "test-sub" satisfying' +
399399
' the constraints: title = /not found/)');
400400
});
401+
402+
it('should check if element is focused', async () => {
403+
const button = await harness.button();
404+
await button.focus();
405+
expect(await button.isFocused()).toBe(true);
406+
await button.blur();
407+
expect(await button.isFocused()).toBe(false);
408+
});
401409
});
402410

403411
describe('HarnessPredicate', () => {

src/cdk/testing/tests/testbed.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ describe('TestbedHarnessEnvironment', () => {
441441
'\n(SubComponentHarness with host element matching selector: "test-sub" satisfying' +
442442
' the constraints: title = /not found/)');
443443
});
444+
445+
it('should check if element is focused', async () => {
446+
const button = await harness.button();
447+
await button.focus();
448+
expect(await button.isFocused()).toBe(true);
449+
await button.blur();
450+
expect(await button.isFocused()).toBe(false);
451+
});
444452
});
445453

446454
describe('HarnessPredicate', () => {

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export interface TestElement {
115115
getProperty(name: string): Promise<any>;
116116
hasClass(name: string): Promise<boolean>;
117117
hover(): Promise<void>;
118+
isFocused(): Promise<boolean>;
118119
matchesSelector(selector: string): Promise<boolean>;
119120
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
120121
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;

tools/public_api_guard/cdk/testing/protractor.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export declare class ProtractorElement implements TestElement {
1111
getProperty(name: string): Promise<any>;
1212
hasClass(name: string): Promise<boolean>;
1313
hover(): Promise<void>;
14+
isFocused(): Promise<boolean>;
1415
matchesSelector(selector: string): Promise<boolean>;
1516
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
1617
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;

tools/public_api_guard/cdk/testing/testbed.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export declare class UnitTestElement implements TestElement {
2424
getProperty(name: string): Promise<any>;
2525
hasClass(name: string): Promise<boolean>;
2626
hover(): Promise<void>;
27+
isFocused(): Promise<boolean>;
2728
matchesSelector(selector: string): Promise<boolean>;
2829
sendKeys(...keys: (string | TestKey)[]): Promise<void>;
2930
sendKeys(modifiers: ModifierKeys, ...keys: (string | TestKey)[]): Promise<void>;

0 commit comments

Comments
 (0)