Skip to content

Commit 70f49a0

Browse files
committed
Add an initial test for infinite query thunks
1 parent 9319061 commit 70f49a0

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { configureStore, isAllOf } from '@reduxjs/toolkit'
2+
import { renderHook, waitFor } from '@testing-library/react'
3+
import { HttpResponse, http } from 'msw'
4+
import util from 'util'
5+
import {
6+
QueryStatus,
7+
createApi,
8+
fetchBaseQuery,
9+
skipToken,
10+
} from '@reduxjs/toolkit/query/react'
11+
import {
12+
actionsReducer,
13+
setupApiStore,
14+
withProvider,
15+
} from '../../tests/utils/helpers'
16+
import type { BaseQueryApi } from '../baseQueryTypes'
17+
import { server } from '@internal/query/tests/mocks/server'
18+
19+
describe('Infinite queries', () => {
20+
beforeEach(() => {
21+
server.resetHandlers()
22+
})
23+
24+
test('Basic infinite query behavior', async () => {
25+
type Pokemon = {
26+
id: string
27+
name: string
28+
}
29+
30+
server.use(
31+
http.get('https://example.com/listItems', ({ request }) => {
32+
const url = new URL(request.url)
33+
const pageString = url.searchParams.get('page')
34+
const pageNum = parseInt(pageString || '0')
35+
36+
const results: Pokemon[] = [
37+
{ id: `${pageNum}`, name: `Pokemon ${pageNum}` },
38+
]
39+
return HttpResponse.json(results)
40+
}),
41+
)
42+
43+
const pokemonApi = createApi({
44+
baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
45+
endpoints: (builder) => ({
46+
getInfinitePokemon: builder.infiniteQuery<Pokemon[], number>({
47+
infiniteQueryOptions: {
48+
getNextPageParam: (
49+
lastPage,
50+
allPages,
51+
lastPageParam,
52+
allPageParams,
53+
) => lastPageParam + 1,
54+
},
55+
query(pageParam = 0) {
56+
return `https://example.com/listItems?page=${pageParam}`
57+
},
58+
}),
59+
}),
60+
})
61+
62+
const storeRef = setupApiStore(pokemonApi, undefined, {
63+
withoutTestLifecycles: true,
64+
})
65+
66+
const res = storeRef.store.dispatch(
67+
pokemonApi.endpoints.getInfinitePokemon.initiate(0, {}),
68+
)
69+
70+
const firstResult = await res
71+
expect(firstResult.status).toBe(QueryStatus.fulfilled)
72+
console.log('Value: ', util.inspect(firstResult, { depth: Infinity }))
73+
74+
if (firstResult.status === QueryStatus.fulfilled) {
75+
expect(firstResult.data.pages).toEqual([
76+
// one page, one entry
77+
[{ id: '0', name: 'Pokemon 0' }],
78+
])
79+
}
80+
81+
const secondRes = storeRef.store.dispatch(
82+
pokemonApi.endpoints.getInfinitePokemon.initiate(0, {
83+
direction: 'forward',
84+
data: firstResult.data,
85+
}),
86+
)
87+
88+
const secondResult = await secondRes
89+
expect(secondResult.status).toBe(QueryStatus.fulfilled)
90+
console.log('Value: ', util.inspect(secondResult, { depth: Infinity }))
91+
if (secondResult.status === QueryStatus.fulfilled) {
92+
expect(secondResult.data.pages).toEqual([
93+
// two pages, one entry each
94+
[{ id: '0', name: 'Pokemon 0' }],
95+
[{ id: '1', name: 'Pokemon 1' }],
96+
])
97+
}
98+
})
99+
})

0 commit comments

Comments
 (0)