Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4fbc167

Browse files
committedJan 9, 2023
(#455) Added find input validation and tests
1 parent a28a734 commit 4fbc167

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
 

‎lib/screen.class.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ describe("Screen.", () => {
150150
);
151151
});
152152

153+
it("should throw on invalid search input", async () => {
154+
// GIVEN
155+
const SUT = new ScreenClass(providerRegistryMock);
156+
157+
// WHEN
158+
const result = SUT.find({ foo: "bar" } as unknown as Image);
159+
160+
// THEN
161+
await expect(result).rejects.toThrowError(
162+
/find requires an Image, a text query or a window query.*/
163+
);
164+
});
165+
153166
it("should resolve with sufficient confidence.", async () => {
154167
// GIVEN
155168
const matchResult = new MatchResult(0.99, searchRegion);
@@ -510,6 +523,19 @@ describe("Screen.", () => {
510523

511524
describe("findAll", () => {
512525
describe("queries", () => {
526+
it("should throw on invalid search input", async () => {
527+
// GIVEN
528+
const SUT = new ScreenClass(providerRegistryMock);
529+
530+
// WHEN
531+
const result = SUT.findAll({ foo: "bar" } as unknown as Image);
532+
533+
// THEN
534+
await expect(result).rejects.toThrowError(
535+
/findAll requires an Image, a text query or a window query.*/
536+
);
537+
});
538+
513539
it("should choose the correct finder implementation for images", async () => {
514540
// GIVEN
515541
const matchResult = new MatchResult(0.99, searchRegion);

‎lib/screen.class.ts

+16
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ export class ScreenClass {
160160
): Promise<FindResult> {
161161
const needle = await searchInput;
162162

163+
if (!isImage(needle) && !isTextQuery(needle) && !isWindowQuery(needle)) {
164+
throw Error(
165+
`find requires an Image, a text query or a window query, but received ${JSON.stringify(
166+
needle
167+
)}`
168+
);
169+
}
170+
163171
try {
164172
if (isWindowQuery(needle)) {
165173
const windowHandle = await this.providerRegistry
@@ -247,6 +255,14 @@ export class ScreenClass {
247255
): Promise<FindResult[]> {
248256
const needle = await searchInput;
249257

258+
if (!isImage(needle) && !isTextQuery(needle) && !isWindowQuery(needle)) {
259+
throw Error(
260+
`findAll requires an Image, a text query or a window query, but received ${JSON.stringify(
261+
needle
262+
)}`
263+
);
264+
}
265+
250266
try {
251267
if (isWindowQuery(needle)) {
252268
const matches = await this.providerRegistry

0 commit comments

Comments
 (0)
Please sign in to comment.