Skip to content

Commit 83fb108

Browse files
committed
Updated Screen.find signature
1 parent d891710 commit 83fb108

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

lib/assert.class.spec.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,71 @@ jest.mock("./screen.class");
99

1010
describe("Assert", () => {
1111
it("isVisible should not throw if a match is found.", async () => {
12-
Screen.prototype.findOnScreen = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
12+
// GIVEN
13+
Screen.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
1314
const screenMock = new Screen(new VisionAdapter());
1415
const SUT = new Assert(screenMock);
16+
const needle = "foo";
1517

16-
await expect(SUT.isVisible("foo")).resolves.not.toThrowError();
18+
// WHEN
19+
20+
// THEN
21+
await expect(SUT.isVisible(needle)).resolves.not.toThrowError();
1722
});
1823

1924
it("isVisible should throw if a match is found.", async () => {
20-
Screen.prototype.findOnScreen = jest.fn(() => Promise.reject("foo"));
25+
// GIVEN
26+
Screen.prototype.find = jest.fn(() => Promise.reject("foo"));
2127
const screenMock = new Screen(new VisionAdapter());
2228
const SUT = new Assert(screenMock);
29+
const needle = "foo";
30+
31+
// WHEN
2332

24-
await expect(SUT.isVisible("foo")).rejects.toThrowError("Element not found");
33+
// THEN
34+
await expect(SUT.isVisible(needle)).rejects.toThrowError(`Element '${needle}' not found`);
2535
});
2636

2737
it("isVisible should throw if a match is found.", async () => {
28-
Screen.prototype.findOnScreen = jest.fn(() => Promise.reject("foo"));
38+
// GIVEN
39+
Screen.prototype.find = jest.fn(() => Promise.reject("foo"));
2940
const screenMock = new Screen(new VisionAdapter());
3041
const SUT = new Assert(screenMock);
3142
const searchRegion = new Region(10, 10, 10, 10);
43+
const needle = "foo";
44+
45+
// WHEN
3246

47+
// THEN
3348
await expect(SUT
34-
.isVisible("foo", searchRegion))
35-
.rejects.toThrowError(`Element not found in region ${searchRegion.toString()}`
49+
.isVisible(needle, searchRegion))
50+
.rejects.toThrowError(`Element '${needle}' not found in region ${searchRegion.toString()}`
3651
);
3752
});
3853

3954
it("isNotVisible should throw if a match is found.", async () => {
40-
Screen.prototype.findOnScreen = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
55+
// GIVEN
56+
Screen.prototype.find = jest.fn(() => Promise.resolve(new Region(0, 0, 100, 100)));
4157
const screenMock = new Screen(new VisionAdapter());
4258
const SUT = new Assert(screenMock);
59+
const needle = "foo";
4360

44-
await expect(SUT.notVisible("foo")).rejects.toThrowError("Element visible");
61+
// WHEN
62+
63+
// THEN
64+
await expect(SUT.notVisible(needle)).rejects.toThrowError(`'${needle}' is visible`);
4565
});
4666

4767
it("isVisible should throw if a match is found.", async () => {
48-
Screen.prototype.findOnScreen = jest.fn(() => Promise.reject("foo"));
68+
// GIVEN
69+
Screen.prototype.find = jest.fn(() => Promise.reject("foo"));
4970
const screenMock = new Screen(new VisionAdapter());
5071
const SUT = new Assert(screenMock);
72+
const needle = "foo";
73+
74+
// WHEN
5175

52-
await expect(SUT.notVisible("foo")).resolves.not.toThrowError();
76+
// THEN
77+
await expect(SUT.notVisible(needle)).resolves.not.toThrowError();
5378
});
5479
});

lib/assert.class.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ export class Assert {
77

88
public async isVisible(pathToNeedle: string, searchRegion?: Region, confidence?: number) {
99
try {
10-
await this.screen.findOnScreen(
10+
await this.screen.find(
1111
pathToNeedle,
1212
{searchRegion, confidence} as LocationParameters,
1313
);
1414
} catch (err) {
1515
if (searchRegion !== undefined) {
1616
throw new Error(
17-
`Element not found in region ${searchRegion.toString()}`,
17+
`Element '${pathToNeedle}' not found in region ${searchRegion.toString()}`,
1818
);
1919
} else {
20-
throw new Error("Element not found");
20+
throw new Error(`Element '${pathToNeedle}' not found`);
2121
}
2222
}
2323
}
2424

2525
public async notVisible(pathToNeedle: string, searchRegion?: Region, confidence?: number) {
2626
try {
27-
await this.screen.findOnScreen(
27+
await this.screen.find(
2828
pathToNeedle,
2929
{searchRegion, confidence} as LocationParameters,
3030
);
3131
} catch (err) {
3232
return;
3333
}
34-
throw new Error("Element visible");
34+
throw new Error(`'${pathToNeedle}' is visible`);
3535
}
3636
}

lib/expect/matchers/toShow.function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const toShow = async (
1212
locationParams.confidence = confidence;
1313
}
1414
try {
15-
await received.findOnScreen(needle, locationParams);
15+
await received.find(needle, locationParams);
1616
return {
1717
message: () => `Expected screen to not show ${needle}`,
1818
pass: true,

lib/screen.class.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("Screen.", () => {
3333

3434
const SUT = new Screen(visionAdapterMock);
3535
const imagePath = "test/path/to/image.png";
36-
await expect(SUT.findOnScreen(imagePath)).resolves.toEqual(matchResult.location);
36+
await expect(SUT.find(imagePath)).resolves.toEqual(matchResult.location);
3737
const matchRequest = new MatchRequest(
3838
expect.any(Image),
3939
imagePath,
@@ -54,7 +54,7 @@ describe("Screen.", () => {
5454

5555
const SUT = new Screen(visionAdapterMock);
5656
const imagePath = "test/path/to/image.png";
57-
await expect(SUT.findOnScreen(imagePath))
57+
await expect(SUT.find(imagePath))
5858
.rejects
5959
.toEqual(`No match for ${imagePath}. Required: ${SUT.config.confidence}, given: ${matchResult.confidence}`);
6060
});
@@ -73,7 +73,7 @@ describe("Screen.", () => {
7373

7474
const imagePath = "test/path/to/image.png";
7575
const parameters = new LocationParameters(undefined, minMatch);
76-
await expect(SUT.findOnScreen(imagePath, parameters)).resolves.toEqual(matchResult.location);
76+
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
7777
const matchRequest = new MatchRequest(
7878
expect.any(Image),
7979
imagePath,
@@ -97,7 +97,7 @@ describe("Screen.", () => {
9797

9898
const imagePath = "test/path/to/image.png";
9999
const parameters = new LocationParameters(customSearchRegion);
100-
await expect(SUT.findOnScreen(imagePath, parameters)).resolves.toEqual(matchResult.location);
100+
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
101101
const matchRequest = new MatchRequest(
102102
expect.any(Image),
103103
imagePath,
@@ -122,7 +122,7 @@ describe("Screen.", () => {
122122

123123
const imagePath = "test/path/to/image.png";
124124
const parameters = new LocationParameters(customSearchRegion, minMatch);
125-
await expect(SUT.findOnScreen(imagePath, parameters)).resolves.toEqual(matchResult.location);
125+
await expect(SUT.find(imagePath, parameters)).resolves.toEqual(matchResult.location);
126126
const matchRequest = new MatchRequest(
127127
expect.any(Image),
128128
imagePath,

lib/screen.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Screen {
2424
return this.vision.screenHeight();
2525
}
2626

27-
public async findOnScreen(
27+
public async find(
2828
pathToNeedle: string,
2929
params?: LocationParameters,
3030
): Promise<Region> {

0 commit comments

Comments
 (0)