@tanstack/react-query 5.24.1 → 5.24.5

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.
Files changed (96) hide show
  1. package/build/codemods/coverage/clover.xml +2 -2
  2. package/build/codemods/coverage/index.html +1 -1
  3. package/build/codemods/coverage/utils/index.html +1 -1
  4. package/build/codemods/coverage/utils/index.js.html +1 -1
  5. package/build/codemods/coverage/utils/transformers/index.html +1 -1
  6. package/build/codemods/coverage/utils/transformers/query-cache-transformer.js.html +1 -1
  7. package/build/codemods/coverage/utils/transformers/query-client-transformer.js.html +1 -1
  8. package/build/codemods/coverage/utils/transformers/use-query-like-transformer.js.html +1 -1
  9. package/build/codemods/coverage/v4/index.html +1 -1
  10. package/build/codemods/coverage/v4/key-transformation.js.html +1 -1
  11. package/build/codemods/coverage/v4/replace-import-specifier.js.html +1 -1
  12. package/build/codemods/coverage/v4/utils/replacers/index.html +1 -1
  13. package/build/codemods/coverage/v4/utils/replacers/key-replacer.js.html +1 -1
  14. package/build/codemods/coverage/v5/is-loading/index.html +1 -1
  15. package/build/codemods/coverage/v5/is-loading/is-loading.js.html +1 -1
  16. package/build/codemods/coverage/v5/keep-previous-data/index.html +1 -1
  17. package/build/codemods/coverage/v5/keep-previous-data/keep-previous-data.js.html +1 -1
  18. package/build/codemods/coverage/v5/keep-previous-data/utils/already-has-placeholder-data-property.js.html +1 -1
  19. package/build/codemods/coverage/v5/keep-previous-data/utils/index.html +1 -1
  20. package/build/codemods/coverage/v5/remove-overloads/index.html +1 -1
  21. package/build/codemods/coverage/v5/remove-overloads/remove-overloads.js.html +1 -1
  22. package/build/codemods/coverage/v5/remove-overloads/transformers/filter-aware-usage-transformer.js.html +1 -1
  23. package/build/codemods/coverage/v5/remove-overloads/transformers/index.html +1 -1
  24. package/build/codemods/coverage/v5/remove-overloads/transformers/query-fn-aware-usage-transformer.js.html +1 -1
  25. package/build/codemods/coverage/v5/remove-overloads/utils/index.html +1 -1
  26. package/build/codemods/coverage/v5/remove-overloads/utils/index.js.html +1 -1
  27. package/build/codemods/coverage/v5/remove-overloads/utils/unknown-usage-error.js.html +1 -1
  28. package/build/codemods/coverage/v5/rename-hydrate/index.html +1 -1
  29. package/build/codemods/coverage/v5/rename-hydrate/rename-hydrate.js.html +1 -1
  30. package/build/codemods/coverage/v5/rename-properties/index.html +1 -1
  31. package/build/codemods/coverage/v5/rename-properties/rename-properties.js.html +1 -1
  32. package/build/legacy/suspense.cjs +1 -1
  33. package/build/legacy/suspense.cjs.map +1 -1
  34. package/build/legacy/suspense.js +1 -1
  35. package/build/legacy/suspense.js.map +1 -1
  36. package/build/legacy/useMutation.cjs +1 -3
  37. package/build/legacy/useMutation.cjs.map +1 -1
  38. package/build/legacy/useMutation.js +1 -3
  39. package/build/legacy/useMutation.js.map +1 -1
  40. package/build/legacy/useSuspenseQueries.cjs +2 -1
  41. package/build/legacy/useSuspenseQueries.cjs.map +1 -1
  42. package/build/legacy/useSuspenseQueries.js +2 -1
  43. package/build/legacy/useSuspenseQueries.js.map +1 -1
  44. package/build/legacy/useSuspenseQuery.cjs +2 -1
  45. package/build/legacy/useSuspenseQuery.cjs.map +1 -1
  46. package/build/legacy/useSuspenseQuery.js +2 -1
  47. package/build/legacy/useSuspenseQuery.js.map +1 -1
  48. package/build/legacy/utils.cjs +4 -0
  49. package/build/legacy/utils.cjs.map +1 -1
  50. package/build/legacy/utils.d.cts +2 -1
  51. package/build/legacy/utils.d.ts +2 -1
  52. package/build/legacy/utils.js +3 -0
  53. package/build/legacy/utils.js.map +1 -1
  54. package/build/modern/suspense.cjs +1 -1
  55. package/build/modern/suspense.cjs.map +1 -1
  56. package/build/modern/suspense.js +1 -1
  57. package/build/modern/suspense.js.map +1 -1
  58. package/build/modern/useMutation.cjs +1 -3
  59. package/build/modern/useMutation.cjs.map +1 -1
  60. package/build/modern/useMutation.js +1 -3
  61. package/build/modern/useMutation.js.map +1 -1
  62. package/build/modern/useSuspenseQueries.cjs +2 -1
  63. package/build/modern/useSuspenseQueries.cjs.map +1 -1
  64. package/build/modern/useSuspenseQueries.js +2 -1
  65. package/build/modern/useSuspenseQueries.js.map +1 -1
  66. package/build/modern/useSuspenseQuery.cjs +2 -1
  67. package/build/modern/useSuspenseQuery.cjs.map +1 -1
  68. package/build/modern/useSuspenseQuery.js +2 -1
  69. package/build/modern/useSuspenseQuery.js.map +1 -1
  70. package/build/modern/utils.cjs +4 -0
  71. package/build/modern/utils.cjs.map +1 -1
  72. package/build/modern/utils.d.cts +2 -1
  73. package/build/modern/utils.d.ts +2 -1
  74. package/build/modern/utils.js +3 -0
  75. package/build/modern/utils.js.map +1 -1
  76. package/package.json +2 -2
  77. package/src/__tests__/infiniteQueryOptions.test-d.tsx +142 -0
  78. package/src/__tests__/queryOptions.test-d.tsx +150 -0
  79. package/src/__tests__/suspense.test-d.tsx +124 -0
  80. package/src/__tests__/suspense.test.tsx +102 -0
  81. package/src/__tests__/useInfiniteQuery.test-d.tsx +140 -0
  82. package/src/__tests__/useMutation.test.tsx +3 -1
  83. package/src/__tests__/useQueries.test-d.tsx +126 -0
  84. package/src/__tests__/useQuery.test-d.tsx +140 -0
  85. package/src/__tests__/utils.tsx +0 -8
  86. package/src/suspense.ts +1 -1
  87. package/src/useMutation.ts +1 -3
  88. package/src/useSuspenseQueries.ts +1 -0
  89. package/src/useSuspenseQuery.ts +1 -0
  90. package/src/utils.ts +2 -0
  91. package/src/__tests__/infiniteQueryOptions.types.test.tsx +0 -177
  92. package/src/__tests__/queryOptions.types.test.tsx +0 -192
  93. package/src/__tests__/suspense.types.test.tsx +0 -147
  94. package/src/__tests__/useInfiniteQuery.type.test.tsx +0 -224
  95. package/src/__tests__/useQueries.types.test.tsx +0 -148
  96. package/src/__tests__/useQuery.types.test.tsx +0 -179
@@ -1,147 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { useSuspenseQuery } from '../useSuspenseQuery'
3
- import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
4
- import { doNotExecute } from './utils'
5
- import type { InfiniteData } from '@tanstack/query-core'
6
- import type { Equal, Expect } from './utils'
7
-
8
- describe('useSuspenseQuery', () => {
9
- it('should always have data defined', () => {
10
- doNotExecute(() => {
11
- const { data } = useSuspenseQuery({
12
- queryKey: ['key'],
13
- queryFn: () => Promise.resolve(5),
14
- })
15
-
16
- const result: Expect<Equal<typeof data, number>> = true
17
- return result
18
- })
19
- })
20
-
21
- it('should not have pending status', () => {
22
- doNotExecute(() => {
23
- const { status } = useSuspenseQuery({
24
- queryKey: ['key'],
25
- queryFn: () => Promise.resolve(5),
26
- })
27
-
28
- const result: Expect<Equal<typeof status, 'error' | 'success'>> = true
29
- return result
30
- })
31
- })
32
-
33
- it('should not allow placeholderData, enabled or throwOnError props', () => {
34
- doNotExecute(() => {
35
- useSuspenseQuery({
36
- queryKey: ['key'],
37
- queryFn: () => Promise.resolve(5),
38
- // @ts-expect-error TS2345
39
- placeholderData: 5,
40
- enabled: true,
41
- })
42
-
43
- useSuspenseQuery({
44
- queryKey: ['key'],
45
- queryFn: () => Promise.resolve(5),
46
- // @ts-expect-error TS2345
47
- enabled: true,
48
- })
49
-
50
- useSuspenseQuery({
51
- queryKey: ['key'],
52
- queryFn: () => Promise.resolve(5),
53
- // @ts-expect-error TS2345
54
- throwOnError: true,
55
- })
56
- })
57
- })
58
-
59
- it('should not return isPlaceholderData', () => {
60
- doNotExecute(() => {
61
- const query = useSuspenseQuery({
62
- queryKey: ['key'],
63
- queryFn: () => Promise.resolve(5),
64
- })
65
-
66
- // @ts-expect-error TS2339
67
- void query.isPlaceholderData
68
- })
69
- })
70
- })
71
-
72
- describe('useSuspenseInfiniteQuery', () => {
73
- it('should always have data defined', () => {
74
- doNotExecute(() => {
75
- const { data } = useSuspenseInfiniteQuery({
76
- queryKey: ['key'],
77
- queryFn: () => Promise.resolve(5),
78
- initialPageParam: 1,
79
- getNextPageParam: () => 1,
80
- })
81
-
82
- const result: Expect<Equal<typeof data, InfiniteData<number, unknown>>> =
83
- true
84
- return result
85
- })
86
- })
87
-
88
- it('should not have pending status', () => {
89
- doNotExecute(() => {
90
- const { status } = useSuspenseInfiniteQuery({
91
- queryKey: ['key'],
92
- queryFn: () => Promise.resolve(5),
93
- initialPageParam: 1,
94
- getNextPageParam: () => 1,
95
- })
96
-
97
- const result: Expect<Equal<typeof status, 'error' | 'success'>> = true
98
- return result
99
- })
100
- })
101
-
102
- it('should not allow placeholderData, enabled or throwOnError props', () => {
103
- doNotExecute(() => {
104
- useSuspenseInfiniteQuery({
105
- queryKey: ['key'],
106
- queryFn: () => Promise.resolve(5),
107
- initialPageParam: 1,
108
- getNextPageParam: () => 1,
109
- // @ts-expect-error TS2345
110
- placeholderData: 5,
111
- enabled: true,
112
- })
113
-
114
- useSuspenseInfiniteQuery({
115
- queryKey: ['key'],
116
- queryFn: () => Promise.resolve(5),
117
- initialPageParam: 1,
118
- getNextPageParam: () => 1,
119
- // @ts-expect-error TS2345
120
- enabled: true,
121
- })
122
-
123
- useSuspenseInfiniteQuery({
124
- queryKey: ['key'],
125
- queryFn: () => Promise.resolve(5),
126
- initialPageParam: 1,
127
- getNextPageParam: () => 1,
128
- // @ts-expect-error TS2345
129
- throwOnError: true,
130
- })
131
- })
132
- })
133
-
134
- it('should not return isPlaceholderData', () => {
135
- doNotExecute(() => {
136
- const query = useSuspenseInfiniteQuery({
137
- queryKey: ['key'],
138
- queryFn: () => Promise.resolve(5),
139
- initialPageParam: 1,
140
- getNextPageParam: () => 1,
141
- })
142
-
143
- // @ts-expect-error TS2339
144
- void query.isPlaceholderData
145
- })
146
- })
147
- })
@@ -1,224 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { QueryClient } from '@tanstack/query-core'
3
- import { useInfiniteQuery } from '../useInfiniteQuery'
4
- import { useQuery } from '../useQuery'
5
- import { doNotExecute } from './utils'
6
- import type { Equal, Expect } from './utils'
7
- import type { InfiniteData } from '@tanstack/query-core'
8
-
9
- describe('pageParam', () => {
10
- it('initialPageParam should define type of param passed to queryFunctionContext', () => {
11
- doNotExecute(() => {
12
- useInfiniteQuery({
13
- queryKey: ['key'],
14
- queryFn: ({ pageParam }) => {
15
- const result: Expect<Equal<number, typeof pageParam>> = true
16
- return result
17
- },
18
- initialPageParam: 1,
19
- getNextPageParam: () => undefined,
20
- })
21
- })
22
- })
23
-
24
- it('direction should be passed to queryFn of useInfiniteQuery', () => {
25
- doNotExecute(() => {
26
- useInfiniteQuery({
27
- queryKey: ['key'],
28
- queryFn: ({ direction }) => {
29
- const result: Expect<
30
- Equal<'forward' | 'backward', typeof direction>
31
- > = true
32
- return result
33
- },
34
- initialPageParam: 1,
35
- getNextPageParam: () => undefined,
36
- })
37
- })
38
- })
39
-
40
- it('there should be no pageParam passed to the queryFn of useQuery', () => {
41
- doNotExecute(() => {
42
- useQuery({
43
- queryKey: ['key'],
44
- // @ts-expect-error there should be no pageParam passed to queryFn of useQuery
45
- queryFn: ({ pageParam }) => {
46
- return String(pageParam)
47
- },
48
- })
49
- })
50
- })
51
-
52
- it('there should be no direction passed to the queryFn of useQuery', () => {
53
- doNotExecute(() => {
54
- useQuery({
55
- queryKey: ['key'],
56
- // @ts-expect-error there should be no pageParam passed to queryFn of useQuery
57
- queryFn: ({ direction }) => {
58
- return String(direction)
59
- },
60
- })
61
- })
62
- })
63
-
64
- it('initialPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
65
- doNotExecute(() => {
66
- const queryClient = new QueryClient()
67
- queryClient.fetchInfiniteQuery({
68
- queryKey: ['key'],
69
- queryFn: ({ pageParam }) => {
70
- const result: Expect<Equal<number, typeof pageParam>> = true
71
- return result
72
- },
73
- initialPageParam: 1,
74
- })
75
- })
76
- })
77
-
78
- it('initialPageParam should define type of param passed to queryFunctionContext for prefetchInfiniteQuery', () => {
79
- doNotExecute(() => {
80
- const queryClient = new QueryClient()
81
- queryClient.prefetchInfiniteQuery({
82
- queryKey: ['key'],
83
- queryFn: ({ pageParam }) => {
84
- const result: Expect<Equal<number, typeof pageParam>> = true
85
- return result
86
- },
87
- initialPageParam: 1,
88
- })
89
- })
90
- })
91
- })
92
- describe('select', () => {
93
- it('should still return paginated data if no select result', () => {
94
- doNotExecute(() => {
95
- const infiniteQuery = useInfiniteQuery({
96
- queryKey: ['key'],
97
- queryFn: ({ pageParam }) => {
98
- return pageParam * 5
99
- },
100
- initialPageParam: 1,
101
- getNextPageParam: () => undefined,
102
- })
103
-
104
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
105
- const result: Expect<
106
- Equal<
107
- InfiniteData<number, unknown> | undefined,
108
- (typeof infiniteQuery)['data']
109
- >
110
- > = true
111
-
112
- return result
113
- })
114
- })
115
-
116
- it('should be able to transform data to arbitrary result', () => {
117
- doNotExecute(() => {
118
- const infiniteQuery = useInfiniteQuery({
119
- queryKey: ['key'],
120
- queryFn: ({ pageParam }) => {
121
- return pageParam * 5
122
- },
123
- initialPageParam: 1,
124
- getNextPageParam: () => undefined,
125
- select: (data) => {
126
- const result: Expect<
127
- Equal<InfiniteData<number, number>, typeof data>
128
- > = true
129
- return result
130
- },
131
- })
132
-
133
- const result: Expect<
134
- Equal<true | undefined, (typeof infiniteQuery)['data']>
135
- > = true
136
- return result
137
- })
138
- })
139
- })
140
- describe('getNextPageParam / getPreviousPageParam', () => {
141
- it('should get typed params', () => {
142
- doNotExecute(() => {
143
- const infiniteQuery = useInfiniteQuery({
144
- queryKey: ['key'],
145
- queryFn: ({ pageParam }) => {
146
- return String(pageParam)
147
- },
148
- initialPageParam: 1,
149
- getNextPageParam: (
150
- lastPage,
151
- allPages,
152
- lastPageParam,
153
- allPageParams,
154
- ) => {
155
- doNotExecute(() => {
156
- const lastPageResult: Expect<Equal<string, typeof lastPage>> = true
157
- return lastPageResult
158
- })
159
- doNotExecute(() => {
160
- const allPagesResult: Expect<
161
- Equal<Array<string>, typeof allPages>
162
- > = true
163
- return allPagesResult
164
- })
165
- doNotExecute(() => {
166
- const lastPageParamResult: Expect<
167
- Equal<number, typeof lastPageParam>
168
- > = true
169
- return lastPageParamResult
170
- })
171
- doNotExecute(() => {
172
- const allPageParamsResult: Expect<
173
- Equal<Array<number>, typeof allPageParams>
174
- > = true
175
- return allPageParamsResult
176
- })
177
-
178
- return undefined
179
- },
180
- getPreviousPageParam: (
181
- firstPage,
182
- allPages,
183
- firstPageParam,
184
- allPageParams,
185
- ) => {
186
- doNotExecute(() => {
187
- const firstPageResult: Expect<Equal<string, typeof firstPage>> =
188
- true
189
- return firstPageResult
190
- })
191
- doNotExecute(() => {
192
- const allPagesResult: Expect<
193
- Equal<Array<string>, typeof allPages>
194
- > = true
195
- return allPagesResult
196
- })
197
- doNotExecute(() => {
198
- const firstPageParamResult: Expect<
199
- Equal<number, typeof firstPageParam>
200
- > = true
201
- return firstPageParamResult
202
- })
203
- doNotExecute(() => {
204
- const allPageParamsResult: Expect<
205
- Equal<Array<number>, typeof allPageParams>
206
- > = true
207
- return allPageParamsResult
208
- })
209
-
210
- return undefined
211
- },
212
- })
213
-
214
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
215
- const result: Expect<
216
- Equal<
217
- InfiniteData<string, unknown> | undefined,
218
- (typeof infiniteQuery)['data']
219
- >
220
- > = true
221
- return result
222
- })
223
- })
224
- })
@@ -1,148 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { queryOptions, useQueries } from '..'
3
- import { doNotExecute } from './utils'
4
- import type { UseQueryOptions } from '..'
5
- import type { Equal, Expect } from './utils'
6
-
7
- describe('UseQueries config object overload', () => {
8
- it('TData should always be defined when initialData is provided as an object', () => {
9
- const query1 = {
10
- queryKey: ['key1'],
11
- queryFn: () => {
12
- return {
13
- wow: true,
14
- }
15
- },
16
- initialData: {
17
- wow: false,
18
- },
19
- }
20
-
21
- const query2 = {
22
- queryKey: ['key2'],
23
- queryFn: () => 'Query Data',
24
- initialData: 'initial data',
25
- }
26
-
27
- const query3 = {
28
- queryKey: ['key2'],
29
- queryFn: () => 'Query Data',
30
- }
31
-
32
- doNotExecute(() => {
33
- const queryResults = useQueries({ queries: [query1, query2, query3] })
34
-
35
- const query1Data = queryResults[0].data
36
- const query2Data = queryResults[1].data
37
- const query3Data = queryResults[2].data
38
-
39
- const result1: Expect<Equal<{ wow: boolean }, typeof query1Data>> = true
40
-
41
- const result2: Expect<Equal<string, typeof query2Data>> = true
42
-
43
- const result3: Expect<Equal<string | undefined, typeof query3Data>> = true
44
-
45
- return result1 && result2 && result3
46
- })
47
- })
48
-
49
- it('TData should be defined when passed through queryOptions', () => {
50
- doNotExecute(() => {
51
- const options = queryOptions({
52
- queryKey: ['key'],
53
- queryFn: () => {
54
- return {
55
- wow: true,
56
- }
57
- },
58
- initialData: {
59
- wow: true,
60
- },
61
- })
62
- const queryResults = useQueries({ queries: [options] })
63
-
64
- const data = queryResults[0].data
65
-
66
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
67
- return result
68
- })
69
- })
70
-
71
- it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
72
- doNotExecute(() => {
73
- const query1 = queryOptions({
74
- queryKey: ['key'],
75
- queryFn: () => Promise.resolve(1),
76
- select: (data) => data > 1,
77
- })
78
-
79
- const query2 = {
80
- queryKey: ['key'],
81
- queryFn: () => Promise.resolve(1),
82
- select: (data: number) => data > 1,
83
- }
84
-
85
- const queryResults = useQueries({ queries: [query1, query2] })
86
- const query1Data = queryResults[0].data
87
- const query2Data = queryResults[1].data
88
-
89
- const result1: Expect<Equal<boolean | undefined, typeof query1Data>> =
90
- true
91
- const result2: Expect<Equal<boolean | undefined, typeof query2Data>> =
92
- true
93
- return result1 && result2
94
- })
95
- })
96
-
97
- it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
98
- doNotExecute(() => {
99
- const queryResults = useQueries({
100
- queries: [
101
- {
102
- queryKey: ['key'],
103
- queryFn: () => {
104
- return {
105
- wow: true,
106
- }
107
- },
108
- initialData: () => undefined as { wow: boolean } | undefined,
109
- },
110
- ],
111
- })
112
-
113
- const data = queryResults[0].data
114
-
115
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
116
- true
117
- return result
118
- })
119
- })
120
-
121
- describe('custom hook', () => {
122
- it('should allow custom hooks using UseQueryOptions', () => {
123
- doNotExecute(() => {
124
- type Data = string
125
-
126
- const useCustomQueries = (
127
- options?: Omit<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
128
- ) => {
129
- return useQueries({
130
- queries: [
131
- {
132
- ...options,
133
- queryKey: ['todos-key'],
134
- queryFn: () => Promise.resolve('data'),
135
- },
136
- ],
137
- })
138
- }
139
-
140
- const queryResults = useCustomQueries()
141
- const data = queryResults[0].data
142
-
143
- const result: Expect<Equal<Data | undefined, typeof data>> = true
144
- return result
145
- })
146
- })
147
- })
148
- })
@@ -1,179 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { useQuery } from '../useQuery'
3
- import { queryOptions } from '../queryOptions'
4
- import { doNotExecute } from './utils'
5
- import type { UseQueryOptions } from '../types'
6
- import type { Equal, Expect } from './utils'
7
-
8
- describe('initialData', () => {
9
- describe('Config object overload', () => {
10
- it('TData should always be defined when initialData is provided as an object', () => {
11
- doNotExecute(() => {
12
- const { data } = useQuery({
13
- queryKey: ['key'],
14
- queryFn: () => {
15
- return {
16
- wow: true,
17
- }
18
- },
19
- initialData: {
20
- wow: true,
21
- },
22
- })
23
-
24
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
25
- return result
26
- })
27
- })
28
-
29
- it('TData should be defined when passed through queryOptions', () => {
30
- doNotExecute(() => {
31
- const options = queryOptions({
32
- queryKey: ['key'],
33
- queryFn: () => {
34
- return {
35
- wow: true,
36
- }
37
- },
38
- initialData: {
39
- wow: true,
40
- },
41
- })
42
- const { data } = useQuery(options)
43
-
44
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
45
- return result
46
- })
47
- })
48
-
49
- it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
50
- doNotExecute(() => {
51
- const options = queryOptions({
52
- queryKey: ['key'],
53
- queryFn: () => Promise.resolve(1),
54
- })
55
-
56
- const query = useQuery({
57
- ...options,
58
- select: (data) => data > 1,
59
- })
60
-
61
- const result: Expect<
62
- Equal<boolean | undefined, (typeof query)['data']>
63
- > = true
64
- return result
65
- })
66
- })
67
-
68
- it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
69
- doNotExecute(() => {
70
- const { data } = useQuery({
71
- queryKey: ['key'],
72
- queryFn: () => {
73
- return {
74
- wow: true,
75
- }
76
- },
77
- initialData: () => ({
78
- wow: true,
79
- }),
80
- })
81
-
82
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
83
- return result
84
- })
85
- })
86
-
87
- it('TData should have undefined in the union when initialData is NOT provided', () => {
88
- doNotExecute(() => {
89
- const { data } = useQuery({
90
- queryKey: ['key'],
91
- queryFn: () => {
92
- return {
93
- wow: true,
94
- }
95
- },
96
- })
97
-
98
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
99
- true
100
- return result
101
- })
102
- })
103
-
104
- it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
105
- doNotExecute(() => {
106
- const { data } = useQuery({
107
- queryKey: ['key'],
108
- queryFn: () => {
109
- return {
110
- wow: true,
111
- }
112
- },
113
- initialData: () => undefined as { wow: boolean } | undefined,
114
- })
115
-
116
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
117
- true
118
- return result
119
- })
120
- })
121
-
122
- it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
123
- doNotExecute(() => {
124
- const { data, isSuccess } = useQuery({
125
- queryKey: ['key'],
126
- queryFn: () => {
127
- return {
128
- wow: true,
129
- }
130
- },
131
- initialData: () => undefined as { wow: boolean } | undefined,
132
- })
133
-
134
- if (isSuccess) {
135
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
136
- return result
137
- }
138
- return false
139
- })
140
- })
141
- })
142
-
143
- describe('custom hook', () => {
144
- it('should allow custom hooks using UseQueryOptions', () => {
145
- doNotExecute(() => {
146
- type Data = string
147
-
148
- const useCustomQuery = (
149
- options?: Omit<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
150
- ) => {
151
- return useQuery({
152
- ...options,
153
- queryKey: ['todos-key'],
154
- queryFn: () => Promise.resolve('data'),
155
- })
156
- }
157
-
158
- const { data } = useCustomQuery()
159
-
160
- const result: Expect<Equal<Data | undefined, typeof data>> = true
161
- return result
162
- })
163
- })
164
- })
165
-
166
- describe('structuralSharing', () => {
167
- it('should restrict to same types', () => {
168
- doNotExecute(() => {
169
- useQuery({
170
- queryKey: ['key'],
171
- queryFn: () => 5,
172
- structuralSharing: (_oldData, newData) => {
173
- return newData
174
- },
175
- })
176
- })
177
- })
178
- })
179
- })