Skip to content

Feature/320/find accept promise #322

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 3 commits into from
Nov 26, 2021
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
31 changes: 16 additions & 15 deletions lib/expect/jest.matcher.function.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Point } from "../point.class";
import { Region } from "../region.class";
import { toBeAt } from "./matchers/toBeAt.function";
import { toBeIn } from "./matchers/toBeIn.function";
import { toShow } from "./matchers/toShow.function";
import {Image} from "../image.class";
import {Point} from "../point.class";
import {Region} from "../region.class";
import {toBeAt} from "./matchers/toBeAt.function";
import {toBeIn} from "./matchers/toBeIn.function";
import {toShow} from "./matchers/toShow.function";
import {FirstArgumentType} from "../typings";
import {ScreenClass} from "../screen.class";

declare global {
namespace jest {
interface Matchers<R> {
toBeAt: (position: Point) => {};
toBeIn: (region: Region) => {};
toShow: (needle: string | Image, confidence?: number) => {};
namespace jest {
interface Matchers<R> {
toBeAt: (position: Point) => {};
toBeIn: (region: Region) => {};
toShow: (needle: FirstArgumentType<typeof ScreenClass.prototype.find>, confidence?: number) => {};
}
}
}
}

export const jestMatchers = {
toBeAt,
toBeIn,
toShow,
toBeAt,
toBeIn,
toShow,
};
4 changes: 2 additions & 2 deletions lib/expect/matchers/toShow.function.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {LocationParameters} from "../../locationparameters.class";
import {ScreenClass} from "../../screen.class";
import {Image} from "../../image.class";
import {FirstArgumentType} from "../../typings";

export const toShow = async (
received: ScreenClass,
needle: string | Image,
needle: FirstArgumentType<typeof ScreenClass.prototype.find>,
confidence?: number,
) => {
let locationParams;
Expand Down
3 changes: 2 additions & 1 deletion lib/screen.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ describe("Screen.", () => {
const matchResult = new MatchResult(0.99, searchRegion);
const SUT = new ScreenClass(providerRegistryMock);
const needle = new Image(100, 100, Buffer.from([]), 3, "needle_image");
const needlePromise = Promise.resolve(needle);

const findMatchMock = jest.fn(() => Promise.resolve(matchResult));
providerRegistryMock.getImageFinder = jest.fn(() => mockPartial<ImageFinderInterface>({
findMatch: findMatchMock
}));

// WHEN
const resultRegion = SUT.find(needle);
const resultRegion = SUT.find(needlePromise);

// THEN
await expect(resultRegion).resolves.toEqual(matchResult.location);
Expand Down
7 changes: 4 additions & 3 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {timeout} from "./util/timeout.function";
import {Image} from "./image.class";
import {ProviderRegistry} from "./provider/provider-registry.class";
import {loadImageResource} from "./imageResources.function";
import {FirstArgumentType} from "./typings";

export type FindHookCallback = (target: MatchResult) => Promise<void>;

Expand Down Expand Up @@ -80,7 +81,7 @@ export class ScreenClass {
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
*/
public async find(
templateImage: string | Image,
templateImage: string | Image | Promise<Image>,
params?: LocationParameters,
): Promise<Region> {
const minMatch = (params && params.confidence) || this.config.confidence;
Expand All @@ -92,7 +93,7 @@ export class ScreenClass {
if (typeof templateImage === "string") {
needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage);
} else {
needle = templateImage;
needle = await templateImage;
}

const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
Expand Down Expand Up @@ -171,7 +172,7 @@ export class ScreenClass {
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
*/
public async waitFor(
templateImage: string | Image,
templateImage: FirstArgumentType<typeof ScreenClass.prototype.find>,
timeoutMs: number = 5000,
params?: LocationParameters,
): Promise<Region> {
Expand Down
1 change: 1 addition & 0 deletions lib/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type FirstArgumentType<T> = T extends (first: infer ArgType, ...args: any[]) => any ? ArgType : never;