|
1 | 1 | import type { BaseQueryFn } from '@reduxjs/toolkit/query'
|
2 | 2 | import { createApi, retry } from '@reduxjs/toolkit/query'
|
3 | 3 | import { setupApiStore, waitMs } from './helpers'
|
| 4 | +import type { RetryOptions } from '../retry' |
4 | 5 |
|
5 | 6 | beforeEach(() => {
|
6 | 7 | jest.useFakeTimers('legacy')
|
@@ -339,4 +340,110 @@ describe('configuration', () => {
|
339 | 340 |
|
340 | 341 | expect(baseBaseQuery).toHaveBeenCalledTimes(9)
|
341 | 342 | })
|
| 343 | + |
| 344 | + test('accepts a custom retryCondition fn', async () => { |
| 345 | + const baseBaseQuery = jest.fn< |
| 346 | + ReturnType<BaseQueryFn>, |
| 347 | + Parameters<BaseQueryFn> |
| 348 | + >() |
| 349 | + baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 350 | + |
| 351 | + const overrideMaxRetries = 3 |
| 352 | + |
| 353 | + const baseQuery = retry(baseBaseQuery, { |
| 354 | + retryCondition: (_, __, { attempt }) => attempt <= overrideMaxRetries, |
| 355 | + }) |
| 356 | + const api = createApi({ |
| 357 | + baseQuery, |
| 358 | + endpoints: (build) => ({ |
| 359 | + q1: build.query({ |
| 360 | + query: () => {}, |
| 361 | + }), |
| 362 | + }), |
| 363 | + }) |
| 364 | + |
| 365 | + const storeRef = setupApiStore(api, undefined, { |
| 366 | + withoutTestLifecycles: true, |
| 367 | + }) |
| 368 | + storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 369 | + |
| 370 | + await loopTimers() |
| 371 | + |
| 372 | + expect(baseBaseQuery).toHaveBeenCalledTimes(overrideMaxRetries + 1) |
| 373 | + }) |
| 374 | + |
| 375 | + test('retryCondition with endpoint config that overrides baseQuery config', async () => { |
| 376 | + const baseBaseQuery = jest.fn< |
| 377 | + ReturnType<BaseQueryFn>, |
| 378 | + Parameters<BaseQueryFn> |
| 379 | + >() |
| 380 | + baseBaseQuery.mockResolvedValue({ error: 'rejected' }) |
| 381 | + |
| 382 | + const baseQuery = retry(baseBaseQuery, { |
| 383 | + maxRetries: 10, |
| 384 | + }) |
| 385 | + const api = createApi({ |
| 386 | + baseQuery, |
| 387 | + endpoints: (build) => ({ |
| 388 | + q1: build.query({ |
| 389 | + query: () => {}, |
| 390 | + extraOptions: { |
| 391 | + retryCondition: (_, __, { attempt }) => attempt <= 5, |
| 392 | + }, |
| 393 | + }), |
| 394 | + }), |
| 395 | + }) |
| 396 | + |
| 397 | + const storeRef = setupApiStore(api, undefined, { |
| 398 | + withoutTestLifecycles: true, |
| 399 | + }) |
| 400 | + storeRef.store.dispatch(api.endpoints.q1.initiate({})) |
| 401 | + |
| 402 | + await loopTimers() |
| 403 | + |
| 404 | + expect(baseBaseQuery).toHaveBeenCalledTimes(6) |
| 405 | + }) |
| 406 | + |
| 407 | + test('retryCondition also works with mutations', async () => { |
| 408 | + const baseBaseQuery = jest.fn< |
| 409 | + ReturnType<BaseQueryFn>, |
| 410 | + Parameters<BaseQueryFn> |
| 411 | + >() |
| 412 | + |
| 413 | + baseBaseQuery |
| 414 | + .mockRejectedValueOnce(new Error('rejected')) |
| 415 | + .mockRejectedValueOnce(new Error('hello retryCondition')) |
| 416 | + .mockRejectedValueOnce(new Error('rejected')) |
| 417 | + .mockResolvedValue({ error: 'hello retryCondition' }) |
| 418 | + |
| 419 | + const baseQuery = retry(baseBaseQuery, {}) |
| 420 | + const api = createApi({ |
| 421 | + baseQuery, |
| 422 | + endpoints: (build) => ({ |
| 423 | + m1: build.mutation({ |
| 424 | + query: () => ({ method: 'PUT' }), |
| 425 | + extraOptions: { |
| 426 | + retryCondition: (e) => e.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.skip('RetryOptions only accepts one of maxRetries or retryCondition', () => { |
| 443 | + // @ts-expect-error Should complain if both exist at once |
| 444 | + const ro: RetryOptions = { |
| 445 | + maxRetries: 5, |
| 446 | + retryCondition: () => false, |
| 447 | + } |
| 448 | + }) |
342 | 449 | })
|
0 commit comments