Open History to see the last commit
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 be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
37 const options = queryOptions({
38 queryKey: ['key'],
39 queryFn: () => Promise.resolve(1),
40 })
41β
42 const query = injectQuery(() => ({
43 ...options,
44 select: (data) => data > 1,
45 }))
46β
47 expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
48 })
49β
50 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
51 const { data } = injectQuery(() => ({
52 queryKey: ['key'],
53 queryFn: () => {
54 return {
55 wow: true,
56 }
57 },
58 initialData: () => ({
59 wow: true,
60 }),
61 }))
62β
63 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
64 })
65β
66 it('TData should have undefined in the union when initialData is NOT provided', () => {
67 const { data } = injectQuery(() => ({
68 queryKey: ['key'],
69 queryFn: () => {
70 return {
71 wow: true,
72 }
73 },
74 }))
75β
76 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
77 })
78β
79 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
80 const { data } = injectQuery(() => ({
81 queryKey: ['key'],
82 queryFn: () => {
83 return {
84 wow: true,
85 }
86 },
87 initialData: () => undefined as { wow: boolean } | undefined,
88 }))
89β
90 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
91 })
92β
93 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
94 const query = injectQuery(() => ({
95 queryKey: ['key'],
96 queryFn: () => {
97 return {
98 wow: true,
99 }
100 },
101 initialData: () => undefined as { wow: boolean } | undefined,
102 }))
103β
104 if (query.isSuccess()) {
105 expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
106 }
107 })
108 })
109β
110 describe('structuralSharing', () => {
111 it('should be able to use structuralSharing with unknown types', () => {
112 // https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
113 injectQuery(() => ({
114 queryKey: ['key'],
115 queryFn: () => 5,
116 structuralSharing: (oldData, newData) => {
117 expectTypeOf(oldData).toBeUnknown()
118 expectTypeOf(newData).toBeUnknown()
119 return newData
120 },
121 }))
122 })
123 })
124})
125β
126describe('Discriminated union return type', () => {
127 test('data should be possibly undefined by default', () => {
128 const query = injectQuery(() => ({
129 queryKey: ['key'],
130 queryFn: () => sleep(0).then(() => 'Some data'),
131 }))
132β
133 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
134 })
135β
136 test('data should be defined when query is success', () => {
137 const query = injectQuery(() => ({
138 queryKey: ['key'],
139 queryFn: () => sleep(0).then(() => 'Some data'),
140 }))
141β
142 if (query.isSuccess()) {
143 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
144 }
145 })
146β
147 test('error should be null when query is success', () => {
148 const query = injectQuery(() => ({
149 queryKey: ['key'],
150 queryFn: () => sleep(0).then(() => 'Some data'),
151 }))
152β
153 if (query.isSuccess()) {
154 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
155 }
156 })
157β
158 test('data should be undefined when query is pending', () => {
159 const query = injectQuery(() => ({
160 queryKey: ['key'],
161 queryFn: () => sleep(0).then(() => 'Some data'),
162 }))
163β
164 if (query.isPending()) {
165 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
166 }
167 })
168β
169 test('error should be defined when query is error', () => {
170 const query = injectQuery(() => ({
171 queryKey: ['key'],
172 queryFn: () => sleep(0).then(() => 'Some data'),
173 }))
174β
175 if (query.isError()) {
176 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
177 }
178 })
179})