Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59f5081

Browse files
author
Sebastian Silbermann
committed
f
1 parent 4bff80b commit 59f5081

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`waitFor DOM reference implementation using fake legacy timers timeout 1`] = `Not done`;
4+
5+
exports[`waitFor DOM reference implementation using fake modern timers timeout 1`] = `Not done`;
6+
7+
exports[`waitFor DOM reference implementation using real timers timeout 1`] = `Not done`;

‎src/__tests__/waitFor.test.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,25 @@ test('does not work after it resolves', async () => {
131131
// @testing-library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
132132
advanceTimersByTime: async timeoutMS => {
133133
const originalContext = context
134-
context = 'no-act'
134+
context = 'act'
135135
try {
136136
jest.advanceTimersByTime(timeoutMS)
137137
} finally {
138138
context = originalContext
139139
}
140140
},
141+
flushPromises: async () => {
142+
const originalContext = context
143+
context = 'no-act'
144+
try {
145+
await await new Promise(r => {
146+
setTimeout(r, 0)
147+
jest.advanceTimersByTime(0)
148+
})
149+
} finally {
150+
context = originalContext
151+
}
152+
},
141153
}
142154

143155
let data = null
@@ -161,3 +173,75 @@ test('does not work after it resolves', async () => {
161173

162174
expect(context).toEqual('initial')
163175
})
176+
177+
/** @type {import('../').FakeClock} */
178+
const jestFakeClock = {
179+
advanceTimersByTime: timeoutMS => {
180+
jest.advanceTimersByTime(timeoutMS)
181+
},
182+
flushPromises: () => {
183+
return new Promise(r => {
184+
setTimeout(r, 0)
185+
jest.advanceTimersByTime(0)
186+
})
187+
},
188+
}
189+
describe.each([
190+
['real timers', {useTimers: () => jest.useRealTimers(), clock: undefined}],
191+
[
192+
'fake legacy timers',
193+
{useTimers: () => jest.useFakeTimers('legacy'), clock: jestFakeClock},
194+
],
195+
[
196+
'fake modern timers',
197+
{useTimers: () => jest.useFakeTimers('modern'), clock: jestFakeClock},
198+
],
199+
])(
200+
'waitFor DOM reference implementation using %s',
201+
(label, {useTimers, clock}) => {
202+
beforeEach(() => {
203+
useTimers()
204+
})
205+
206+
afterEach(() => {
207+
jest.useRealTimers()
208+
})
209+
210+
test('void callback', async () => {
211+
await expect(waitFor(() => {}, {clock})).resolves.toBeUndefined()
212+
})
213+
214+
test('callback passes after timeout', async () => {
215+
let state = 'pending'
216+
setTimeout(() => {
217+
state = 'done'
218+
}, 10)
219+
220+
await expect(
221+
waitFor(
222+
() => {
223+
if (state !== 'done') {
224+
throw new Error('Not done')
225+
}
226+
},
227+
{clock, interval: 5},
228+
),
229+
).resolves.toBeUndefined()
230+
})
231+
232+
test('timeout', async () => {
233+
const state = 'pending'
234+
235+
await expect(
236+
waitFor(
237+
() => {
238+
if (state !== 'done') {
239+
throw new Error('Not done')
240+
}
241+
},
242+
{clock, timeout: 10},
243+
),
244+
).rejects.toThrowErrorMatchingSnapshot()
245+
})
246+
},
247+
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /