Skip to content

Commit 1f2cf1f

Browse files
committed
address feedback
1 parent b18d19d commit 1f2cf1f

File tree

8 files changed

+82
-56
lines changed

8 files changed

+82
-56
lines changed

src/cdk-experimental/testing/component-harness.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export interface LocatorFactory {
9999
* @return An asynchronous locator function that searches for elements with the given selector,
100100
* and either finds one or throws an error
101101
*/
102-
requiredLocator(selector: string): AsyncFn<TestElement>;
102+
locatorForRequired(selector: string): AsyncFn<TestElement>;
103103

104104
/**
105105
* Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
@@ -110,7 +110,7 @@ export interface LocatorFactory {
110110
* @return An asynchronous locator function that searches components matching the given harness
111111
* type, and either returns a `ComponentHarness` for the component, or throws an error.
112112
*/
113-
requiredLocator<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
113+
locatorForRequired<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
114114
AsyncFn<T>;
115115

116116
/**
@@ -122,7 +122,7 @@ export interface LocatorFactory {
122122
* @return An asynchronous locator function that searches for elements with the given selector,
123123
* and either finds one or returns null.
124124
*/
125-
optionalLocator(selector: string): AsyncFn<TestElement | null>;
125+
locatorForOptional(selector: string): AsyncFn<TestElement | null>;
126126

127127
/**
128128
* Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
@@ -133,7 +133,7 @@ export interface LocatorFactory {
133133
* @return An asynchronous locator function that searches components matching the given harness
134134
* type, and either returns a `ComponentHarness` for the component, or null if none is found.
135135
*/
136-
optionalLocator<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
136+
locatorForOptional<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
137137
AsyncFn<T | null>;
138138

139139
/**
@@ -144,7 +144,7 @@ export interface LocatorFactory {
144144
* @return An asynchronous locator function that searches for elements with the given selector,
145145
* and either finds one or throws an error
146146
*/
147-
allLocator(selector: string): AsyncFn<TestElement[]>;
147+
locatorForAll(selector: string): AsyncFn<TestElement[]>;
148148

149149
/**
150150
* Creates an asynchronous locator function that can be used to find a list of
@@ -155,7 +155,8 @@ export interface LocatorFactory {
155155
* @return An asynchronous locator function that searches components matching the given harness
156156
* type, and returns a list of `ComponentHarness`es.
157157
*/
158-
allLocator<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
158+
locatorForAll<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
159+
AsyncFn<T[]>;
159160
}
160161

161162
/**
@@ -189,7 +190,7 @@ export abstract class ComponentHarness {
189190
* @return An asynchronous locator function that searches for elements with the given selector,
190191
* and either finds one or throws an error
191192
*/
192-
protected requiredLocator(selector: string): AsyncFn<TestElement>;
193+
protected locatorForRequired(selector: string): AsyncFn<TestElement>;
193194

194195
/**
195196
* Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
@@ -200,11 +201,11 @@ export abstract class ComponentHarness {
200201
* @return An asynchronous locator function that searches components matching the given harness
201202
* type, and either returns a `ComponentHarness` for the component, or throws an error.
202203
*/
203-
protected requiredLocator<T extends ComponentHarness>(
204+
protected locatorForRequired<T extends ComponentHarness>(
204205
harnessType: ComponentHarnessConstructor<T>): AsyncFn<T>;
205206

206-
protected requiredLocator(arg: any): any {
207-
return this.locatorFacotry.requiredLocator(arg);
207+
protected locatorForRequired(arg: any): any {
208+
return this.locatorFacotry.locatorForRequired(arg);
208209
}
209210

210211
/**
@@ -216,7 +217,7 @@ export abstract class ComponentHarness {
216217
* @return An asynchronous locator function that searches for elements with the given selector,
217218
* and either finds one or returns null.
218219
*/
219-
protected optionalLocator(selector: string): AsyncFn<TestElement | null>;
220+
protected locatorForOptional(selector: string): AsyncFn<TestElement | null>;
220221

221222
/**
222223
* Creates an asynchronous locator function that can be used to find a `ComponentHarness` for a
@@ -227,11 +228,11 @@ export abstract class ComponentHarness {
227228
* @return An asynchronous locator function that searches components matching the given harness
228229
* type, and either returns a `ComponentHarness` for the component, or null if none is found.
229230
*/
230-
protected optionalLocator<T extends ComponentHarness>(
231+
protected locatorForOptional<T extends ComponentHarness>(
231232
harnessType: ComponentHarnessConstructor<T>): AsyncFn<T | null>;
232233

233-
protected optionalLocator(arg: any): any {
234-
return this.locatorFacotry.optionalLocator(arg);
234+
protected locatorForOptional(arg: any): any {
235+
return this.locatorFacotry.locatorForOptional(arg);
235236
}
236237

237238
/**
@@ -242,7 +243,7 @@ export abstract class ComponentHarness {
242243
* @return An asynchronous locator function that searches for elements with the given selector,
243244
* and either finds one or throws an error
244245
*/
245-
protected allLocator(selector: string): AsyncFn<TestElement[]>;
246+
protected locatorForAll(selector: string): AsyncFn<TestElement[]>;
246247

247248
/**
248249
* Creates an asynchronous locator function that can be used to find a list of
@@ -253,11 +254,11 @@ export abstract class ComponentHarness {
253254
* @return An asynchronous locator function that searches components matching the given harness
254255
* type, and returns a list of `ComponentHarness`es.
255256
*/
256-
protected allLocator<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
257+
protected locatorForAll<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
257258
AsyncFn<T[]>;
258259

259-
protected allLocator(arg: any): any {
260-
return this.locatorFacotry.allLocator(arg);
260+
protected locatorForAll(arg: any): any {
261+
return this.locatorFacotry.locatorForAll(arg);
261262
}
262263
}
263264

src/cdk-experimental/testing/harness-environment.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
3333
}
3434

3535
// Implemented as part of the `LocatorFactory` interface.
36-
requiredLocator(selector: string): AsyncFn<TestElement>;
37-
requiredLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T>;
38-
requiredLocator<T extends ComponentHarness>(
36+
locatorForRequired(selector: string): AsyncFn<TestElement>;
37+
locatorForRequired<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
38+
AsyncFn<T>;
39+
locatorForRequired<T extends ComponentHarness>(
3940
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T> {
4041
return async () => {
4142
if (typeof arg === 'string') {
@@ -55,10 +56,10 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
5556
}
5657

5758
// Implemented as part of the `LocatorFactory` interface.
58-
optionalLocator(selector: string): AsyncFn<TestElement | null>;
59-
optionalLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
59+
locatorForOptional(selector: string): AsyncFn<TestElement | null>;
60+
locatorForOptional<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
6061
AsyncFn<T | null>;
61-
optionalLocator<T extends ComponentHarness>(
62+
locatorForOptional<T extends ComponentHarness>(
6263
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T | null> {
6364
return async () => {
6465
if (typeof arg === 'string') {
@@ -72,9 +73,9 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
7273
}
7374

7475
// Implemented as part of the `LocatorFactory` interface.
75-
allLocator(selector: string): AsyncFn<TestElement[]>;
76-
allLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
77-
allLocator<T extends ComponentHarness>(
76+
locatorForAll(selector: string): AsyncFn<TestElement[]>;
77+
locatorForAll<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
78+
locatorForAll<T extends ComponentHarness>(
7879
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement[] | T[]> {
7980
return async () => {
8081
if (typeof arg === 'string') {
@@ -88,18 +89,18 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
8889

8990
// Implemented as part of the `HarnessLoader` interface.
9091
requiredHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T> {
91-
return this.requiredLocator(harness)();
92+
return this.locatorForRequired(harness)();
9293
}
9394

9495
// Implemented as part of the `HarnessLoader` interface.
9596
optionalHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
9697
Promise<T | null> {
97-
return this.optionalLocator(harness)();
98+
return this.locatorForOptional(harness)();
9899
}
99100

100101
// Implemented as part of the `HarnessLoader` interface.
101102
allHarnesses<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T[]> {
102-
return this.allLocator(harness)();
103+
return this.locatorForAll(harness)();
103104
}
104105

105106
// Implemented as part of the `HarnessLoader` interface.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ProtractorElement implements TestElement {
1414
constructor(readonly element: ElementFinder) {}
1515

1616
async blur(): Promise<void> {
17-
return this.element['blur']();
17+
return browser.executeScript('arguments[0].blur()', this.element);
1818
}
1919

2020
async clear(): Promise<void> {
@@ -26,7 +26,7 @@ export class ProtractorElement implements TestElement {
2626
}
2727

2828
async focus(): Promise<void> {
29-
return this.element['focus']();
29+
return browser.executeScript('arguments[0].focus()', this.element);
3030
}
3131

3232
async getCssValue(property: string): Promise<string> {

src/cdk-experimental/testing/testbed/testbed-harness-environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
7777

7878
protected async getAllRawElements(selector: string): Promise<Element[]> {
7979
await this._stabilize();
80-
return Array.prototype.slice.call(this.rawRootElement.querySelectorAll(selector));
80+
return Array.from(this.rawRootElement.querySelectorAll(selector));
8181
}
8282
}

src/cdk-experimental/testing/tests/harnesses/main-component-harness.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@ export class WrongComponentHarness extends ComponentHarness {
1717
export class MainComponentHarness extends ComponentHarness {
1818
static readonly hostSelector = 'test-main';
1919

20-
readonly title = this.requiredLocator('h1');
21-
readonly asyncCounter = this.requiredLocator('#asyncCounter');
22-
readonly counter = this.requiredLocator('#counter');
23-
readonly input = this.requiredLocator('#input');
24-
readonly value = this.requiredLocator('#value');
25-
readonly allLabels = this.allLocator('label');
26-
readonly allLists = this.allLocator(SubComponentHarness);
27-
readonly memo = this.requiredLocator('textarea');
20+
readonly title = this.locatorForRequired('h1');
21+
readonly button = this.locatorForRequired('button');
22+
readonly asyncCounter = this.locatorForRequired('#asyncCounter');
23+
readonly counter = this.locatorForRequired('#counter');
24+
readonly input = this.locatorForRequired('#input');
25+
readonly value = this.locatorForRequired('#value');
26+
readonly allLabels = this.locatorForAll('label');
27+
readonly allLists = this.locatorForAll(SubComponentHarness);
28+
readonly memo = this.locatorForRequired('textarea');
2829
// Allow null for element
29-
readonly nullItem = this.optionalLocator('wrong locator');
30+
readonly nullItem = this.locatorForOptional('wrong locator');
3031
// Allow null for component harness
31-
readonly nullComponentHarness = this.optionalLocator(WrongComponentHarness);
32-
readonly errorItem = this.requiredLocator('wrong locator');
32+
readonly nullComponentHarness = this.locatorForOptional(WrongComponentHarness);
33+
readonly errorItem = this.locatorForRequired('wrong locator');
3334

34-
readonly globalEl = this.documentRootLocatorFactory().requiredLocator('.sibling');
35-
readonly errorGlobalEl = this.documentRootLocatorFactory().requiredLocator('wrong locator');
36-
readonly nullGlobalEl = this.documentRootLocatorFactory().optionalLocator('wrong locator');
35+
readonly globalEl = this.documentRootLocatorFactory().locatorForRequired('.sibling');
36+
readonly errorGlobalEl = this.documentRootLocatorFactory().locatorForRequired('wrong locator');
37+
readonly nullGlobalEl = this.documentRootLocatorFactory().locatorForOptional('wrong locator');
3738

38-
readonly optionalDiv = this.optionalLocator('div');
39-
readonly optionalSubComponent = this.optionalLocator(SubComponentHarness);
40-
readonly errorSubComponent = this.requiredLocator(WrongComponentHarness);
39+
readonly optionalDiv = this.locatorForOptional('div');
40+
readonly optionalSubComponent = this.locatorForOptional(SubComponentHarness);
41+
readonly errorSubComponent = this.locatorForRequired(WrongComponentHarness);
4142

42-
private _button = this.requiredLocator('button');
43-
private _testTools = this.requiredLocator(SubComponentHarness);
43+
private _testTools = this.locatorForRequired(SubComponentHarness);
4444

4545
async increaseCounter(times: number) {
46-
const button = await this._button();
46+
const button = await this.button();
4747
for (let i = 0; i < times; i++) {
4848
await button.click();
4949
}

src/cdk-experimental/testing/tests/harnesses/sub-component-harness.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {TestElement} from '../../test-element';
1212
export class SubComponentHarness extends ComponentHarness {
1313
static readonly hostSelector = 'test-sub';
1414

15-
readonly title = this.requiredLocator('h2');
16-
readonly getItems = this.allLocator('li');
17-
readonly globalElement = this.documentRootLocatorFactory().requiredLocator('#username');
15+
readonly title = this.locatorForRequired('h2');
16+
readonly getItems = this.locatorForAll('li');
17+
readonly globalElement = this.documentRootLocatorFactory().locatorForRequired('#username');
1818

1919
async getItem(index: number): Promise<TestElement> {
2020
const items = await this.getItems();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,5 +232,16 @@ describe('ProtractorHarnessEnvironment', () => {
232232
const title = await harness.title();
233233
expect(await title.getCssValue('height')).toBe('50px');
234234
});
235+
236+
it('should focus and blur element', async () => {
237+
let button = await harness.button();
238+
expect(await (await browser.switchTo().activeElement()).getText())
239+
.not.toBe(await button.text());
240+
await button.focus();
241+
expect(await (await browser.switchTo().activeElement()).getText()).toBe(await button.text());
242+
await button.blur();
243+
expect(await (await browser.switchTo().activeElement()).getText())
244+
.not.toBe(await button.text());
245+
});
235246
});
236247
});

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {SubComponentHarness} from './harnesses/sub-component-harness';
66
import {TestComponentsModule} from './test-components-module';
77
import {TestMainComponent} from './test-main-component';
88

9+
function activeElementText() {
10+
return document.activeElement && (document.activeElement as HTMLElement).innerText || '';
11+
}
12+
913
describe('TestbedHarnessEnvironment', () => {
1014
let fixture: ComponentFixture<{}>;
1115

@@ -247,5 +251,14 @@ describe('TestbedHarnessEnvironment', () => {
247251
const title = await harness.title();
248252
expect(await title.getCssValue('height')).toBe('50px');
249253
});
254+
255+
it('should focus and blur element', async () => {
256+
let button = await harness.button();
257+
expect(activeElementText()).not.toBe(await button.text());
258+
await button.focus();
259+
expect(activeElementText()).toBe(await button.text());
260+
await button.blur();
261+
expect(activeElementText()).not.toBe(await button.text());
262+
});
250263
});
251264
});

0 commit comments

Comments
 (0)