Skip to content

Commit

Permalink
Test getPreviousParam behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Oct 27, 2024
1 parent 2adfa58 commit b52dd2e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ export function buildThunks<
param: unknown,
previous?: boolean,
): Promise<QueryReturnValue> => {
// This should handle cases where there is no `getPrevPageParam`,
// or `getPPP` returned nullish
if (param == null && data.pages.length) {
return Promise.resolve({ data })
}
Expand Down
39 changes: 39 additions & 0 deletions packages/toolkit/src/query/tests/infiniteQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -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 }),
Expand Down Expand Up @@ -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' }],
])
}
})
})

0 comments on commit b52dd2e

Please sign in to comment.