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 11, 2025
1 parent 1808932 commit 5217161
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 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
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 5217161

Please sign in to comment.