@tanstack/angular-query-experimental 5.103.2 → 5.104.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/create-base-query.d.mts +168 -0
- package/dist/create-base-query.d.mts.map +1 -0
- package/dist/create-base-query.mjs +91 -0
- package/dist/create-base-query.mjs.map +1 -0
- package/dist/devtools/index.d.mts +3 -0
- package/dist/devtools/index.mjs +2 -0
- package/dist/devtools/production/index.d.mts +4 -0
- package/dist/devtools/production/index.mjs +2 -0
- package/dist/devtools/stub.d.mts +5 -0
- package/dist/devtools/stub.d.mts.map +1 -0
- package/dist/devtools/stub.mjs +9 -0
- package/dist/devtools/stub.mjs.map +1 -0
- package/dist/devtools/types.d.mts +106 -0
- package/dist/devtools/types.d.mts.map +1 -0
- package/dist/devtools/types.mjs +1 -0
- package/{devtools/with-devtools.d.ts → dist/devtools/with-devtools.d.mts} +4 -1
- package/dist/devtools/with-devtools.d.mts.map +1 -0
- package/dist/devtools/with-devtools.mjs +110 -0
- package/dist/devtools/with-devtools.mjs.map +1 -0
- package/dist/devtools-panel/index.d.mts +3 -0
- package/dist/devtools-panel/index.mjs +2 -0
- package/{devtools-panel/inject-devtools-panel.d.ts → dist/devtools-panel/inject-devtools-panel.d.mts} +4 -1
- package/dist/devtools-panel/inject-devtools-panel.d.mts.map +1 -0
- package/dist/devtools-panel/inject-devtools-panel.mjs +67 -0
- package/dist/devtools-panel/inject-devtools-panel.mjs.map +1 -0
- package/dist/devtools-panel/production/index.d.mts +4 -0
- package/dist/devtools-panel/production/index.mjs +2 -0
- package/dist/devtools-panel/stub.d.mts +5 -0
- package/dist/devtools-panel/stub.d.mts.map +1 -0
- package/dist/devtools-panel/stub.mjs +7 -0
- package/dist/devtools-panel/stub.mjs.map +1 -0
- package/dist/devtools-panel/types.d.mts +51 -0
- package/dist/devtools-panel/types.d.mts.map +1 -0
- package/dist/devtools-panel/types.mjs +1 -0
- package/dist/index.d.mts +16 -0
- package/{index.mjs → dist/index.mjs} +8 -21
- package/dist/index.mjs.map +1 -0
- package/{infinite-query-options.d.ts → dist/infinite-query-options.d.mts} +28 -25
- package/dist/infinite-query-options.d.mts.map +1 -0
- package/dist/infinite-query-options.mjs +8 -0
- package/dist/infinite-query-options.mjs.map +1 -0
- package/{inject-infinite-query.d.ts → dist/inject-infinite-query.d.mts} +13 -10
- package/dist/inject-infinite-query.d.mts.map +1 -0
- package/dist/inject-infinite-query.mjs +13 -0
- package/dist/inject-infinite-query.mjs.map +1 -0
- package/{inject-is-fetching.d.ts → dist/inject-is-fetching.d.mts} +11 -8
- package/dist/inject-is-fetching.d.mts.map +1 -0
- package/dist/inject-is-fetching.mjs +69 -0
- package/dist/inject-is-fetching.mjs.map +1 -0
- package/{inject-is-mutating.d.ts → dist/inject-is-mutating.d.mts} +11 -8
- package/dist/inject-is-mutating.d.mts.map +1 -0
- package/dist/inject-is-mutating.mjs +52 -0
- package/dist/inject-is-mutating.mjs.map +1 -0
- package/{inject-is-restoring.d.ts → dist/inject-is-restoring.d.mts} +10 -8
- package/dist/inject-is-restoring.d.mts.map +1 -0
- package/dist/inject-is-restoring.mjs +35 -0
- package/dist/inject-is-restoring.mjs.map +1 -0
- package/{inject-mutation-state.d.ts → dist/inject-mutation-state.d.mts} +13 -11
- package/dist/inject-mutation-state.d.mts.map +1 -0
- package/dist/inject-mutation-state.mjs +103 -0
- package/dist/inject-mutation-state.mjs.map +1 -0
- package/{inject-mutation.d.ts → dist/inject-mutation.d.mts} +12 -9
- package/dist/inject-mutation.d.mts.map +1 -0
- package/dist/inject-mutation.mjs +226 -0
- package/dist/inject-mutation.mjs.map +1 -0
- package/dist/inject-queries-experimental/index.d.mts +2 -0
- package/dist/inject-queries-experimental/index.mjs +2 -0
- package/{inject-queries.d.ts → dist/inject-queries.d.mts} +33 -53
- package/dist/inject-queries.d.mts.map +1 -0
- package/dist/inject-queries.mjs +203 -0
- package/dist/inject-queries.mjs.map +1 -0
- package/{inject-query-client.d.ts → dist/inject-query-client.d.mts} +6 -3
- package/dist/inject-query-client.d.mts.map +1 -0
- package/dist/inject-query-client.mjs +23 -0
- package/dist/inject-query-client.mjs.map +1 -0
- package/{inject-query.d.ts → dist/inject-query.d.mts} +13 -10
- package/dist/inject-query.d.mts.map +1 -0
- package/dist/inject-query.mjs +12 -0
- package/dist/inject-query.mjs.map +1 -0
- package/{mutation-options.d.ts → dist/mutation-options.d.mts} +5 -2
- package/dist/mutation-options.d.mts.map +1 -0
- package/dist/mutation-options.mjs +8 -0
- package/dist/mutation-options.mjs.map +1 -0
- package/dist/pending-tasks-compat.d.mts +9 -0
- package/dist/pending-tasks-compat.d.mts.map +1 -0
- package/dist/pending-tasks-compat.mjs +13 -0
- package/dist/pending-tasks-compat.mjs.map +1 -0
- package/{providers.d.ts → dist/providers.d.mts} +7 -5
- package/dist/providers.d.mts.map +1 -0
- package/dist/providers.mjs +124 -0
- package/dist/providers.mjs.map +1 -0
- package/{query-options.d.ts → dist/query-options.d.mts} +34 -31
- package/dist/query-options.d.mts.map +1 -0
- package/dist/query-options.mjs +8 -0
- package/dist/query-options.mjs.map +1 -0
- package/{signal-proxy.d.ts → dist/signal-proxy.d.mts} +5 -4
- package/dist/signal-proxy.d.mts.map +1 -0
- package/dist/signal-proxy.mjs +35 -0
- package/dist/signal-proxy.mjs.map +1 -0
- package/{types.d.ts → dist/types.d.mts} +24 -26
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +6 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +50 -20
- package/src/__tests__/infinite-query-options.test-d.ts +314 -0
- package/src/__tests__/infinite-query-options.test.ts +19 -0
- package/src/__tests__/inject-devtools-panel.test.ts +196 -0
- package/src/__tests__/inject-infinite-query.test-d.ts +66 -0
- package/src/__tests__/inject-infinite-query.test.ts +227 -0
- package/src/__tests__/inject-is-fetching.test.ts +108 -0
- package/src/__tests__/inject-is-mutating.test.ts +113 -0
- package/src/__tests__/inject-is-restoring.test.ts +93 -0
- package/src/__tests__/inject-mutation-state.test-d.ts +22 -0
- package/src/__tests__/inject-mutation-state.test.ts +198 -0
- package/src/__tests__/inject-mutation.test-d.ts +158 -0
- package/src/__tests__/inject-mutation.test.ts +1161 -0
- package/src/__tests__/inject-queries.test-d.ts +196 -0
- package/src/__tests__/inject-queries.test.ts +240 -0
- package/src/__tests__/inject-query.test-d.ts +357 -0
- package/src/__tests__/inject-query.test.ts +700 -0
- package/src/__tests__/mutation-options.test-d.ts +234 -0
- package/src/__tests__/mutation-options.test.ts +344 -0
- package/src/__tests__/pending-tasks.test.ts +644 -0
- package/src/__tests__/provide-query-client.test.ts +38 -0
- package/src/__tests__/provide-tanstack-query.test.ts +38 -0
- package/src/__tests__/query-options.test-d.ts +262 -0
- package/src/__tests__/query-options.test.ts +14 -0
- package/src/__tests__/signal-proxy.test.ts +27 -0
- package/src/__tests__/test-utils.ts +77 -0
- package/src/__tests__/with-devtools.test.ts +674 -0
- package/src/create-base-query.ts +176 -0
- package/src/devtools/index.ts +8 -0
- package/src/devtools/production/index.ts +1 -0
- package/src/devtools/stub.ts +7 -0
- package/src/devtools/types.ts +116 -0
- package/src/devtools/with-devtools.ts +182 -0
- package/src/devtools-panel/index.ts +8 -0
- package/src/devtools-panel/inject-devtools-panel.ts +110 -0
- package/src/devtools-panel/production/index.ts +1 -0
- package/src/devtools-panel/stub.ts +7 -0
- package/src/devtools-panel/types.ts +57 -0
- package/src/index.ts +60 -0
- package/src/infinite-query-options.ts +354 -0
- package/src/inject-infinite-query.ts +296 -0
- package/src/inject-is-fetching.ts +97 -0
- package/src/inject-is-mutating.ts +80 -0
- package/src/inject-is-restoring.ts +53 -0
- package/src/inject-mutation-state.ts +167 -0
- package/src/inject-mutation.ts +328 -0
- package/src/inject-queries-experimental/index.ts +1 -0
- package/src/inject-queries.ts +480 -0
- package/src/inject-query-client.ts +22 -0
- package/src/inject-query.ts +208 -0
- package/src/mutation-options.ts +121 -0
- package/src/pending-tasks-compat.ts +27 -0
- package/src/providers.ts +170 -0
- package/src/query-options.ts +292 -0
- package/src/signal-proxy.ts +46 -0
- package/src/types.ts +429 -0
- package/create-base-query.d.ts +0 -164
- package/create-base-query.mjs +0 -99
- package/create-base-query.mjs.map +0 -1
- package/devtools/index.d.ts +0 -2
- package/devtools/index.mjs +0 -5
- package/devtools/index.mjs.map +0 -1
- package/devtools/production/index.d.ts +0 -1
- package/devtools/stub.d.ts +0 -2
- package/devtools/stub.mjs +0 -8
- package/devtools/stub.mjs.map +0 -1
- package/devtools/types.d.ts +0 -103
- package/devtools/with-devtools.mjs +0 -106
- package/devtools/with-devtools.mjs.map +0 -1
- package/devtools-panel/index.d.ts +0 -2
- package/devtools-panel/index.mjs +0 -5
- package/devtools-panel/index.mjs.map +0 -1
- package/devtools-panel/inject-devtools-panel.mjs +0 -72
- package/devtools-panel/inject-devtools-panel.mjs.map +0 -1
- package/devtools-panel/production/index.d.ts +0 -1
- package/devtools-panel/stub.d.ts +0 -2
- package/devtools-panel/stub.mjs +0 -8
- package/devtools-panel/stub.mjs.map +0 -1
- package/devtools-panel/types.d.ts +0 -48
- package/index.d.ts +0 -25
- package/index.mjs.map +0 -1
- package/infinite-query-options.mjs +0 -7
- package/infinite-query-options.mjs.map +0 -1
- package/inject-infinite-query.mjs +0 -18
- package/inject-infinite-query.mjs.map +0 -1
- package/inject-is-fetching.mjs +0 -31
- package/inject-is-fetching.mjs.map +0 -1
- package/inject-is-mutating.mjs +0 -31
- package/inject-is-mutating.mjs.map +0 -1
- package/inject-is-restoring.mjs +0 -21
- package/inject-is-restoring.mjs.map +0 -1
- package/inject-mutation-state.mjs +0 -51
- package/inject-mutation-state.mjs.map +0 -1
- package/inject-mutation.mjs +0 -94
- package/inject-mutation.mjs.map +0 -1
- package/inject-queries-experimental/index.d.ts +0 -1
- package/inject-queries-experimental/index.mjs +0 -5
- package/inject-queries-experimental/index.mjs.map +0 -1
- package/inject-queries.mjs +0 -82
- package/inject-queries.mjs.map +0 -1
- package/inject-query-client.mjs +0 -9
- package/inject-query-client.mjs.map +0 -1
- package/inject-query.mjs +0 -14
- package/inject-query.mjs.map +0 -1
- package/mutation-options.mjs +0 -7
- package/mutation-options.mjs.map +0 -1
- package/pending-tasks-compat.d.ts +0 -7
- package/pending-tasks-compat.mjs +0 -19
- package/pending-tasks-compat.mjs.map +0 -1
- package/providers.mjs +0 -32
- package/providers.mjs.map +0 -1
- package/query-options.mjs +0 -7
- package/query-options.mjs.map +0 -1
- package/signal-proxy.mjs +0 -29
- package/signal-proxy.mjs.map +0 -1
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest'
|
|
2
|
+
import { queryKey, sleep } from '@tanstack/query-test-utils'
|
|
3
|
+
import { injectQuery, queryOptions } from '..'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { CreateQueryOptions, OmitKeyof, QueryFunction } from '..'
|
|
6
|
+
|
|
7
|
+
describe('injectQuery', () => {
|
|
8
|
+
it('should return the correct types', () => {
|
|
9
|
+
const key = queryKey()
|
|
10
|
+
// unspecified query function should default to unknown
|
|
11
|
+
const noQueryFn = injectQuery(() => ({
|
|
12
|
+
queryKey: key,
|
|
13
|
+
}))
|
|
14
|
+
expectTypeOf(noQueryFn.data()).toEqualTypeOf<unknown>()
|
|
15
|
+
expectTypeOf(noQueryFn.error()).toEqualTypeOf<Error | null>()
|
|
16
|
+
|
|
17
|
+
// it should infer the result type from the query function
|
|
18
|
+
const fromQueryFn = injectQuery(() => ({
|
|
19
|
+
queryKey: key,
|
|
20
|
+
queryFn: () => 'test',
|
|
21
|
+
}))
|
|
22
|
+
expectTypeOf(fromQueryFn.data()).toEqualTypeOf<string | undefined>()
|
|
23
|
+
expectTypeOf(fromQueryFn.error()).toEqualTypeOf<Error | null>()
|
|
24
|
+
|
|
25
|
+
// it should be possible to specify the result type
|
|
26
|
+
const withResult = injectQuery<string>(() => ({
|
|
27
|
+
queryKey: key,
|
|
28
|
+
queryFn: () => 'test',
|
|
29
|
+
}))
|
|
30
|
+
expectTypeOf(withResult.data()).toEqualTypeOf<string | undefined>()
|
|
31
|
+
expectTypeOf(withResult.error()).toEqualTypeOf<Error | null>()
|
|
32
|
+
|
|
33
|
+
// it should be possible to specify the error type
|
|
34
|
+
type CustomErrorType = { message: string }
|
|
35
|
+
const withError = injectQuery<string, CustomErrorType>(() => ({
|
|
36
|
+
queryKey: key,
|
|
37
|
+
queryFn: () => 'test',
|
|
38
|
+
}))
|
|
39
|
+
expectTypeOf(withError.data()).toEqualTypeOf<string | undefined>()
|
|
40
|
+
expectTypeOf(withError.error()).toEqualTypeOf<CustomErrorType | null>()
|
|
41
|
+
|
|
42
|
+
// it should infer the result type from the configuration
|
|
43
|
+
const withResultInfer = injectQuery(() => ({
|
|
44
|
+
queryKey: key,
|
|
45
|
+
queryFn: () => true,
|
|
46
|
+
}))
|
|
47
|
+
expectTypeOf(withResultInfer.data()).toEqualTypeOf<boolean | undefined>()
|
|
48
|
+
expectTypeOf(withResultInfer.error()).toEqualTypeOf<Error | null>()
|
|
49
|
+
|
|
50
|
+
// it should be possible to specify a union type as result type
|
|
51
|
+
const unionTypeSync = injectQuery(() => ({
|
|
52
|
+
queryKey: key,
|
|
53
|
+
queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)),
|
|
54
|
+
}))
|
|
55
|
+
expectTypeOf(unionTypeSync.data()).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
56
|
+
const unionTypeAsync = injectQuery<'a' | 'b'>(() => ({
|
|
57
|
+
queryKey: key,
|
|
58
|
+
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
59
|
+
}))
|
|
60
|
+
expectTypeOf(unionTypeAsync.data()).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
61
|
+
|
|
62
|
+
// it should error when the query function result does not match with the specified type
|
|
63
|
+
// @ts-expect-error
|
|
64
|
+
injectQuery<number>(() => ({ queryKey: key, queryFn: () => 'test' }))
|
|
65
|
+
|
|
66
|
+
// it should infer the result type from a generic query function
|
|
67
|
+
/**
|
|
68
|
+
*
|
|
69
|
+
*/
|
|
70
|
+
function queryFn<T = string>(): Promise<T> {
|
|
71
|
+
return Promise.resolve({} as T)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const fromGenericQueryFn = injectQuery(() => ({
|
|
75
|
+
queryKey: key,
|
|
76
|
+
queryFn: () => queryFn(),
|
|
77
|
+
}))
|
|
78
|
+
expectTypeOf(fromGenericQueryFn.data()).toEqualTypeOf<string | undefined>()
|
|
79
|
+
expectTypeOf(fromGenericQueryFn.error()).toEqualTypeOf<Error | null>()
|
|
80
|
+
|
|
81
|
+
type MyData = number
|
|
82
|
+
type MyQueryKey = readonly ['my-data', number]
|
|
83
|
+
|
|
84
|
+
const getMyDataArrayKey: QueryFunction<MyData, MyQueryKey> = ({
|
|
85
|
+
queryKey: [, n],
|
|
86
|
+
}) => {
|
|
87
|
+
return n + 42
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fromMyDataArrayKeyQueryFn = injectQuery(() => ({
|
|
91
|
+
queryKey: ['my-data', 100] as const,
|
|
92
|
+
queryFn: getMyDataArrayKey,
|
|
93
|
+
}))
|
|
94
|
+
expectTypeOf(fromMyDataArrayKeyQueryFn.data()).toEqualTypeOf<
|
|
95
|
+
number | undefined
|
|
96
|
+
>()
|
|
97
|
+
|
|
98
|
+
// it should handle query-functions that return Promise<any>
|
|
99
|
+
const fromPromiseAnyQueryFn = injectQuery(() => ({
|
|
100
|
+
queryKey: key,
|
|
101
|
+
queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
|
|
102
|
+
}))
|
|
103
|
+
expectTypeOf(fromPromiseAnyQueryFn.data()).toEqualTypeOf<any | undefined>()
|
|
104
|
+
|
|
105
|
+
const getMyDataStringKey: QueryFunction<MyData, ['1']> = (context) => {
|
|
106
|
+
expectTypeOf(context.queryKey).toEqualTypeOf<['1']>()
|
|
107
|
+
return Number(context.queryKey[0]) + 42
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const fromGetMyDataStringKeyQueryFn = injectQuery(() => ({
|
|
111
|
+
queryKey: ['1'] as ['1'],
|
|
112
|
+
queryFn: getMyDataStringKey,
|
|
113
|
+
}))
|
|
114
|
+
expectTypeOf(fromGetMyDataStringKeyQueryFn.data()).toEqualTypeOf<
|
|
115
|
+
number | undefined
|
|
116
|
+
>()
|
|
117
|
+
|
|
118
|
+
// handles wrapped queries with custom fetcher passed as inline queryFn
|
|
119
|
+
const createWrappedQuery = <
|
|
120
|
+
TQueryKey extends [string, Record<string, unknown>?],
|
|
121
|
+
TQueryFnData,
|
|
122
|
+
TError,
|
|
123
|
+
TData = TQueryFnData,
|
|
124
|
+
>(
|
|
125
|
+
qk: TQueryKey,
|
|
126
|
+
fetcher: (
|
|
127
|
+
obj: TQueryKey[1],
|
|
128
|
+
token: string,
|
|
129
|
+
// return type must be wrapped with TQueryFnReturn
|
|
130
|
+
) => Promise<TQueryFnData>,
|
|
131
|
+
options?: OmitKeyof<
|
|
132
|
+
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
133
|
+
'queryKey' | 'queryFn' | 'initialData',
|
|
134
|
+
'safely'
|
|
135
|
+
>,
|
|
136
|
+
) =>
|
|
137
|
+
injectQuery(() => ({
|
|
138
|
+
queryKey: qk,
|
|
139
|
+
queryFn: () => fetcher(qk[1], 'token'),
|
|
140
|
+
...options,
|
|
141
|
+
}))
|
|
142
|
+
const fromWrappedQuery = createWrappedQuery([''], () =>
|
|
143
|
+
Promise.resolve('1'),
|
|
144
|
+
)
|
|
145
|
+
expectTypeOf(fromWrappedQuery.data()).toEqualTypeOf<string | undefined>()
|
|
146
|
+
|
|
147
|
+
// handles wrapped queries with custom fetcher passed directly to createQuery
|
|
148
|
+
const createWrappedFuncStyleQuery = <
|
|
149
|
+
TQueryKey extends [string, Record<string, unknown>?],
|
|
150
|
+
TQueryFnData,
|
|
151
|
+
TError,
|
|
152
|
+
TData = TQueryFnData,
|
|
153
|
+
>(
|
|
154
|
+
qk: TQueryKey,
|
|
155
|
+
fetcher: () => Promise<TQueryFnData>,
|
|
156
|
+
options?: OmitKeyof<
|
|
157
|
+
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
158
|
+
'queryKey' | 'queryFn' | 'initialData',
|
|
159
|
+
'safely'
|
|
160
|
+
>,
|
|
161
|
+
) => injectQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options }))
|
|
162
|
+
const fromWrappedFuncStyleQuery = createWrappedFuncStyleQuery([''], () =>
|
|
163
|
+
Promise.resolve(true),
|
|
164
|
+
)
|
|
165
|
+
expectTypeOf(fromWrappedFuncStyleQuery.data()).toEqualTypeOf<
|
|
166
|
+
boolean | undefined
|
|
167
|
+
>()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
describe('initialData', () => {
|
|
171
|
+
describe('Config object overload', () => {
|
|
172
|
+
it('TData should always be defined when initialData is provided as an object', () => {
|
|
173
|
+
const key = queryKey()
|
|
174
|
+
const { data } = injectQuery(() => ({
|
|
175
|
+
queryKey: key,
|
|
176
|
+
queryFn: () => ({ wow: true }),
|
|
177
|
+
initialData: { wow: true },
|
|
178
|
+
}))
|
|
179
|
+
|
|
180
|
+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('TData should be defined when passed through queryOptions', () => {
|
|
184
|
+
const key = queryKey()
|
|
185
|
+
const options = () =>
|
|
186
|
+
queryOptions({
|
|
187
|
+
queryKey: key,
|
|
188
|
+
queryFn: () => {
|
|
189
|
+
return {
|
|
190
|
+
wow: true,
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
initialData: {
|
|
194
|
+
wow: true,
|
|
195
|
+
},
|
|
196
|
+
})
|
|
197
|
+
const { data } = injectQuery(options)
|
|
198
|
+
|
|
199
|
+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
|
|
203
|
+
const key = queryKey()
|
|
204
|
+
const options = queryOptions({
|
|
205
|
+
queryKey: key,
|
|
206
|
+
queryFn: () => Promise.resolve(1),
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
const query = injectQuery(() => ({
|
|
210
|
+
...options,
|
|
211
|
+
select: (data) => data > 1,
|
|
212
|
+
}))
|
|
213
|
+
|
|
214
|
+
expectTypeOf(query.data).toEqualTypeOf<Signal<boolean | undefined>>()
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
|
|
218
|
+
const key = queryKey()
|
|
219
|
+
const { data } = injectQuery(() => ({
|
|
220
|
+
queryKey: key,
|
|
221
|
+
queryFn: () => {
|
|
222
|
+
return {
|
|
223
|
+
wow: true,
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
initialData: () => ({
|
|
227
|
+
wow: true,
|
|
228
|
+
}),
|
|
229
|
+
}))
|
|
230
|
+
|
|
231
|
+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('TData should have undefined in the union when initialData is NOT provided', () => {
|
|
235
|
+
const key = queryKey()
|
|
236
|
+
const { data } = injectQuery(() => ({
|
|
237
|
+
queryKey: key,
|
|
238
|
+
queryFn: () => {
|
|
239
|
+
return {
|
|
240
|
+
wow: true,
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
}))
|
|
244
|
+
|
|
245
|
+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
|
|
249
|
+
const key = queryKey()
|
|
250
|
+
const { data } = injectQuery(() => ({
|
|
251
|
+
queryKey: key,
|
|
252
|
+
queryFn: () => {
|
|
253
|
+
return {
|
|
254
|
+
wow: true,
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
initialData: () => undefined as { wow: boolean } | undefined,
|
|
258
|
+
}))
|
|
259
|
+
|
|
260
|
+
expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean } | undefined>>()
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
|
|
264
|
+
const key = queryKey()
|
|
265
|
+
const query = injectQuery(() => ({
|
|
266
|
+
queryKey: key,
|
|
267
|
+
queryFn: () => {
|
|
268
|
+
return {
|
|
269
|
+
wow: true,
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
initialData: () => undefined as { wow: boolean } | undefined,
|
|
273
|
+
}))
|
|
274
|
+
|
|
275
|
+
if (query.isSuccess()) {
|
|
276
|
+
expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
describe('structuralSharing', () => {
|
|
282
|
+
it('should be able to use structuralSharing with unknown types', () => {
|
|
283
|
+
const key = queryKey()
|
|
284
|
+
// https://github.com/TanStack/query/issues/6525#issuecomment-1938411343
|
|
285
|
+
injectQuery(() => ({
|
|
286
|
+
queryKey: key,
|
|
287
|
+
queryFn: () => 5,
|
|
288
|
+
structuralSharing: (oldData, newData) => {
|
|
289
|
+
expectTypeOf(oldData).toBeUnknown()
|
|
290
|
+
expectTypeOf(newData).toBeUnknown()
|
|
291
|
+
return newData
|
|
292
|
+
},
|
|
293
|
+
}))
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
describe('Discriminated union return type', () => {
|
|
299
|
+
it('data should be possibly undefined by default', () => {
|
|
300
|
+
const key = queryKey()
|
|
301
|
+
const query = injectQuery(() => ({
|
|
302
|
+
queryKey: key,
|
|
303
|
+
queryFn: () => sleep(0).then(() => 'Some data'),
|
|
304
|
+
}))
|
|
305
|
+
|
|
306
|
+
expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('data should be defined when query is success', () => {
|
|
310
|
+
const key = queryKey()
|
|
311
|
+
const query = injectQuery(() => ({
|
|
312
|
+
queryKey: key,
|
|
313
|
+
queryFn: () => sleep(0).then(() => 'Some data'),
|
|
314
|
+
}))
|
|
315
|
+
|
|
316
|
+
if (query.isSuccess()) {
|
|
317
|
+
expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
it('error should be null when query is success', () => {
|
|
322
|
+
const key = queryKey()
|
|
323
|
+
const query = injectQuery(() => ({
|
|
324
|
+
queryKey: key,
|
|
325
|
+
queryFn: () => sleep(0).then(() => 'Some data'),
|
|
326
|
+
}))
|
|
327
|
+
|
|
328
|
+
if (query.isSuccess()) {
|
|
329
|
+
expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
|
|
330
|
+
}
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
it('data should be undefined when query is pending', () => {
|
|
334
|
+
const key = queryKey()
|
|
335
|
+
const query = injectQuery(() => ({
|
|
336
|
+
queryKey: key,
|
|
337
|
+
queryFn: () => sleep(0).then(() => 'Some data'),
|
|
338
|
+
}))
|
|
339
|
+
|
|
340
|
+
if (query.isPending()) {
|
|
341
|
+
expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
it('error should be defined when query is error', () => {
|
|
346
|
+
const key = queryKey()
|
|
347
|
+
const query = injectQuery(() => ({
|
|
348
|
+
queryKey: key,
|
|
349
|
+
queryFn: () => sleep(0).then(() => 'Some data'),
|
|
350
|
+
}))
|
|
351
|
+
|
|
352
|
+
if (query.isError()) {
|
|
353
|
+
expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
})
|
|
357
|
+
})
|