11import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2- import { waitFor } from '@testing-library/dom'
32import { MutationObserver } from '..'
43import { createQueryClient , queryKey , sleep } from './utils'
54import type { QueryClient } from '..'
@@ -8,20 +7,19 @@ describe('mutationObserver', () => {
87 let queryClient : QueryClient
98
109 beforeEach ( ( ) => {
10+ vi . useFakeTimers ( )
1111 queryClient = createQueryClient ( )
1212 queryClient . mount ( )
1313 } )
1414
1515 afterEach ( ( ) => {
1616 queryClient . clear ( )
17+ vi . useRealTimers ( )
1718 } )
1819
1920 test ( 'onUnsubscribe should not remove the current mutation observer if there is still a subscription' , async ( ) => {
2021 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 ) ,
2523 } )
2624
2725 const subscription1Handler = vi . fn ( )
@@ -34,7 +32,7 @@ describe('mutationObserver', () => {
3432
3533 unsubscribe1 ( )
3634
37- await waitFor ( ( ) => {
35+ await vi . waitFor ( ( ) => {
3836 // 1 call: loading
3937 expect ( subscription1Handler ) . toBeCalledTimes ( 1 )
4038 // 2 calls: loading, success
@@ -47,48 +45,44 @@ describe('mutationObserver', () => {
4745
4846 test ( 'unsubscribe should remove observer to trigger GC' , async ( ) => {
4947 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 ) ,
5449 gcTime : 10 ,
5550 } )
5651
5752 const subscriptionHandler = vi . fn ( )
5853
5954 const unsubscribe = mutation . subscribe ( subscriptionHandler )
6055
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+ )
6460
6561 unsubscribe ( )
6662
67- await waitFor ( ( ) =>
63+ await vi . waitFor ( ( ) =>
6864 expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
6965 )
7066 } )
7167
7268 test ( 'reset should remove observer to trigger GC' , async ( ) => {
7369 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 ) ,
7871 gcTime : 10 ,
7972 } )
8073
8174 const subscriptionHandler = vi . fn ( )
8275
8376 const unsubscribe = mutation . subscribe ( subscriptionHandler )
8477
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+ )
8882
8983 mutation . reset ( )
9084
91- await waitFor ( ( ) =>
85+ await vi . waitFor ( ( ) =>
9286 expect ( queryClient . getMutationCache ( ) . findAll ( ) ) . toHaveLength ( 0 ) ,
9387 )
9488
@@ -99,22 +93,21 @@ describe('mutationObserver', () => {
9993 const key = queryKey ( )
10094 const mutation = new MutationObserver ( queryClient , {
10195 mutationKey : [ ...key , '1' ] ,
102- mutationFn : async ( text : string ) => {
103- await sleep ( 5 )
104- return text
105- } ,
96+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
10697 } )
10798
10899 const subscriptionHandler = vi . fn ( )
109100
110101 const unsubscribe = mutation . subscribe ( subscriptionHandler )
111102
112- await mutation . mutate ( 'input' )
103+ mutation . mutate ( 'input' )
113104
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+ )
118111
119112 mutation . setOptions ( {
120113 mutationKey : [ ...key , '2' ] ,
@@ -131,27 +124,26 @@ describe('mutationObserver', () => {
131124 const key = queryKey ( )
132125 const mutationObserver = new MutationObserver ( queryClient , {
133126 mutationKey : [ ...key , '1' ] ,
134- mutationFn : async ( text : string ) => {
135- await sleep ( 5 )
136- return text
137- } ,
127+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
138128 } )
139129
140130 const subscriptionHandler = vi . fn ( )
141131
142132 const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
143133
144- await mutationObserver . mutate ( 'input' )
134+ mutationObserver . mutate ( 'input' )
145135
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+ )
155147
156148 mutationObserver . setOptions ( {
157149 mutationKey : [ ...key , '2' ] ,
@@ -173,25 +165,24 @@ describe('mutationObserver', () => {
173165 test ( 'changing mutation meta should not affect successful mutations' , async ( ) => {
174166 const mutationObserver = new MutationObserver ( queryClient , {
175167 meta : { a : 1 } ,
176- mutationFn : async ( text : string ) => {
177- await sleep ( 5 )
178- return text
179- } ,
168+ mutationFn : ( text : string ) => sleep ( 5 ) . then ( ( ) => text ) ,
180169 } )
181170
182171 const subscriptionHandler = vi . fn ( )
183172
184173 const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
185174
186- await mutationObserver . mutate ( 'input' )
175+ mutationObserver . mutate ( 'input' )
187176
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+ )
195186
196187 mutationObserver . setOptions ( {
197188 meta : { a : 2 } ,
@@ -209,10 +200,7 @@ describe('mutationObserver', () => {
209200 } )
210201
211202 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 )
216204 const mutationObserver = new MutationObserver ( queryClient , {
217205 meta : { a : 1 } ,
218206 mutationFn,
@@ -222,14 +210,16 @@ describe('mutationObserver', () => {
222210
223211 const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
224212
225- await mutationObserver . mutate ( 'input' )
213+ mutationObserver . mutate ( 'input' )
214+ await vi . advanceTimersByTimeAsync ( 5 )
226215
227216 mutationObserver . setOptions ( {
228217 meta : { a : 2 } ,
229218 mutationFn,
230219 } )
231220
232- await mutationObserver . mutate ( 'input' )
221+ mutationObserver . mutate ( 'input' )
222+ await vi . advanceTimersByTimeAsync ( 5 )
233223
234224 const mutations = queryClient . getMutationCache ( ) . findAll ( )
235225 expect ( mutations [ 0 ] ) . toMatchObject ( {
@@ -253,24 +243,23 @@ describe('mutationObserver', () => {
253243 test ( 'changing mutation meta should not affect rejected mutations' , async ( ) => {
254244 const mutationObserver = new MutationObserver ( queryClient , {
255245 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' ) ) ) ,
260248 } )
261249
262250 const subscriptionHandler = vi . fn ( )
263251
264252 const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
265253
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+ )
274263
275264 mutationObserver . setOptions ( {
276265 meta : { a : 2 } ,
@@ -289,20 +278,15 @@ describe('mutationObserver', () => {
289278 test ( 'changing mutation meta should affect pending mutations' , async ( ) => {
290279 const mutationObserver = new MutationObserver ( queryClient , {
291280 meta : { a : 1 } ,
292- mutationFn : async ( text : string ) => {
293- await sleep ( 20 )
294- return text
295- } ,
281+ mutationFn : ( text : string ) => sleep ( 20 ) . then ( ( ) => text ) ,
296282 } )
297283
298284 const subscriptionHandler = vi . fn ( )
299285
300286 const unsubscribe = mutationObserver . subscribe ( subscriptionHandler )
301287
302288 mutationObserver . mutate ( 'input' )
303-
304- await sleep ( 0 )
305-
289+ await vi . advanceTimersByTimeAsync ( 5 )
306290 expect ( queryClient . getMutationCache ( ) . find ( { } ) ) . toMatchObject ( {
307291 options : { meta : { a : 1 } } ,
308292 state : {
0 commit comments