1
- import { describe , expect , it , vi } from 'vitest'
1
+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
2
2
import { QueryClient } from '@tanstack/query-core'
3
3
import { sleep } from '@tanstack/query-test-utils'
4
4
import { fireEvent } from '@testing-library/react'
@@ -8,6 +8,14 @@ import { renderWithClient } from './utils'
8
8
import type { MutationState } from '@tanstack/query-core'
9
9
10
10
describe ( 'mutationOptions' , ( ) => {
11
+ beforeEach ( ( ) => {
12
+ vi . useFakeTimers ( )
13
+ } )
14
+
15
+ afterEach ( ( ) => {
16
+ vi . useRealTimers ( )
17
+ } )
18
+
11
19
it ( 'should return the object received as a parameter without any modification.' , ( ) => {
12
20
const object = {
13
21
mutationKey : [ 'key' ] ,
@@ -54,12 +62,12 @@ describe('mutationOptions', () => {
54
62
const rendered = renderWithClient ( queryClient , < Page /> )
55
63
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
56
64
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 )
63
71
} )
64
72
65
73
it ( 'should return the number of fetching mutations when used with queryClient.isMutating' , async ( ) => {
@@ -86,12 +94,12 @@ describe('mutationOptions', () => {
86
94
const rendered = renderWithClient ( queryClient , < Mutation /> )
87
95
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
88
96
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 )
95
103
} )
96
104
97
105
it ( 'should return the number of fetching mutations when used with useMutationState' , async ( ) => {
@@ -122,7 +130,8 @@ describe('mutationOptions', () => {
122
130
const rendered = renderWithClient ( queryClient , < Mutation /> )
123
131
fireEvent . click ( rendered . getByRole ( 'button' , { name : / m u t a t e / i } ) )
124
132
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' )
127
136
} )
128
137
} )
0 commit comments