Skip to content

Commit 627a5cf

Browse files
feat: add advanceTimers option (#907)
Co-authored-by: Philipp Fritsche <[email protected]>
1 parent 2b9d00f commit 627a5cf

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

src/keyboard/keyboardAction.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export async function keyboardAction(
2222
for (let i = 0; i < actions.length; i++) {
2323
await keyboardKeyAction(config, actions[i])
2424

25-
if (typeof config.delay === 'number' && i < actions.length - 1) {
26-
await wait(config.delay)
25+
if (i < actions.length - 1) {
26+
await wait(config)
2727
}
2828
}
2929
}
@@ -32,7 +32,7 @@ async function keyboardKeyAction(
3232
config: Config,
3333
{keyDef, releasePrevious, releaseSelf, repeat}: KeyboardAction,
3434
) {
35-
const {document, keyboardState, delay} = config
35+
const {document, keyboardState} = config
3636
const getCurrentElement = () => getActive(document)
3737

3838
// Release the key automatically if it was pressed before.
@@ -50,8 +50,8 @@ async function keyboardKeyAction(
5050
await keypress(keyDef, getCurrentElement, config)
5151
}
5252

53-
if (typeof delay === 'number' && i < repeat) {
54-
await wait(delay)
53+
if (i < repeat) {
54+
await wait(config)
5555
}
5656
}
5757

src/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ export interface Options {
120120
* Defaults to `true` when calling the APIs per `setup`.
121121
*/
122122
writeToClipboard?: boolean
123+
124+
/**
125+
* A function to be called internally to advance your fake timers (if applicable)
126+
*
127+
* @example jest.advanceTimersByTime
128+
*/
129+
advanceTimers?: ((delay: number) => Promise<void>) | ((delay: number) => void)
123130
}
124131

125132
/**
@@ -137,6 +144,7 @@ export const defaultOptionsDirect: Required<Options> = {
137144
skipClick: false,
138145
skipHover: false,
139146
writeToClipboard: false,
147+
advanceTimers: () => Promise.resolve(),
140148
}
141149

142150
/**

src/pointer/pointerAction.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ export async function pointerAction(config: Config, actions: PointerAction[]) {
3737
? pointerPress(config, {...action, target, coords})
3838
: pointerMove(config, {...action, target, coords}))
3939

40-
if (typeof config.delay === 'number') {
41-
if (i < actions.length - 1) {
42-
await wait(config.delay)
43-
}
40+
if (i < actions.length - 1) {
41+
await wait(config)
4442
}
4543
}
4644

src/utils/misc/wait.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
export function wait(time?: number) {
2-
return new Promise<void>(resolve => setTimeout(() => resolve(), time))
1+
import {Config} from '../../setup'
2+
3+
export function wait(config: Config) {
4+
const delay = config.delay
5+
if (typeof delay !== 'number') {
6+
return
7+
}
8+
return Promise.all([
9+
new Promise<void>(resolve => setTimeout(() => resolve(), delay)),
10+
config.advanceTimers(delay),
11+
])
312
}

tests/utils/misc/wait.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {wait} from '#src/utils/misc/wait'
2+
3+
test('advances timers when set', async () => {
4+
jest.useFakeTimers()
5+
jest.setTimeout(50)
6+
// If this wasn't advancing fake timers, we'd timeout and fail the test
7+
await wait(10000, jest.advanceTimersByTime)
8+
jest.useRealTimers()
9+
})

0 commit comments

Comments
 (0)