diff --git a/src/keyboard/keyboardAction.ts b/src/keyboard/keyboardAction.ts index 2a0f444e..d4887978 100644 --- a/src/keyboard/keyboardAction.ts +++ b/src/keyboard/keyboardAction.ts @@ -22,8 +22,8 @@ export async function keyboardAction( for (let i = 0; i < actions.length; i++) { await keyboardKeyAction(config, actions[i]) - if (typeof config.delay === 'number' && i < actions.length - 1) { - await wait(config.delay) + if (i < actions.length - 1) { + await wait(config) } } } @@ -32,7 +32,7 @@ async function keyboardKeyAction( config: Config, {keyDef, releasePrevious, releaseSelf, repeat}: KeyboardAction, ) { - const {document, keyboardState, delay} = config + const {document, keyboardState} = config const getCurrentElement = () => getActive(document) // Release the key automatically if it was pressed before. @@ -50,8 +50,8 @@ async function keyboardKeyAction( await keypress(keyDef, getCurrentElement, config) } - if (typeof delay === 'number' && i < repeat) { - await wait(delay) + if (i < repeat) { + await wait(config) } } diff --git a/src/options.ts b/src/options.ts index 8d91e879..1e970b0c 100644 --- a/src/options.ts +++ b/src/options.ts @@ -120,6 +120,13 @@ export interface Options { * Defaults to `true` when calling the APIs per `setup`. */ writeToClipboard?: boolean + + /** + * A function to be called internally to advance your fake timers (if applicable) + * + * @example jest.advanceTimersByTime + */ + advanceTimers?: ((delay: number) => Promise) | ((delay: number) => void) } /** @@ -137,6 +144,7 @@ export const defaultOptionsDirect: Required = { skipClick: false, skipHover: false, writeToClipboard: false, + advanceTimers: () => Promise.resolve(), } /** diff --git a/src/pointer/pointerAction.ts b/src/pointer/pointerAction.ts index d5323c2e..7ca73410 100644 --- a/src/pointer/pointerAction.ts +++ b/src/pointer/pointerAction.ts @@ -37,10 +37,8 @@ export async function pointerAction(config: Config, actions: PointerAction[]) { ? pointerPress(config, {...action, target, coords}) : pointerMove(config, {...action, target, coords})) - if (typeof config.delay === 'number') { - if (i < actions.length - 1) { - await wait(config.delay) - } + if (i < actions.length - 1) { + await wait(config) } } diff --git a/src/utils/misc/wait.ts b/src/utils/misc/wait.ts index 69ad59b5..aac99961 100644 --- a/src/utils/misc/wait.ts +++ b/src/utils/misc/wait.ts @@ -1,3 +1,12 @@ -export function wait(time?: number) { - return new Promise(resolve => setTimeout(() => resolve(), time)) +import {Config} from '../../setup' + +export function wait(config: Config) { + const delay = config.delay + if (typeof delay !== 'number') { + return + } + return Promise.all([ + new Promise(resolve => setTimeout(() => resolve(), delay)), + config.advanceTimers(delay), + ]) } diff --git a/tests/utils/misc/wait.ts b/tests/utils/misc/wait.ts new file mode 100644 index 00000000..3f88f9ce --- /dev/null +++ b/tests/utils/misc/wait.ts @@ -0,0 +1,9 @@ +import {wait} from '#src/utils/misc/wait' + +test('advances timers when set', async () => { + jest.useFakeTimers() + jest.setTimeout(50) + // If this wasn't advancing fake timers, we'd timeout and fail the test + await wait(10000, jest.advanceTimersByTime) + jest.useRealTimers() +})