Skip to content

Commit 03d4923

Browse files
authored
(#390) Refactored mouse class such that leftClick and rightClick reuse click (#391)
1 parent 752aa4f commit 03d4923

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

Diff for: lib/mouse.class.spec.ts

+45-37
Original file line numberDiff line numberDiff line change
@@ -107,42 +107,6 @@ describe("Mouse class", () => {
107107
expect(result).toBe(SUT);
108108
});
109109

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-
146110
it("update mouse position along path on move", async () => {
147111
// GIVEN
148112
const SUT = new MouseClass(providerRegistryMock);
@@ -259,5 +223,49 @@ describe("Mouse class", () => {
259223
expect(clickMock).toBeCalledWith(expected);
260224
});
261225
});
262-
})
226+
227+
describe("leftClick", () => {
228+
it("should use click internally", async () => {
229+
// GIVEN
230+
const SUT = new MouseClass(providerRegistryMock);
231+
232+
const clickSpy = jest.spyOn(SUT, "click");
233+
const clickMock = jest.fn();
234+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
235+
setMouseDelay: jest.fn(),
236+
click: clickMock
237+
}));
238+
239+
// WHEN
240+
const result = await SUT.leftClick();
241+
242+
// THEN
243+
expect(clickSpy).toBeCalledWith(Button.LEFT);
244+
expect(clickMock).toBeCalledWith(Button.LEFT);
245+
expect(result).toBe(SUT);
246+
});
247+
});
248+
249+
describe("rightClick", () => {
250+
it("should use click internally", async () => {
251+
// GIVEN
252+
const SUT = new MouseClass(providerRegistryMock);
253+
254+
const clickSpy = jest.spyOn(SUT, "click");
255+
const clickMock = jest.fn();
256+
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
257+
setMouseDelay: jest.fn(),
258+
click: clickMock
259+
}));
260+
261+
// WHEN
262+
const result = await SUT.rightClick();
263+
264+
// THEN
265+
expect(clickSpy).toBeCalledWith(Button.RIGHT);
266+
expect(clickMock).toBeCalledWith(Button.RIGHT);
267+
expect(result).toBe(SUT);
268+
});
269+
});
270+
});
263271
});

Diff for: lib/mouse.class.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,14 @@ export class MouseClass {
8383
* {@link leftClick} performs a click with the left mouse button
8484
*/
8585
public async leftClick(): Promise<MouseClass> {
86-
return new Promise<MouseClass>(async resolve => {
87-
await sleep(this.config.autoDelayMs);
88-
await this.providerRegistry.getMouse().leftClick();
89-
resolve(this);
90-
});
86+
return this.click(Button.LEFT);
9187
}
9288

9389
/**
9490
* {@link rightClick} performs a click with the right mouse button
9591
*/
9692
public async rightClick(): Promise<MouseClass> {
97-
return new Promise<MouseClass>(async (resolve, reject) => {
98-
try {
99-
await sleep(this.config.autoDelayMs);
100-
await this.providerRegistry.getMouse().rightClick();
101-
resolve(this);
102-
} catch (e) {
103-
reject(e);
104-
}
105-
});
93+
return this.click(Button.RIGHT);
10694
}
10795

10896
/**

0 commit comments

Comments
 (0)