| 1 | import type { skipToken, InfiniteData } from '@reduxjs/toolkit/query/react'
|
|---|
| 2 | import {
|
|---|
| 3 | createApi,
|
|---|
| 4 | fetchBaseQuery,
|
|---|
| 5 | QueryStatus,
|
|---|
| 6 | } from '@reduxjs/toolkit/query/react'
|
|---|
| 7 | import { setupApiStore } from '../../tests/utils/helpers'
|
|---|
| 8 | import { createSlice } from '@internal/createSlice'
|
|---|
| 9 |
|
|---|
| 10 | describe('Infinite queries', () => {
|
|---|
| 11 | test('Basic infinite query behavior', async () => {
|
|---|
| 12 | type Pokemon = {
|
|---|
| 13 | id: string
|
|---|
| 14 | name: string
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | const pokemonApi = createApi({
|
|---|
| 18 | baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
|
|---|
| 19 | endpoints: (build) => ({
|
|---|
| 20 | getInfinitePokemon: build.infiniteQuery<Pokemon[], string, number>({
|
|---|
| 21 | infiniteQueryOptions: {
|
|---|
| 22 | initialPageParam: 0,
|
|---|
| 23 | getNextPageParam: (
|
|---|
| 24 | lastPage,
|
|---|
| 25 | allPages,
|
|---|
| 26 | lastPageParam,
|
|---|
| 27 | allPageParams,
|
|---|
| 28 | queryArg,
|
|---|
| 29 | ) => {
|
|---|
| 30 | expectTypeOf(lastPage).toEqualTypeOf<Pokemon[]>()
|
|---|
| 31 |
|
|---|
| 32 | expectTypeOf(allPages).toEqualTypeOf<Pokemon[][]>()
|
|---|
| 33 |
|
|---|
| 34 | expectTypeOf(lastPageParam).toBeNumber()
|
|---|
| 35 |
|
|---|
| 36 | expectTypeOf(allPageParams).toEqualTypeOf<number[]>()
|
|---|
| 37 |
|
|---|
| 38 | expectTypeOf(queryArg).toBeString()
|
|---|
| 39 |
|
|---|
| 40 | return lastPageParam + 1
|
|---|
| 41 | },
|
|---|
| 42 | },
|
|---|
| 43 | query({ pageParam, queryArg }) {
|
|---|
| 44 | expectTypeOf(pageParam).toBeNumber()
|
|---|
| 45 | expectTypeOf(queryArg).toBeString()
|
|---|
| 46 |
|
|---|
| 47 | return `https://example.com/listItems?page=${pageParam}`
|
|---|
| 48 | },
|
|---|
| 49 | async onCacheEntryAdded(arg, api) {
|
|---|
| 50 | const data = await api.cacheDataLoaded
|
|---|
| 51 | expectTypeOf(data.data).toEqualTypeOf<
|
|---|
| 52 | InfiniteData<Pokemon[], number>
|
|---|
| 53 | >()
|
|---|
| 54 | },
|
|---|
| 55 | async onQueryStarted(arg, api) {
|
|---|
| 56 | const data = await api.queryFulfilled
|
|---|
| 57 | expectTypeOf(data.data).toEqualTypeOf<
|
|---|
| 58 | InfiniteData<Pokemon[], number>
|
|---|
| 59 | >()
|
|---|
| 60 | },
|
|---|
| 61 | providesTags: (result) => {
|
|---|
| 62 | expectTypeOf(result).toEqualTypeOf<
|
|---|
| 63 | InfiniteData<Pokemon[], number> | undefined
|
|---|
| 64 | >()
|
|---|
| 65 | return []
|
|---|
| 66 | },
|
|---|
| 67 | }),
|
|---|
| 68 | }),
|
|---|
| 69 | })
|
|---|
| 70 |
|
|---|
| 71 | const storeRef = setupApiStore(pokemonApi, undefined, {
|
|---|
| 72 | withoutTestLifecycles: true,
|
|---|
| 73 | })
|
|---|
| 74 |
|
|---|
| 75 | expectTypeOf(pokemonApi.endpoints.getInfinitePokemon.initiate)
|
|---|
| 76 | .parameter(0)
|
|---|
| 77 | .toBeString()
|
|---|
| 78 |
|
|---|
| 79 | expectTypeOf(pokemonApi.useGetInfinitePokemonInfiniteQuery).toBeFunction()
|
|---|
| 80 |
|
|---|
| 81 | expectTypeOf<
|
|---|
| 82 | Parameters<
|
|---|
| 83 | typeof pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery
|
|---|
| 84 | >[0]
|
|---|
| 85 | >().toEqualTypeOf<string | typeof skipToken>()
|
|---|
| 86 |
|
|---|
| 87 | expectTypeOf(pokemonApi.endpoints.getInfinitePokemon.useInfiniteQueryState)
|
|---|
| 88 | .parameter(0)
|
|---|
| 89 | .toEqualTypeOf<string | typeof skipToken>()
|
|---|
| 90 |
|
|---|
| 91 | expectTypeOf(
|
|---|
| 92 | pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuerySubscription,
|
|---|
| 93 | )
|
|---|
| 94 | .parameter(0)
|
|---|
| 95 | .toEqualTypeOf<string | typeof skipToken>()
|
|---|
| 96 |
|
|---|
| 97 | const slice = createSlice({
|
|---|
| 98 | name: 'pokemon',
|
|---|
| 99 | initialState: {} as { data: Pokemon[] },
|
|---|
| 100 | reducers: {},
|
|---|
| 101 | extraReducers: (builder) => {
|
|---|
| 102 | builder.addMatcher(
|
|---|
| 103 | pokemonApi.endpoints.getInfinitePokemon.matchFulfilled,
|
|---|
| 104 | (state, action) => {
|
|---|
| 105 | expectTypeOf(action.payload).toEqualTypeOf<
|
|---|
| 106 | InfiniteData<Pokemon[], number>
|
|---|
| 107 | >()
|
|---|
| 108 | },
|
|---|
| 109 | )
|
|---|
| 110 | },
|
|---|
| 111 | })
|
|---|
| 112 |
|
|---|
| 113 | const res = storeRef.store.dispatch(
|
|---|
| 114 | pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}),
|
|---|
| 115 | )
|
|---|
| 116 |
|
|---|
| 117 | const firstResult = await res
|
|---|
| 118 |
|
|---|
| 119 | if (firstResult.status === QueryStatus.fulfilled) {
|
|---|
| 120 | expectTypeOf(firstResult.data.pages).toEqualTypeOf<Pokemon[][]>()
|
|---|
| 121 | expectTypeOf(firstResult.data.pageParams).toEqualTypeOf<number[]>()
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | storeRef.store.dispatch(
|
|---|
| 125 | pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {
|
|---|
| 126 | direction: 'forward',
|
|---|
| 127 | }),
|
|---|
| 128 | )
|
|---|
| 129 |
|
|---|
| 130 | const useGetInfinitePokemonQuery =
|
|---|
| 131 | pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery
|
|---|
| 132 |
|
|---|
| 133 | expectTypeOf<
|
|---|
| 134 | Parameters<typeof useGetInfinitePokemonQuery>[0]
|
|---|
| 135 | >().toEqualTypeOf<string | typeof skipToken>()
|
|---|
| 136 |
|
|---|
| 137 | function PokemonList() {
|
|---|
| 138 | const {
|
|---|
| 139 | data,
|
|---|
| 140 | currentData,
|
|---|
| 141 | isFetching,
|
|---|
| 142 | isUninitialized,
|
|---|
| 143 | isSuccess,
|
|---|
| 144 | fetchNextPage,
|
|---|
| 145 | } = useGetInfinitePokemonQuery('a')
|
|---|
| 146 |
|
|---|
| 147 | expectTypeOf(data).toEqualTypeOf<
|
|---|
| 148 | InfiniteData<Pokemon[], number> | undefined
|
|---|
| 149 | >()
|
|---|
| 150 |
|
|---|
| 151 | if (isSuccess) {
|
|---|
| 152 | expectTypeOf(data.pages).toEqualTypeOf<Pokemon[][]>()
|
|---|
| 153 | expectTypeOf(data.pageParams).toEqualTypeOf<number[]>()
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | if (currentData) {
|
|---|
| 157 | expectTypeOf(currentData.pages).toEqualTypeOf<Pokemon[][]>()
|
|---|
| 158 | expectTypeOf(currentData.pageParams).toEqualTypeOf<number[]>()
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | const handleClick = async () => {
|
|---|
| 162 | const res = await fetchNextPage()
|
|---|
| 163 |
|
|---|
| 164 | if (res.status === QueryStatus.fulfilled) {
|
|---|
| 165 | expectTypeOf(res.data.pages).toEqualTypeOf<Pokemon[][]>()
|
|---|
| 166 | expectTypeOf(res.data.pageParams).toEqualTypeOf<number[]>()
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | })
|
|---|
| 171 | })
|
|---|