Skip to content

Commit 575f013

Browse files
committed
nits
1 parent 1f2cf1f commit 575f013

File tree

4 files changed

+48
-65
lines changed

4 files changed

+48
-65
lines changed

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import {TestElement} from './test-element';
2424
export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
2525
protected constructor(protected rawRootElement: E) {}
2626

27-
// Part of the `HarnessLoader` interface, delegated to concrete implementation.
28-
abstract documentRootLocatorFactory(): LocatorFactory;
27+
// Implemented as part of the `LocatorFactory` interface.
28+
documentRootLocatorFactory(): LocatorFactory {
29+
return this.createEnvironment(this.getDocumentRoot());
30+
}
2931

3032
// Implemented as part of the `LocatorFactory` interface.
3133
rootElement(): TestElement {
@@ -34,7 +36,7 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
3436

3537
// Implemented as part of the `LocatorFactory` interface.
3638
locatorForRequired(selector: string): AsyncFn<TestElement>;
37-
locatorForRequired<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
39+
locatorForRequired<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
3840
AsyncFn<T>;
3941
locatorForRequired<T extends ComponentHarness>(
4042
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T> {
@@ -57,7 +59,7 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
5759

5860
// Implemented as part of the `LocatorFactory` interface.
5961
locatorForOptional(selector: string): AsyncFn<TestElement | null>;
60-
locatorForOptional<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
62+
locatorForOptional<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
6163
AsyncFn<T | null>;
6264
locatorForOptional<T extends ComponentHarness>(
6365
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement | T | null> {
@@ -74,7 +76,8 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
7476

7577
// Implemented as part of the `LocatorFactory` interface.
7678
locatorForAll(selector: string): AsyncFn<TestElement[]>;
77-
locatorForAll<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): AsyncFn<T[]>;
79+
locatorForAll<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
80+
AsyncFn<T[]>;
7881
locatorForAll<T extends ComponentHarness>(
7982
arg: string | ComponentHarnessConstructor<T>): AsyncFn<TestElement[] | T[]> {
8083
return async () => {
@@ -88,50 +91,57 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
8891
}
8992

9093
// Implemented as part of the `HarnessLoader` interface.
91-
requiredHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T> {
92-
return this.locatorForRequired(harness)();
94+
requiredHarness<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
95+
Promise<T> {
96+
return this.locatorForRequired(harnessType)();
9397
}
9498

9599
// Implemented as part of the `HarnessLoader` interface.
96-
optionalHarness<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>):
97-
Promise<T | null> {
98-
return this.locatorForOptional(harness)();
100+
optionalHarness<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
101+
Promise<T | null> {
102+
return this.locatorForOptional(harnessType)();
99103
}
100104

101105
// Implemented as part of the `HarnessLoader` interface.
102-
allHarnesses<T extends ComponentHarness>(harness: ComponentHarnessConstructor<T>): Promise<T[]> {
103-
return this.locatorForAll(harness)();
106+
allHarnesses<T extends ComponentHarness>(harnessType: ComponentHarnessConstructor<T>):
107+
Promise<T[]> {
108+
return this.locatorForAll(harnessType)();
104109
}
105110

106111
// Implemented as part of the `HarnessLoader` interface.
107112
async findRequired(selector: string): Promise<HarnessLoader> {
108113
const element = await this.getRawElement(selector);
109114
if (element) {
110-
return this.createHarnessLoader(element);
115+
return this.createEnvironment(element);
111116
}
112117
throw Error(`Expected to find element matching selector: "${selector}", but none was found`);
113118
}
114119

115120
// Implemented as part of the `HarnessLoader` interface.
116121
async findOptional(selector: string): Promise<HarnessLoader | null> {
117122
const element = await this.getRawElement(selector);
118-
return element ? this.createHarnessLoader(element) : null;
123+
return element ? this.createEnvironment(element) : null;
119124
}
120125

121126
// Implemented as part of the `HarnessLoader` interface.
122127
async findAll(selector: string): Promise<HarnessLoader[]> {
123-
return (await this.getAllRawElements(selector)).map(e => this.createHarnessLoader(e));
128+
return (await this.getAllRawElements(selector)).map(e => this.createEnvironment(e));
129+
}
130+
131+
/** Creates a `ComponentHarness` for the given harness type with the given raw host element. */
132+
protected createComponentHarness<T extends ComponentHarness>(
133+
harnessType: ComponentHarnessConstructor<T>, element: E): T {
134+
return new harnessType(this.createEnvironment(element));
124135
}
125136

137+
/** Gets the root element for the document. */
138+
protected abstract getDocumentRoot(): E;
139+
126140
/** Creates a `TestElement` from a raw element. */
127141
protected abstract createTestElement(element: E): TestElement;
128142

129-
/** Creates a `ComponentHarness` for the given harness type with the given raw host element. */
130-
protected abstract createComponentHarness<T extends ComponentHarness>(
131-
harnessType: ComponentHarnessConstructor<T>, element: E): T;
132-
133143
/** Creates a `HarnessLoader` rooted at the given raw element. */
134-
protected abstract createHarnessLoader(element: E): HarnessLoader;
144+
protected abstract createEnvironment(element: E): HarnessEnvironment<E>;
135145

136146
/**
137147
* Gets the first element matching the given selector under this environment's root element, or

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
*/
88

99
import {by, element as protractorElement, ElementFinder} from 'protractor';
10-
import {
11-
ComponentHarness,
12-
ComponentHarnessConstructor,
13-
HarnessLoader,
14-
LocatorFactory,
15-
} from '../component-harness';
10+
import {HarnessLoader} from '../component-harness';
1611
import {HarnessEnvironment} from '../harness-environment';
1712
import {TestElement} from '../test-element';
1813
import {ProtractorElement} from './protractor-element';
@@ -28,20 +23,15 @@ export class ProtractorHarnessEnvironment extends HarnessEnvironment<ElementFind
2823
return new ProtractorHarnessEnvironment(protractorElement(by.css('body')));
2924
}
3025

31-
documentRootLocatorFactory(): LocatorFactory {
32-
return new ProtractorHarnessEnvironment(protractorElement(by.css('body')));
26+
protected getDocumentRoot(): ElementFinder {
27+
return protractorElement(by.css('body'));
3328
}
3429

3530
protected createTestElement(element: ElementFinder): TestElement {
3631
return new ProtractorElement(element);
3732
}
3833

39-
protected createComponentHarness<T extends ComponentHarness>(
40-
harnessType: ComponentHarnessConstructor<T>, element: ElementFinder): T {
41-
return new harnessType(new ProtractorHarnessEnvironment(element));
42-
}
43-
44-
protected createHarnessLoader(element: ElementFinder): HarnessLoader {
34+
protected createEnvironment(element: ElementFinder): HarnessEnvironment<ElementFinder> {
4535
return new ProtractorHarnessEnvironment(element);
4636
}
4737

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,20 @@
77
*/
88

99
import {ComponentFixture} from '@angular/core/testing';
10-
import {
11-
ComponentHarness,
12-
ComponentHarnessConstructor,
13-
HarnessLoader,
14-
LocatorFactory
15-
} from '../component-harness';
10+
import {ComponentHarness, ComponentHarnessConstructor, HarnessLoader} from '../component-harness';
1611
import {HarnessEnvironment} from '../harness-environment';
1712
import {TestElement} from '../test-element';
1813
import {UnitTestElement} from './unit-test-element';
1914

2015
/** A `HarnessEnvironment` implementation for Angular's Testbed. */
2116
export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
22-
protected constructor(rawRootElement: Element, private _stabilize: () => Promise<void>) {
17+
protected constructor(rawRootElement: Element, private _fixture: ComponentFixture<unknown>) {
2318
super(rawRootElement);
2419
}
2520

2621
/** Creates a `HarnessLoader` rooted at the given fixture's root element. */
2722
static create(fixture: ComponentFixture<unknown>): HarnessLoader {
28-
const stabilize = async () => {
29-
fixture.detectChanges();
30-
await fixture.whenStable();
31-
};
32-
return new TestbedHarnessEnvironment(fixture.nativeElement, stabilize);
23+
return new TestbedHarnessEnvironment(fixture.nativeElement, fixture);
3324
}
3425

3526
/**
@@ -40,34 +31,21 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
4031
*/
4132
static async harnessForFixtureRoot<T extends ComponentHarness>(
4233
fixture: ComponentFixture<unknown>, harnessType: ComponentHarnessConstructor<T>): Promise<T> {
43-
const stabilize = async () => {
44-
fixture.detectChanges();
45-
await fixture.whenStable();
46-
};
47-
const environment = new TestbedHarnessEnvironment(fixture.nativeElement, stabilize);
34+
const environment = new TestbedHarnessEnvironment(fixture.nativeElement, fixture);
4835
await environment._stabilize();
4936
return environment.createComponentHarness(harnessType, fixture.nativeElement);
5037
}
5138

52-
documentRootLocatorFactory(): LocatorFactory {
53-
let element = this.rawRootElement;
54-
while (element.parentElement) {
55-
element = element.parentElement;
56-
}
57-
return new TestbedHarnessEnvironment(element, this._stabilize);
39+
protected getDocumentRoot(): Element {
40+
return this._fixture.nativeElement;
5841
}
5942

6043
protected createTestElement(element: Element): TestElement {
6144
return new UnitTestElement(element, this._stabilize);
6245
}
6346

64-
protected createComponentHarness<T extends ComponentHarness>(
65-
harnessType: ComponentHarnessConstructor<T>, element: Element): T {
66-
return new harnessType(new TestbedHarnessEnvironment(element, this._stabilize));
67-
}
68-
69-
protected createHarnessLoader(element: Element): HarnessLoader {
70-
return new TestbedHarnessEnvironment(element, this._stabilize);
47+
protected createEnvironment(element: Element): HarnessEnvironment<Element> {
48+
return new TestbedHarnessEnvironment(element, this._fixture);
7149
}
7250

7351
protected async getRawElement(selector: string): Promise<Element | null> {
@@ -79,4 +57,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
7957
await this._stabilize();
8058
return Array.from(this.rawRootElement.querySelectorAll(selector));
8159
}
60+
61+
private _stabilize = async (): Promise<void> => {
62+
this._fixture.detectChanges();
63+
await this._fixture.whenStable();
64+
}
8265
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {TestElement} from '../test-element';
1717

1818
function isTextInput(element: Element): element is HTMLInputElement | HTMLTextAreaElement {
1919
return element.nodeName.toLowerCase() === 'input' ||
20-
element.nodeName.toLowerCase() === 'textarea' ;
20+
element.nodeName.toLowerCase() === 'textarea' ;
2121
}
2222

2323
/** A `TestElement` implementation for unit tests. */

0 commit comments

Comments
 (0)