Skip to content

Commit b0d9b25

Browse files
authored
refactor(cdk/testing): reuse stabilize callback when creating test element (#24094)
Any time we were creating a new test element, we were giving it a new stabilization callback. These changes reuse the same one between all elements in order to reduce the amount of memory for each element.
1 parent 2d571af commit b0d9b25

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/cdk/testing/harness-environment.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ type ParsedQueries<T extends ComponentHarness> = {
4545
*/
4646
export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
4747
// Implemented as part of the `LocatorFactory` interface.
48-
rootElement: TestElement;
49-
50-
protected constructor(protected rawRootElement: E) {
51-
this.rootElement = this.createTestElement(rawRootElement);
48+
get rootElement(): TestElement {
49+
this._rootElement = this._rootElement || this.createTestElement(this.rawRootElement);
50+
return this._rootElement;
51+
}
52+
set rootElement(element: TestElement) {
53+
this._rootElement = element;
5254
}
55+
private _rootElement: TestElement | undefined;
56+
57+
protected constructor(protected rawRootElement: E) {}
5358

5459
// Implemented as part of the `LocatorFactory` interface.
5560
documentRootLocatorFactory(): LocatorFactory {

src/cdk/testing/selenium-webdriver/selenium-web-driver-harness-environment.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ export class SeleniumWebDriverHarnessEnvironment extends HarnessEnvironment<
7474
/** The options for this environment. */
7575
private _options: WebDriverHarnessEnvironmentOptions;
7676

77+
/** Environment stabilization callback passed to the created test elements. */
78+
private _stabilizeCallback: () => Promise<void>;
79+
7780
protected constructor(
7881
rawRootElement: () => webdriver.WebElement,
7982
options?: WebDriverHarnessEnvironmentOptions,
8083
) {
8184
super(rawRootElement);
8285
this._options = {...defaultEnvironmentOptions, ...options};
86+
this._stabilizeCallback = () => this.forceStabilize();
8387
}
8488

8589
/** Gets the ElementFinder corresponding to the given TestElement. */
@@ -123,7 +127,7 @@ export class SeleniumWebDriverHarnessEnvironment extends HarnessEnvironment<
123127

124128
/** Creates a `TestElement` from a raw element. */
125129
protected createTestElement(element: () => webdriver.WebElement): TestElement {
126-
return new SeleniumWebDriverElement(element, () => this.forceStabilize());
130+
return new SeleniumWebDriverElement(element, this._stabilizeCallback);
127131
}
128132

129133
/** Creates a `HarnessLoader` rooted at the given raw element. */

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
9696
/** The options for this environment. */
9797
private _options: TestbedHarnessEnvironmentOptions;
9898

99+
/** Environment stabilization callback passed to the created test elements. */
100+
private _stabilizeCallback: () => Promise<void>;
101+
99102
protected constructor(
100103
rawRootElement: Element,
101104
private _fixture: ComponentFixture<unknown>,
@@ -104,6 +107,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
104107
super(rawRootElement);
105108
this._options = {...defaultEnvironmentOptions, ...options};
106109
this._taskState = TaskStateZoneInterceptor.setup();
110+
this._stabilizeCallback = () => this.forceStabilize();
107111
installAutoChangeDetectionStatusHandler(_fixture);
108112
_fixture.componentRef.onDestroy(() => {
109113
uninstallAutoChangeDetectionStatusHandler(_fixture);
@@ -198,7 +202,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
198202

199203
/** Creates a `TestElement` from a raw element. */
200204
protected createTestElement(element: Element): TestElement {
201-
return new UnitTestElement(element, () => this.forceStabilize());
205+
return new UnitTestElement(element, this._stabilizeCallback);
202206
}
203207

204208
/** Creates a `HarnessLoader` rooted at the given raw element. */

tools/public_api_guard/cdk/testing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
117117
// (undocumented)
118118
protected rawRootElement: E;
119119
// (undocumented)
120-
rootElement: TestElement;
120+
get rootElement(): TestElement;
121+
set rootElement(element: TestElement);
121122
// (undocumented)
122123
rootHarnessLoader(): Promise<HarnessLoader>;
123124
// (undocumented)

0 commit comments

Comments
 (0)