diff --git a/packages/toolkit/src/query/core/buildThunks.ts b/packages/toolkit/src/query/core/buildThunks.ts index 3eb93c39b4..7b45aa105b 100644 --- a/packages/toolkit/src/query/core/buildThunks.ts +++ b/packages/toolkit/src/query/core/buildThunks.ts @@ -433,6 +433,8 @@ export function buildThunks< param: unknown, previous?: boolean, ): Promise => { + // This should handle cases where there is no `getPrevPageParam`, + // or `getPPP` returned nullish if (param == null && data.pages.length) { return Promise.resolve({ data }) } diff --git a/packages/toolkit/src/query/tests/infiniteQueries.test.ts b/packages/toolkit/src/query/tests/infiniteQueries.test.ts index d6b4b1f444..692a43e21c 100644 --- a/packages/toolkit/src/query/tests/infiniteQueries.test.ts +++ b/packages/toolkit/src/query/tests/infiniteQueries.test.ts @@ -58,7 +58,16 @@ describe('Infinite queries', () => { lastPageParam, allPageParams, ) => lastPageParam + 1, + getPreviousPageParam: ( + firstPage, + allPages, + firstPageParam, + allPageParams, + ) => { + return firstPageParam > 0 ? firstPageParam - 1 : undefined + }, }, + // Actual query arg type should be `number` query(pageParam) { return `https://example.com/listItems?page=${pageParam}` @@ -110,6 +119,21 @@ describe('Infinite queries', () => { ]) } + const entry1PrevPageMissing = await storeRef.store.dispatch( + pokemonApi.endpoints.getInfinitePokemon.initiate('fire', { + direction: 'backward', + }), + ) + + if (entry1PrevPageMissing.status === QueryStatus.fulfilled) { + // There is no p + expect(entry1PrevPageMissing.data.pages).toEqual([ + // two pages, one entry each + [{ id: '0', name: 'Pokemon 0' }], + [{ id: '1', name: 'Pokemon 1' }], + ]) + } + // console.log( // 'API state: ', // util.inspect(storeRef.store.getState().api, { depth: Infinity }), @@ -141,5 +165,20 @@ describe('Infinite queries', () => { [{ id: '4', name: 'Pokemon 4' }], ]) } + + const entry2PrevPage = await storeRef.store.dispatch( + pokemonApi.endpoints.getInfinitePokemon.initiate('water', { + direction: 'backward', + }), + ) + + if (entry2PrevPage.status === QueryStatus.fulfilled) { + expect(entry2PrevPage.data.pages).toEqual([ + // three pages, one entry each + [{ id: '2', name: 'Pokemon 2' }], + [{ id: '3', name: 'Pokemon 3' }], + [{ id: '4', name: 'Pokemon 4' }], + ]) + } }) })