Skip to content

Commit bbd6368

Browse files
committed
split up files
1 parent ab02d1b commit bbd6368

File tree

12 files changed

+379
-327
lines changed

12 files changed

+379
-327
lines changed

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

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -33,109 +33,6 @@ export interface LocatorFactory {
3333
allLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
3434
}
3535

36-
/** Interface that is used to find elements in the DOM and create harnesses for them. */
37-
export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
38-
protected constructor(protected rawRootElement: E) {}
39-
40-
abstract documentRootLocatorFactory(): LocatorFactory;
41-
42-
protected abstract createTestElement(element: E): TestElement;
43-
44-
protected abstract createComponentHarness<T extends ComponentHarness>(
45-
harnessType: ComponentHarnessConstructor<T>, element: E): T;
46-
47-
protected abstract createHarnessLoader(element: E): HarnessLoader;
48-
49-
protected abstract getRawElement(selector: string): Promise<E | null>;
50-
51-
protected abstract getAllRawElements(selector: string): Promise<E[]>;
52-
53-
rootElement(): TestElement {
54-
return this.createTestElement(this.rawRootElement);
55-
}
56-
57-
requiredLocator(selector: string): AsyncFn<TestElement>;
58-
requiredLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T>;
59-
requiredLocator<T extends ComponentHarness>(
60-
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T> {
61-
return async () => {
62-
if (typeof arg === 'string') {
63-
const element = await this.getRawElement(arg);
64-
if (element) {
65-
return this.createTestElement(element);
66-
}
67-
} else {
68-
const element = await this.getRawElement(arg.hostSelector);
69-
if (element) {
70-
return this.createComponentHarness(arg, element);
71-
}
72-
}
73-
const selector = typeof arg === 'string' ? arg : arg.hostSelector;
74-
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
75-
};
76-
}
77-
78-
optionalLocator(selector: string): AsyncFn<TestElement | null>;
79-
optionalLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
80-
AsyncFn<T | null>;
81-
optionalLocator<T extends ComponentHarness>(
82-
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T | null> {
83-
return async () => {
84-
if (typeof arg === 'string') {
85-
const element = await this.getRawElement(arg);
86-
return element ? this.createTestElement(element) : null;
87-
} else {
88-
const element = await this.getRawElement(arg.hostSelector);
89-
return element ? this.createComponentHarness(arg, element) : null;
90-
}
91-
};
92-
}
93-
94-
allLocator(selector: string): AsyncFn<TestElement[]>;
95-
allLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
96-
allLocator<T extends ComponentHarness>(
97-
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement[] | T[]> {
98-
return async () => {
99-
if (typeof arg === 'string') {
100-
return (await this.getAllRawElements(arg)).map(e => this.createTestElement(e));
101-
} else {
102-
return (await this.getAllRawElements(arg.hostSelector))
103-
.map(e => this.createComponentHarness(arg, e));
104-
}
105-
};
106-
}
107-
108-
requiredHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T> {
109-
return this.requiredLocator(harness)();
110-
}
111-
112-
optionalHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
113-
Promise<T | null> {
114-
return this.optionalLocator(harness)();
115-
}
116-
117-
allHarnesses<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T[]> {
118-
return this.allLocator(harness)();
119-
}
120-
121-
async findRequired(selector: string): Promise<HarnessLoader> {
122-
const element = await this.getRawElement(selector);
123-
if (element) {
124-
return this.createHarnessLoader(element);
125-
}
126-
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
127-
}
128-
129-
async findOptional(selector: string): Promise<HarnessLoader | null> {
130-
const element = await this.getRawElement(selector);
131-
return element ? this.createHarnessLoader(element) : null;
132-
}
133-
134-
async findAll(selector: string): Promise<HarnessLoader[]> {
135-
return (await this.getAllRawElements(selector)).map(e => this.createHarnessLoader(e));
136-
}
137-
}
138-
13936
/**
14037
* Base Component Harness
14138
* This base component harness provides the basic ability to locate element and
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {
10+
AsyncFn,
11+
ComponentHarness,
12+
ComponentHarnessConstructor,
13+
HarnessLoader,
14+
LocatorFactory
15+
} from './component-harness';
16+
import {TestElement} from './test-element';
17+
18+
/** Interface that is used to find elements in the DOM and create harnesses for them. */
19+
export abstract class AbstractHarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
20+
protected constructor(protected rawRootElement: E) {}
21+
22+
abstract documentRootLocatorFactory(): LocatorFactory;
23+
24+
rootElement(): TestElement {
25+
return this.createTestElement(this.rawRootElement);
26+
}
27+
28+
requiredLocator(selector: string): AsyncFn<TestElement>;
29+
requiredLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T>;
30+
requiredLocator<T extends ComponentHarness>(
31+
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T> {
32+
return async () => {
33+
if (typeof arg === 'string') {
34+
const element = await this.getRawElement(arg);
35+
if (element) {
36+
return this.createTestElement(element);
37+
}
38+
} else {
39+
const element = await this.getRawElement(arg.hostSelector);
40+
if (element) {
41+
return this.createComponentHarness(arg, element);
42+
}
43+
}
44+
const selector = typeof arg === 'string' ? arg : arg.hostSelector;
45+
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
46+
};
47+
}
48+
49+
optionalLocator(selector: string): AsyncFn<TestElement | null>;
50+
optionalLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
51+
AsyncFn<T | null>;
52+
optionalLocator<T extends ComponentHarness>(
53+
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T | null> {
54+
return async () => {
55+
if (typeof arg === 'string') {
56+
const element = await this.getRawElement(arg);
57+
return element ? this.createTestElement(element) : null;
58+
} else {
59+
const element = await this.getRawElement(arg.hostSelector);
60+
return element ? this.createComponentHarness(arg, element) : null;
61+
}
62+
};
63+
}
64+
65+
allLocator(selector: string): AsyncFn<TestElement[]>;
66+
allLocator<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
67+
allLocator<T extends ComponentHarness>(
68+
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement[] | T[]> {
69+
return async () => {
70+
if (typeof arg === 'string') {
71+
return (await this.getAllRawElements(arg)).map(e => this.createTestElement(e));
72+
} else {
73+
return (await this.getAllRawElements(arg.hostSelector))
74+
.map(e => this.createComponentHarness(arg, e));
75+
}
76+
};
77+
}
78+
79+
requiredHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T> {
80+
return this.requiredLocator(harness)();
81+
}
82+
83+
optionalHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
84+
Promise<T | null> {
85+
return this.optionalLocator(harness)();
86+
}
87+
88+
allHarnesses<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T[]> {
89+
return this.allLocator(harness)();
90+
}
91+
92+
async findRequired(selector: string): Promise<HarnessLoader> {
93+
const element = await this.getRawElement(selector);
94+
if (element) {
95+
return this.createHarnessLoader(element);
96+
}
97+
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
98+
}
99+
100+
async findOptional(selector: string): Promise<HarnessLoader | null> {
101+
const element = await this.getRawElement(selector);
102+
return element ? this.createHarnessLoader(element) : null;
103+
}
104+
105+
async findAll(selector: string): Promise<HarnessLoader[]> {
106+
return (await this.getAllRawElements(selector)).map(e => this.createHarnessLoader(e));
107+
}
108+
109+
protected abstract createTestElement(element: E): TestElement;
110+
111+
protected abstract createComponentHarness<T extends ComponentHarness>(
112+
harnessType: ComponentHarnessConstructor<T>, element: E): T;
113+
114+
protected abstract createHarnessLoader(element: E): HarnessLoader;
115+
116+
protected abstract getRawElement(selector: string): Promise<E | null>;
117+
118+
protected abstract getAllRawElements(selector: string): Promise<E[]>;
119+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './protractor-element';
10+
export * from './protractor-harness-environment';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {browser, ElementFinder} from 'protractor';
10+
import {TestElement} from '../test-element';
11+
12+
export class ProtractorElement implements TestElement {
13+
constructor(readonly element: ElementFinder) {}
14+
15+
async blur(): Promise<void> {
16+
return this.element['blur']();
17+
}
18+
19+
async clear(): Promise<void> {
20+
return this.element.clear();
21+
}
22+
23+
async click(): Promise<void> {
24+
return this.element.click();
25+
}
26+
27+
async focus(): Promise<void> {
28+
return this.element['focus']();
29+
}
30+
31+
async getCssValue(property: string): Promise<string> {
32+
return this.element.getCssValue(property);
33+
}
34+
35+
async hover(): Promise<void> {
36+
return browser.actions()
37+
.mouseMove(await this.element.getWebElement())
38+
.perform();
39+
}
40+
41+
async sendKeys(keys: string): Promise<void> {
42+
return this.element.sendKeys(keys);
43+
}
44+
45+
async text(): Promise<string> {
46+
return this.element.getText();
47+
}
48+
49+
async getAttribute(name: string): Promise<string|null> {
50+
return this.element.getAttribute(name);
51+
}
52+
}

src/cdk-experimental/testing/protractor.ts renamed to src/cdk-experimental/testing/protractor/protractor-harness-environment.ts

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {browser, by, element as protractorElement, ElementFinder} from 'protractor';
10-
9+
import {by, element as protractorElement, ElementFinder} from 'protractor';
1110
import {
12-
AbstractHarnessEnvironment,
1311
ComponentHarness,
1412
ComponentHarnessConstructor,
1513
HarnessLoader,
1614
LocatorFactory,
17-
} from './component-harness';
18-
import {TestElement} from './test-element';
15+
} from '../component-harness';
16+
import {AbstractHarnessEnvironment} from '../harness-environment';
17+
import {TestElement} from '../test-element';
18+
import {ProtractorElement} from './protractor-element';
1919

2020
export class ProtractorHarnessEnvironment extends AbstractHarnessEnvironment<ElementFinder> {
2121
protected constructor(rawRootElement: ElementFinder) {
@@ -54,45 +54,3 @@ export class ProtractorHarnessEnvironment extends AbstractHarnessEnvironment<Ele
5454
(result: ElementFinder[], el: ElementFinder) => el ? result.concat([el]) : result, []);
5555
}
5656
}
57-
58-
class ProtractorElement implements TestElement {
59-
constructor(readonly element: ElementFinder) {}
60-
61-
async blur(): Promise<void> {
62-
return this.element['blur']();
63-
}
64-
65-
async clear(): Promise<void> {
66-
return this.element.clear();
67-
}
68-
69-
async click(): Promise<void> {
70-
return this.element.click();
71-
}
72-
73-
async focus(): Promise<void> {
74-
return this.element['focus']();
75-
}
76-
77-
async getCssValue(property: string): Promise<string> {
78-
return this.element.getCssValue(property);
79-
}
80-
81-
async hover(): Promise<void> {
82-
return browser.actions()
83-
.mouseMove(await this.element.getWebElement())
84-
.perform();
85-
}
86-
87-
async sendKeys(keys: string): Promise<void> {
88-
return this.element.sendKeys(keys);
89-
}
90-
91-
async text(): Promise<string> {
92-
return this.element.getText();
93-
}
94-
95-
async getAttribute(name: string): Promise<string|null> {
96-
return this.element.getAttribute(name);
97-
}
98-
}

src/cdk-experimental/testing/public-api.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as protractor from './protractor';
10-
import * as testbed from './testbed';
11-
129
export * from './component-harness';
10+
export * from './harness-environment';
11+
export * from './protractor';
1312
export * from './test-element';
14-
export {protractor, testbed};
13+
export * from './testbed';

0 commit comments

Comments
 (0)