Skip to content

Commit 24e9cd5

Browse files
committed
refactor(cdk/testing): reuse stabilize callback when creating test element
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 97ec228 commit 24e9cd5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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. */

0 commit comments

Comments
 (0)