Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add advanceTimers option #907

Merged
merged 4 commits into from Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})