Open History to see the last commit
1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { computed, reactive, ref } from 'vue-demi'
3import { queryKey, 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('data should be possibly undefined by default', () => {
10 const key = queryKey()
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('data should be defined when query is success', () => {
27 const key = queryKey()
28 const query = reactive(
29 useInfiniteQuery({
30 queryKey: key,
31 queryFn: () => sleep(0).then(() => 'Some data'),
32 getNextPageParam: () => undefined,
33 initialPageParam: 0,
34 }),
35 )
36β
37 if (query.isSuccess) {
38 // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
39 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
40 }
41 })
42β
43 it('error should be null when query is success', () => {
44 const key = queryKey()
45 const query = reactive(
46 useInfiniteQuery({
47 queryKey: key,
48 queryFn: () => sleep(0).then(() => 'Some data'),
49 getNextPageParam: () => undefined,
50 initialPageParam: 0,
51 }),
52 )
53β
54 if (query.isSuccess) {
55 expectTypeOf(query.error).toEqualTypeOf<null>()
56 }
57 })
58β
59 it('data should be undefined when query is pending', () => {
60 const key = queryKey()
61 const query = reactive(
62 useInfiniteQuery({
63 queryKey: key,
64 queryFn: () => sleep(0).then(() => 'Some data'),
65 getNextPageParam: () => undefined,
66 initialPageParam: 0,
67 }),
68 )
69β
70 if (query.isPending) {
71 expectTypeOf(query.data).toEqualTypeOf<undefined>()
72 }
73 })
74β
75 it('error should be defined when query is error', () => {
76 const key = queryKey()
77 const query = reactive(
78 useInfiniteQuery({
79 queryKey: key,
80 queryFn: () => sleep(0).then(() => 'Some data'),
81 getNextPageParam: () => undefined,
82 initialPageParam: 0,
83 }),
84 )
85β
86 if (query.isError) {
87 expectTypeOf(query.error).toEqualTypeOf<Error>()
88 }
89 })
90β
91 it('should accept computed options', () => {
92 const key = queryKey()
93 const options = computed(() => ({
94 queryKey: key,
95 queryFn: () => sleep(0).then(() => 'Some data'),
96 getNextPageParam: () => undefined,
97 initialPageParam: 0,
98 }))
99 const query = reactive(useInfiniteQuery(options))
100β
101 if (query.isSuccess) {
102 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
103 }
104 })
105β
106 it('should accept computed options using infiniteQueryOptions', () => {
107 const key = queryKey()
108 const options = computed(() =>
109 infiniteQueryOptions({
110 queryKey: key,
111 queryFn: () => sleep(0).then(() => 'Some data'),
112 getNextPageParam: () => undefined,
113 initialPageParam: 0,
114 }),
115 )
116 const query = reactive(useInfiniteQuery(options))
117β
118 if (query.isSuccess) {
119 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
120 }
121 })
122β
123 it('should accept plain options using infiniteQueryOptions', () => {
124 const key = queryKey()
125 const options = () =>
126 infiniteQueryOptions({
127 queryKey: key,
128 queryFn: () => sleep(0).then(() => 'Some data'),
129 getNextPageParam: () => undefined,
130 initialPageParam: 0,
131 })
132 const query = reactive(useInfiniteQuery(options))
133β
134 if (query.isSuccess) {
135 expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
136 }
137 })
138})
139β
140describe('queryKey reactivity rules', () => {
141 it('should reject a bare reactive getter for the whole queryKey array', () => {
142 const id = ref(1)
143 assertType(
144 useInfiniteQuery({
145 // @ts-expect-error when passed directly to useInfiniteQuery, queryKey cannot be a bare
146 // reactive getter for the whole array (queryOptions() allows this)
147 queryKey: () => ['post', id.value],
148 queryFn: () => sleep(0).then(() => 'Some data'),
149 getNextPageParam: () => undefined,
150 initialPageParam: 0,
151 }),
152 )
153 })
154})