Skip to content

Commit

Permalink
feat: add advanceTimers option (#907)
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Fritsche <ph.fritsche@gmail.com>
  • Loading branch information
CreativeTechGuy and ph-fritsche committed Apr 11, 2022
1 parent 2b9d00f commit 627a5cf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/keyboard/keyboardAction.ts
Expand Up @@ -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)
}
}
}
Expand All @@ -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.
Expand All @@ -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)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/options.ts
Expand Up @@ -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<void>) | ((delay: number) => void)
}

/**
Expand All @@ -137,6 +144,7 @@ export const defaultOptionsDirect: Required<Options> = {
skipClick: false,
skipHover: false,
writeToClipboard: false,
advanceTimers: () => Promise.resolve(),
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/pointer/pointerAction.ts
Expand Up @@ -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)
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/utils/misc/wait.ts
@@ -1,3 +1,12 @@
export function wait(time?: number) {
return new Promise<void>(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<void>(resolve => setTimeout(() => resolve(), delay)),
config.advanceTimers(delay),
])
}
9 changes: 9 additions & 0 deletions 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()
})

0 comments on commit 627a5cf

Please sign in to comment.