Skip to content

Commit b20b8f3

Browse files
committed
Add transformResponse test
1 parent 551c2a5 commit b20b8f3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

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)