Skip to content

Commit 5765378

Browse files
sukvvonmanudeli
andauthored
test(react-query/mutationOptions): switch to fake timers, remove 'vi.waitFor', and add 'advanceTimersByTimeAsync' (#9397)
Co-authored-by: Jonghyeon Ko <[email protected]>
1 parent f580f08 commit 5765378

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

packages/react-query/src/__tests__/mutationOptions.test.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { QueryClient } from '@tanstack/query-core'
33
import { sleep } from '@tanstack/query-test-utils'
44
import { fireEvent } from '@testing-library/react'
@@ -8,6 +8,14 @@ import { renderWithClient } from './utils'
88
import type { MutationState } from '@tanstack/query-core'
99

1010
describe('mutationOptions', () => {
11+
beforeEach(() => {
12+
vi.useFakeTimers()
13+
})
14+
15+
afterEach(() => {
16+
vi.useRealTimers()
17+
})
18+
1119
it('should return the object received as a parameter without any modification.', () => {
1220
const object = {
1321
mutationKey: ['key'],
@@ -54,12 +62,12 @@ describe('mutationOptions', () => {
5462
const rendered = renderWithClient(queryClient, <Page />)
5563
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
5664

57-
await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
58-
await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
59-
await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(0))
60-
await vi.waitFor(() =>
61-
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
62-
)
65+
expect(isMutatingArray[0]).toEqual(0)
66+
await vi.advanceTimersByTimeAsync(0)
67+
expect(isMutatingArray[1]).toEqual(1)
68+
await vi.advanceTimersByTimeAsync(51)
69+
expect(isMutatingArray[2]).toEqual(0)
70+
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
6371
})
6472

6573
it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
@@ -86,12 +94,12 @@ describe('mutationOptions', () => {
8694
const rendered = renderWithClient(queryClient, <Mutation />)
8795
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
8896

89-
await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
90-
await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
91-
await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(0))
92-
await vi.waitFor(() =>
93-
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
94-
)
97+
expect(isMutatingArray[0]).toEqual(0)
98+
await vi.advanceTimersByTimeAsync(0)
99+
expect(isMutatingArray[1]).toEqual(1)
100+
await vi.advanceTimersByTimeAsync(501)
101+
expect(isMutatingArray[2]).toEqual(0)
102+
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
95103
})
96104

97105
it('should return the number of fetching mutations when used with useMutationState', async () => {
@@ -122,7 +130,8 @@ describe('mutationOptions', () => {
122130
const rendered = renderWithClient(queryClient, <Mutation />)
123131
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
124132

125-
await vi.waitFor(() => expect(mutationStateArray.length).toEqual(1))
126-
await vi.waitFor(() => expect(mutationStateArray[0]?.data).toEqual('data'))
133+
await vi.advanceTimersByTimeAsync(0)
134+
expect(mutationStateArray.length).toEqual(1)
135+
expect(mutationStateArray[0]?.data).toEqual('data')
127136
})
128137
})

0 commit comments

Comments
 (0)