| 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 |
) => { |
| 29 |
expectTypeOf(lastPage).toEqualTypeOf<Pokemon[]>() |
| 30 |
|
| 31 |
expectTypeOf(allPages).toEqualTypeOf<Pokemon[][]>() |
| 32 |
|
| 33 |
expectTypeOf(lastPageParam).toBeNumber() |
| 34 |
|
| 35 |
expectTypeOf(allPageParams).toEqualTypeOf<number[]>() |
| 36 |
|
| 37 |
return lastPageParam + 1 |
| 38 |
}, |
| 39 |
}, |
| 40 |
query({ pageParam, queryArg }) { |
| 41 |
expectTypeOf(pageParam).toBeNumber() |
| 42 |
expectTypeOf(queryArg).toBeString() |
| 43 |
|
| 44 |
return `https://example.com/listItems?page=${pageParam}` |
| 45 |
}, |
| 46 |
async onCacheEntryAdded(arg, api) { |
| 47 |
const data = await api.cacheDataLoaded |
| 48 |
expectTypeOf(data.data).toEqualTypeOf< |
| 49 |
InfiniteData<Pokemon[], number> |
| 50 |
>() |
| 51 |
}, |
| 52 |
async onQueryStarted(arg, api) { |
| 53 |
const data = await api.queryFulfilled |
| 54 |
expectTypeOf(data.data).toEqualTypeOf< |
| 55 |
InfiniteData<Pokemon[], number> |
| 56 |
>() |
| 57 |
}, |
| 58 |
providesTags: (result) => { |
| 59 |
expectTypeOf(result).toEqualTypeOf< |
| 60 |
InfiniteData<Pokemon[], number> | undefined |
| 61 |
>() |
| 62 |
return [] |
| 63 |
}, |
| 64 |
}), |
| 65 |
}), |
| 66 |
}) |
| 67 |
|
| 68 |
const storeRef = setupApiStore(pokemonApi, undefined, { |
| 69 |
withoutTestLifecycles: true, |
| 70 |
}) |
| 71 |
|
| 72 |
expectTypeOf(pokemonApi.endpoints.getInfinitePokemon.initiate) |
| 73 |
.parameter(0) |
| 74 |
.toBeString() |
| 75 |
|
| 76 |
expectTypeOf(pokemonApi.useGetInfinitePokemonInfiniteQuery).toBeFunction() |
| 77 |
|
| 78 |
expectTypeOf(pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery) |
| 79 |
.parameter(0) |
| 80 |
.toEqualTypeOf<string | typeof skipToken>() |
| 81 |
|
| 82 |
expectTypeOf(pokemonApi.endpoints.getInfinitePokemon.useInfiniteQueryState) |
| 83 |
.parameter(0) |
| 84 |
.toEqualTypeOf<string | typeof skipToken>() |
| 85 |
|
| 86 |
expectTypeOf( |
| 87 |
pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuerySubscription, |
| 88 |
) |
| 89 |
.parameter(0) |
| 90 |
.toEqualTypeOf<string | typeof skipToken>() |
| 91 |
|
| 92 |
const slice = createSlice({ |
| 93 |
name: 'pokemon', |
| 94 |
initialState: {} as { data: Pokemon[] }, |
| 95 |
reducers: {}, |
| 96 |
extraReducers: (builder) => { |
| 97 |
builder.addMatcher( |
| 98 |
pokemonApi.endpoints.getInfinitePokemon.matchFulfilled, |
| 99 |
(state, action) => { |
| 100 |
expectTypeOf(action.payload).toEqualTypeOf< |
| 101 |
InfiniteData<Pokemon[], number> |
| 102 |
>() |
| 103 |
}, |
| 104 |
) |
| 105 |
}, |
| 106 |
}) |
| 107 |
|
| 108 |
const res = storeRef.store.dispatch( |
| 109 |
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}), |
| 110 |
) |
| 111 |
|
| 112 |
const firstResult = await res |
| 113 |
|
| 114 |
if (firstResult.status === QueryStatus.fulfilled) { |
| 115 |
expectTypeOf(firstResult.data.pages).toEqualTypeOf<Pokemon[][]>() |
| 116 |
expectTypeOf(firstResult.data.pageParams).toEqualTypeOf<number[]>() |
| 117 |
} |
| 118 |
|
| 119 |
storeRef.store.dispatch( |
| 120 |
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', { |
| 121 |
direction: 'forward', |
| 122 |
}), |
| 123 |
) |
| 124 |
|
| 125 |
const useGetInfinitePokemonQuery = |
| 126 |
pokemonApi.endpoints.getInfinitePokemon.useInfiniteQuery |
| 127 |
|
| 128 |
expectTypeOf(useGetInfinitePokemonQuery) |
| 129 |
.parameter(0) |
| 130 |
.toEqualTypeOf<string | typeof skipToken>() |
| 131 |
|
| 132 |
function PokemonList() { |
| 133 |
const { |
| 134 |
data, |
| 135 |
currentData, |
| 136 |
isFetching, |
| 137 |
isUninitialized, |
| 138 |
isSuccess, |
| 139 |
fetchNextPage, |
| 140 |
} = useGetInfinitePokemonQuery('a') |
| 141 |
|
| 142 |
expectTypeOf(data).toEqualTypeOf< |
| 143 |
InfiniteData<Pokemon[], number> | undefined |
| 144 |
>() |
| 145 |
|
| 146 |
if (isSuccess) { |
| 147 |
expectTypeOf(data.pages).toEqualTypeOf<Pokemon[][]>() |
| 148 |
expectTypeOf(data.pageParams).toEqualTypeOf<number[]>() |
| 149 |
} |
| 150 |
|
| 151 |
if (currentData) { |
| 152 |
expectTypeOf(currentData.pages).toEqualTypeOf<Pokemon[][]>() |
| 153 |
expectTypeOf(currentData.pageParams).toEqualTypeOf<number[]>() |
| 154 |
} |
| 155 |
|
| 156 |
const handleClick = async () => { |
| 157 |
const res = await fetchNextPage() |
| 158 |
|
| 159 |
if (res.status === QueryStatus.fulfilled) { |
| 160 |
expectTypeOf(res.data.pages).toEqualTypeOf<Pokemon[][]>() |
| 161 |
expectTypeOf(res.data.pageParams).toEqualTypeOf<number[]>() |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
}) |
| 166 |
}) |