Skip to content

Commit

Permalink
test: reset DTL asyncWrapper before asserting setTimeout calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Jan 12, 2025
1 parent a76db96 commit 139d18f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
23 changes: 23 additions & 0 deletions tests/_helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
4 changes: 3 additions & 1 deletion tests/keyboard/index.ts
Original file line number Diff line number Diff line change
@@ -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('<input/>', {focus: false})
Expand Down Expand Up @@ -113,6 +113,8 @@ test('continue typing with state', async () => {
describe('delay', () => {
const spy = mocks.spyOn(global, 'setTimeout')

beforeAll(() => resetWrappers())

beforeEach(() => {
spy.mockClear()
})
Expand Down
24 changes: 14 additions & 10 deletions tests/keyboard/keyboardAction.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -180,13 +180,17 @@ describe('prevent default behavior', () => {
})
})

test('do not call setTimeout with delay `null`', async () => {
const {user} = setup(`<div></div>`)
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(`<div></div>`)
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()
})
})
4 changes: 3 additions & 1 deletion tests/pointer/index.ts
Original file line number Diff line number Diff line change
@@ -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(`<div></div>`)
Expand Down Expand Up @@ -60,6 +60,8 @@ test('apply modifiers from keyboardstate', async () => {
describe('delay', () => {
const spy = mocks.spyOn(global, 'setTimeout')

beforeAll(() => resetWrappers())

beforeEach(() => {
spy.mockClear()
})
Expand Down

0 comments on commit 139d18f

Please sign in to comment.