Skip to content

Commit 5798a84

Browse files
authored
Feature/320/find accept promise (#322)
* (#320) Make find, and the functions re-using it, accept Promise<Image> * (#320) Update toShow matcher accordingly * (#320) Update tests
1 parent 2af703a commit 5798a84

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

lib/expect/jest.matcher.function.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { Point } from "../point.class";
2-
import { Region } from "../region.class";
3-
import { toBeAt } from "./matchers/toBeAt.function";
4-
import { toBeIn } from "./matchers/toBeIn.function";
5-
import { toShow } from "./matchers/toShow.function";
6-
import {Image} from "../image.class";
1+
import {Point} from "../point.class";
2+
import {Region} from "../region.class";
3+
import {toBeAt} from "./matchers/toBeAt.function";
4+
import {toBeIn} from "./matchers/toBeIn.function";
5+
import {toShow} from "./matchers/toShow.function";
6+
import {FirstArgumentType} from "../typings";
7+
import {ScreenClass} from "../screen.class";
78

89
declare global {
9-
namespace jest {
10-
interface Matchers<R> {
11-
toBeAt: (position: Point) => {};
12-
toBeIn: (region: Region) => {};
13-
toShow: (needle: string | Image, confidence?: number) => {};
10+
namespace jest {
11+
interface Matchers<R> {
12+
toBeAt: (position: Point) => {};
13+
toBeIn: (region: Region) => {};
14+
toShow: (needle: FirstArgumentType<typeof ScreenClass.prototype.find>, confidence?: number) => {};
15+
}
1416
}
15-
}
1617
}
1718

1819
export const jestMatchers = {
19-
toBeAt,
20-
toBeIn,
21-
toShow,
20+
toBeAt,
21+
toBeIn,
22+
toShow,
2223
};

lib/expect/matchers/toShow.function.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {LocationParameters} from "../../locationparameters.class";
22
import {ScreenClass} from "../../screen.class";
3-
import {Image} from "../../image.class";
3+
import {FirstArgumentType} from "../../typings";
44

55
export const toShow = async (
66
received: ScreenClass,
7-
needle: string | Image,
7+
needle: FirstArgumentType<typeof ScreenClass.prototype.find>,
88
confidence?: number,
99
) => {
1010
let locationParams;

lib/screen.class.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ describe("Screen.", () => {
3939
const matchResult = new MatchResult(0.99, searchRegion);
4040
const SUT = new ScreenClass(providerRegistryMock);
4141
const needle = new Image(100, 100, Buffer.from([]), 3, "needle_image");
42+
const needlePromise = Promise.resolve(needle);
4243

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

4849
// WHEN
49-
const resultRegion = SUT.find(needle);
50+
const resultRegion = SUT.find(needlePromise);
5051

5152
// THEN
5253
await expect(resultRegion).resolves.toEqual(matchResult.location);

lib/screen.class.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {timeout} from "./util/timeout.function";
99
import {Image} from "./image.class";
1010
import {ProviderRegistry} from "./provider/provider-registry.class";
1111
import {loadImageResource} from "./imageResources.function";
12+
import {FirstArgumentType} from "./typings";
1213

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

@@ -80,7 +81,7 @@ export class ScreenClass {
8081
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
8182
*/
8283
public async find(
83-
templateImage: string | Image,
84+
templateImage: string | Image | Promise<Image>,
8485
params?: LocationParameters,
8586
): Promise<Region> {
8687
const minMatch = (params && params.confidence) || this.config.confidence;
@@ -92,7 +93,7 @@ export class ScreenClass {
9293
if (typeof templateImage === "string") {
9394
needle = await loadImageResource(this.providerRegistry, this.config.resourceDirectory, templateImage);
9495
} else {
95-
needle = templateImage;
96+
needle = await templateImage;
9697
}
9798

9899
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
@@ -171,7 +172,7 @@ export class ScreenClass {
171172
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
172173
*/
173174
public async waitFor(
174-
templateImage: string | Image,
175+
templateImage: FirstArgumentType<typeof ScreenClass.prototype.find>,
175176
timeoutMs: number = 5000,
176177
params?: LocationParameters,
177178
): Promise<Region> {

lib/typings.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type FirstArgumentType<T> = T extends (first: infer ArgType, ...args: any[]) => any ? ArgType : never;

0 commit comments

Comments
 (0)