Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { createKey, sleep } from '@tanstack/query-test-utils'
4import { useInfiniteQuery } from '../useInfiniteQuery'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import type { InfiniteData } from '@tanstack/query-core'
7β
8describe('Discriminated union return type', () => {
9 it('should have possibly undefined data by default', () => {
10 const key = createKey()
11 const query = reactive(
12 useInfiniteQuery({
13 queryKey: key,
14 queryFn: () => sleep(0).then(() => 'Some data'),
15 getNextPageParam: () => undefined,
16 initialPageParam: 0,
17 }),
18 )
19β
20 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
21 expectTypeOf(query.data).toEqualTypeOf<
22 InfiniteData<string, unknown> | undefined
23 >()
24 })
25β
26 it('should have defined data when initialData is provided as an object', () => {
27 const key = createKey()
28 const query = reactive(
29 useInfiniteQuery({
30 queryKey: key,
31 queryFn: () => sleep(0).then(() => 'Some data'),
32 getNextPageParam: () => undefined,
33 initialPageParam: 0,
34 initialData: { pages: ['Some data'], pageParams: [0] },
35 }),
36 )
37β
38 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
39 })
40β
41 it('should have defined data when initialData is provided as a function which always returns the data', () => {
42 const key = createKey()
43 const query = reactive(
44 useInfiniteQuery({
45 queryKey: key,
46 queryFn: () => sleep(0).then(() => 'Some data'),
47 getNextPageParam: () => undefined,
48 initialPageParam: 0,
49 initialData: () => ({ pages: ['Some data'], pageParams: [0] }),
50 }),
51 )
52β
53 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
54 })
55β
56 it('should have defined data when query is success', () => {
57 const key = createKey()
58 const query = reactive(
59 useInfiniteQuery({
60 queryKey: key,
61 queryFn: () => sleep(0).then(() => 'Some data'),
62 getNextPageParam: () => undefined,
63 initialPageParam: 0,
64 }),
65 )
66β
67 if (query.isSuccess) {
68 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
69 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
70 }
71 })
72β
73 it('should have null error when query is success', () => {
74 const key = createKey()
75 const query = reactive(
76 useInfiniteQuery({
77 queryKey: key,
78 queryFn: () => sleep(0).then(() => 'Some data'),
79 getNextPageParam: () => undefined,
80 initialPageParam: 0,
81 }),
82 )
83β
84 if (query.isSuccess) {
85 expectTypeOf(query.error).toEqualTypeOf<null>()
86 }
87 })
88β
89 it('should have undefined data when query is pending', () => {
90 const key = createKey()
91 const query = reactive(
92 useInfiniteQuery({
93 queryKey: key,
94 queryFn: () => sleep(0).then(() => 'Some data'),
95 getNextPageParam: () => undefined,
96 initialPageParam: 0,
97 }),
98 )
99β
100 if (query.isPending) {
101 expectTypeOf(query.data).toEqualTypeOf<undefined>()
102 }
103 })
104β
105 it('should have defined error when query is error', () => {
106 const key = createKey()
107 const query = reactive(
108 useInfiniteQuery({
109 queryKey: key,
110 queryFn: () => sleep(0).then(() => 'Some data'),
111 getNextPageParam: () => undefined,
112 initialPageParam: 0,
113 }),
114 )
115β
116 if (query.isError) {
117 expectTypeOf(query.error).toEqualTypeOf<Error>()
118 }
119 })
120β
121 it('should accept computed options', () => {
122 const key = createKey()
123 const options = computed(() => ({
124 queryKey: key,
125 queryFn: () => sleep(0).then(() => 'Some data'),
126 getNextPageParam: () => undefined,
127 initialPageParam: 0,
128 }))
129 const query = reactive(useInfiniteQuery(options))
130β
131 if (query.isSuccess) {
132 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
133 }
134 })
135β
136 it('should accept computed options using infiniteQueryOptions', () => {
137 const key = createKey()
138 const options = computed(() =>
139 infiniteQueryOptions({
140 queryKey: key,
141 queryFn: () => sleep(0).then(() => 'Some data'),
142 getNextPageParam: () => undefined,
143 initialPageParam: 0,
144 }),
145 )
146 const query = reactive(useInfiniteQuery(options))
147β
148 if (query.isSuccess) {
149 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
150 }
151 })
152β
153 it('should accept plain options using infiniteQueryOptions', () => {
154 const key = createKey()
155 const options = () =>
156 infiniteQueryOptions({
157 queryKey: key,
158 queryFn: () => sleep(0).then(() => 'Some data'),
159 getNextPageParam: () => undefined,
160 initialPageParam: 0,
161 })
162 const query = reactive(useInfiniteQuery(options))
163β
164 if (query.isSuccess) {
165 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
166 }
167 })
168})
169β
170describe('queryKey reactivity rules', () => {
171 it('should reject a bare reactive getter for the whole queryKey array', () => {
172 const id = ref(1)
173 assertType(
174 useInfiniteQuery({
175 // @ts-expect-error when passed directly to useInfiniteQuery, queryKey cannot be a bare
176 // reactive getter for the whole array (queryOptions() allows this)
177 queryKey: () => ['post', id.value],
178 queryFn: () => sleep(0).then(() => 'Some data'),
179 getNextPageParam: () => undefined,
180 initialPageParam: 0,
181 }),
182 )
183 })
184})