|
| 1 | +import { assertType, describe, expectTypeOf, it, test } from 'vitest' |
| 2 | +import { QueryClient, dataTagSymbol } from '@tanstack/query-core' |
| 3 | +import { infiniteQueryOptions } from '../infinite-query-options' |
| 4 | +import { injectInfiniteQuery } from '../inject-infinite-query' |
| 5 | +import { injectQuery } from '../inject-query' |
| 6 | +import type { |
| 7 | + DataTag, |
| 8 | + InfiniteData, |
| 9 | + InitialDataFunction, |
| 10 | +} from '@tanstack/query-core' |
| 11 | + |
| 12 | +describe('infiniteQueryOptions', () => { |
| 13 | + it('should not allow excess properties', () => { |
| 14 | + assertType( |
| 15 | + infiniteQueryOptions({ |
| 16 | + queryKey: ['key'], |
| 17 | + queryFn: () => Promise.resolve('data'), |
| 18 | + getNextPageParam: () => 1, |
| 19 | + initialPageParam: 1, |
| 20 | + // @ts-expect-error this is a good error, because stallTime does not exist! |
| 21 | + stallTime: 1000, |
| 22 | + }), |
| 23 | + ) |
| 24 | + }) |
| 25 | + it('should infer types for callbacks', () => { |
| 26 | + infiniteQueryOptions({ |
| 27 | + queryKey: ['key'], |
| 28 | + queryFn: () => Promise.resolve('data'), |
| 29 | + staleTime: 1000, |
| 30 | + getNextPageParam: () => 1, |
| 31 | + initialPageParam: 1, |
| 32 | + select: (data) => { |
| 33 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() |
| 34 | + }, |
| 35 | + }) |
| 36 | + }) |
| 37 | + it('should work when passed to useInfiniteQuery', () => { |
| 38 | + const options = infiniteQueryOptions({ |
| 39 | + queryKey: ['key'], |
| 40 | + queryFn: () => Promise.resolve('string'), |
| 41 | + getNextPageParam: () => 1, |
| 42 | + initialPageParam: 1, |
| 43 | + }) |
| 44 | + |
| 45 | + const { data } = injectInfiniteQuery(() => options) |
| 46 | + |
| 47 | + // known issue: type of pageParams is unknown when returned from useInfiniteQuery |
| 48 | + expectTypeOf(data()).toEqualTypeOf< |
| 49 | + InfiniteData<string, unknown> | undefined |
| 50 | + >() |
| 51 | + }) |
| 52 | + |
| 53 | + it('should work when passed to fetchInfiniteQuery', async () => { |
| 54 | + const options = infiniteQueryOptions({ |
| 55 | + queryKey: ['key'], |
| 56 | + queryFn: () => Promise.resolve('string'), |
| 57 | + getNextPageParam: () => 1, |
| 58 | + initialPageParam: 1, |
| 59 | + }) |
| 60 | + |
| 61 | + const data = await new QueryClient().fetchInfiniteQuery(options) |
| 62 | + |
| 63 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() |
| 64 | + }) |
| 65 | + it('should tag the queryKey with the result type of the QueryFn', () => { |
| 66 | + const { queryKey } = infiniteQueryOptions({ |
| 67 | + queryKey: ['key'], |
| 68 | + queryFn: () => Promise.resolve('string'), |
| 69 | + getNextPageParam: () => 1, |
| 70 | + initialPageParam: 1, |
| 71 | + }) |
| 72 | + |
| 73 | + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>() |
| 74 | + }) |
| 75 | + it('should tag the queryKey even if no promise is returned', () => { |
| 76 | + const { queryKey } = infiniteQueryOptions({ |
| 77 | + queryKey: ['key'], |
| 78 | + queryFn: () => 'string', |
| 79 | + getNextPageParam: () => 1, |
| 80 | + initialPageParam: 1, |
| 81 | + }) |
| 82 | + |
| 83 | + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>() |
| 84 | + }) |
| 85 | + it('should tag the queryKey with the result type of the QueryFn if select is used', () => { |
| 86 | + const { queryKey } = infiniteQueryOptions({ |
| 87 | + queryKey: ['key'], |
| 88 | + queryFn: () => Promise.resolve('string'), |
| 89 | + select: (data) => data.pages, |
| 90 | + getNextPageParam: () => 1, |
| 91 | + initialPageParam: 1, |
| 92 | + }) |
| 93 | + |
| 94 | + expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>() |
| 95 | + }) |
| 96 | + it('should return the proper type when passed to getQueryData', () => { |
| 97 | + const { queryKey } = infiniteQueryOptions({ |
| 98 | + queryKey: ['key'], |
| 99 | + queryFn: () => Promise.resolve('string'), |
| 100 | + getNextPageParam: () => 1, |
| 101 | + initialPageParam: 1, |
| 102 | + }) |
| 103 | + |
| 104 | + const queryClient = new QueryClient() |
| 105 | + const data = queryClient.getQueryData(queryKey) |
| 106 | + |
| 107 | + expectTypeOf(data).toEqualTypeOf< |
| 108 | + InfiniteData<string, unknown> | undefined |
| 109 | + >() |
| 110 | + }) |
| 111 | + it('should properly type when passed to setQueryData', () => { |
| 112 | + const { queryKey } = infiniteQueryOptions({ |
| 113 | + queryKey: ['key'], |
| 114 | + queryFn: () => Promise.resolve('string'), |
| 115 | + getNextPageParam: () => 1, |
| 116 | + initialPageParam: 1, |
| 117 | + }) |
| 118 | + |
| 119 | + const queryClient = new QueryClient() |
| 120 | + const data = queryClient.setQueryData(queryKey, (prev) => { |
| 121 | + expectTypeOf(prev).toEqualTypeOf< |
| 122 | + InfiniteData<string, unknown> | undefined |
| 123 | + >() |
| 124 | + return prev |
| 125 | + }) |
| 126 | + |
| 127 | + expectTypeOf(data).toEqualTypeOf< |
| 128 | + InfiniteData<string, unknown> | undefined |
| 129 | + >() |
| 130 | + }) |
| 131 | + |
| 132 | + test('should not be allowed to be passed to non-infinite query functions', () => { |
| 133 | + const queryClient = new QueryClient() |
| 134 | + const options = infiniteQueryOptions({ |
| 135 | + queryKey: ['key'], |
| 136 | + queryFn: () => Promise.resolve('string'), |
| 137 | + getNextPageParam: () => 1, |
| 138 | + initialPageParam: 1, |
| 139 | + }) |
| 140 | + assertType( |
| 141 | + // @ts-expect-error cannot pass infinite options to non-infinite query functions |
| 142 | + injectQuery(() => options), |
| 143 | + ) |
| 144 | + assertType( |
| 145 | + // @ts-expect-error cannot pass infinite options to non-infinite query functions |
| 146 | + queryClient.ensureQueryData(options), |
| 147 | + ) |
| 148 | + assertType( |
| 149 | + // @ts-expect-error cannot pass infinite options to non-infinite query functions |
| 150 | + queryClient.fetchQuery(options), |
| 151 | + ) |
| 152 | + assertType( |
| 153 | + // @ts-expect-error cannot pass infinite options to non-infinite query functions |
| 154 | + queryClient.prefetchQuery(options), |
| 155 | + ) |
| 156 | + }) |
| 157 | + |
| 158 | + test('allow optional initialData function', () => { |
| 159 | + const initialData: { example: boolean } | undefined = { example: true } |
| 160 | + const queryOptions = infiniteQueryOptions({ |
| 161 | + queryKey: ['example'], |
| 162 | + queryFn: () => initialData, |
| 163 | + initialData: initialData |
| 164 | + ? () => ({ pages: [initialData], pageParams: [] }) |
| 165 | + : undefined, |
| 166 | + getNextPageParam: () => 1, |
| 167 | + initialPageParam: 1, |
| 168 | + }) |
| 169 | + expectTypeOf(queryOptions.initialData).toMatchTypeOf< |
| 170 | + | InitialDataFunction<InfiniteData<{ example: boolean }, number>> |
| 171 | + | InfiniteData<{ example: boolean }, number> |
| 172 | + | undefined |
| 173 | + >() |
| 174 | + }) |
| 175 | + |
| 176 | + test('allow optional initialData object', () => { |
| 177 | + const initialData: { example: boolean } | undefined = { example: true } |
| 178 | + const queryOptions = infiniteQueryOptions({ |
| 179 | + queryKey: ['example'], |
| 180 | + queryFn: () => initialData, |
| 181 | + initialData: initialData |
| 182 | + ? { pages: [initialData], pageParams: [] } |
| 183 | + : undefined, |
| 184 | + getNextPageParam: () => 1, |
| 185 | + initialPageParam: 1, |
| 186 | + }) |
| 187 | + expectTypeOf(queryOptions.initialData).toMatchTypeOf< |
| 188 | + | InitialDataFunction<InfiniteData<{ example: boolean }, number>> |
| 189 | + | InfiniteData<{ example: boolean }, number> |
| 190 | + | undefined |
| 191 | + >() |
| 192 | + }) |
| 193 | + |
| 194 | + it('should return a custom query key type', () => { |
| 195 | + type MyQueryKey = [Array<string>, { type: 'foo' }] |
| 196 | + |
| 197 | + const options = infiniteQueryOptions({ |
| 198 | + queryKey: [['key'], { type: 'foo' }] as MyQueryKey, |
| 199 | + queryFn: () => Promise.resolve(1), |
| 200 | + getNextPageParam: () => 1, |
| 201 | + initialPageParam: 1, |
| 202 | + }) |
| 203 | + |
| 204 | + expectTypeOf(options.queryKey).toEqualTypeOf< |
| 205 | + DataTag<MyQueryKey, InfiniteData<number>, Error> |
| 206 | + >() |
| 207 | + }) |
| 208 | + |
| 209 | + it('should return a custom query key type with datatag', () => { |
| 210 | + type MyQueryKey = DataTag< |
| 211 | + [Array<string>, { type: 'foo' }], |
| 212 | + number, |
| 213 | + Error & { myMessage: string } |
| 214 | + > |
| 215 | + |
| 216 | + const options = infiniteQueryOptions({ |
| 217 | + queryKey: [['key'], { type: 'foo' }] as MyQueryKey, |
| 218 | + queryFn: () => Promise.resolve(1), |
| 219 | + getNextPageParam: () => 1, |
| 220 | + initialPageParam: 1, |
| 221 | + }) |
| 222 | + |
| 223 | + expectTypeOf(options.queryKey).toEqualTypeOf< |
| 224 | + DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }> |
| 225 | + >() |
| 226 | + }) |
| 227 | +}) |
0 commit comments