Skip to content

refactor(cdk/testing): reuse stabilize callback when creating test element #24094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/cdk/testing/harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ type ParsedQueries<T extends ComponentHarness> = {
*/
export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
// Implemented as part of the `LocatorFactory` interface.
rootElement: TestElement;

protected constructor(protected rawRootElement: E) {
this.rootElement = this.createTestElement(rawRootElement);
get rootElement(): TestElement {
this._rootElement = this._rootElement || this.createTestElement(this.rawRootElement);
return this._rootElement;
}
set rootElement(element: TestElement) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this getter/setter, but the problem is that this class calls createTestElement in the constructor before the sub-classes have had a change to assign the stabilization method.

this._rootElement = element;
}
private _rootElement: TestElement | undefined;

protected constructor(protected rawRootElement: E) {}

// Implemented as part of the `LocatorFactory` interface.
documentRootLocatorFactory(): LocatorFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ export class SeleniumWebDriverHarnessEnvironment extends HarnessEnvironment<
/** The options for this environment. */
private _options: WebDriverHarnessEnvironmentOptions;

/** Environment stabilization callback passed to the created test elements. */
private _stabilizeCallback: () => Promise<void>;

protected constructor(
rawRootElement: () => webdriver.WebElement,
options?: WebDriverHarnessEnvironmentOptions,
) {
super(rawRootElement);
this._options = {...defaultEnvironmentOptions, ...options};
this._stabilizeCallback = () => this.forceStabilize();
}

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

/** Creates a `TestElement` from a raw element. */
protected createTestElement(element: () => webdriver.WebElement): TestElement {
return new SeleniumWebDriverElement(element, () => this.forceStabilize());
return new SeleniumWebDriverElement(element, this._stabilizeCallback);
}

/** Creates a `HarnessLoader` rooted at the given raw element. */
Expand Down
6 changes: 5 additions & 1 deletion src/cdk/testing/testbed/testbed-harness-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
/** The options for this environment. */
private _options: TestbedHarnessEnvironmentOptions;

/** Environment stabilization callback passed to the created test elements. */
private _stabilizeCallback: () => Promise<void>;

protected constructor(
rawRootElement: Element,
private _fixture: ComponentFixture<unknown>,
Expand All @@ -104,6 +107,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
super(rawRootElement);
this._options = {...defaultEnvironmentOptions, ...options};
this._taskState = TaskStateZoneInterceptor.setup();
this._stabilizeCallback = () => this.forceStabilize();
installAutoChangeDetectionStatusHandler(_fixture);
_fixture.componentRef.onDestroy(() => {
uninstallAutoChangeDetectionStatusHandler(_fixture);
Expand Down Expand Up @@ -198,7 +202,7 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {

/** Creates a `TestElement` from a raw element. */
protected createTestElement(element: Element): TestElement {
return new UnitTestElement(element, () => this.forceStabilize());
return new UnitTestElement(element, this._stabilizeCallback);
}

/** Creates a `HarnessLoader` rooted at the given raw element. */
Expand Down
3 changes: 2 additions & 1 deletion tools/public_api_guard/cdk/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
// (undocumented)
protected rawRootElement: E;
// (undocumented)
rootElement: TestElement;
get rootElement(): TestElement;
set rootElement(element: TestElement);
// (undocumented)
rootHarnessLoader(): Promise<HarnessLoader>;
// (undocumented)
Expand Down