1- import { act , renderHook , waitFor } from ' @testing-library/react' ;
1+ import { act , renderHook , waitFor } from " @testing-library/react" ;
22
3- import { renderHookServer } from ' @/tests' ;
3+ import { renderHookServer } from " @/tests" ;
44
5- import { useMutation } from ' ./useMutation' ;
5+ import { useMutation } from " ./useMutation" ;
66
7- it ( 'Should use mutation' , ( ) => {
8- const { result } = renderHook ( ( ) => useMutation ( ( ) => Promise . resolve ( 'data' ) ) ) ;
7+ it ( "Should use mutation" , ( ) => {
8+ const { result } = renderHook ( ( ) =>
9+ useMutation ( ( ) => Promise . resolve ( "data" ) )
10+ ) ;
911
1012 expect ( result . current . data ) . toBeNull ( ) ;
1113 expect ( result . current . error ) . toBeNull ( ) ;
1214 expect ( result . current . isError ) . toBeFalsy ( ) ;
1315 expect ( result . current . isLoading ) . toBeFalsy ( ) ;
1416 expect ( result . current . isSuccess ) . toBeFalsy ( ) ;
15- expect ( result . current . mutate ) . toBeTypeOf ( ' function' ) ;
16- expect ( result . current . mutateAsync ) . toBeTypeOf ( ' function' ) ;
17+ expect ( result . current . mutate ) . toBeTypeOf ( " function" ) ;
18+ expect ( result . current . mutateAsync ) . toBeTypeOf ( " function" ) ;
1719} ) ;
1820
19- it ( 'Should use mutation on server side' , ( ) => {
20- const { result } = renderHookServer ( ( ) => useMutation ( ( ) => Promise . resolve ( 'data' ) ) ) ;
21+ it ( "Should use mutation on server side" , ( ) => {
22+ const { result } = renderHookServer ( ( ) =>
23+ useMutation ( ( ) => Promise . resolve ( "data" ) )
24+ ) ;
2125
2226 expect ( result . current . data ) . toBeNull ( ) ;
2327 expect ( result . current . error ) . toBeNull ( ) ;
2428 expect ( result . current . isError ) . toBeFalsy ( ) ;
2529 expect ( result . current . isLoading ) . toBeFalsy ( ) ;
2630 expect ( result . current . isSuccess ) . toBeFalsy ( ) ;
27- expect ( result . current . mutate ) . toBeTypeOf ( ' function' ) ;
28- expect ( result . current . mutateAsync ) . toBeTypeOf ( ' function' ) ;
31+ expect ( result . current . mutate ) . toBeTypeOf ( " function" ) ;
32+ expect ( result . current . mutateAsync ) . toBeTypeOf ( " function" ) ;
2933} ) ;
3034
31- it ( 'Should mutate data successfully' , async ( ) => {
32- const { result } = renderHook ( ( ) => useMutation ( ( ) => Promise . resolve ( 'data' ) ) ) ;
35+ it ( "Should mutate data successfully" , async ( ) => {
36+ const { result } = renderHook ( ( ) =>
37+ useMutation ( ( ) => Promise . resolve ( "data" ) )
38+ ) ;
3339
3440 act ( result . current . mutate ) ;
3541
@@ -39,43 +45,50 @@ it('Should mutate data successfully', async () => {
3945 await waitFor ( ( ) => {
4046 expect ( result . current . isLoading ) . toBeFalsy ( ) ;
4147 expect ( result . current . isSuccess ) . toBeTruthy ( ) ;
42- expect ( result . current . data ) . toBe ( ' data' ) ;
48+ expect ( result . current . data ) . toBe ( " data" ) ;
4349 } ) ;
4450} ) ;
4551
46- it ( ' Should handle errors' , async ( ) => {
47- const { result } = renderHook ( ( ) => useMutation ( ( ) => Promise . reject ( new Error ( 'error' ) ) ) ) ;
48-
49- act ( result . current . mutate ) ;
52+ it ( " Should handle errors" , async ( ) => {
53+ const { result } = renderHook ( ( ) =>
54+ useMutation ( ( ) => Promise . reject ( new Error ( "error" ) ) )
55+ ) ;
5056
51- expect ( result . current . isLoading ) . toBeTruthy ( ) ;
52- expect ( result . current . isError ) . toBeFalsy ( ) ;
53- expect ( result . current . error ) . toBeNull ( ) ;
57+ await act ( async ( ) => {
58+ try {
59+ await result . current . mutateAsync ( ) ;
60+ expect ( result . current . isLoading ) . toBeTruthy ( ) ;
61+ expect ( result . current . isError ) . toBeFalsy ( ) ;
62+ expect ( result . current . error ) . toBeNull ( ) ;
63+ } catch { }
64+ } ) ;
5465
5566 await waitFor ( ( ) => {
5667 expect ( result . current . isLoading ) . toBeFalsy ( ) ;
5768 expect ( result . current . isError ) . toBeTruthy ( ) ;
58- expect ( result . current . error ) . toEqual ( new Error ( ' error' ) ) ;
69+ expect ( result . current . error ) . toEqual ( new Error ( " error" ) ) ;
5970 expect ( result . current . data ) . toBeNull ( ) ;
6071 } ) ;
6172} ) ;
6273
63- it ( 'Should mutate async' , async ( ) => {
64- const { result } = renderHook ( ( ) => useMutation ( ( input ) => Promise . resolve ( `data-${ input } ` ) ) ) ;
74+ it ( "Should mutate async" , async ( ) => {
75+ const { result } = renderHook ( ( ) =>
76+ useMutation ( ( input ) => Promise . resolve ( `data-${ input } ` ) )
77+ ) ;
6578
6679 await act ( async ( ) => {
67- const response = await result . current . mutateAsync ( ' test' ) ;
68- expect ( response ) . toBe ( ' data-test' ) ;
80+ const response = await result . current . mutateAsync ( " test" ) ;
81+ expect ( response ) . toBe ( " data-test" ) ;
6982 } ) ;
7083
71- expect ( result . current . data ) . toBe ( ' data-test' ) ;
84+ expect ( result . current . data ) . toBe ( " data-test" ) ;
7285 expect ( result . current . isSuccess ) . toBeTruthy ( ) ;
7386} ) ;
7487
75- it ( ' Should triggered onSuccess callback' , async ( ) => {
88+ it ( " Should triggered onSuccess callback" , async ( ) => {
7689 const { result } = renderHook ( ( ) =>
77- useMutation ( ( ) => Promise . resolve ( ' data' ) , {
78- onSuccess : ( data ) => expect ( data ) . toBe ( ' data' )
90+ useMutation ( ( ) => Promise . resolve ( " data" ) , {
91+ onSuccess : ( data ) => expect ( data ) . toBe ( " data" ) ,
7992 } )
8093 ) ;
8194
@@ -84,10 +97,10 @@ it('Should triggered onSuccess callback', async () => {
8497 await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBeTruthy ( ) ) ;
8598} ) ;
8699
87- it ( ' Should triggered onError callback' , async ( ) => {
100+ it ( " Should triggered onError callback" , async ( ) => {
88101 const { result } = renderHook ( ( ) =>
89- useMutation ( ( ) => Promise . reject ( new Error ( ' error' ) ) , {
90- onError : ( error ) => expect ( error ) . toEqual ( new Error ( ' error' ) )
102+ useMutation ( ( ) => Promise . reject ( new Error ( " error" ) ) , {
103+ onError : ( error ) => expect ( error ) . toEqual ( new Error ( " error" ) ) ,
91104 } )
92105 ) ;
93106
@@ -96,19 +109,19 @@ it('Should triggered onError callback', async () => {
96109 await waitFor ( ( ) => expect ( result . current . isError ) . toBeTruthy ( ) ) ;
97110} ) ;
98111
99- it ( ' Should retry on error once' , async ( ) => {
112+ it ( " Should retry on error once" , async ( ) => {
100113 let retries = 0 ;
101114
102115 const { result } = renderHook ( ( ) =>
103116 useMutation (
104117 ( ) =>
105118 new Promise ( ( resolve , reject ) => {
106- if ( retries === 1 ) resolve ( ' data' ) ;
119+ if ( retries === 1 ) resolve ( " data" ) ;
107120 retries ++ ;
108- reject ( new Error ( ' error' ) ) ;
121+ reject ( new Error ( " error" ) ) ;
109122 } ) ,
110123 {
111- retry : true
124+ retry : true ,
112125 }
113126 )
114127 ) ;
@@ -117,22 +130,22 @@ it('Should retry on error once', async () => {
117130
118131 expect ( result . current . data ) . toBeNull ( ) ;
119132
120- await waitFor ( ( ) => expect ( result . current . data ) . toBe ( ' data' ) ) ;
133+ await waitFor ( ( ) => expect ( result . current . data ) . toBe ( " data" ) ) ;
121134} ) ;
122135
123- it ( ' Should retry on error multiple times' , async ( ) => {
136+ it ( " Should retry on error multiple times" , async ( ) => {
124137 let retries = 0 ;
125138
126139 const { result } = renderHook ( ( ) =>
127140 useMutation (
128141 ( ) =>
129142 new Promise ( ( resolve , reject ) => {
130- if ( retries === 2 ) resolve ( ' data' ) ;
143+ if ( retries === 2 ) resolve ( " data" ) ;
131144 retries ++ ;
132- reject ( new Error ( ' error' ) ) ;
145+ reject ( new Error ( " error" ) ) ;
133146 } ) ,
134147 {
135- retry : 2
148+ retry : 2 ,
136149 }
137150 )
138151 ) ;
@@ -141,40 +154,44 @@ it('Should retry on error multiple times', async () => {
141154
142155 expect ( result . current . data ) . toBeNull ( ) ;
143156
144- await waitFor ( ( ) => expect ( result . current . data ) . toBe ( ' data' ) ) ;
157+ await waitFor ( ( ) => expect ( result . current . data ) . toBe ( " data" ) ) ;
145158} ) ;
146159
147- it ( ' Should override global options with mutate options' , async ( ) => {
160+ it ( " Should override global options with mutate options" , async ( ) => {
148161 const globalOnSuccess = vi . fn ( ) ;
149162 const localOnSuccess = vi . fn ( ) ;
150163
151164 const { result } = renderHook ( ( ) =>
152- useMutation ( ( ) => Promise . resolve ( ' data' ) , {
153- onSuccess : globalOnSuccess
165+ useMutation ( ( ) => Promise . resolve ( " data" ) , {
166+ onSuccess : globalOnSuccess ,
154167 } )
155168 ) ;
156169
157170 act ( ( ) => result . current . mutate ( undefined , { onSuccess : localOnSuccess } ) ) ;
158171
159172 await waitFor ( ( ) => expect ( result . current . isSuccess ) . toBeTruthy ( ) ) ;
160173
161- expect ( localOnSuccess ) . toHaveBeenCalledWith ( ' data' ) ;
174+ expect ( localOnSuccess ) . toHaveBeenCalledWith ( " data" ) ;
162175 expect ( globalOnSuccess ) . not . toHaveBeenCalled ( ) ;
163176} ) ;
164177
165- it ( ' Should reset error state on successful mutation' , async ( ) => {
178+ it ( " Should reset error state on successful mutation" , async ( ) => {
166179 let shouldFail = true ;
167180
168181 const { result } = renderHook ( ( ) =>
169182 useMutation ( ( ) => {
170183 if ( shouldFail ) {
171- return Promise . reject ( new Error ( ' error' ) ) ;
184+ return Promise . reject ( new Error ( " error" ) ) ;
172185 }
173- return Promise . resolve ( ' data' ) ;
186+ return Promise . resolve ( " data" ) ;
174187 } )
175188 ) ;
176189
177- act ( result . current . mutate ) ;
190+ await act ( async ( ) => {
191+ try {
192+ await result . current . mutateAsync ( ) ;
193+ } catch { }
194+ } ) ;
178195
179196 await waitFor ( ( ) => expect ( result . current . isError ) . toBeTruthy ( ) ) ;
180197
@@ -188,35 +205,35 @@ it('Should reset error state on successful mutation', async () => {
188205 } ) ;
189206} ) ;
190207
191- it ( ' Should retry by number delay' , async ( ) => {
208+ it ( " Should retry by number delay" , async ( ) => {
192209 let retries = 0 ;
193210 const { result } = renderHook ( ( ) =>
194211 useMutation ( ( ) => {
195212 retries ++ ;
196213 if ( retries < 2 ) {
197- return Promise . reject ( new Error ( ' error' ) ) ;
214+ return Promise . reject ( new Error ( " error" ) ) ;
198215 }
199- return Promise . resolve ( ' data' ) ;
216+ return Promise . resolve ( " data" ) ;
200217 } )
201218 ) ;
202219
203220 act ( ( ) => result . current . mutate ( undefined , { retry : 1 , retryDelay : 100 } ) ) ;
204221
205222 expect ( result . current . isLoading ) . toBeTruthy ( ) ;
206223
207- await waitFor ( ( ) => expect ( result . current . data ) . toBe ( ' data' ) ) ;
224+ await waitFor ( ( ) => expect ( result . current . data ) . toBe ( " data" ) ) ;
208225} ) ;
209226
210- it ( ' Should retry by number global delay' , async ( ) => {
227+ it ( " Should retry by number global delay" , async ( ) => {
211228 let retries = 0 ;
212229 const { result } = renderHook ( ( ) =>
213230 useMutation (
214231 ( ) => {
215232 retries ++ ;
216233 if ( retries < 2 ) {
217- return Promise . reject ( new Error ( ' error' ) ) ;
234+ return Promise . reject ( new Error ( " error" ) ) ;
218235 }
219- return Promise . resolve ( ' data' ) ;
236+ return Promise . resolve ( " data" ) ;
220237 } ,
221238 { retryDelay : 100 , retry : 1 }
222239 )
@@ -226,20 +243,20 @@ it('Should retry by number global delay', async () => {
226243
227244 expect ( result . current . isLoading ) . toBeTruthy ( ) ;
228245
229- await waitFor ( ( ) => expect ( result . current . data ) . toBe ( ' data' ) ) ;
246+ await waitFor ( ( ) => expect ( result . current . data ) . toBe ( " data" ) ) ;
230247} ) ;
231248
232- it ( ' Should retry by function delay' , async ( ) => {
249+ it ( " Should retry by function delay" , async ( ) => {
233250 const retryDelay = vi . fn ( ( ) => 100 ) ;
234251 let retries = 0 ;
235252
236253 const { result } = renderHook ( ( ) =>
237254 useMutation ( ( ) => {
238255 retries ++ ;
239256 if ( retries < 2 ) {
240- return Promise . reject ( new Error ( ' error' ) ) ;
257+ return Promise . reject ( new Error ( " error" ) ) ;
241258 }
242- return Promise . resolve ( ' data' ) ;
259+ return Promise . resolve ( " data" ) ;
243260 } )
244261 ) ;
245262
@@ -248,7 +265,7 @@ it('Should retry by function delay', async () => {
248265 expect ( result . current . isLoading ) . toBeTruthy ( ) ;
249266
250267 await waitFor ( ( ) => {
251- expect ( result . current . data ) . toBe ( ' data' ) ;
268+ expect ( result . current . data ) . toBe ( " data" ) ;
252269 expect ( retryDelay ) . toHaveBeenCalledOnce ( ) ;
253270 } ) ;
254271} ) ;
0 commit comments