Skip to content

Commit 82e9545

Browse files
committed
(#188) Both pressKey and releaseKey should respect a configured delay
1 parent fdddf52 commit 82e9545

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/keyboard.class.spec.ts

+44
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,48 @@ describe("Keyboard", () => {
149149
// THEN
150150
expect(keyMock).toHaveBeenCalledTimes(payload.length);
151151
});
152+
153+
describe("autoDelayMs", () => {
154+
it("pressKey should respect configured delay", async () => {
155+
// GIVEN
156+
const SUT = new KeyboardClass(providerRegistryMock);
157+
const delay = 100;
158+
SUT.config.autoDelayMs = delay;
159+
160+
const keyMock = jest.fn();
161+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
162+
setKeyboardDelay: jest.fn(),
163+
pressKey: keyMock
164+
}));
165+
166+
// WHEN
167+
const start = Date.now();
168+
await SUT.pressKey(Key.A);
169+
const duration = Date.now() - start;
170+
171+
// THEN
172+
expect(duration).toBeGreaterThanOrEqual(delay);
173+
});
174+
175+
it("should pass a list of input keys down to the releaseKey call.", async () => {
176+
// GIVEN
177+
const SUT = new KeyboardClass(providerRegistryMock);
178+
const delay = 100;
179+
SUT.config.autoDelayMs = delay;
180+
181+
const keyMock = jest.fn();
182+
providerRegistryMock.getKeyboard = jest.fn(() => mockPartial<KeyboardProviderInterface>({
183+
setKeyboardDelay: jest.fn(),
184+
releaseKey: keyMock
185+
}));
186+
187+
// WHEN
188+
const start = Date.now();
189+
await SUT.releaseKey(Key.A);
190+
const duration = Date.now() - start;
191+
192+
// THEN
193+
expect(duration).toBeGreaterThanOrEqual(delay);
194+
});
195+
});
152196
});

lib/keyboard.class.ts

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class KeyboardClass {
7373
public pressKey(...keys: Key[]): Promise<KeyboardClass> {
7474
return new Promise<KeyboardClass>(async (resolve, reject) => {
7575
try {
76+
await sleep(this.config.autoDelayMs);
7677
await this.providerRegistry.getKeyboard().pressKey(...keys);
7778
resolve(this);
7879
} catch (e) {
@@ -95,6 +96,7 @@ export class KeyboardClass {
9596
public releaseKey(...keys: Key[]): Promise<KeyboardClass> {
9697
return new Promise<KeyboardClass>(async (resolve, reject) => {
9798
try {
99+
await sleep(this.config.autoDelayMs);
98100
await this.providerRegistry.getKeyboard().releaseKey(...keys);
99101
resolve(this);
100102
} catch (e) {

0 commit comments

Comments
 (0)