Skip to content

Commit dbce780

Browse files
authored
Remove dead slice code and add transformResponse test (#4823)
1 parent ef90600 commit dbce780

File tree

2 files changed

+73
-71
lines changed

2 files changed

+73
-71
lines changed

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -582,77 +582,6 @@ export function buildSlice({
582582
},
583583
})
584584

585-
const infiniteQuerySlice = createSlice({
586-
name: `${reducerPath}/infinitequeries`,
587-
initialState: initialState as QueryState<any>,
588-
reducers: {
589-
fetchNextPage(
590-
d,
591-
a: PayloadAction<
592-
{
593-
endpointName: string
594-
requestId: string
595-
options: Subscribers[number]
596-
} & QuerySubstateIdentifier
597-
>,
598-
) {
599-
// Dummy
600-
},
601-
unsubscribeQueryResult(
602-
d,
603-
a: PayloadAction<{ requestId: string } & QuerySubstateIdentifier>,
604-
) {
605-
// Dummy
606-
},
607-
internal_getRTKQSubscriptions() {},
608-
},
609-
// extraReducers(builder) {
610-
// builder
611-
// .addCase(queryThunk.fulfilled, (draft, { meta, payload }) => {
612-
// updateQuerySubstateIfExists(
613-
// draft,
614-
// meta.arg.queryCacheKey,
615-
// (substate) => {
616-
// const { infiniteQueryOptions } = definitions[
617-
// meta.arg.endpointName
618-
// ] as InfiniteQueryDefinition<any, any, any, any>
619-
// substate.status = QueryStatus.fulfilled
620-
// if(!infiniteQueryOptions) return
621-
//
622-
// if (substate.data !== undefined) {
623-
// const { fulfilledTimeStamp, arg, baseQueryMeta, requestId } =
624-
// meta
625-
// // There's existing cache data. Let the user merge it in themselves.
626-
// // We're already inside an Immer-powered reducer, and the user could just mutate `substate.data`
627-
// // themselves inside of `merge()`. But, they might also want to return a new value.
628-
// // Try to let Immer figure that part out, save the result, and assign it to `substate.data`.
629-
// substate.data = payload
630-
// } else {
631-
// // Presumably a fresh request. Just cache the response data.
632-
// substate.data = payload
633-
// }
634-
// } else {
635-
// // Assign or safely update the cache data.
636-
// substate.data =
637-
// definitions[meta.arg.endpointName].structuralSharing ?? true
638-
// ? copyWithStructuralSharing(
639-
// isDraft(substate.data)
640-
// ? original(substate.data)
641-
// : substate.data,
642-
// payload,
643-
// )
644-
// : payload
645-
// }
646-
//
647-
// delete substate.error
648-
// substate.fulfilledTimeStamp = meta.fulfilledTimeStamp
649-
// },
650-
// )
651-
// })
652-
653-
// },
654-
})
655-
656585
// Dummy slice to generate actions
657586
const subscriptionSlice = createSlice({
658587
name: `${reducerPath}/subscriptions`,

packages/toolkit/src/query/tests/infiniteQueries.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,4 +858,77 @@ describe('Infinite queries', () => {
858858
meta: undefined,
859859
})
860860
})
861+
862+
test('Can use transformResponse', async () => {
863+
type PokemonPage = { items: Pokemon[]; page: number }
864+
const pokemonApi = createApi({
865+
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
866+
endpoints: (builder) => ({
867+
getInfinitePokemonWithTransform: builder.infiniteQuery<
868+
PokemonPage,
869+
string,
870+
number
871+
>({
872+
infiniteQueryOptions: {
873+
initialPageParam: 0,
874+
getNextPageParam: (
875+
lastPage,
876+
allPages,
877+
// Page param type should be `number`
878+
lastPageParam,
879+
allPageParams,
880+
) => lastPageParam + 1,
881+
},
882+
query(pageParam) {
883+
return `https://example.com/listItems?page=${pageParam}`
884+
},
885+
transformResponse(baseQueryReturnValue: Pokemon[], meta, arg) {
886+
expect(Array.isArray(baseQueryReturnValue)).toBe(true)
887+
return {
888+
items: baseQueryReturnValue,
889+
page: arg,
890+
}
891+
},
892+
}),
893+
}),
894+
})
895+
896+
const storeRef = setupApiStore(
897+
pokemonApi,
898+
{ ...actionsReducer },
899+
{
900+
withoutTestLifecycles: true,
901+
},
902+
)
903+
904+
const checkResultData = (
905+
result: InfiniteQueryResult,
906+
expectedValues: PokemonPage[],
907+
) => {
908+
expect(result.status).toBe(QueryStatus.fulfilled)
909+
if (result.status === QueryStatus.fulfilled) {
910+
expect(result.data.pages).toEqual(expectedValues)
911+
}
912+
}
913+
914+
const res1 = storeRef.store.dispatch(
915+
pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', {}),
916+
)
917+
918+
const entry1InitialLoad = await res1
919+
checkResultData(entry1InitialLoad, [
920+
{ items: [{ id: '0', name: 'Pokemon 0' }], page: 0 },
921+
])
922+
923+
const entry1Updated = await storeRef.store.dispatch(
924+
pokemonApi.endpoints.getInfinitePokemonWithTransform.initiate('fire', {
925+
direction: 'forward',
926+
}),
927+
)
928+
929+
checkResultData(entry1Updated, [
930+
{ items: [{ id: '0', name: 'Pokemon 0' }], page: 0 },
931+
{ items: [{ id: '1', name: 'Pokemon 1' }], page: 1 },
932+
])
933+
})
861934
})

0 commit comments

Comments
 (0)