Skip to content

Commit

Permalink
(#403) Both pressButton and releaseButton now respect configured …
Browse files Browse the repository at this point in the history
…autoDelay (#404)
  • Loading branch information
s1hofmann authored May 17, 2022
1 parent b410d7f commit a148207
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/mouse.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,50 @@ describe("Mouse class", () => {
expect(pressed).toBe(SUT);
expect(released).toBe(SUT);
});

describe("autoDelayMs", () => {
it("pressButton should respect configured delay", async () => {
// GIVEN
const SUT = new MouseClass(providerRegistryMock);
const delay = 100;
SUT.config.autoDelayMs = delay;

const mouseMock = jest.fn();
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
setMouseDelay: jest.fn(),
pressButton: mouseMock
}));

// WHEN
const start = Date.now();
await SUT.pressButton(Button.LEFT);
const duration = Date.now() - start;

// THEN
expect(duration).toBeGreaterThanOrEqual(delay);
});

it("releaseButton should respect configured delay", async () => {
// GIVEN
const SUT = new MouseClass(providerRegistryMock);
const delay = 100;
SUT.config.autoDelayMs = delay;

const mouseMock = jest.fn();
providerRegistryMock.getMouse = jest.fn(() => mockPartial<MouseProviderInterface>({
setMouseDelay: jest.fn(),
releaseButton: mouseMock
}));

// WHEN
const start = Date.now();
await SUT.releaseButton(Button.LEFT);
const duration = Date.now() - start;

// THEN
expect(duration).toBeGreaterThanOrEqual(delay);
});
});
});

describe("click and doubleClick", () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/mouse.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class MouseClass {
public async pressButton(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().pressButton(btn);
resolve(this);
} catch (e) {
Expand All @@ -202,6 +203,7 @@ export class MouseClass {
public async releaseButton(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().releaseButton(btn);
resolve(this);
} catch (e) {
Expand All @@ -217,6 +219,7 @@ export class MouseClass {
public async click(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().click(btn);
resolve(this);
} catch (e) {
Expand All @@ -232,6 +235,7 @@ export class MouseClass {
public async doubleClick(btn: Button): Promise<MouseClass> {
return new Promise<MouseClass>(async (resolve, reject) => {
try {
await sleep(this.config.autoDelayMs);
await this.providerRegistry.getMouse().doubleClick(btn);
resolve(this);
} catch (e) {
Expand Down

0 comments on commit a148207

Please sign in to comment.