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

inject-query.test-d.ts

191 lines5.5 KB

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