|
1 | | -import { configureStore, isAllOf } from '@reduxjs/toolkit' |
| 1 | +import { configureStore } from '@reduxjs/toolkit' |
2 | 2 | import { createApi } from '@reduxjs/toolkit/query/react' |
3 | 3 | import { renderHook, waitFor } from '@testing-library/react' |
4 | | -import { actionsReducer, withProvider } from '../../tests/utils/helpers' |
| 4 | +import { |
| 5 | + actionsReducer, |
| 6 | + setupApiStore, |
| 7 | + withProvider, |
| 8 | +} from '../../tests/utils/helpers' |
5 | 9 | import type { BaseQueryApi } from '../baseQueryTypes' |
6 | 10 |
|
7 | 11 | describe('baseline thunk behavior', () => { |
@@ -246,50 +250,171 @@ describe('re-triggering behavior on arg change', () => { |
246 | 250 | }) |
247 | 251 |
|
248 | 252 | describe('prefetch', () => { |
249 | | - const baseQuery = () => ({ data: null }) |
| 253 | + const baseQuery = () => ({ data: { name: 'Test User' } }) |
| 254 | + |
250 | 255 | const api = createApi({ |
251 | 256 | baseQuery, |
| 257 | + tagTypes: ['User'], |
252 | 258 | endpoints: (build) => ({ |
253 | | - getUser: build.query<any, any>({ |
254 | | - query: (obj) => obj, |
| 259 | + getUser: build.query<any, number>({ |
| 260 | + query: (id) => ({ url: `user/${id}` }), |
| 261 | + providesTags: (result, error, id) => [{ type: 'User', id }], |
| 262 | + }), |
| 263 | + updateUser: build.mutation<any, { id: number; name: string }>({ |
| 264 | + query: ({ id, name }) => ({ |
| 265 | + url: `user/${id}`, |
| 266 | + method: 'PUT', |
| 267 | + body: { name }, |
| 268 | + }), |
| 269 | + invalidatesTags: (result, error, { id }) => [{ type: 'User', id }], |
255 | 270 | }), |
256 | 271 | }), |
| 272 | + keepUnusedDataFor: 0.1, // 100ms for faster test cleanup |
257 | 273 | }) |
258 | 274 |
|
259 | | - const store = configureStore({ |
260 | | - reducer: { [api.reducerPath]: api.reducer, ...actionsReducer }, |
261 | | - middleware: (gDM) => gDM().concat(api.middleware), |
| 275 | + let storeRef = setupApiStore( |
| 276 | + api, |
| 277 | + { ...actionsReducer }, |
| 278 | + { |
| 279 | + withoutListeners: true, |
| 280 | + }, |
| 281 | + ) |
| 282 | + |
| 283 | + let getSubscriptions: () => Map<string, any> |
| 284 | + let getSubscriptionCount: (queryCacheKey: string) => number |
| 285 | + |
| 286 | + beforeEach(() => { |
| 287 | + storeRef = setupApiStore( |
| 288 | + api, |
| 289 | + { ...actionsReducer }, |
| 290 | + { |
| 291 | + withoutListeners: true, |
| 292 | + }, |
| 293 | + ) |
| 294 | + // Get subscription helpers |
| 295 | + const subscriptionSelectors = storeRef.store.dispatch( |
| 296 | + api.internalActions.internal_getRTKQSubscriptions(), |
| 297 | + ) as any |
| 298 | + getSubscriptions = subscriptionSelectors.getSubscriptions |
| 299 | + getSubscriptionCount = subscriptionSelectors.getSubscriptionCount |
262 | 300 | }) |
263 | | - it('should attach isPrefetch if prefetching', async () => { |
264 | | - store.dispatch(api.util.prefetch('getUser', 1, {})) |
265 | 301 |
|
266 | | - await Promise.all(store.dispatch(api.util.getRunningQueriesThunk())) |
| 302 | + describe('subscription behavior', () => { |
| 303 | + it('prefetch should NOT create a subscription', async () => { |
| 304 | + const queryCacheKey = 'getUser(1)' |
267 | 305 |
|
268 | | - const isPrefetch = ( |
269 | | - action: any, |
270 | | - ): action is { meta: { arg: { isPrefetch: true } } } => |
271 | | - action?.meta?.arg?.isPrefetch |
| 306 | + // Initially no subscriptions |
| 307 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
272 | 308 |
|
273 | | - expect(store.getState().actions).toMatchSequence( |
274 | | - api.internalActions.middlewareRegistered.match, |
275 | | - isAllOf(api.endpoints.getUser.matchPending, isPrefetch), |
276 | | - isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch), |
277 | | - ) |
| 309 | + // Dispatch prefetch |
| 310 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, {})) |
| 311 | + await Promise.all( |
| 312 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 313 | + ) |
278 | 314 |
|
279 | | - // compare against a regular initiate call |
280 | | - await store.dispatch( |
281 | | - api.endpoints.getUser.initiate(1, { forceRefetch: true }), |
282 | | - ) |
| 315 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 316 | + }) |
283 | 317 |
|
284 | | - const isNotPrefetch = (action: any): action is unknown => |
285 | | - !isPrefetch(action) |
| 318 | + it('prefetch allows cache cleanup after keepUnusedDataFor', async () => { |
| 319 | + const queryCacheKey = 'getUser(1)' |
286 | 320 |
|
287 | | - expect(store.getState().actions).toMatchSequence( |
288 | | - api.internalActions.middlewareRegistered.match, |
289 | | - isAllOf(api.endpoints.getUser.matchPending, isPrefetch), |
290 | | - isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch), |
291 | | - isAllOf(api.endpoints.getUser.matchPending, isNotPrefetch), |
292 | | - isAllOf(api.endpoints.getUser.matchFulfilled, isNotPrefetch), |
293 | | - ) |
| 321 | + // Prefetch the data |
| 322 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, {})) |
| 323 | + await Promise.all( |
| 324 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 325 | + ) |
| 326 | + |
| 327 | + // Verify data is in cache |
| 328 | + let state = api.endpoints.getUser.select(1)(storeRef.store.getState()) |
| 329 | + expect(state.data).toEqual({ name: 'Test User' }) |
| 330 | + |
| 331 | + // Wait longer than keepUnusedDataFor |
| 332 | + await new Promise((resolve) => setTimeout(resolve, 150)) |
| 333 | + |
| 334 | + state = api.endpoints.getUser.select(1)(storeRef.store.getState()) |
| 335 | + expect(state.status).toBe('uninitialized') |
| 336 | + expect(state.data).toBeUndefined() |
| 337 | + }) |
| 338 | + |
| 339 | + it('prefetch does NOT trigger refetch on tag invalidation', async () => { |
| 340 | + // Prefetch user 1 |
| 341 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, {})) |
| 342 | + await Promise.all( |
| 343 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 344 | + ) |
| 345 | + |
| 346 | + // Verify data is in cache |
| 347 | + let state = api.endpoints.getUser.select(1)(storeRef.store.getState()) |
| 348 | + expect(state.data).toEqual({ name: 'Test User' }) |
| 349 | + |
| 350 | + // Invalidate the tag by updating the user |
| 351 | + await storeRef.store.dispatch( |
| 352 | + api.endpoints.updateUser.initiate({ id: 1, name: 'Updated' }), |
| 353 | + ) |
| 354 | + |
| 355 | + // Since there's no subscription, the cache entry gets removed on invalidation |
| 356 | + await Promise.all( |
| 357 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 358 | + ) |
| 359 | + |
| 360 | + // Cache entry should be cleared (no subscription to keep it alive) |
| 361 | + state = api.endpoints.getUser.select(1)(storeRef.store.getState()) |
| 362 | + expect(state.status).toBe('uninitialized') |
| 363 | + expect(state.data).toBeUndefined() |
| 364 | + }) |
| 365 | + |
| 366 | + it('multiple prefetches do not accumulate subscriptions', async () => { |
| 367 | + const queryCacheKey = 'getUser(1)' |
| 368 | + |
| 369 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 370 | + |
| 371 | + // First prefetch |
| 372 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, {})) |
| 373 | + await Promise.all( |
| 374 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 375 | + ) |
| 376 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 377 | + |
| 378 | + // Second prefetch (force refetch) |
| 379 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, { force: true })) |
| 380 | + await Promise.all( |
| 381 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 382 | + ) |
| 383 | + |
| 384 | + // Still no subscriptions |
| 385 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 386 | + |
| 387 | + // Third prefetch |
| 388 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, { force: true })) |
| 389 | + await Promise.all( |
| 390 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 391 | + ) |
| 392 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 393 | + }) |
| 394 | + |
| 395 | + it('prefetch followed by regular query should work correctly', async () => { |
| 396 | + const queryCacheKey = 'getUser(1)' |
| 397 | + |
| 398 | + // Prefetch first |
| 399 | + storeRef.store.dispatch(api.util.prefetch('getUser', 1, {})) |
| 400 | + await Promise.all( |
| 401 | + storeRef.store.dispatch(api.util.getRunningQueriesThunk()), |
| 402 | + ) |
| 403 | + |
| 404 | + // No subscription from prefetch |
| 405 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 406 | + |
| 407 | + // Now create a real subscription via initiate |
| 408 | + const promise = storeRef.store.dispatch(api.endpoints.getUser.initiate(1)) |
| 409 | + |
| 410 | + // Should have 1 subscription from the initiate call |
| 411 | + expect(getSubscriptionCount(queryCacheKey)).toBe(1) |
| 412 | + |
| 413 | + // Unsubscribe |
| 414 | + promise.unsubscribe() |
| 415 | + |
| 416 | + // Subscription should be cleaned up |
| 417 | + expect(getSubscriptionCount(queryCacheKey)).toBe(0) |
| 418 | + }) |
294 | 419 | }) |
295 | 420 | }) |
0 commit comments