Skip to content
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

enhance base harness wrappers #143

Merged
merged 1 commit into from
Mar 25, 2024
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
41 changes: 30 additions & 11 deletions src/app/tests/foundation/base.component.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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`)
Expand All @@ -89,7 +113,7 @@ export class BaseHarness extends ComponentHarness {
lookup: () => Promise<T | null>
action?: (result: T) => Promise<void>
errorMessage: string
}): Promise<void> {
}): Promise<T> {
// no need to do polling in unit tests
// since component harnesses stabilize them internally
const result = await this.getLookupResult(options.lookup)
Expand All @@ -101,6 +125,7 @@ export class BaseHarness extends ComponentHarness {
await options.action(result)
}
}
return result
}

protected markAssertionAsValidExpectation(): void {
Expand All @@ -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<string, string>): 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)
Expand Down
3 changes: 2 additions & 1 deletion src/app/tests/foundation/waiting.component.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class WaitingHarness extends BaseHarness {
lookup: () => Promise<T | null>
action?: (result: T) => Promise<void>
errorMessage: string
}): Promise<void> {
}): Promise<T> {
let result = await this.getLookupResult(options.lookup)
const endTime = Date.now() + this.waitForTimeoutInterval
while (!result && Date.now() < endTime) {
Expand All @@ -41,6 +41,7 @@ export class WaitingHarness extends BaseHarness {
await options.action(result)
}
}
return result
}

public withTimeout(timeoutInterval?: number): this {
Expand Down