| 1 |
import type { BaseQueryFn, FetchBaseQueryError } from '@reduxjs/toolkit/query' |
| 2 |
import { createApi, retry } from '@reduxjs/toolkit/query' |
| 3 |
import { setupApiStore } from '../../tests/utils/helpers' |
| 4 |
|
| 5 |
beforeEach(() => { |
| 6 |
vi.useFakeTimers() |
| 7 |
}) |
| 8 |
|
| 9 |
const loopTimers = async (max: number = 12) => { |
| 10 |
let count = 0 |
| 11 |
while (count < max) { |
| 12 |
await vi.advanceTimersByTimeAsync(1) |
| 13 |
vi.advanceTimersByTime(120_000) |
| 14 |
count++ |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
describe('configuration', () => { |
| 19 |
test('retrying without any config options', async () => { |
| 20 |
const baseBaseQuery = vi.fn< |
| 21 |
Parameters<BaseQueryFn>, |
| 22 |
ReturnType<BaseQueryFn> |
| 23 |
>() |
| 24 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 25 |
|
| 26 |
const baseQuery = retry(baseBaseQuery) |
| 27 |
const api = createApi({ |
| 28 |
baseQuery, |
| 29 |
endpoints: (build) => ({ |
| 30 |
q1: build.query({ |
| 31 |
query: () => {}, |
| 32 |
}), |
| 33 |
}), |
| 34 |
}) |
| 35 |
|
| 36 |
const storeRef = setupApiStore(api, undefined, { |
| 37 |
withoutTestLifecycles: true, |
| 38 |
}) |
| 39 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 40 |
|
| 41 |
await loopTimers(7) |
| 42 |
|
| 43 |
expect(baseBaseQuery).toHaveBeenCalledTimes(6) |
| 44 |
}) |
| 45 |
|
| 46 |
test('retrying with baseQuery config that overrides default behavior (maxRetries: 5)', async () => { |
| 47 |
const baseBaseQuery = vi.fn< |
| 48 |
Parameters<BaseQueryFn>, |
| 49 |
ReturnType<BaseQueryFn> |
| 50 |
>() |
| 51 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 52 |
|
| 53 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 3 }) |
| 54 |
const api = createApi({ |
| 55 |
baseQuery, |
| 56 |
endpoints: (build) => ({ |
| 57 |
q1: build.query({ |
| 58 |
query: () => {}, |
| 59 |
}), |
| 60 |
}), |
| 61 |
}) |
| 62 |
|
| 63 |
const storeRef = setupApiStore(api, undefined, { |
| 64 |
withoutTestLifecycles: true, |
| 65 |
}) |
| 66 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 67 |
|
| 68 |
await loopTimers(5) |
| 69 |
|
| 70 |
expect(baseBaseQuery).toHaveBeenCalledTimes(4) |
| 71 |
}) |
| 72 |
|
| 73 |
test('retrying with endpoint config that overrides baseQuery config', async () => { |
| 74 |
const baseBaseQuery = vi.fn< |
| 75 |
Parameters<BaseQueryFn>, |
| 76 |
ReturnType<BaseQueryFn> |
| 77 |
>() |
| 78 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 79 |
|
| 80 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 3 }) |
| 81 |
const api = createApi({ |
| 82 |
baseQuery, |
| 83 |
endpoints: (build) => ({ |
| 84 |
q1: build.query({ |
| 85 |
query: () => {}, |
| 86 |
}), |
| 87 |
q2: build.query({ |
| 88 |
query: () => {}, |
| 89 |
extraOptions: { maxRetries: 8 }, |
| 90 |
}), |
| 91 |
}), |
| 92 |
}) |
| 93 |
|
| 94 |
const storeRef = setupApiStore(api, undefined, { |
| 95 |
withoutTestLifecycles: true, |
| 96 |
}) |
| 97 |
|
| 98 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 99 |
await loopTimers(5) |
| 100 |
|
| 101 |
expect(baseBaseQuery).toHaveBeenCalledTimes(4) |
| 102 |
|
| 103 |
baseBaseQuery.mockClear() |
| 104 |
|
| 105 |
storeRef.store.dispatch(api.endpoints.q2.initiate({})) |
| 106 |
|
| 107 |
await loopTimers(10) |
| 108 |
|
| 109 |
expect(baseBaseQuery).toHaveBeenCalledTimes(9) |
| 110 |
}) |
| 111 |
|
| 112 |
test('stops retrying a query after a success', async () => { |
| 113 |
const baseBaseQuery = vi.fn< |
| 114 |
Parameters<BaseQueryFn>, |
| 115 |
ReturnType<BaseQueryFn> |
| 116 |
>() |
| 117 |
baseBaseQuery |
| 118 |
.mockResolvedValueOnce({ error: 'rejected' }) |
| 119 |
.mockResolvedValueOnce({ error: 'rejected' }) |
| 120 |
.mockResolvedValue({ data: { success: true } }) |
| 121 |
|
| 122 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 10 }) |
| 123 |
const api = createApi({ |
| 124 |
baseQuery, |
| 125 |
endpoints: (build) => ({ |
| 126 |
q1: build.mutation({ |
| 127 |
query: () => {}, |
| 128 |
}), |
| 129 |
}), |
| 130 |
}) |
| 131 |
|
| 132 |
const storeRef = setupApiStore(api, undefined, { |
| 133 |
withoutTestLifecycles: true, |
| 134 |
}) |
| 135 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 136 |
|
| 137 |
await loopTimers(6) |
| 138 |
|
| 139 |
expect(baseBaseQuery).toHaveBeenCalledTimes(3) |
| 140 |
}) |
| 141 |
|
| 142 |
test('retrying also works with mutations', async () => { |
| 143 |
const baseBaseQuery = vi.fn< |
| 144 |
Parameters<BaseQueryFn>, |
| 145 |
ReturnType<BaseQueryFn> |
| 146 |
>() |
| 147 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 148 |
|
| 149 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 3 }) |
| 150 |
const api = createApi({ |
| 151 |
baseQuery, |
| 152 |
endpoints: (build) => ({ |
| 153 |
m1: build.mutation({ |
| 154 |
query: () => ({ method: 'PUT' }), |
| 155 |
}), |
| 156 |
}), |
| 157 |
}) |
| 158 |
|
| 159 |
const storeRef = setupApiStore(api, undefined, { |
| 160 |
withoutTestLifecycles: true, |
| 161 |
}) |
| 162 |
|
| 163 |
storeRef.store.dispatch(api.endpoints.m1.initiate({})) |
| 164 |
|
| 165 |
await loopTimers(5) |
| 166 |
|
| 167 |
expect(baseBaseQuery).toHaveBeenCalledTimes(4) |
| 168 |
}) |
| 169 |
|
| 170 |
test('retrying stops after a success from a mutation', async () => { |
| 171 |
const baseBaseQuery = vi.fn< |
| 172 |
Parameters<BaseQueryFn>, |
| 173 |
ReturnType<BaseQueryFn> |
| 174 |
>() |
| 175 |
baseBaseQuery |
| 176 |
.mockRejectedValueOnce(new Error('rejected')) |
| 177 |
.mockRejectedValueOnce(new Error('rejected')) |
| 178 |
.mockResolvedValue({ data: { success: true } }) |
| 179 |
|
| 180 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 3 }) |
| 181 |
const api = createApi({ |
| 182 |
baseQuery, |
| 183 |
endpoints: (build) => ({ |
| 184 |
m1: build.mutation({ |
| 185 |
query: () => ({ method: 'PUT' }), |
| 186 |
}), |
| 187 |
}), |
| 188 |
}) |
| 189 |
|
| 190 |
const storeRef = setupApiStore(api, undefined, { |
| 191 |
withoutTestLifecycles: true, |
| 192 |
}) |
| 193 |
|
| 194 |
storeRef.store.dispatch(api.endpoints.m1.initiate({})) |
| 195 |
|
| 196 |
await loopTimers(5) |
| 197 |
|
| 198 |
expect(baseBaseQuery).toHaveBeenCalledTimes(3) |
| 199 |
}) |
| 200 |
test('non-error-cases should **not** retry', async () => { |
| 201 |
const baseBaseQuery = vi.fn< |
| 202 |
Parameters<BaseQueryFn>, |
| 203 |
ReturnType<BaseQueryFn> |
| 204 |
>() |
| 205 |
baseBaseQuery.mockResolvedValue({ data: { success: true } }) |
| 206 |
|
| 207 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 3 }) |
| 208 |
const api = createApi({ |
| 209 |
baseQuery, |
| 210 |
endpoints: (build) => ({ |
| 211 |
q1: build.query({ |
| 212 |
query: () => {}, |
| 213 |
}), |
| 214 |
}), |
| 215 |
}) |
| 216 |
|
| 217 |
const storeRef = setupApiStore(api, undefined, { |
| 218 |
withoutTestLifecycles: true, |
| 219 |
}) |
| 220 |
|
| 221 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 222 |
|
| 223 |
await loopTimers(2) |
| 224 |
|
| 225 |
expect(baseBaseQuery).toHaveBeenCalledOnce() |
| 226 |
}) |
| 227 |
test('calling retry.fail(error) will skip retrying and expose the error directly', async () => { |
| 228 |
const error = { message: 'banana' } |
| 229 |
|
| 230 |
const baseBaseQuery = vi.fn< |
| 231 |
Parameters<BaseQueryFn>, |
| 232 |
ReturnType<BaseQueryFn> |
| 233 |
>() |
| 234 |
baseBaseQuery.mockImplementation((input) => { |
| 235 |
retry.fail(error) |
| 236 |
return { data: `this won't happen` } |
| 237 |
}) |
| 238 |
|
| 239 |
const baseQuery = retry(baseBaseQuery) |
| 240 |
const api = createApi({ |
| 241 |
baseQuery, |
| 242 |
endpoints: (build) => ({ |
| 243 |
q1: build.query({ |
| 244 |
query: () => {}, |
| 245 |
}), |
| 246 |
}), |
| 247 |
}) |
| 248 |
|
| 249 |
const storeRef = setupApiStore(api, undefined, { |
| 250 |
withoutTestLifecycles: true, |
| 251 |
}) |
| 252 |
|
| 253 |
const result = await storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 254 |
|
| 255 |
await loopTimers(2) |
| 256 |
|
| 257 |
expect(baseBaseQuery).toHaveBeenCalledOnce() |
| 258 |
expect(result.error).toEqual(error) |
| 259 |
expect(result).toEqual({ |
| 260 |
endpointName: 'q1', |
| 261 |
error, |
| 262 |
isError: true, |
| 263 |
isLoading: false, |
| 264 |
isSuccess: false, |
| 265 |
isUninitialized: false, |
| 266 |
originalArgs: expect.any(Object), |
| 267 |
requestId: expect.any(String), |
| 268 |
startedTimeStamp: expect.any(Number), |
| 269 |
status: 'rejected', |
| 270 |
}) |
| 271 |
}) |
| 272 |
|
| 273 |
test('wrapping retry(retry(..., { maxRetries: 3 }), { maxRetries: 3 }) should retry 16 times', async () => { |
| 274 |
/** |
| 275 |
* Note: |
| 276 |
* This will retry 16 total times because we try the initial + 3 retries (sum: 4), then retry that process 3 times (starting at 0 for a total of 4)... 4x4=16 (allegedly) |
| 277 |
*/ |
| 278 |
const baseBaseQuery = vi.fn< |
| 279 |
Parameters<BaseQueryFn>, |
| 280 |
ReturnType<BaseQueryFn> |
| 281 |
>() |
| 282 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 283 |
|
| 284 |
const baseQuery = retry(retry(baseBaseQuery, { maxRetries: 3 }), { |
| 285 |
maxRetries: 3, |
| 286 |
}) |
| 287 |
const api = createApi({ |
| 288 |
baseQuery, |
| 289 |
endpoints: (build) => ({ |
| 290 |
q1: build.query({ |
| 291 |
query: () => {}, |
| 292 |
}), |
| 293 |
}), |
| 294 |
}) |
| 295 |
|
| 296 |
const storeRef = setupApiStore(api, undefined, { |
| 297 |
withoutTestLifecycles: true, |
| 298 |
}) |
| 299 |
|
| 300 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 301 |
|
| 302 |
await loopTimers(18) |
| 303 |
|
| 304 |
expect(baseBaseQuery).toHaveBeenCalledTimes(16) |
| 305 |
}) |
| 306 |
|
| 307 |
test('accepts a custom backoff fn', async () => { |
| 308 |
const baseBaseQuery = vi.fn< |
| 309 |
Parameters<BaseQueryFn>, |
| 310 |
ReturnType<BaseQueryFn> |
| 311 |
>() |
| 312 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 313 |
|
| 314 |
const baseQuery = retry(baseBaseQuery, { |
| 315 |
maxRetries: 8, |
| 316 |
backoff: async (attempt, maxRetries) => { |
| 317 |
const attempts = Math.min(attempt, maxRetries) |
| 318 |
const timeout = attempts * 300 // Scale up by 300ms per request, ex: 300ms, 600ms, 900ms, 1200ms... |
| 319 |
await new Promise((resolve) => |
| 320 |
setTimeout((res: any) => resolve(res), timeout), |
| 321 |
) |
| 322 |
}, |
| 323 |
}) |
| 324 |
const api = createApi({ |
| 325 |
baseQuery, |
| 326 |
endpoints: (build) => ({ |
| 327 |
q1: build.query({ |
| 328 |
query: () => {}, |
| 329 |
}), |
| 330 |
}), |
| 331 |
}) |
| 332 |
|
| 333 |
const storeRef = setupApiStore(api, undefined, { |
| 334 |
withoutTestLifecycles: true, |
| 335 |
}) |
| 336 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 337 |
|
| 338 |
await loopTimers() |
| 339 |
|
| 340 |
expect(baseBaseQuery).toHaveBeenCalledTimes(9) |
| 341 |
}) |
| 342 |
|
| 343 |
test('accepts a custom retryCondition fn', async () => { |
| 344 |
const baseBaseQuery = vi.fn< |
| 345 |
Parameters<BaseQueryFn>, |
| 346 |
ReturnType<BaseQueryFn> |
| 347 |
>() |
| 348 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 349 |
|
| 350 |
const overrideMaxRetries = 3 |
| 351 |
|
| 352 |
const baseQuery = retry(baseBaseQuery, { |
| 353 |
retryCondition: (_, __, { attempt }) => attempt <= overrideMaxRetries, |
| 354 |
}) |
| 355 |
const api = createApi({ |
| 356 |
baseQuery, |
| 357 |
endpoints: (build) => ({ |
| 358 |
q1: build.query({ |
| 359 |
query: () => {}, |
| 360 |
}), |
| 361 |
}), |
| 362 |
}) |
| 363 |
|
| 364 |
const storeRef = setupApiStore(api, undefined, { |
| 365 |
withoutTestLifecycles: true, |
| 366 |
}) |
| 367 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 368 |
|
| 369 |
await loopTimers() |
| 370 |
|
| 371 |
expect(baseBaseQuery).toHaveBeenCalledTimes(overrideMaxRetries + 1) |
| 372 |
}) |
| 373 |
|
| 374 |
test('retryCondition with endpoint config that overrides baseQuery config', async () => { |
| 375 |
const baseBaseQuery = vi.fn< |
| 376 |
Parameters<BaseQueryFn>, |
| 377 |
ReturnType<BaseQueryFn> |
| 378 |
>() |
| 379 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 380 |
|
| 381 |
const baseQuery = retry(baseBaseQuery, { |
| 382 |
maxRetries: 10, |
| 383 |
}) |
| 384 |
const api = createApi({ |
| 385 |
baseQuery, |
| 386 |
endpoints: (build) => ({ |
| 387 |
q1: build.query({ |
| 388 |
query: () => {}, |
| 389 |
extraOptions: { |
| 390 |
retryCondition: (_, __, { attempt }) => attempt <= 5, |
| 391 |
}, |
| 392 |
}), |
| 393 |
}), |
| 394 |
}) |
| 395 |
|
| 396 |
const storeRef = setupApiStore(api, undefined, { |
| 397 |
withoutTestLifecycles: true, |
| 398 |
}) |
| 399 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 400 |
|
| 401 |
await loopTimers() |
| 402 |
|
| 403 |
expect(baseBaseQuery).toHaveBeenCalledTimes(6) |
| 404 |
}) |
| 405 |
|
| 406 |
test('retryCondition also works with mutations', async () => { |
| 407 |
const baseBaseQuery = vi.fn< |
| 408 |
Parameters<BaseQueryFn>, |
| 409 |
ReturnType<BaseQueryFn> |
| 410 |
>() |
| 411 |
|
| 412 |
baseBaseQuery |
| 413 |
.mockRejectedValueOnce(new Error('rejected')) |
| 414 |
.mockRejectedValueOnce(new Error('hello retryCondition')) |
| 415 |
.mockRejectedValueOnce(new Error('rejected')) |
| 416 |
.mockResolvedValue({ error: 'hello retryCondition' }) |
| 417 |
|
| 418 |
const baseQuery = retry(baseBaseQuery, {}) |
| 419 |
const api = createApi({ |
| 420 |
baseQuery, |
| 421 |
endpoints: (build) => ({ |
| 422 |
m1: build.mutation({ |
| 423 |
query: () => ({ method: 'PUT' }), |
| 424 |
extraOptions: { |
| 425 |
retryCondition: (e) => |
| 426 |
(e as FetchBaseQueryError).data === 'hello retryCondition', |
| 427 |
}, |
| 428 |
}), |
| 429 |
}), |
| 430 |
}) |
| 431 |
|
| 432 |
const storeRef = setupApiStore(api, undefined, { |
| 433 |
withoutTestLifecycles: true, |
| 434 |
}) |
| 435 |
storeRef.store.dispatch(api.endpoints.m1.initiate({})) |
| 436 |
|
| 437 |
await loopTimers() |
| 438 |
|
| 439 |
expect(baseBaseQuery).toHaveBeenCalledTimes(4) |
| 440 |
}) |
| 441 |
|
| 442 |
test('Specifying maxRetries as 0 in RetryOptions prevents retries', async () => { |
| 443 |
const baseBaseQuery = vi.fn< |
| 444 |
Parameters<BaseQueryFn>, |
| 445 |
ReturnType<BaseQueryFn> |
| 446 |
>() |
| 447 |
baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 448 |
|
| 449 |
const baseQuery = retry(baseBaseQuery, { maxRetries: 0 }) |
| 450 |
const api = createApi({ |
| 451 |
baseQuery, |
| 452 |
endpoints: (build) => ({ |
| 453 |
q1: build.query({ |
| 454 |
query: () => {}, |
| 455 |
}), |
| 456 |
}), |
| 457 |
}) |
| 458 |
|
| 459 |
const storeRef = setupApiStore(api, undefined, { |
| 460 |
withoutTestLifecycles: true, |
| 461 |
}) |
| 462 |
|
| 463 |
storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 464 |
await loopTimers(2) |
| 465 |
|
| 466 |
expect(baseBaseQuery).toHaveBeenCalledOnce() |
| 467 |
}) |
| 468 |
}) |