Skip to content

Commit d860ec5

Browse files
committed
(#310) Migrated mouse.class to not use adapters
1 parent 8f50b25 commit d860ec5

File tree

2 files changed

+223
-162
lines changed

2 files changed

+223
-162
lines changed

lib/mouse.class.spec.ts

+206-145
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,217 @@
1-
import { NativeAdapter } from "./adapter/native.adapter.class";
2-
import { Button } from "./button.enum";
3-
import { MouseClass } from "./mouse.class";
4-
import { Point } from "./point.class";
5-
import { LineHelper } from "./util/linehelper.class";
6-
import providerRegistry from "./provider/provider-registry.class";
7-
8-
jest.mock("./adapter/native.adapter.class");
1+
import {Button} from "./button.enum";
2+
import {MouseClass} from "./mouse.class";
3+
import {Point} from "./point.class";
4+
import {LineHelper} from "./util/linehelper.class";
5+
import {ProviderRegistry} from "./provider/provider-registry.class";
6+
import {mockPartial} from "sneer";
7+
import {MouseProviderInterface} from "./provider";
98

109
beforeEach(() => {
11-
jest.resetAllMocks();
10+
jest.clearAllMocks();
1211
});
1312

1413
const linehelper = new LineHelper();
1514

15+
const providerRegistryMock = mockPartial<ProviderRegistry>({
16+
getMouse(): MouseProviderInterface {
17+
return mockPartial<MouseProviderInterface>({
18+
setMouseDelay: jest.fn()
19+
})
20+
}
21+
});
22+
1623
describe("Mouse class", () => {
17-
it("should have a default delay of 500 ms", () => {
18-
// GIVEN
19-
const adapterMock = new NativeAdapter(providerRegistry);
20-
const SUT = new MouseClass(adapterMock);
21-
22-
// WHEN
23-
24-
// THEN
25-
expect(SUT.config.autoDelayMs).toEqual(100);
26-
});
27-
28-
it("should forward scrollLeft to the native adapter class", async () => {
29-
// GIVEN
30-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
31-
const SUT = new MouseClass(nativeAdapterMock);
32-
const scrollAmount = 5;
33-
34-
// WHEN
35-
const result = await SUT.scrollLeft(scrollAmount);
36-
37-
// THEN
38-
expect(nativeAdapterMock.scrollLeft).toBeCalledWith(scrollAmount);
39-
expect(result).toBe(SUT);
40-
});
41-
42-
it("should forward scrollRight to the native adapter class", async () => {
43-
// GIVEN
44-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
45-
const SUT = new MouseClass(nativeAdapterMock);
46-
const scrollAmount = 5;
47-
48-
// WHEN
49-
const result = await SUT.scrollRight(scrollAmount);
50-
51-
// THEN
52-
expect(nativeAdapterMock.scrollRight).toBeCalledWith(scrollAmount);
53-
expect(result).toBe(SUT);
54-
});
55-
56-
it("should forward scrollDown to the native adapter class", async () => {
57-
// GIVEN
58-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
59-
const SUT = new MouseClass(nativeAdapterMock);
60-
const scrollAmount = 5;
61-
62-
// WHEN
63-
const result = await SUT.scrollDown(scrollAmount);
64-
65-
// THEN
66-
expect(nativeAdapterMock.scrollDown).toBeCalledWith(scrollAmount);
67-
expect(result).toBe(SUT);
68-
});
69-
70-
it("should forward scrollUp to the native adapter class", async () => {
71-
// GIVEN
72-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
73-
const SUT = new MouseClass(nativeAdapterMock);
74-
const scrollAmount = 5;
75-
76-
// WHEN
77-
const result = await SUT.scrollUp(scrollAmount);
78-
79-
// THEN
80-
expect(nativeAdapterMock.scrollUp).toBeCalledWith(scrollAmount);
81-
expect(result).toBe(SUT);
82-
});
83-
84-
it("should forward leftClick to the native adapter class", async () => {
85-
// GIVEN
86-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
87-
const SUT = new MouseClass(nativeAdapterMock);
88-
89-
// WHEN
90-
const result = await SUT.leftClick();
91-
92-
// THEN
93-
expect(nativeAdapterMock.leftClick).toBeCalled();
94-
expect(result).toBe(SUT);
95-
});
96-
97-
it("should forward rightClick to the native adapter class", async () => {
98-
// GIVEN
99-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
100-
const SUT = new MouseClass(nativeAdapterMock);
101-
102-
// WHEN
103-
const result = await SUT.rightClick();
104-
105-
// THEN
106-
expect(nativeAdapterMock.rightClick).toBeCalled();
107-
expect(result).toBe(SUT);
108-
});
109-
110-
it("update mouse position along path on move", async () => {
111-
// GIVEN
112-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
113-
const SUT = new MouseClass(nativeAdapterMock);
114-
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
115-
116-
// WHEN
117-
const result = await SUT.move(path);
118-
119-
// THEN
120-
expect(nativeAdapterMock.setMousePosition).toBeCalledTimes(path.length);
121-
expect(result).toBe(SUT);
122-
});
123-
124-
it("should press and hold left mouse button, move and release left mouse button on drag", async () => {
125-
// GIVEN
126-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
127-
const SUT = new MouseClass(nativeAdapterMock);
128-
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
129-
130-
// WHEN
131-
const result = await SUT.drag(path);
132-
133-
// THEN
134-
expect(nativeAdapterMock.pressButton).toBeCalledWith(Button.LEFT);
135-
expect(nativeAdapterMock.setMousePosition).toBeCalledTimes(path.length);
136-
expect(nativeAdapterMock.releaseButton).toBeCalledWith(Button.LEFT);
137-
expect(result).toBe(SUT);
138-
});
24+
it("should have a default delay of 500 ms", () => {
25+
// GIVEN
26+
const SUT = new MouseClass(providerRegistryMock);
27+
28+
// WHEN
29+
30+
// THEN
31+
expect(SUT.config.autoDelayMs).toEqual(100);
32+
});
33+
34+
it("should forward scrollLeft to the provider", async () => {
35+
// GIVEN
36+
const SUT = new MouseClass(providerRegistryMock);
37+
const scrollAmount = 5;
38+
39+
const scrollMock = jest.fn();
40+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
41+
setMouseDelay: jest.fn(),
42+
scrollLeft: scrollMock
43+
}));
44+
45+
// WHEN
46+
const result = await SUT.scrollLeft(scrollAmount);
47+
48+
// THEN
49+
expect(scrollMock).toBeCalledWith(scrollAmount);
50+
expect(result).toBe(SUT);
51+
});
52+
53+
it("should forward scrollRight to the provider", async () => {
54+
// GIVEN
55+
const SUT = new MouseClass(providerRegistryMock);
56+
const scrollAmount = 5;
57+
58+
const scrollMock = jest.fn();
59+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
60+
setMouseDelay: jest.fn(),
61+
scrollRight: scrollMock
62+
}));
63+
64+
// WHEN
65+
const result = await SUT.scrollRight(scrollAmount);
66+
67+
// THEN
68+
expect(scrollMock).toBeCalledWith(scrollAmount);
69+
expect(result).toBe(SUT);
70+
});
71+
72+
it("should forward scrollDown to the provider", async () => {
73+
// GIVEN
74+
const SUT = new MouseClass(providerRegistryMock);
75+
const scrollAmount = 5;
76+
77+
const scrollMock = jest.fn();
78+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
79+
setMouseDelay: jest.fn(),
80+
scrollDown: scrollMock
81+
}));
82+
83+
// WHEN
84+
const result = await SUT.scrollDown(scrollAmount);
85+
86+
// THEN
87+
expect(scrollMock).toBeCalledWith(scrollAmount);
88+
expect(result).toBe(SUT);
89+
});
90+
91+
it("should forward scrollUp to the provider", async () => {
92+
// GIVEN
93+
const SUT = new MouseClass(providerRegistryMock);
94+
const scrollAmount = 5;
95+
96+
const scrollMock = jest.fn();
97+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
98+
setMouseDelay: jest.fn(),
99+
scrollUp: scrollMock
100+
}));
101+
102+
// WHEN
103+
const result = await SUT.scrollUp(scrollAmount);
104+
105+
// THEN
106+
expect(scrollMock).toBeCalledWith(scrollAmount);
107+
expect(result).toBe(SUT);
108+
});
109+
110+
it("should forward leftClick to the provider", async () => {
111+
// GIVEN
112+
const SUT = new MouseClass(providerRegistryMock);
113+
114+
const clickMock = jest.fn();
115+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
116+
setMouseDelay: jest.fn(),
117+
leftClick: clickMock
118+
}));
119+
120+
// WHEN
121+
const result = await SUT.leftClick();
122+
123+
// THEN
124+
expect(clickMock).toBeCalled();
125+
expect(result).toBe(SUT);
126+
});
127+
128+
it("should forward rightClick to the provider", async () => {
129+
// GIVEN
130+
const SUT = new MouseClass(providerRegistryMock);
131+
132+
const clickMock = jest.fn();
133+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
134+
setMouseDelay: jest.fn(),
135+
rightClick: clickMock
136+
}));
137+
138+
// WHEN
139+
const result = await SUT.rightClick();
140+
141+
// THEN
142+
expect(clickMock).toBeCalled();
143+
expect(result).toBe(SUT);
144+
});
145+
146+
it("update mouse position along path on move", async () => {
147+
// GIVEN
148+
const SUT = new MouseClass(providerRegistryMock);
149+
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
150+
151+
const setPositionMock = jest.fn();
152+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
153+
setMouseDelay: jest.fn(),
154+
setMousePosition: setPositionMock
155+
}));
156+
157+
// WHEN
158+
const result = await SUT.move(path);
159+
160+
// THEN
161+
expect(setPositionMock).toBeCalledTimes(path.length);
162+
expect(result).toBe(SUT);
163+
});
164+
165+
it("should press and hold left mouse button, move and release left mouse button on drag", async () => {
166+
// GIVEN
167+
const SUT = new MouseClass(providerRegistryMock);
168+
const path = linehelper.straightLine(new Point(0, 0), new Point(10, 10));
169+
170+
const setPositionMock = jest.fn();
171+
const pressButtonMock = jest.fn();
172+
const releaseButtonMock = jest.fn();
173+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
174+
setMouseDelay: jest.fn(),
175+
setMousePosition: setPositionMock,
176+
pressButton: pressButtonMock,
177+
releaseButton: releaseButtonMock
178+
}));
179+
180+
// WHEN
181+
const result = await SUT.drag(path);
182+
183+
// THEN
184+
expect(pressButtonMock).toBeCalledWith(Button.LEFT);
185+
expect(setPositionMock).toBeCalledTimes(path.length);
186+
expect(releaseButtonMock).toBeCalledWith(Button.LEFT);
187+
expect(result).toBe(SUT);
188+
});
139189
});
140190

141191
describe("Mousebuttons", () => {
142-
it.each([
143-
[Button.LEFT, Button.LEFT],
144-
[Button.MIDDLE, Button.MIDDLE],
145-
[Button.RIGHT, Button.RIGHT],
146-
] as Array<[Button, Button]>)("should be pressed and released", async (input: Button, expected: Button) => {
147-
const nativeAdapterMock = new NativeAdapter(providerRegistry);
148-
const SUT = new MouseClass(nativeAdapterMock);
149-
const pressed = await SUT.pressButton(input);
150-
const released = await SUT.releaseButton(input);
151-
expect(nativeAdapterMock.pressButton).toBeCalledWith(expected);
152-
expect(nativeAdapterMock.releaseButton).toBeCalledWith(expected);
153-
expect(pressed).toBe(SUT);
154-
expect(released).toBe(SUT);
155-
});
192+
it.each([
193+
[Button.LEFT, Button.LEFT],
194+
[Button.MIDDLE, Button.MIDDLE],
195+
[Button.RIGHT, Button.RIGHT],
196+
] as Array<[Button, Button]>)("should be pressed and released", async (input: Button, expected: Button) => {
197+
// GIVEN
198+
const SUT = new MouseClass(providerRegistryMock);
199+
const pressButtonMock = jest.fn();
200+
const releaseButtonMock = jest.fn();
201+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
202+
setMouseDelay: jest.fn(),
203+
pressButton: pressButtonMock,
204+
releaseButton: releaseButtonMock
205+
}));
206+
207+
// WHEN
208+
const pressed = await SUT.pressButton(input);
209+
const released = await SUT.releaseButton(input);
210+
211+
// THEN
212+
expect(pressButtonMock).toBeCalledWith(expected);
213+
expect(releaseButtonMock).toBeCalledWith(expected);
214+
expect(pressed).toBe(SUT);
215+
expect(released).toBe(SUT);
216+
});
156217
});

0 commit comments

Comments
 (0)