Skip to content

Commit 1ec9d62

Browse files
authored
Feature/307/default imagereader imagewriter (#308)
* (#307) Add jimp dependencz * (#307) Implement image reader * (#307) Implement image writer * (#307) Test data * (#307) Register both providers * (#307) Stick to BGR color format when loading images * (#307) Expose imagereader and imagewriter through utility functions * (#307) Updated tests to verify calls to Jimp * (#307) Mock jimp in testcases using the pluginRegistry to avoid ReferenceErrors * (#307) Mock jimp in e2e testcases to avoid ReferenceErrors
1 parent ab84e1d commit 1ec9d62

21 files changed

+752
-0
lines changed

e2e/plugin-test/keyboard.class.e2e.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { jestMatchers, Key, keyboard, screen } from "@nut-tree/nut-js";
22
import "@nut-tree/template-matcher";
33

4+
jest.mock('jimp', () => {});
5+
46
jest.setTimeout(30000);
57
expect.extend(jestMatchers);
68

index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const assert = new AssertClass(screen);
5050
const {straightTo, up, down, left, right} = createMovementApi(nativeActions, lineHelper);
5151
const {getWindows, getActiveWindow} = createWindowApi(nativeActions);
5252

53+
const loadImage = providerRegistry.getImageReader().load;
54+
const saveImage = providerRegistry.getImageWriter().store;
55+
5356
export {
5457
clipboard,
5558
keyboard,
@@ -63,4 +66,6 @@ export {
6366
right,
6467
getWindows,
6568
getActiveWindow,
69+
loadImage,
70+
saveImage
6671
};

lib/adapter/native.adapter.class.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.mock("../provider/native/clipboardy-clipboard.class");
1212
jest.mock("../provider/native/libnut-mouse.class");
1313
jest.mock("../provider/native/libnut-keyboard.class");
1414
jest.mock("../provider/native/libnut-window.class");
15+
jest.mock('jimp', () => {});
1516

1617
let clipboardMock: ClipboardAction;
1718
let keyboardMock: KeyboardAction;

lib/adapter/vision.adapter.class.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {VisionAdapter} from "./vision.adapter.class";
66
import providerRegistry from "../provider/provider-registry.class";
77
import {MatchResult} from "../match-result.class";
88

9+
jest.mock('jimp', () => {});
910
jest.mock("../provider/native/libnut-screen.class");
1011

1112
const finderMock = {

lib/assert.class.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Region} from "./region.class";
44
import {ScreenClass} from "./screen.class";
55
import providerRegistry from "./provider/provider-registry.class";
66

7+
jest.mock('jimp', () => {});
78
jest.mock("./adapter/native.adapter.class");
89
jest.mock("./adapter/vision.adapter.class");
910
jest.mock("./screen.class");

lib/clipboard.class.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {NativeAdapter} from "./adapter/native.adapter.class";
22
import {ClipboardClass} from "./clipboard.class";
33
import providerRegistry from "./provider/provider-registry.class";
44

5+
jest.mock('jimp', () => {});
56
jest.mock("./adapter/native.adapter.class");
67

78
beforeEach(() => {

lib/expect/matchers/toBeAt.function.e2e.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { mouse } from "../../../index";
22
import { Point } from "../../point.class";
33
import { toBeAt } from "./toBeAt.function";
44

5+
jest.mock('jimp', () => {});
6+
57
const targetPoint = new Point(100, 100);
68

79
describe(".toBeAt", () => {

lib/expect/matchers/toBeIn.function.e2e.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Point } from "../../point.class";
33
import { Region } from "../../region.class";
44
import { toBeIn } from "./toBeIn.function";
55

6+
jest.mock('jimp', () => {});
7+
68
const targetPoint = new Point(400, 400);
79

810
describe(".toBeIn", () => {
463 Bytes
Loading
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ImageReader from "./jimp-image-reader.class";
2+
import {join} from "path";
3+
import Jimp from "jimp";
4+
5+
jest.mock('gifwrap', () => {});
6+
jest.mock('jimp', () => {
7+
class JimpMock {
8+
bitmap = {
9+
width: 100,
10+
height: 100,
11+
data: Buffer.from([]),
12+
}
13+
hasAlpha = () => false
14+
static read = jest.fn(() => Promise.resolve(new JimpMock()))
15+
}
16+
17+
return ({
18+
__esModule: true,
19+
default: JimpMock
20+
})
21+
});
22+
23+
afterEach(() => jest.resetAllMocks());
24+
25+
describe('Jimp image reader', () => {
26+
it('should return an Image object', async () => {
27+
// GIVEN
28+
const inputPath = join(__dirname, "__mocks__", "calculator.png");
29+
const scanMock = jest.fn();
30+
Jimp.prototype.scan = scanMock;
31+
const SUT = new ImageReader();
32+
33+
// WHEN
34+
await SUT.load(inputPath);
35+
36+
// THEN
37+
expect(scanMock).toHaveBeenCalledTimes(1);
38+
expect(Jimp.read).toBeCalledTimes(1);
39+
expect(Jimp.read).toBeCalledWith(inputPath);
40+
});
41+
42+
it('should reject on loading failures', async () => {
43+
// GIVEN
44+
const inputPath = "/some/path/to/file";
45+
const expectedError = "Error during load";
46+
const SUT = new ImageReader();
47+
Jimp.read = jest.fn(() => {
48+
throw new Error(expectedError);
49+
})
50+
51+
// WHEN
52+
try {
53+
await SUT.load(inputPath);
54+
} catch (err) {
55+
// THEN
56+
expect(err).toStrictEqual(Error(expectedError));
57+
}
58+
});
59+
});

0 commit comments

Comments
 (0)