Skip to content

Commit 8bf2214

Browse files
sukvvonmanudeli
andauthored
test(react-query/useMutationState): remove 'vi.waitFor' and add 'advanceTimersByTimeAsync' (#9353)
Co-authored-by: Jonghyeon Ko <[email protected]>
1 parent 3eebcff commit 8bf2214

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ describe('useIsMutating', () => {
6262
// [ +0, 1, 2, 1, +0 ]
6363
// our batching strategy might yield different results
6464

65-
await vi.waitFor(() => expect(isMutatingArray[0]).toEqual(0))
66-
await vi.waitFor(() => expect(isMutatingArray[1]).toEqual(1))
67-
await vi.waitFor(() => expect(isMutatingArray[2]).toEqual(2))
68-
await vi.waitFor(() =>
69-
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0),
70-
)
65+
await vi.advanceTimersByTimeAsync(40)
66+
expect(isMutatingArray[0]).toEqual(0)
67+
expect(isMutatingArray[1]).toEqual(1)
68+
expect(isMutatingArray[2]).toEqual(2)
69+
await vi.advanceTimersByTimeAsync(1)
70+
expect(isMutatingArray[isMutatingArray.length - 1]).toEqual(0)
7171
})
7272

7373
it('should filter correctly by mutationKey', async () => {
@@ -99,7 +99,8 @@ describe('useIsMutating', () => {
9999
}
100100

101101
renderWithClient(queryClient, <Page />)
102-
await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
102+
await vi.advanceTimersByTimeAsync(101)
103+
expect(isMutatingArray).toEqual([0, 1, 0])
103104
})
104105

105106
it('should filter correctly by predicate', async () => {
@@ -134,7 +135,8 @@ describe('useIsMutating', () => {
134135
}
135136

136137
renderWithClient(queryClient, <Page />)
137-
await vi.waitFor(() => expect(isMutatingArray).toEqual([0, 1, 0]))
138+
await vi.advanceTimersByTimeAsync(101)
139+
expect(isMutatingArray).toEqual([0, 1, 0])
138140
})
139141

140142
it('should use provided custom queryClient', async () => {
@@ -163,13 +165,20 @@ describe('useIsMutating', () => {
163165

164166
const rendered = render(<Page></Page>)
165167

166-
await vi.waitFor(() =>
167-
expect(rendered.getByText('mutating: 1')).toBeInTheDocument(),
168-
)
168+
await vi.advanceTimersByTimeAsync(0)
169+
expect(rendered.getByText('mutating: 1')).toBeInTheDocument()
169170
})
170171
})
171172

172173
describe('useMutationState', () => {
174+
beforeEach(() => {
175+
vi.useFakeTimers()
176+
})
177+
178+
afterEach(() => {
179+
vi.useRealTimers()
180+
})
181+
173182
it('should return variables after calling mutate', async () => {
174183
const queryClient = new QueryClient()
175184
const variables: Array<Array<unknown>> = []
@@ -211,11 +220,12 @@ describe('useMutationState', () => {
211220

212221
const rendered = renderWithClient(queryClient, <Page />)
213222

214-
await vi.waitFor(() => rendered.getByText('data: null'))
223+
rendered.getByText('data: null')
215224

216225
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
217226

218-
await vi.waitFor(() => rendered.getByText('data: data1'))
227+
await vi.advanceTimersByTimeAsync(151)
228+
rendered.getByText('data: data1')
219229

220230
expect(variables).toEqual([[], [1], []])
221231
})

0 commit comments

Comments
 (0)