@@ -20,8 +20,7 @@ describe("useWithRetry", () => {
20
20
const { result } = renderHook ( ( ) => useWithRetry ( mockFn ) ) ;
21
21
22
22
expect ( result . current . isLoading ) . toBe ( false ) ;
23
- expect ( result . current . retryAt ) . toBe ( null ) ;
24
- expect ( result . current . attemptCount ) . toBe ( 0 ) ;
23
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
25
24
} ) ;
26
25
27
26
it ( "should execute function successfully on first attempt" , async ( ) => {
@@ -35,8 +34,7 @@ describe("useWithRetry", () => {
35
34
36
35
expect ( mockFn ) . toHaveBeenCalledTimes ( 1 ) ;
37
36
expect ( result . current . isLoading ) . toBe ( false ) ;
38
- expect ( result . current . retryAt ) . toBe ( null ) ;
39
- expect ( result . current . attemptCount ) . toBe ( 0 ) ;
37
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
40
38
} ) ;
41
39
42
40
it ( "should set isLoading to true during execution" , async ( ) => {
@@ -95,7 +93,7 @@ describe("useWithRetry", () => {
95
93
96
94
expect ( mockFn ) . toHaveBeenCalledTimes ( 3 ) ;
97
95
expect ( result . current . isLoading ) . toBe ( false ) ;
98
- expect ( result . current . retryAt ) . toBe ( null ) ;
96
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
99
97
} ) ;
100
98
101
99
it ( "should stop retrying after max attempts (10)" , async ( ) => {
@@ -123,8 +121,7 @@ describe("useWithRetry", () => {
123
121
124
122
// After 10 attempts, should stop retrying
125
123
expect ( result . current . isLoading ) . toBe ( false ) ;
126
- expect ( result . current . retryAt ) . toBe ( null ) ;
127
- expect ( result . current . attemptCount ) . toBe ( 10 ) ; // Should preserve final attempt count
124
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
128
125
} ) ;
129
126
130
127
it ( "should respect max delay of 10 minutes" , async ( ) => {
@@ -161,7 +158,7 @@ describe("useWithRetry", () => {
161
158
162
159
expect ( mockFn ) . toHaveBeenCalledTimes ( 10 ) ;
163
160
expect ( result . current . isLoading ) . toBe ( false ) ;
164
- expect ( result . current . retryAt ) . toBe ( null ) ;
161
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
165
162
} ) ;
166
163
167
164
it ( "should cancel previous retry when call is invoked again" , async ( ) => {
@@ -187,7 +184,7 @@ describe("useWithRetry", () => {
187
184
188
185
expect ( mockFn ) . toHaveBeenCalledTimes ( 2 ) ;
189
186
expect ( result . current . isLoading ) . toBe ( false ) ;
190
- expect ( result . current . retryAt ) . toBe ( null ) ;
187
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
191
188
192
189
// Advance time to ensure previous retry was cancelled
193
190
await act ( async ( ) => {
@@ -198,7 +195,9 @@ describe("useWithRetry", () => {
198
195
} ) ;
199
196
200
197
it ( "should set retryAt when scheduling retry" , async ( ) => {
201
- mockFn . mockRejectedValue ( new Error ( "Failure" ) ) ;
198
+ mockFn
199
+ . mockRejectedValueOnce ( new Error ( "Failure" ) )
200
+ . mockResolvedValueOnce ( undefined ) ;
202
201
203
202
const { result } = renderHook ( ( ) => useWithRetry ( mockFn ) ) ;
204
203
@@ -221,7 +220,7 @@ describe("useWithRetry", () => {
221
220
jest . advanceTimersByTime ( 1000 ) ;
222
221
} ) ;
223
222
224
- expect ( result . current . retryAt ) . toBe ( null ) ;
223
+ expect ( result . current . retryAt ) . toBe ( undefined ) ;
225
224
} ) ;
226
225
227
226
it ( "should cleanup timer on unmount" , async ( ) => {
@@ -248,38 +247,4 @@ describe("useWithRetry", () => {
248
247
// Function should not have been called again
249
248
expect ( mockFn ) . toHaveBeenCalledTimes ( 1 ) ;
250
249
} ) ;
251
-
252
- it ( "should preserve attemptCount when max attempts reached" , async ( ) => {
253
- mockFn . mockRejectedValue ( new Error ( "Always fails" ) ) ;
254
-
255
- const { result } = renderHook ( ( ) => useWithRetry ( mockFn ) ) ;
256
-
257
- // Start the call
258
- await act ( async ( ) => {
259
- await result . current . call ( ) ;
260
- } ) ;
261
-
262
- expect ( result . current . attemptCount ) . toBe ( 1 ) ;
263
-
264
- // Fast-forward through 9 more retries to reach max attempts
265
- for ( let i = 1 ; i < 10 ; i ++ ) {
266
- const delay = Math . min ( 1000 * 2 ** ( i - 1 ) , 600000 ) ;
267
- await act ( async ( ) => {
268
- jest . advanceTimersByTime ( delay ) ;
269
- } ) ;
270
- expect ( result . current . attemptCount ) . toBe ( i + 1 ) ;
271
- }
272
-
273
- // After max attempts, attemptCount should be preserved
274
- expect ( result . current . attemptCount ) . toBe ( 10 ) ;
275
- expect ( result . current . isLoading ) . toBe ( false ) ;
276
- expect ( result . current . retryAt ) . toBe ( null ) ;
277
-
278
- // Calling again should reset attemptCount
279
- await act ( async ( ) => {
280
- await result . current . call ( ) ;
281
- } ) ;
282
-
283
- expect ( result . current . attemptCount ) . toBe ( 1 ) ; // Reset on new call
284
- } ) ;
285
250
} ) ;
0 commit comments