diff --git a/src/setup/setup.ts b/src/setup/setup.ts index fc5b2971..b2f7cb3f 100644 --- a/src/setup/setup.ts +++ b/src/setup/setup.ts @@ -8,6 +8,7 @@ import { attachClipboardStubToView, getDocumentFromNode, setLevelRef, + wait, } from '../utils' import type {Instance, UserEvent, UserEventApi} from './index' import {Config} from './config' @@ -80,7 +81,12 @@ function wrapAndBindImpl< function method(...args: Args) { setLevelRef(instance[Config], ApiLevel.Call) - return wrapAsync(() => impl.apply(instance, args)) + return wrapAsync(() => + impl.apply(instance, args).then(async ret => { + await wait(instance[Config]) + return ret + }), + ) } Object.defineProperty(method, 'name', {get: () => impl.name}) diff --git a/tests/keyboard/index.ts b/tests/keyboard/index.ts index 5e8763ec..af482eaf 100644 --- a/tests/keyboard/index.ts +++ b/tests/keyboard/index.ts @@ -123,9 +123,7 @@ describe('delay', () => { const time0 = performance.now() await user.keyboard('foo') - // we don't call delay after the last action - // TODO: Should we call it? - expect(spy).toBeCalledTimes(2) + expect(spy.mock.calls.length).toBeGreaterThanOrEqual(2) expect(time0).toBeLessThan(performance.now() - 20) }) diff --git a/tests/keyboard/keyboardAction.ts b/tests/keyboard/keyboardAction.ts index c937ebff..12dbd95c 100644 --- a/tests/keyboard/keyboardAction.ts +++ b/tests/keyboard/keyboardAction.ts @@ -184,7 +184,9 @@ test('do not call setTimeout with delay `null`', async () => { const {user} = setup(`
`) const spy = jest.spyOn(global, 'setTimeout') await user.keyboard('ab') - expect(spy).toBeCalledTimes(1) + expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1) + + spy.mockClear() await user.setup({delay: null}).keyboard('cd') - expect(spy).toBeCalledTimes(1) + expect(spy).not.toBeCalled() }) diff --git a/tests/pointer/index.ts b/tests/pointer/index.ts index ab9ed688..670ef111 100644 --- a/tests/pointer/index.ts +++ b/tests/pointer/index.ts @@ -73,9 +73,7 @@ describe('delay', () => { '[/MouseLeft]', ]) - // we don't call delay after the last action - // TODO: Should we call it? - expect(spy).toBeCalledTimes(2) + expect(spy.mock.calls.length).toBeGreaterThanOrEqual(2) expect(time0).toBeLessThan(performance.now() - 20) }) diff --git a/tests/setup/_mockApis.ts b/tests/setup/_mockApis.ts index 0879054e..ce1a46bc 100644 --- a/tests/setup/_mockApis.ts +++ b/tests/setup/_mockApis.ts @@ -6,13 +6,12 @@ import {Instance, UserEventApi} from '#src/setup' // `const` are not initialized when mocking is executed, but `function` are when prefixed with `mock` function mockApis() {} // access the `function` as object -type mockApisRefHack = (() => void) & - { - [name in keyof UserEventApi]: { - mock: APIMock - real: UserEventApi[name] - } +type mockApisRefHack = (() => void) & { + [name in keyof UserEventApi]: { + mock: APIMock + real: UserEventApi[name] } +} // make the tests more readable by applying the typecast here export function getSpy(k: keyof UserEventApi) { @@ -34,6 +33,10 @@ interface APIMock this: Instance, ...args: Parameters