Skip to content

Commit

Permalink
refactor: simplify expect existence evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDienst-askui committed Jun 17, 2024
1 parent fd45d4e commit c85b7c9
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions packages/askui-nodejs/src/execution/ui-control-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ import { Instruction, StepReporter } from '../core/reporting';
export type RelationsForConvenienceMethods = 'nearestTo' | 'leftOf' | 'above' | 'rightOf' | 'below' | 'contains';

export type ExpectExistenceAllowedElementClasses = 'text' | 'button' | 'checkbox' | 'switch' | 'textfield' | 'element';
export type ExpectExistenceInputParameter = {
export interface ExpectExistenceInputParameter {
type: ExpectExistenceAllowedElementClasses;
label: string;
matching?: 'similar' | 'exact' | 'regex';
relation?: {
type: RelationsForConvenienceMethods;
text?: string;
};
exists?: boolean;
};
export interface ExpectExistenceElement extends ExpectExistenceInputParameter {
exists: boolean;
}
export type ExpectExistenceReturnValue = {
everythingExists: boolean;
elements: ExpectExistenceInputParameter[];
elements: ExpectExistenceElement[];
};

export class UiControlClient extends ApiCommands {
Expand Down Expand Up @@ -624,16 +626,11 @@ export class UiControlClient extends ApiCommands {
// eslint-disable-next-line class-methods-use-this
private async evaluateFinalExpectExistenceCommand(
finalCommand: FluentFiltersOrRelationsGetter,
element: ExpectExistenceInputParameter,
): Promise<ExpectExistenceInputParameter> {
const el = element;
el.exists = false;
): Promise<boolean> {
if ((await finalCommand.exec()).length > 0) {
el.exists = true;
return true;
}
return new Promise<ExpectExistenceInputParameter>((resolve) => {
resolve(el);
});
return false;
}

// eslint-disable-next-line class-methods-use-this
Expand Down Expand Up @@ -767,7 +764,7 @@ export class UiControlClient extends ApiCommands {
}

returnValue.elements.push(
(await this.evaluateFinalExpectExistenceCommand(finalCommand, element)),
{...element, exists: await this.evaluateFinalExpectExistenceCommand(finalCommand)},
);
break;
}
Expand All @@ -788,7 +785,7 @@ export class UiControlClient extends ApiCommands {
}

returnValue.elements.push(
(await this.evaluateFinalExpectExistenceCommand(finalCommand, element)),
{...element, exists: await this.evaluateFinalExpectExistenceCommand(finalCommand)},
);
break;
}
Expand All @@ -807,7 +804,7 @@ export class UiControlClient extends ApiCommands {
}

returnValue.elements.push(
(await this.evaluateFinalExpectExistenceCommand(finalCommand, element)),
{...element, exists: await this.evaluateFinalExpectExistenceCommand(finalCommand)},
);
break;
}
Expand All @@ -834,7 +831,7 @@ export class UiControlClient extends ApiCommands {
}

returnValue.elements.push(
(await this.evaluateFinalExpectExistenceCommand(finalCommand, element)),
{...element, exists: await this.evaluateFinalExpectExistenceCommand(finalCommand)},
);
break;
}
Expand Down

0 comments on commit c85b7c9

Please sign in to comment.