From ad52cc7595588a9f1114ddbd8df83a64546c108b Mon Sep 17 00:00:00 2001 From: Anton Barysau Date: Mon, 25 Mar 2024 19:50:21 +0300 Subject: [PATCH] enhance base harness wrappers use polling in harness based wrappers to make use of them in e2e test environment; --- .../foundation/base.component.harness.ts | 41 ++++++++++++++----- .../foundation/waiting.component.harness.ts | 3 +- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/app/tests/foundation/base.component.harness.ts b/src/app/tests/foundation/base.component.harness.ts index 3cd0053..0163faa 100644 --- a/src/app/tests/foundation/base.component.harness.ts +++ b/src/app/tests/foundation/base.component.harness.ts @@ -62,10 +62,34 @@ export class BaseHarness extends ComponentHarness { }, '') } + protected getThisHarnessCopy(): this { + return Object.create( + Object.getPrototypeOf(this), + Object.getOwnPropertyDescriptors(this) + ) + } + + /** + * we need to call this method at the beginning of target methods + * if we want to make them work with harness based wrappers; + * + * All wrappers are based on data-id attribute; + * However, in some cases we don't want to expose data-id values in tests and rely on something else; + * e.g. we don't want to work with entity ids in e2e tests since we generally don't know them; + * Entity ids could be use to provide unique data-ids, but we use either attributes to figure them out on the fly; + * During the process we have to look up for harnesses with these specific attributes; + * The process should use polling to be useful in e2e environment; + */ protected async updateAncestorSelector(): Promise { if (this.ancestorHarnessConfig) { - const harness = await this.locatorFor(this.ancestorHarnessConfig.itemHarnessPredicate)() - const host = await harness.host() + const lookupConfig = this.ancestorHarnessConfig + const ancestor = await this.waitFor({ + lookup: async () => { + return await this.locatorFor(lookupConfig.itemHarnessPredicate)() + }, + errorMessage: `No ancestor with given predicate found` + }) + const host = await ancestor.host() const hostId = await host.getAttribute(this.idAttribute) if (!hostId) { throw new Error(`Cannot execute the method: the host element of the harness returned by the wrapper does not have ${this.idAttribute} property`) @@ -89,7 +113,7 @@ export class BaseHarness extends ComponentHarness { lookup: () => Promise action?: (result: T) => Promise errorMessage: string - }): Promise { + }): Promise { // no need to do polling in unit tests // since component harnesses stabilize them internally const result = await this.getLookupResult(options.lookup) @@ -101,6 +125,7 @@ export class BaseHarness extends ComponentHarness { await options.action(result) } } + return result } protected markAssertionAsValidExpectation(): void { @@ -116,19 +141,13 @@ export class BaseHarness extends ComponentHarness { *******************************/ // + public inElement(id: string): this { - const copy = Object.create( - Object.getPrototypeOf(this), - Object.getOwnPropertyDescriptors(this) - ) as this + const copy = this.getThisHarnessCopy() copy.ancestorSelector = `div${this.getIdSelector(id)} ` return copy } public inMatTableRow(tableId: string, rowFilter: Record): this { - const copy = Object.create( - Object.getPrototypeOf(this), - Object.getOwnPropertyDescriptors(this) - ) as this + const copy = this.getThisHarnessCopy() copy.ancestorHarnessConfig = { itemHarnessPredicate: MatRowHarness.with({ ancestor: this.getIdSelector(tableId) diff --git a/src/app/tests/foundation/waiting.component.harness.ts b/src/app/tests/foundation/waiting.component.harness.ts index 9259a0f..8ca9c64 100644 --- a/src/app/tests/foundation/waiting.component.harness.ts +++ b/src/app/tests/foundation/waiting.component.harness.ts @@ -26,7 +26,7 @@ export class WaitingHarness extends BaseHarness { lookup: () => Promise action?: (result: T) => Promise errorMessage: string - }): Promise { + }): Promise { let result = await this.getLookupResult(options.lookup) const endTime = Date.now() + this.waitForTimeoutInterval while (!result && Date.now() < endTime) { @@ -41,6 +41,7 @@ export class WaitingHarness extends BaseHarness { await options.action(result) } } + return result } public withTimeout(timeoutInterval?: number): this {