Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

inject-queries.test-d.ts

196 lines5.5 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { createKey } from '@tanstack/query-test-utils'
3import { skipToken } from '..'
4import { injectQueries } from '../inject-queries'
5import { queryOptions } from '../query-options'
6import type { CreateQueryOptions, CreateQueryResult, OmitKeyof } from '..'
7import type { Signal } from '@angular/core'
8​
9describe('injectQueries', () => {
10 describe('config object overload', () => {
11 it('should always define TData when initialData is provided as an object', () => {
12 const key1 = createKey()
13 const key2 = createKey()
14 const key3 = createKey()
15​
16 const query1 = {
17 queryKey: key1,
18 queryFn: () => {
19 return {
20 wow: true,
21 }
22 },
23 initialData: {
24 wow: false,
25 },
26 }
27​
28 const query2 = {
29 queryKey: key2,
30 queryFn: () => 'Query Data',
31 initialData: 'initial data',
32 }
33​
34 const query3 = {
35 queryKey: key3,
36 queryFn: () => 'Query Data',
37 }
38​
39 const queryResults = injectQueries(() => ({
40 queries: [query1, query2, query3],
41 }))
42​
43 const query1Data = queryResults()[0].data()
44 const query2Data = queryResults()[1].data()
45 const query3Data = queryResults()[2].data()
46​
47 expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean }>()
48 expectTypeOf(query2Data).toEqualTypeOf<string>()
49 expectTypeOf(query3Data).toEqualTypeOf<string | undefined>()
50 })
51​
52 it('should define TData when passed through queryOptions', () => {
53 const key = createKey()
54 const options = queryOptions({
55 queryKey: key,
56 queryFn: () => {
57 return {
58 wow: true,
59 }
60 },
61 initialData: {
62 wow: true,
63 },
64 })
65 const queryResults = injectQueries(() => ({ queries: [options] }))
66​
67 const data = queryResults()[0].data()
68​
69 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
70 })
71​
72 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into injectQuery', () => {
73 const key1 = createKey()
74 const key2 = createKey()
75​
76 const query1 = queryOptions({
77 queryKey: key1,
78 queryFn: () => Promise.resolve(1),
79 select: (data) => data > 1,
80 })
81​
82 const query2 = {
83 queryKey: key2,
84 queryFn: () => Promise.resolve(1),
85 select: (data: number) => data > 1,
86 }
87​
88 const queryResults = injectQueries(() => ({ queries: [query1, query2] }))
89 const query1Data = queryResults()[0].data()
90 const query2Data = queryResults()[1].data()
91​
92 expectTypeOf(query1Data).toEqualTypeOf<boolean | undefined>()
93 expectTypeOf(query2Data).toEqualTypeOf<boolean | undefined>()
94 })
95​
96 it('should have undefined in the TData union when initialData is provided as a function which can return undefined', () => {
97 const key = createKey()
98 const queryResults = injectQueries(() => ({
99 queries: [
100 {
101 queryKey: key,
102 queryFn: () => {
103 return {
104 wow: true,
105 }
106 },
107 initialData: () => undefined as { wow: boolean } | undefined,
108 },
109 ],
110 }))
111​
112 const data = queryResults()[0].data()
113​
114 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
115 })
116​
117 describe('custom injectable', () => {
118 it('should allow custom hooks using UseQueryOptions', () => {
119 type Data = string
120 const key = createKey()
121​
122 const injectCustomQueries = (
123 options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
124 ) => {
125 return injectQueries(() => ({
126 queries: [
127 {
128 ...options,
129 queryKey: key,
130 queryFn: () => Promise.resolve('data'),
131 },
132 ],
133 }))
134 }
135​
136 const queryResults = injectCustomQueries()
137 const data = queryResults()[0].data()
138​
139 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
140 })
141 })
142​
143 it('should have correct TData type when conditional skipToken is passed', () => {
144 const key = createKey()
145 const queryResults = injectQueries(() => ({
146 queries: [
147 {
148 queryKey: key,
149 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
150 },
151 ],
152 }))
153​
154 const firstResult = queryResults()[0]
155​
156 expectTypeOf(firstResult).toEqualTypeOf<
157 CreateQueryResult<number, Error>
158 >()
159 expectTypeOf(firstResult.data()).toEqualTypeOf<number | undefined>()
160 })
161​
162 it('should return correct data for dynamic queries with mixed result types', () => {
163 const key1 = createKey()
164 const key2 = createKey()
165​
166 const Queries1 = {
167 get: () =>
168 queryOptions({
169 queryKey: key1,
170 queryFn: () => Promise.resolve(1),
171 }),
172 }
173 const Queries2 = {
174 get: () =>
175 queryOptions({
176 queryKey: key2,
177 queryFn: () => Promise.resolve(true),
178 }),
179 }
180​
181 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
182 const result = injectQueries(() => ({
183 queries: [...queries1List, { ...Queries2.get() }],
184 }))
185​
186 expectTypeOf(result).branded.toEqualTypeOf<
187 Signal<
188 [
189 ...Array<CreateQueryResult<number, Error>>,
190 CreateQueryResult<boolean, Error>,
191 ]
192 >
193 >()
194 })
195 })
196})