Skip to content

Commit 509064a

Browse files
sukvvonmanudeli
andauthored
test(solid-query/useIsMutating): switch to fake timers and replace 'waitFor' with 'vi.waitFor' (#9378)
Co-authored-by: Jonghyeon Ko <[email protected]>
1 parent 27d82a7 commit 509064a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

packages/solid-query/src/__tests__/useIsMutating.test.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, expect, it, vi } from 'vitest'
2-
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { fireEvent, render } from '@solidjs/testing-library'
33
import { Show, createEffect, createRenderEffect, createSignal } from 'solid-js'
44
import * as QueryCore from '@tanstack/query-core'
55
import { sleep } from '@tanstack/query-test-utils'
@@ -12,6 +12,14 @@ import {
1212
import { setActTimeout } from './utils'
1313

1414
describe('useIsMutating', () => {
15+
beforeEach(() => {
16+
vi.useFakeTimers()
17+
})
18+
19+
afterEach(() => {
20+
vi.useRealTimers()
21+
})
22+
1523
it('should return the number of fetching mutations', async () => {
1624
const isMutatingArray: Array<number> = []
1725
const queryClient = new QueryClient()
@@ -64,7 +72,7 @@ describe('useIsMutating', () => {
6472
<Page />
6573
</QueryClientProvider>
6674
))
67-
await waitFor(() => expect(isMutatingArray).toEqual([0, 1, 2, 1, 0]))
75+
await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 2, 1, 0]))
6876
})
6977

7078
it('should filter correctly by mutationKey', async () => {
@@ -109,7 +117,7 @@ describe('useIsMutating', () => {
109117
</QueryClientProvider>
110118
))
111119
// Unlike React, IsMutating Wont re-render twice with mutation2
112-
await waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
120+
await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
113121
})
114122

115123
it('should filter correctly by predicate', async () => {
@@ -158,7 +166,7 @@ describe('useIsMutating', () => {
158166
))
159167

160168
// Again, No unnecessary re-renders like React
161-
await waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
169+
await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
162170
})
163171

164172
it('should use provided custom queryClient', async () => {
@@ -188,7 +196,7 @@ describe('useIsMutating', () => {
188196

189197
const rendered = render(() => <Page></Page>)
190198

191-
await waitFor(() =>
199+
await vi.waitFor(() =>
192200
expect(rendered.getByText('mutating: 1')).toBeInTheDocument(),
193201
)
194202
})
@@ -251,7 +259,7 @@ describe('useIsMutating', () => {
251259
// Should not display the console error
252260
// "Warning: Can't perform a React state update on an unmounted component"
253261

254-
await sleep(20)
262+
await vi.advanceTimersByTimeAsync(20)
255263
MutationCacheSpy.mockRestore()
256264
})
257265
})

0 commit comments

Comments
 (0)