|
| 1 | +import { describe, expectTypeOf, it } from 'vitest' |
| 2 | +import { QueryClient } from '@tanstack/query-core' |
| 3 | +import { infiniteQueryOptions } from '../infiniteQueryOptions' |
| 4 | +import { useInfiniteQuery } from '../useInfiniteQuery' |
| 5 | +import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery' |
| 6 | +import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core' |
| 7 | + |
| 8 | +describe('queryOptions', () => { |
| 9 | + it('should not allow excess properties', () => { |
| 10 | + infiniteQueryOptions({ |
| 11 | + queryKey: ['key'], |
| 12 | + queryFn: () => Promise.resolve('data'), |
| 13 | + getNextPageParam: () => 1, |
| 14 | + initialPageParam: 1, |
| 15 | + // @ts-expect-error this is a good error, because stallTime does not exist! |
| 16 | + stallTime: 1000, |
| 17 | + }) |
| 18 | + }) |
| 19 | + it('should infer types for callbacks', () => { |
| 20 | + infiniteQueryOptions({ |
| 21 | + queryKey: ['key'], |
| 22 | + queryFn: () => Promise.resolve('data'), |
| 23 | + staleTime: 1000, |
| 24 | + getNextPageParam: () => 1, |
| 25 | + initialPageParam: 1, |
| 26 | + select: (data) => { |
| 27 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() |
| 28 | + }, |
| 29 | + }) |
| 30 | + }) |
| 31 | + it('should work when passed to useInfiniteQuery', () => { |
| 32 | + const options = infiniteQueryOptions({ |
| 33 | + queryKey: ['key'], |
| 34 | + queryFn: () => Promise.resolve('string'), |
| 35 | + getNextPageParam: () => 1, |
| 36 | + initialPageParam: 1, |
| 37 | + }) |
| 38 | + |
| 39 | + const { data } = useInfiniteQuery(options) |
| 40 | + |
| 41 | + // known issue: type of pageParams is unknown when returned from useInfiniteQuery |
| 42 | + expectTypeOf(data).toEqualTypeOf< |
| 43 | + InfiniteData<string, unknown> | undefined |
| 44 | + >() |
| 45 | + }) |
| 46 | + it('should work when passed to useSuspenseInfiniteQuery', () => { |
| 47 | + const options = infiniteQueryOptions({ |
| 48 | + queryKey: ['key'], |
| 49 | + queryFn: () => Promise.resolve('string'), |
| 50 | + getNextPageParam: () => 1, |
| 51 | + initialPageParam: 1, |
| 52 | + }) |
| 53 | + |
| 54 | + const { data } = useSuspenseInfiniteQuery(options) |
| 55 | + |
| 56 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>() |
| 57 | + }) |
| 58 | + it('should work when passed to fetchInfiniteQuery', async () => { |
| 59 | + const options = infiniteQueryOptions({ |
| 60 | + queryKey: ['key'], |
| 61 | + queryFn: () => Promise.resolve('string'), |
| 62 | + getNextPageParam: () => 1, |
| 63 | + initialPageParam: 1, |
| 64 | + }) |
| 65 | + |
| 66 | + const data = await new QueryClient().fetchInfiniteQuery(options) |
| 67 | + |
| 68 | + expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() |
| 69 | + }) |
| 70 | + it('should tag the queryKey with the result type of the QueryFn', () => { |
| 71 | + const { queryKey } = infiniteQueryOptions({ |
| 72 | + queryKey: ['key'], |
| 73 | + queryFn: () => Promise.resolve('string'), |
| 74 | + getNextPageParam: () => 1, |
| 75 | + initialPageParam: 1, |
| 76 | + }) |
| 77 | + |
| 78 | + expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< |
| 79 | + InfiniteData<string> |
| 80 | + >() |
| 81 | + }) |
| 82 | + it('should tag the queryKey even if no promise is returned', () => { |
| 83 | + const { queryKey } = infiniteQueryOptions({ |
| 84 | + queryKey: ['key'], |
| 85 | + queryFn: () => 'string', |
| 86 | + getNextPageParam: () => 1, |
| 87 | + initialPageParam: 1, |
| 88 | + }) |
| 89 | + |
| 90 | + expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< |
| 91 | + InfiniteData<string> |
| 92 | + >() |
| 93 | + }) |
| 94 | + it('should tag the queryKey with the result type of the QueryFn if select is used', () => { |
| 95 | + const { queryKey } = infiniteQueryOptions({ |
| 96 | + queryKey: ['key'], |
| 97 | + queryFn: () => Promise.resolve('string'), |
| 98 | + select: (data) => data.pages, |
| 99 | + getNextPageParam: () => 1, |
| 100 | + initialPageParam: 1, |
| 101 | + }) |
| 102 | + |
| 103 | + expectTypeOf<(typeof queryKey)[typeof dataTagSymbol]>().toEqualTypeOf< |
| 104 | + InfiniteData<string> |
| 105 | + >() |
| 106 | + }) |
| 107 | + it('should return the proper type when passed to getQueryData', () => { |
| 108 | + const { queryKey } = infiniteQueryOptions({ |
| 109 | + queryKey: ['key'], |
| 110 | + queryFn: () => Promise.resolve('string'), |
| 111 | + getNextPageParam: () => 1, |
| 112 | + initialPageParam: 1, |
| 113 | + }) |
| 114 | + |
| 115 | + const queryClient = new QueryClient() |
| 116 | + const data = queryClient.getQueryData(queryKey) |
| 117 | + |
| 118 | + expectTypeOf(data).toEqualTypeOf< |
| 119 | + InfiniteData<string, unknown> | undefined |
| 120 | + >() |
| 121 | + }) |
| 122 | + it('should properly type when passed to setQueryData', () => { |
| 123 | + const { queryKey } = infiniteQueryOptions({ |
| 124 | + queryKey: ['key'], |
| 125 | + queryFn: () => Promise.resolve('string'), |
| 126 | + getNextPageParam: () => 1, |
| 127 | + initialPageParam: 1, |
| 128 | + }) |
| 129 | + |
| 130 | + const queryClient = new QueryClient() |
| 131 | + const data = queryClient.setQueryData(queryKey, (prev) => { |
| 132 | + expectTypeOf(prev).toEqualTypeOf< |
| 133 | + InfiniteData<string, unknown> | undefined |
| 134 | + >() |
| 135 | + return prev |
| 136 | + }) |
| 137 | + |
| 138 | + expectTypeOf(data).toEqualTypeOf< |
| 139 | + InfiniteData<string, unknown> | undefined |
| 140 | + >() |
| 141 | + }) |
| 142 | +}) |
0 commit comments