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

inject-query.test-d.ts

194 lines5.8 KB

1import { describe, expectTypeOf, it, test } from 'vitest'
2import { queryKey, sleep } from '@tanstack/query-test-utils'
3import { injectQuery, queryOptions } from '..'
4import type { Signal } from '@angular/core'
5​
6describe('injectQuery', () => {
7 describe('initialData', () => {
8 describe('Config object overload', () => {
9 it('TData should always be defined when initialData is provided as an object', () => {
10 const key = queryKey()
11 const { data } = injectQuery(() => ({
12 queryKey: key,
13 queryFn: () => ({ wow: true }),
14 initialData: { wow: true },
15 }))
16​
17 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
18 })
19​
20 it('TData should be defined when passed through queryOptions', () => {
21 const key = queryKey()
22 const options = () =>
23 queryOptions({
24 queryKey: key,
25 queryFn: () => {
26 return {
27 wow: true,
28 }
29 },
30 initialData: {
31 wow: true,
32 },
33 })
34 const { data } = injectQuery(options)
35​
36 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
37 })
38​
39 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
40 const key = queryKey()
41 const options = queryOptions({
42 queryKey: key,
43 queryFn: () => Promise.resolve(1),
44 })
45​
46 const query = injectQuery(() => ({
47 ...options,
48 select: (data) => data > 1,
49 }))
50​
51 expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
52 })
53​
54 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
55 const key = queryKey()
56 const { data } = injectQuery(() => ({
57 queryKey: key,
58 queryFn: () => {
59 return {
60 wow: true,
61 }
62 },
63 initialData: () => ({
64 wow: true,
65 }),
66 }))
67​
68 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
69 })
70​
71 it('TData should have undefined in the union when initialData is NOT provided', () => {
72 const key = queryKey()
73 const { data } = injectQuery(() => ({
74 queryKey: key,
75 queryFn: () => {
76 return {
77 wow: true,
78 }
79 },
80 }))
81​
82 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
83 })
84​
85 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
86 const key = queryKey()
87 const { data } = injectQuery(() => ({
88 queryKey: key,
89 queryFn: () => {
90 return {
91 wow: true,
92 }
93 },
94 initialData: () => undefined as { wow: boolean } | undefined,
95 }))
96​
97 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
98 })
99​
100 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
101 const key = queryKey()
102 const query = injectQuery(() => ({
103 queryKey: key,
104 queryFn: () => {
105 return {
106 wow: true,
107 }
108 },
109 initialData: () => undefined as { wow: boolean } | undefined,
110 }))
111​
112 if (query.isSuccess()) {
113 expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
114 }
115 })
116 })
117​
118 describe('structuralSharing', () => {
119 it('should be able to use structuralSharing with unknown types', () => {
120 const key = queryKey()
121 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
122 injectQuery(() => ({
123 queryKey: key,
124 queryFn: () => 5,
125 structuralSharing: (oldData, newData) => {
126 expectTypeOf(oldData).toBeUnknown()
127 expectTypeOf(newData).toBeUnknown()
128 return newData
129 },
130 }))
131 })
132 })
133 })
134​
135 describe('Discriminated union return type', () => {
136 test('data should be possibly undefined by default', () => {
137 const key = queryKey()
138 const query = injectQuery(() => ({
139 queryKey: key,
140 queryFn: () => sleep(0).then(() => 'Some data'),
141 }))
142​
143 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
144 })
145​
146 test('data should be defined when query is success', () => {
147 const key = queryKey()
148 const query = injectQuery(() => ({
149 queryKey: key,
150 queryFn: () => sleep(0).then(() => 'Some data'),
151 }))
152​
153 if (query.isSuccess()) {
154 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
155 }
156 })
157​
158 test('error should be null when query is success', () => {
159 const key = queryKey()
160 const query = injectQuery(() => ({
161 queryKey: key,
162 queryFn: () => sleep(0).then(() => 'Some data'),
163 }))
164​
165 if (query.isSuccess()) {
166 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
167 }
168 })
169​
170 test('data should be undefined when query is pending', () => {
171 const key = queryKey()
172 const query = injectQuery(() => ({
173 queryKey: key,
174 queryFn: () => sleep(0).then(() => 'Some data'),
175 }))
176​
177 if (query.isPending()) {
178 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
179 }
180 })
181​
182 test('error should be defined when query is error', () => {
183 const key = queryKey()
184 const query = injectQuery(() => ({
185 queryKey: key,
186 queryFn: () => sleep(0).then(() => 'Some data'),
187 }))
188​
189 if (query.isError()) {
190 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
191 }
192 })
193 })
194})