1
1
import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2
- import { waitFor } from '@testing-library/dom'
3
2
import { MutationObserver } from '..'
4
3
import { createQueryClient , queryKey , sleep } from './utils'
5
4
import type { QueryClient } from '..'
@@ -8,20 +7,19 @@ describe('mutationObserver', () => {
8
7
let queryClient : QueryClient
9
8
10
9
beforeEach ( ( ) => {
10
+ vi . useFakeTimers ( )
11
11
queryClient = createQueryClient ( )
12
12
queryClient . mount ( )
13
13
} )
14
14
15
15
afterEach ( ( ) => {
16
16
queryClient . clear ( )
17
+ vi . useRealTimers ( )
17
18
} )
18
19
19
20
test ( 'onUnsubscribe should not remove the current mutation observer if there is still a subscription' , async ( ) => {
20
21
const mutation = new MutationObserver ( queryClient , {
21
- mutationFn : async ( text : string ) => {
22
- await sleep ( 20 )
23
- return text
24
- } ,
22
+ mutationFn : ( text : string ) => sleep ( 20 ) . then ( ( ) => text ) ,
25
23
} )
26
24
27
25
const subscription1Handler = vi . fn ( )
@@ -34,7 +32,7 @@ describe('mutationObserver', () => {
34
32
35
33
unsubscribe1 ( )
36
34
37
- await waitFor ( ( ) => {
35
+ await vi . waitFor ( ( ) => {
38
36
// 1 call: loading
39
37
expect ( subscription1Handler ) . toBeCalledTimes ( 1 )
40
38
// 2 calls: loading, success
@@ -47,48 +45,44 @@ describe('mutationObserver', () => {
47
45
48
46
test ( 'unsubscribe should remove observer to trigger GC' , async ( ) => {
49
47
const mutation = new MutationObserver ( queryClient , {
50
- mutationFn : async ( text : string ) => {
51
- await sleep ( 5 )
52
- return text
53
- } ,
48
+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
54
49
gcTime : 10 ,
55
50
} )
56
51
57
52
const subscriptionHandler = vi . fn ( )
58
53
59
54
const unsubscribe = mutation . subscribe ( subscriptionHandler )
60
55
61
- await mutation . mutate ( 'input' )
62
-
63
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 )
56
+ mutation . mutate ( 'input' )
57
+ await vi . waitFor ( ( ) =>
58
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 ) ,
59
+ )
64
60
65
61
unsubscribe ( )
66
62
67
- await waitFor ( ( ) =>
63
+ await vi . waitFor ( ( ) =>
68
64
expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
69
65
)
70
66
} )
71
67
72
68
test ( 'reset should remove observer to trigger GC' , async ( ) => {
73
69
const mutation = new MutationObserver ( queryClient , {
74
- mutationFn : async ( text : string ) => {
75
- await sleep ( 5 )
76
- return text
77
- } ,
70
+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
78
71
gcTime : 10 ,
79
72
} )
80
73
81
74
const subscriptionHandler = vi . fn ( )
82
75
83
76
const unsubscribe = mutation . subscribe ( subscriptionHandler )
84
77
85
- await mutation . mutate ( 'input' )
86
-
87
- expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 )
78
+ mutation . mutate ( 'input' )
79
+ await vi . waitFor ( ( ) =>
80
+ expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 1 ) ,
81
+ )
88
82
89
83
mutation . reset ( )
90
84
91
- await waitFor ( ( ) =>
85
+ await vi . waitFor ( ( ) =>
92
86
expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
93
87
)
94
88
@@ -99,22 +93,21 @@ describe('mutationObserver', () => {
99
93
const key = queryKey ( )
100
94
const mutation = new MutationObserver ( queryClient , {
101
95
mutationKey : [ ...key , '1' ] ,
102
- mutationFn : async ( text : string ) => {
103
- await sleep ( 5 )
104
- return text
105
- } ,
96
+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
106
97
} )
107
98
108
99
const subscriptionHandler = vi . fn ( )
109
100
110
101
const unsubscribe = mutation . subscribe ( subscriptionHandler )
111
102
112
- await mutation . mutate ( 'input' )
103
+ mutation . mutate ( 'input' )
113
104
114
- expect ( mutation . getCurrentResult ( ) ) . toMatchObject ( {
115
- status : 'success' ,
116
- data : 'input' ,
117
- } )
105
+ await vi . waitFor ( ( ) =>
106
+ expect ( mutation . getCurrentResult ( ) ) . toMatchObject ( {
107
+ status : 'success' ,
108
+ data : 'input' ,
109
+ } ) ,
110
+ )
118
111
119
112
mutation . setOptions ( {
120
113
mutationKey : [ ...key , '2' ] ,
@@ -131,27 +124,26 @@ describe('mutationObserver', () => {
131
124
const key = queryKey ( )
132
125
const mutationObserver = new MutationObserver ( queryClient , {
133
126
mutationKey : [ ...key , '1' ] ,
134
- mutationFn : async ( text : string ) => {
135
- await sleep ( 5 )
136
- return text
137
- } ,
127
+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
138
128
} )
139
129
140
130
const subscriptionHandler = vi . fn ( )
141
131
142
132
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
143
133
144
- await mutationObserver . mutate ( 'input' )
134
+ mutationObserver . mutate ( 'input' )
145
135
146
- expect (
147
- queryClient . getMutationCache ( ) . find ( { mutationKey : [ ...key , '1' ] } ) ,
148
- ) . toMatchObject ( {
149
- options : { mutationKey : [ ...key , '1' ] } ,
150
- state : {
151
- status : 'success' ,
152
- data : 'input' ,
153
- } ,
154
- } )
136
+ await vi . waitFor ( ( ) =>
137
+ expect (
138
+ queryClient . getMutationCache ( ) . find ( { mutationKey : [ ...key , '1' ] } ) ,
139
+ ) . toMatchObject ( {
140
+ options : { mutationKey : [ ...key , '1' ] } ,
141
+ state : {
142
+ status : 'success' ,
143
+ data : 'input' ,
144
+ } ,
145
+ } ) ,
146
+ )
155
147
156
148
mutationObserver . setOptions ( {
157
149
mutationKey : [ ...key , '2' ] ,
@@ -173,25 +165,24 @@ describe('mutationObserver', () => {
173
165
test ( 'changing mutation meta should not affect successful mutations' , async ( ) => {
174
166
const mutationObserver = new MutationObserver ( queryClient , {
175
167
meta : { a : 1 } ,
176
- mutationFn : async ( text : string ) => {
177
- await sleep ( 5 )
178
- return text
179
- } ,
168
+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
180
169
} )
181
170
182
171
const subscriptionHandler = vi . fn ( )
183
172
184
173
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
185
174
186
- await mutationObserver . mutate ( 'input' )
175
+ mutationObserver . mutate ( 'input' )
187
176
188
- expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
189
- options : { meta : { a : 1 } } ,
190
- state : {
191
- status : 'success' ,
192
- data : 'input' ,
193
- } ,
194
- } )
177
+ await vi . waitFor ( ( ) =>
178
+ expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
179
+ options : { meta : { a : 1 } } ,
180
+ state : {
181
+ status : 'success' ,
182
+ data : 'input' ,
183
+ } ,
184
+ } ) ,
185
+ )
195
186
196
187
mutationObserver . setOptions ( {
197
188
meta : { a : 2 } ,
@@ -209,10 +200,7 @@ describe('mutationObserver', () => {
209
200
} )
210
201
211
202
test ( 'mutation cache should have different meta when updated between mutations' , async ( ) => {
212
- const mutationFn = async ( text : string ) => {
213
- await sleep ( 5 )
214
- return text
215
- }
203
+ const mutationFn = ( text : string ) => sleep ( 5 ) . then ( ( ) => text )
216
204
const mutationObserver = new MutationObserver ( queryClient , {
217
205
meta : { a : 1 } ,
218
206
mutationFn,
@@ -222,14 +210,16 @@ describe('mutationObserver', () => {
222
210
223
211
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
224
212
225
- await mutationObserver . mutate ( 'input' )
213
+ mutationObserver . mutate ( 'input' )
214
+ await vi . advanceTimersByTimeAsync ( 5 )
226
215
227
216
mutationObserver . setOptions ( {
228
217
meta : { a : 2 } ,
229
218
mutationFn,
230
219
} )
231
220
232
- await mutationObserver . mutate ( 'input' )
221
+ mutationObserver . mutate ( 'input' )
222
+ await vi . advanceTimersByTimeAsync ( 5 )
233
223
234
224
const mutations = queryClient . getMutationCache ( ) . findAll ( )
235
225
expect ( mutations [ 0 ] ) . toMatchObject ( {
@@ -253,24 +243,23 @@ describe('mutationObserver', () => {
253
243
test ( 'changing mutation meta should not affect rejected mutations' , async ( ) => {
254
244
const mutationObserver = new MutationObserver ( queryClient , {
255
245
meta : { a : 1 } ,
256
- mutationFn : async ( _ : string ) => {
257
- await sleep ( 5 )
258
- return Promise . reject ( new Error ( 'err' ) )
259
- } ,
246
+ mutationFn : ( _ : string ) =>
247
+ sleep ( 5 ) . then ( ( ) => Promise . reject ( new Error ( 'err' ) ) ) ,
260
248
} )
261
249
262
250
const subscriptionHandler = vi . fn ( )
263
251
264
252
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
265
253
266
- await mutationObserver . mutate ( 'input' ) . catch ( ( ) => undefined )
267
-
268
- expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
269
- options : { meta : { a : 1 } } ,
270
- state : {
271
- status : 'error' ,
272
- } ,
273
- } )
254
+ mutationObserver . mutate ( 'input' ) . catch ( ( ) => undefined )
255
+ await vi . waitFor ( ( ) =>
256
+ expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
257
+ options : { meta : { a : 1 } } ,
258
+ state : {
259
+ status : 'error' ,
260
+ } ,
261
+ } ) ,
262
+ )
274
263
275
264
mutationObserver . setOptions ( {
276
265
meta : { a : 2 } ,
@@ -289,20 +278,15 @@ describe('mutationObserver', () => {
289
278
test ( 'changing mutation meta should affect pending mutations' , async ( ) => {
290
279
const mutationObserver = new MutationObserver ( queryClient , {
291
280
meta : { a : 1 } ,
292
- mutationFn : async ( text : string ) => {
293
- await sleep ( 20 )
294
- return text
295
- } ,
281
+ mutationFn : ( text : string ) => sleep ( 20 ) . then ( ( ) => text ) ,
296
282
} )
297
283
298
284
const subscriptionHandler = vi . fn ( )
299
285
300
286
const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
301
287
302
288
mutationObserver . mutate ( 'input' )
303
-
304
- await sleep ( 0 )
305
-
289
+ await vi . advanceTimersByTimeAsync ( 5 )
306
290
expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
307
291
options : { meta : { a : 1 } } ,
308
292
state : {
0 commit comments