Skip to content

Commit 9937291

Browse files
committed
Test getPreviousParam behavior
1 parent bb32ba8 commit 9937291

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ export function buildThunks<
433433
param: unknown,
434434
previous?: boolean,
435435
): Promise<QueryReturnValue> => {
436+
// This should handle cases where there is no `getPrevPageParam`,
437+
// or `getPPP` returned nullish
436438
if (param == null && data.pages.length) {
437439
return Promise.resolve({ data })
438440
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ describe('Infinite queries', () => {
5858
lastPageParam,
5959
allPageParams,
6060
) => lastPageParam + 1,
61+
getPreviousPageParam: (
62+
firstPage,
63+
allPages,
64+
firstPageParam,
65+
allPageParams,
66+
) => {
67+
return firstPageParam > 0 ? firstPageParam - 1 : undefined
68+
},
6169
},
70+
6271
// Actual query arg type should be `number`
6372
query(pageParam) {
6473
return `https://example.com/listItems?page=${pageParam}`
@@ -110,6 +119,21 @@ describe('Infinite queries', () => {
110119
])
111120
}
112121

122+
const entry1PrevPageMissing = await storeRef.store.dispatch(
123+
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {
124+
direction: 'backward',
125+
}),
126+
)
127+
128+
if (entry1PrevPageMissing.status === QueryStatus.fulfilled) {
129+
// There is no p
130+
expect(entry1PrevPageMissing.data.pages).toEqual([
131+
// two pages, one entry each
132+
[{ id: '0', name: 'Pokemon 0' }],
133+
[{ id: '1', name: 'Pokemon 1' }],
134+
])
135+
}
136+
113137
// console.log(
114138
// 'API state: ',
115139
// util.inspect(storeRef.store.getState().api, { depth: Infinity }),
@@ -141,5 +165,20 @@ describe('Infinite queries', () => {
141165
[{ id: '4', name: 'Pokemon 4' }],
142166
])
143167
}
168+
169+
const entry2PrevPage = await storeRef.store.dispatch(
170+
pokemonApi.endpoints.getInfinitePokemon.initiate('water', {
171+
direction: 'backward',
172+
}),
173+
)
174+
175+
if (entry2PrevPage.status === QueryStatus.fulfilled) {
176+
expect(entry2PrevPage.data.pages).toEqual([
177+
// three pages, one entry each
178+
[{ id: '2', name: 'Pokemon 2' }],
179+
[{ id: '3', name: 'Pokemon 3' }],
180+
[{ id: '4', name: 'Pokemon 4' }],
181+
])
182+
}
144183
})
145184
})

0 commit comments

Comments
 (0)