Skip to content

Commit 4922f06

Browse files
authored
test(query-core): use fake timers for mutationCache.test.tsx (#8807)
* test(query-core): use fake timers for mutationCache.test.tsx * chore: update
1 parent 730b79a commit 4922f06

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

‎packages/query-core/src/__tests__/mutationCache.test.tsx

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { describe, expect, test, vi } from 'vitest'
2-
import { waitFor } from '@testing-library/dom'
1+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
32
import { MutationCache, MutationObserver } from '..'
43
import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
54

65
describe('mutationCache', () => {
6+
beforeEach(() => {
7+
vi.useFakeTimers()
8+
})
9+
10+
afterEach(() => {
11+
vi.useRealTimers()
12+
})
13+
714
describe('MutationCacheConfig error callbacks', () => {
815
test('should call onError and onSettled when a mutation errors', async () => {
916
const key = queryKey()
@@ -49,12 +56,12 @@ describe('mutationCache', () => {
4956
const states: Array<number> = []
5057
const onError = async () => {
5158
states.push(1)
52-
await sleep(1)
59+
await vi.advanceTimersByTimeAsync(1)
5360
states.push(2)
5461
}
5562
const onSettled = async () => {
5663
states.push(5)
57-
await sleep(1)
64+
await vi.advanceTimersByTimeAsync(1)
5865
states.push(6)
5966
}
6067
const testCache = new MutationCache({ onError, onSettled })
@@ -68,19 +75,20 @@ describe('mutationCache', () => {
6875
mutationFn: () => Promise.reject(new Error('error')),
6976
onError: async () => {
7077
states.push(3)
71-
await sleep(1)
78+
await vi.advanceTimersByTimeAsync(1)
7279
states.push(4)
7380
},
7481
onSettled: async () => {
7582
states.push(7)
76-
await sleep(1)
83+
await vi.advanceTimersByTimeAsync(1)
7784
states.push(8)
7885
},
7986
},
8087
'vars',
8188
)
8289
} catch {}
8390

91+
await vi.runAllTimersAsync()
8492
expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
8593
})
8694
})
@@ -139,7 +147,7 @@ describe('mutationCache', () => {
139147
const testCache = new MutationCache({ onSuccess, onSettled })
140148
const testClient = createQueryClient({ mutationCache: testCache })
141149

142-
await executeMutation(
150+
executeMutation(
143151
testClient,
144152
{
145153
mutationKey: key,
@@ -157,6 +165,7 @@ describe('mutationCache', () => {
157165
},
158166
'vars',
159167
)
168+
await vi.runAllTimersAsync()
160169

161170
expect(states).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
162171
})
@@ -195,7 +204,7 @@ describe('mutationCache', () => {
195204
const testCache = new MutationCache({ onMutate })
196205
const testClient = createQueryClient({ mutationCache: testCache })
197206

198-
await executeMutation(
207+
executeMutation(
199208
testClient,
200209
{
201210
mutationKey: key,
@@ -208,6 +217,7 @@ describe('mutationCache', () => {
208217
},
209218
'vars',
210219
)
220+
await vi.runAllTimersAsync()
211221

212222
expect(states).toEqual([1, 2, 3, 4])
213223
})
@@ -288,7 +298,7 @@ describe('mutationCache', () => {
288298
const testCache = new MutationCache()
289299
const testClient = createQueryClient({ mutationCache: testCache })
290300
const onSuccess = vi.fn()
291-
await executeMutation(
301+
executeMutation(
292302
testClient,
293303
{
294304
mutationKey: ['a', 1],
@@ -298,10 +308,8 @@ describe('mutationCache', () => {
298308
},
299309
1,
300310
)
301-
302311
expect(testCache.getAll()).toHaveLength(1)
303-
await sleep(10)
304-
await waitFor(() => {
312+
await vi.waitFor(() => {
305313
expect(testCache.getAll()).toHaveLength(0)
306314
})
307315
expect(onSuccess).toHaveBeenCalledTimes(1)
@@ -318,12 +326,12 @@ describe('mutationCache', () => {
318326
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
319327
observer.mutate(1)
320328
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
321-
await sleep(10)
329+
await vi.advanceTimersByTimeAsync(10)
322330
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
323331
unsubscribe()
324332
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
325-
await sleep(10)
326-
await waitFor(() => {
333+
await vi.advanceTimersByTimeAsync(10)
334+
await vi.waitFor(() => {
327335
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
328336
})
329337
})
@@ -343,12 +351,12 @@ describe('mutationCache', () => {
343351
observer.mutate(1)
344352
unsubscribe()
345353
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
346-
await sleep(10)
354+
await vi.advanceTimersByTimeAsync(10)
347355
// unsubscribe should not remove even though gcTime has elapsed b/c mutation is still pending
348356
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
349-
await sleep(10)
357+
await vi.advanceTimersByTimeAsync(10)
350358
// should be removed after an additional gcTime wait
351-
await waitFor(() => {
359+
await vi.waitFor(() => {
352360
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
353361
})
354362
expect(onSuccess).toHaveBeenCalledTimes(1)
@@ -367,7 +375,7 @@ describe('mutationCache', () => {
367375
const unsubscribe = observer.subscribe(() => undefined)
368376
observer.mutate(1)
369377
unsubscribe()
370-
await waitFor(() => {
378+
await vi.waitFor(() => {
371379
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
372380
})
373381
expect(onSuccess).toHaveBeenCalledTimes(1)

0 commit comments

Comments
 (0)