diff --git a/tests/_helpers/index.ts b/tests/_helpers/index.ts index 90d047dc..ccde82ca 100644 --- a/tests/_helpers/index.ts +++ b/tests/_helpers/index.ts @@ -1,9 +1,32 @@ // this is pretty helpful: // https://codesandbox.io/s/quizzical-worker-eo909 +import { configure, getConfig } from '@testing-library/dom' + export {render, setup} from './setup' export {addEventListener, addListeners} from './listeners' export function isJsdomEnv() { return window.navigator.userAgent.includes(' jsdom/') } + +/** + * Reset the DTL wrappers + * + * Framework libraries configure the wrappers in DTL as side effect when being imported. + * In the Toolbox testenvs that side effect is triggered by including the library as a setup file. + */ +export function resetWrappers() { + // eslint-disable-next-line @typescript-eslint/unbound-method + const { asyncWrapper, eventWrapper } = {...getConfig()} + + configure({ + asyncWrapper: (cb) => cb(), + eventWrapper: (cb) => cb(), + }) + + return () => configure({ + asyncWrapper, + eventWrapper, + }) +} diff --git a/tests/keyboard/index.ts b/tests/keyboard/index.ts index 32ec1133..7ecdfd87 100644 --- a/tests/keyboard/index.ts +++ b/tests/keyboard/index.ts @@ -1,5 +1,5 @@ import userEvent from '#src' -import {addListeners, render, setup} from '#testHelpers' +import {addListeners, render, resetWrappers, setup} from '#testHelpers' test('type without focus', async () => { const {element, user} = setup('', {focus: false}) @@ -113,6 +113,8 @@ test('continue typing with state', async () => { describe('delay', () => { const spy = mocks.spyOn(global, 'setTimeout') + beforeAll(() => resetWrappers()) + beforeEach(() => { spy.mockClear() }) diff --git a/tests/keyboard/keyboardAction.ts b/tests/keyboard/keyboardAction.ts index 3d4ccf55..c7b29db7 100644 --- a/tests/keyboard/keyboardAction.ts +++ b/tests/keyboard/keyboardAction.ts @@ -1,6 +1,6 @@ import cases from 'jest-in-case' import userEvent from '#src' -import {render, setup} from '#testHelpers' +import {render, resetWrappers, setup} from '#testHelpers' // Maybe this should not trigger keypress event on HTMLAnchorElement // see https://github.com/testing-library/user-event/issues/589 @@ -180,13 +180,17 @@ describe('prevent default behavior', () => { }) }) -test('do not call setTimeout with delay `null`', async () => { - const {user} = setup(`
`) - const spy = mocks.spyOn(global, 'setTimeout') - await user.keyboard('ab') - expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1) - - spy.mockClear() - await user.setup({delay: null}).keyboard('cd') - expect(spy).not.toBeCalled() +describe('delay', () => { + beforeAll(() => resetWrappers()) + + test('do not call setTimeout with delay `null`', async () => { + const {user} = setup(``) + const spy = mocks.spyOn(global, 'setTimeout') + await user.keyboard('ab') + expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1) + + spy.mockClear() + await user.setup({delay: null}).keyboard('cd') + expect(spy).not.toBeCalled() + }) }) diff --git a/tests/pointer/index.ts b/tests/pointer/index.ts index b512fc16..e89855db 100644 --- a/tests/pointer/index.ts +++ b/tests/pointer/index.ts @@ -1,6 +1,6 @@ import {type SpyInstance} from 'jest-mock' import {PointerEventsCheckLevel} from '#src' -import {setup} from '#testHelpers' +import {resetWrappers, setup} from '#testHelpers' test('continue previous target', async () => { const {element, getEvents, user} = setup(``) @@ -60,6 +60,8 @@ test('apply modifiers from keyboardstate', async () => { describe('delay', () => { const spy = mocks.spyOn(global, 'setTimeout') + beforeAll(() => resetWrappers()) + beforeEach(() => { spy.mockClear() })