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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { configureStore } from '@reduxjs/toolkit'
import { createApi } from '@reduxjs/toolkit/query/react'
import { renderHook, waitFor } from '@testing-library/react'
import {
  actionsReducer,
  setupApiStore,
  withProvider,
} from '../../tests/utils/helpers'
import type { BaseQueryApi } from '../baseQueryTypes'

describe('baseline thunk behavior', () => {
  test('handles a non-async baseQuery without error', async () => {
    const baseQuery = (args?: any) => ({ data: args })
    const api = createApi({
      baseQuery,
      endpoints: (build) => ({
        getUser: build.query<unknown, number>({
          query(id) {
            return { url: `user/${id}` }
          },
        }),
      }),
    })
    const { getUser } = api.endpoints
    const store = configureStore({
      reducer: {
        [api.reducerPath]: api.reducer,
      },
      middleware: (gDM) => gDM().concat(api.middleware),
    })

    const promise = store.dispatch(getUser.initiate(1))
    const { data } = await promise

    expect(data).toEqual({
      url: 'user/1',
    })

    const storeResult = getUser.select(1)(store.getState())
    expect(storeResult).toEqual({
      data: {
        url: 'user/1',
      },
      endpointName: 'getUser',
      isError: false,
      isLoading: false,
      isSuccess: true,
      isUninitialized: false,
      originalArgs: 1,
      requestId: expect.any(String),
      status: 'fulfilled',
      startedTimeStamp: expect.any(Number),
      fulfilledTimeStamp: expect.any(Number),
    })
  })

  test('passes the extraArgument property to the baseQueryApi', async () => {
    const baseQuery = (_args: any, api: BaseQueryApi) => ({ data: api.extra })
    const api = createApi({
      baseQuery,
      endpoints: (build) => ({
        getUser: build.query<unknown, void>({
          query: () => '',
        }),
      }),
    })
    const store = configureStore({
      reducer: {
        [api.reducerPath]: api.reducer,
      },
      middleware: (gDM) =>
        gDM({ thunk: { extraArgument: 'cakes' } }).concat(api.middleware),
    })
    const { getUser } = api.endpoints
    const { data } = await store.dispatch(getUser.initiate())
    expect(data).toBe('cakes')
  })

  test('only triggers transformResponse when a query method is actually used', async () => {
    const baseQuery = (args?: any) => ({ data: args })
    const transformResponse = vi.fn((response: any) => response)
    const api = createApi({
      baseQuery,
      endpoints: (build) => ({
        hasQuery: build.query<string, string>({
          query: (arg) => 'test',
          transformResponse,
        }),
        hasQueryFn: build.query<string, void>(
          // @ts-expect-error
          {
            queryFn: () => ({ data: 'test' }),
            transformResponse,
          },
        ),
      }),
    })

    const store = configureStore({
      reducer: {
        [api.reducerPath]: api.reducer,
      },
      middleware: (gDM) =>
        gDM({ thunk: { extraArgument: 'cakes' } }).concat(api.middleware),
    })

    await store.dispatch(api.util.upsertQueryData('hasQuery', 'a', 'test'))
    expect(transformResponse).not.toHaveBeenCalled()

    transformResponse.mockReset()

    await store.dispatch(api.endpoints.hasQuery.initiate('b'))
    expect(transformResponse).toHaveBeenCalledTimes(1)

    transformResponse.mockReset()

    await store.dispatch(api.endpoints.hasQueryFn.initiate())
    expect(transformResponse).not.toHaveBeenCalled()
  })
})

describe('re-triggering behavior on arg change', () => {
  const api = createApi({
    baseQuery: () => ({ data: null }),
    endpoints: (build) => ({
      getUser: build.query<any, any>({
        query: (obj) => obj,
      }),
    }),
  })
  const { getUser } = api.endpoints
  const store = configureStore({
    reducer: { [api.reducerPath]: api.reducer },
    middleware: (gDM) => gDM().concat(api.middleware),
  })

  const spy = vi.spyOn(getUser, 'initiate')
  beforeEach(() => void spy.mockClear())

  test('re-trigger on literal value change', async () => {
    const { result, rerender } = renderHook(
      (props) => getUser.useQuery(props),
      {
        wrapper: withProvider(store),
        initialProps: 5,
      },
    )

    await waitFor(() => {
      expect(result.current.status).not.toBe('pending')
    })

    expect(spy).toHaveBeenCalledOnce()

    for (let x = 1; x < 3; x++) {
      rerender(6)
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledTimes(2)
    }

    for (let x = 1; x < 3; x++) {
      rerender(7)
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledTimes(3)
    }
  })

  test('only re-trigger on shallow-equal arg change', async () => {
    const { result, rerender } = renderHook(
      (props) => getUser.useQuery(props),
      {
        wrapper: withProvider(store),
        initialProps: { name: 'Bob', likes: 'iceCream' },
      },
    )

    await waitFor(() => {
      expect(result.current.status).not.toBe('pending')
    })
    expect(spy).toHaveBeenCalledOnce()

    for (let x = 1; x < 3; x++) {
      rerender({ name: 'Bob', likes: 'waffles' })
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledTimes(2)
    }

    for (let x = 1; x < 3; x++) {
      rerender({ name: 'Alice', likes: 'waffles' })
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledTimes(3)
    }
  })

  test('re-triggers every time on deeper value changes', async () => {
    const name = 'Tim'

    const { result, rerender } = renderHook(
      (props) => getUser.useQuery(props),
      {
        wrapper: withProvider(store),
        initialProps: { person: { name } },
      },
    )

    await waitFor(() => {
      expect(result.current.status).not.toBe('pending')
    })
    expect(spy).toHaveBeenCalledOnce()

    for (let x = 1; x < 3; x++) {
      rerender({ person: { name: name + x } })
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledTimes(x + 1)
    }
  })

  test('do not re-trigger if the order of keys change while maintaining the same values', async () => {
    const { result, rerender } = renderHook(
      (props) => getUser.useQuery(props),
      {
        wrapper: withProvider(store),
        initialProps: { name: 'Tim', likes: 'Bananas' },
      },
    )

    await waitFor(() => {
      expect(result.current.status).not.toBe('pending')
    })
    expect(spy).toHaveBeenCalledOnce()

    for (let x = 1; x < 3; x++) {
      rerender({ likes: 'Bananas', name: 'Tim' })
      await waitFor(() => {
        expect(result.current.status).not.toBe('pending')
      })
      expect(spy).toHaveBeenCalledOnce()
    }
  })
})

describe('prefetch', () => {
  const baseQuery = () => ({ data: { name: 'Test User' } })

  const api = createApi({
    baseQuery,
    tagTypes: ['User'],
    endpoints: (build) => ({
      getUser: build.query<any, number>({
        query: (id) => ({ url: `user/${id}` }),
        providesTags: (result, error, id) => [{ type: 'User', id }],
      }),
      updateUser: build.mutation<any, { id: number; name: string }>({
        query: ({ id, name }) => ({
          url: `user/${id}`,
          method: 'PUT',
          body: { name },
        }),
        invalidatesTags: (result, error, { id }) => [{ type: 'User', id }],
      }),
    }),
    keepUnusedDataFor: 0.1, // 100ms for faster test cleanup
  })

  let storeRef = setupApiStore(
    api,
    { ...actionsReducer },
    {
      withoutListeners: true,
    },
  )

  let getSubscriptions: () => Map<string, any>
  let getSubscriptionCount: (queryCacheKey: string) => number

  beforeEach(() => {
    storeRef = setupApiStore(
      api,
      { ...actionsReducer },
      {
        withoutListeners: true,
      },
    )
    // Get subscription helpers
    const subscriptionSelectors = storeRef.store.dispatch(
      api.internalActions.internal_getRTKQSubscriptions(),
    ) as any
    getSubscriptions = subscriptionSelectors.getSubscriptions
    getSubscriptionCount = subscriptionSelectors.getSubscriptionCount
  })

  describe('subscription behavior', () => {
    it('prefetch should NOT create a subscription', async () => {
      const queryCacheKey = 'getUser(1)'

      // Initially no subscriptions
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)

      // Dispatch prefetch
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, {}))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      expect(getSubscriptionCount(queryCacheKey)).toBe(0)
    })

    it('prefetch allows cache cleanup after keepUnusedDataFor', async () => {
      const queryCacheKey = 'getUser(1)'

      // Prefetch the data
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, {}))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      // Verify data is in cache
      let state = api.endpoints.getUser.select(1)(storeRef.store.getState())
      expect(state.data).toEqual({ name: 'Test User' })

      // Wait longer than keepUnusedDataFor
      await new Promise((resolve) => setTimeout(resolve, 150))

      state = api.endpoints.getUser.select(1)(storeRef.store.getState())
      expect(state.status).toBe('uninitialized')
      expect(state.data).toBeUndefined()
    })

    it('prefetch does NOT trigger refetch on tag invalidation', async () => {
      // Prefetch user 1
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, {}))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      // Verify data is in cache
      let state = api.endpoints.getUser.select(1)(storeRef.store.getState())
      expect(state.data).toEqual({ name: 'Test User' })

      // Invalidate the tag by updating the user
      await storeRef.store.dispatch(
        api.endpoints.updateUser.initiate({ id: 1, name: 'Updated' }),
      )

      // Since there's no subscription, the cache entry gets removed on invalidation
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      // Cache entry should be cleared (no subscription to keep it alive)
      state = api.endpoints.getUser.select(1)(storeRef.store.getState())
      expect(state.status).toBe('uninitialized')
      expect(state.data).toBeUndefined()
    })

    it('multiple prefetches do not accumulate subscriptions', async () => {
      const queryCacheKey = 'getUser(1)'

      expect(getSubscriptionCount(queryCacheKey)).toBe(0)

      // First prefetch
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, {}))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)

      // Second prefetch (force refetch)
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, { force: true }))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      // Still no subscriptions
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)

      // Third prefetch
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, { force: true }))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)
    })

    it('prefetch followed by regular query should work correctly', async () => {
      const queryCacheKey = 'getUser(1)'

      // Prefetch first
      storeRef.store.dispatch(api.util.prefetch('getUser', 1, {}))
      await Promise.all(
        storeRef.store.dispatch(api.util.getRunningQueriesThunk()),
      )

      // No subscription from prefetch
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)

      // Now create a real subscription via initiate
      const promise = storeRef.store.dispatch(api.endpoints.getUser.initiate(1))

      // Should have 1 subscription from the initiate call
      expect(getSubscriptionCount(queryCacheKey)).toBe(1)

      // Unsubscribe
      promise.unsubscribe()

      // Subscription should be cleaned up
      expect(getSubscriptionCount(queryCacheKey)).toBe(0)
    })
  })
})