diff --git a/packages/toolkit/src/query/core/buildSlice.ts b/packages/toolkit/src/query/core/buildSlice.ts index 62a113a1e3..59174d4850 100644 --- a/packages/toolkit/src/query/core/buildSlice.ts +++ b/packages/toolkit/src/query/core/buildSlice.ts @@ -582,77 +582,6 @@ export function buildSlice({ }, }) - const infiniteQuerySlice = createSlice({ - name: `${reducerPath}/infinitequeries`, - initialState: initialState as QueryState, - reducers: { - fetchNextPage( - d, - a: PayloadAction< - { - endpointName: string - requestId: string - options: Subscribers[number] - } & QuerySubstateIdentifier - >, - ) { - // Dummy - }, - unsubscribeQueryResult( - d, - a: PayloadAction<{ requestId: string } & QuerySubstateIdentifier>, - ) { - // Dummy - }, - internal_getRTKQSubscriptions() {}, - }, - // extraReducers(builder) { - // builder - // .addCase(queryThunk.fulfilled, (draft, { meta, payload }) => { - // updateQuerySubstateIfExists( - // draft, - // meta.arg.queryCacheKey, - // (substate) => { - // const { infiniteQueryOptions } = definitions[ - // meta.arg.endpointName - // ] as InfiniteQueryDefinition - // substate.status = QueryStatus.fulfilled - // if(!infiniteQueryOptions) return - // - // if (substate.data !== undefined) { - // const { fulfilledTimeStamp, arg, baseQueryMeta, requestId } = - // meta - // // There's existing cache data. Let the user merge it in themselves. - // // We're already inside an Immer-powered reducer, and the user could just mutate `substate.data` - // // themselves inside of `merge()`. But, they might also want to return a new value. - // // Try to let Immer figure that part out, save the result, and assign it to `substate.data`. - // substate.data = payload - // } else { - // // Presumably a fresh request. Just cache the response data. - // substate.data = payload - // } - // } else { - // // Assign or safely update the cache data. - // substate.data = - // definitions[meta.arg.endpointName].structuralSharing ?? true - // ? copyWithStructuralSharing( - // isDraft(substate.data) - // ? original(substate.data) - // : substate.data, - // payload, - // ) - // : payload - // } - // - // delete substate.error - // substate.fulfilledTimeStamp = meta.fulfilledTimeStamp - // }, - // ) - // }) - - // }, - }) - // Dummy slice to generate actions const subscriptionSlice = createSlice({ name: `${reducerPath}/subscriptions`, diff --git a/packages/toolkit/src/query/tests/infiniteQueries.test.ts b/packages/toolkit/src/query/tests/infiniteQueries.test.ts index 90284bd74f..2f6ec32301 100644 --- a/packages/toolkit/src/query/tests/infiniteQueries.test.ts +++ b/packages/toolkit/src/query/tests/infiniteQueries.test.ts @@ -858,4 +858,77 @@ describe('Infinite queries', () => { meta: undefined, }) }) + + test('Can use transformResponse', async () => { + type PokemonPage = { items: Pokemon[]; page: number } + const pokemonApi = createApi({ + baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }), + endpoints: (builder) => ({ + getInfinitePokemonWithTransform: builder.infiniteQuery< + PokemonPage, + string, + number + >({ + infiniteQueryOptions: { + initialPageParam: 0, + getNextPageParam: ( + lastPage, + allPages, + // Page param type should be `number` + lastPageParam, + allPageParams, + ) => lastPageParam + 1, + }, + query(pageParam) { + return `https://example.com/listItems?page=${pageParam}` + }, + transformResponse(baseQueryReturnValue: Pokemon[], meta, arg) { + expect(Array.isArray(baseQueryReturnValue)).toBe(true) + return { + items: baseQueryReturnValue, + page: arg, + } + }, + }), + }), + }) + + const storeRef = setupApiStore( + pokemonApi, + { ...actionsReducer }, + { + withoutTestLifecycles: true, + }, + ) + + const checkResultData = ( + result: InfiniteQueryResult, + expectedValues: PokemonPage[], + ) => { + expect(result.status).toBe(QueryStatus.fulfilled) + if (result.status === QueryStatus.fulfilled) { + expect(result.data.pages).toEqual(expectedValues) + } + } + + const res1 = storeRef.store.dispatch( + pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', {}), + ) + + const entry1InitialLoad = await res1 + checkResultData(entry1InitialLoad, [ + { items: [{ id: '0', name: 'Pokemon 0' }], page: 0 }, + ]) + + const entry1Updated = await storeRef.store.dispatch( + pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', { + direction: 'forward', + }), + ) + + checkResultData(entry1Updated, [ + { items: [{ id: '0', name: 'Pokemon 0' }], page: 0 }, + { items: [{ id: '1', name: 'Pokemon 1' }], page: 1 }, + ]) + }) })