Skip to content

Commit a28a734

Browse files
committed
(#455) Added tests for different find queries
1 parent 1d78e2e commit a28a734

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

lib/screen.class.spec.ts

+198
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
} from "./provider";
1616
import { OptionalSearchParameters } from "./optionalsearchparameters.class";
1717
import { NoopLogProvider } from "./provider/log/noop-log-provider.class";
18+
import { TextQuery, WindowQuery } from "./query.class";
19+
import { TextFinderInterface } from "./provider/text-finder.interface";
20+
import { WindowFinderInterface } from "./provider/window-finder.interface";
1821

1922
jest.mock("jimp", () => {});
2023

@@ -49,6 +52,104 @@ beforeEach(() => {
4952

5053
describe("Screen.", () => {
5154
describe("find", () => {
55+
describe("queries", () => {
56+
it("should choose the correct finder implementation for images", async () => {
57+
// GIVEN
58+
const matchResult = new MatchResult(0.99, searchRegion);
59+
60+
const SUT = new ScreenClass(providerRegistryMock);
61+
const needle = new Image(
62+
100,
63+
100,
64+
Buffer.from([]),
65+
3,
66+
"needle_image",
67+
4,
68+
100 * 4
69+
);
70+
const needlePromise = Promise.resolve(needle);
71+
72+
const findMatchMock = jest.fn(() => Promise.resolve(matchResult));
73+
providerRegistryMock.getImageFinder = jest.fn(() =>
74+
mockPartial<ImageFinderInterface>({
75+
findMatch: findMatchMock,
76+
})
77+
);
78+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
79+
80+
// WHEN
81+
await SUT.find(needlePromise);
82+
83+
// THEN
84+
expect(findMatchMock).toHaveBeenCalledTimes(1);
85+
});
86+
87+
it("should choose the correct finder implementation for window queries", async () => {
88+
// GIVEN
89+
const SUT = new ScreenClass(providerRegistryMock);
90+
const needle: WindowQuery = {
91+
id: "window-query",
92+
type: "window",
93+
by: {
94+
title: "query",
95+
},
96+
};
97+
98+
const findMatchMock = jest.fn(() => Promise.resolve(1234));
99+
providerRegistryMock.getWindowFinder = jest.fn(() =>
100+
mockPartial<WindowFinderInterface>({
101+
findMatch: findMatchMock,
102+
})
103+
);
104+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
105+
106+
// WHEN
107+
await SUT.find(needle);
108+
109+
// THEN
110+
expect(findMatchMock).toHaveBeenCalledTimes(1);
111+
});
112+
113+
it.each<TextQuery>([
114+
{
115+
id: "dummy",
116+
type: "text",
117+
by: {
118+
word: "dummy-query",
119+
},
120+
},
121+
{
122+
id: "dummy",
123+
type: "text",
124+
by: {
125+
line: "dummy-query",
126+
},
127+
},
128+
])(
129+
"should choose the correct finder implementation for text queries",
130+
async (needle: TextQuery) => {
131+
// GIVEN
132+
const matchResult = new MatchResult(0.99, searchRegion);
133+
134+
const SUT = new ScreenClass(providerRegistryMock);
135+
136+
const findMatchMock = jest.fn(() => Promise.resolve(matchResult));
137+
providerRegistryMock.getTextFinder = jest.fn(() =>
138+
mockPartial<TextFinderInterface>({
139+
findMatch: findMatchMock,
140+
})
141+
);
142+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
143+
144+
// WHEN
145+
await SUT.find(needle);
146+
147+
// THEN
148+
expect(findMatchMock).toHaveBeenCalledTimes(1);
149+
}
150+
);
151+
});
152+
52153
it("should resolve with sufficient confidence.", async () => {
53154
// GIVEN
54155
const matchResult = new MatchResult(0.99, searchRegion);
@@ -408,6 +509,103 @@ describe("Screen.", () => {
408509
});
409510

410511
describe("findAll", () => {
512+
describe("queries", () => {
513+
it("should choose the correct finder implementation for images", async () => {
514+
// GIVEN
515+
const matchResult = new MatchResult(0.99, searchRegion);
516+
517+
const SUT = new ScreenClass(providerRegistryMock);
518+
const needle = new Image(
519+
100,
520+
100,
521+
Buffer.from([]),
522+
3,
523+
"needle_image",
524+
4,
525+
100 * 4
526+
);
527+
const needlePromise = Promise.resolve(needle);
528+
529+
const findMatchMock = jest.fn(() => Promise.resolve([matchResult]));
530+
providerRegistryMock.getImageFinder = jest.fn(() =>
531+
mockPartial<ImageFinderInterface>({
532+
findMatches: findMatchMock,
533+
})
534+
);
535+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
536+
537+
// WHEN
538+
await SUT.findAll(needlePromise);
539+
540+
// THEN
541+
expect(findMatchMock).toHaveBeenCalledTimes(1);
542+
});
543+
544+
it("should choose the correct finder implementation for window queries", async () => {
545+
// GIVEN
546+
const SUT = new ScreenClass(providerRegistryMock);
547+
const needle: WindowQuery = {
548+
id: "window-query",
549+
type: "window",
550+
by: {
551+
title: "query",
552+
},
553+
};
554+
555+
const findMatchMock = jest.fn(() => Promise.resolve([1234]));
556+
providerRegistryMock.getWindowFinder = jest.fn(() =>
557+
mockPartial<WindowFinderInterface>({
558+
findMatches: findMatchMock,
559+
})
560+
);
561+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
562+
563+
// WHEN
564+
await SUT.findAll(needle);
565+
566+
// THEN
567+
expect(findMatchMock).toHaveBeenCalledTimes(1);
568+
});
569+
570+
it.each<TextQuery>([
571+
{
572+
id: "dummy",
573+
type: "text",
574+
by: {
575+
word: "dummy-query",
576+
},
577+
},
578+
{
579+
id: "dummy",
580+
type: "text",
581+
by: {
582+
line: "dummy-query",
583+
},
584+
},
585+
])(
586+
"should choose the correct finder implementation for text queries",
587+
async (needle: TextQuery) => {
588+
// GIVEN
589+
const matchResult = new MatchResult(0.99, searchRegion);
590+
591+
const SUT = new ScreenClass(providerRegistryMock);
592+
593+
const findMatchMock = jest.fn(() => Promise.resolve([matchResult]));
594+
providerRegistryMock.getTextFinder = jest.fn(() =>
595+
mockPartial<TextFinderInterface>({
596+
findMatches: findMatchMock,
597+
})
598+
);
599+
providerRegistryMock.getLogProvider = () => new NoopLogProvider();
600+
601+
// WHEN
602+
await SUT.findAll(needle);
603+
604+
// THEN
605+
expect(findMatchMock).toHaveBeenCalledTimes(1);
606+
}
607+
);
608+
});
411609
it("should call registered hook before resolve", async () => {
412610
// GIVEN
413611
const matchResult = new MatchResult(0.99, searchRegion);

0 commit comments

Comments
 (0)