@tanstack/solid-query 5.25.0 → 5.26.3
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-query",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.26.3",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Solid",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
],
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"solid-js": "^1.8.14",
|
|
51
|
-
"@tanstack/query-core": "5.
|
|
51
|
+
"@tanstack/query-core": "5.26.3"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"tsup-preset-solid": "^2.2.0",
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
|
|
2
2
|
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
|
|
3
3
|
import * as QueryCore from '@tanstack/query-core'
|
|
4
|
-
|
|
5
4
|
import { createRenderEffect, createSignal } from 'solid-js'
|
|
6
5
|
import {
|
|
7
6
|
QueriesObserver,
|
|
@@ -9,18 +8,9 @@ import {
|
|
|
9
8
|
QueryClientProvider,
|
|
10
9
|
createQueries,
|
|
11
10
|
} from '..'
|
|
12
|
-
import { createQueryClient,
|
|
13
|
-
import type {
|
|
14
|
-
|
|
15
|
-
QueryKey,
|
|
16
|
-
SkipToken,
|
|
17
|
-
} from '@tanstack/query-core'
|
|
18
|
-
import type {
|
|
19
|
-
CreateQueryResult,
|
|
20
|
-
QueryFunction,
|
|
21
|
-
QueryObserverResult,
|
|
22
|
-
SolidQueryOptions,
|
|
23
|
-
} from '..'
|
|
11
|
+
import { createQueryClient, queryKey, sleep } from './utils'
|
|
12
|
+
import type { QueryFunctionContext, QueryKey } from '@tanstack/query-core'
|
|
13
|
+
import type { CreateQueryResult, QueryFunction, SolidQueryOptions } from '..'
|
|
24
14
|
|
|
25
15
|
describe('useQueries', () => {
|
|
26
16
|
const queryCache = new QueryCache()
|
|
@@ -104,13 +94,15 @@ describe('useQueries', () => {
|
|
|
104
94
|
},
|
|
105
95
|
],
|
|
106
96
|
}))
|
|
107
|
-
expectTypeOf
|
|
108
|
-
expectTypeOf
|
|
109
|
-
expectTypeOf
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
expectTypeOf<
|
|
113
|
-
expectTypeOf<
|
|
97
|
+
expectTypeOf(result1[0]).toEqualTypeOf<CreateQueryResult<number>>()
|
|
98
|
+
expectTypeOf(result1[1]).toEqualTypeOf<CreateQueryResult<string>>()
|
|
99
|
+
expectTypeOf(result1[2]).toEqualTypeOf<
|
|
100
|
+
CreateQueryResult<Array<string>, boolean>
|
|
101
|
+
>()
|
|
102
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
103
|
+
expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
|
|
104
|
+
expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
|
|
105
|
+
expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
|
|
114
106
|
|
|
115
107
|
// TData (3rd element) takes precedence over TQueryFnData (1st element)
|
|
116
108
|
const result2 = createQueries<
|
|
@@ -121,8 +113,7 @@ describe('useQueries', () => {
|
|
|
121
113
|
queryKey: key1,
|
|
122
114
|
queryFn: () => 'string',
|
|
123
115
|
select: (a) => {
|
|
124
|
-
expectTypeOf<string>(
|
|
125
|
-
expectTypeNotAny(a)
|
|
116
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
126
117
|
return a.toLowerCase()
|
|
127
118
|
},
|
|
128
119
|
},
|
|
@@ -130,17 +121,20 @@ describe('useQueries', () => {
|
|
|
130
121
|
queryKey: key2,
|
|
131
122
|
queryFn: () => 'string',
|
|
132
123
|
select: (a) => {
|
|
133
|
-
expectTypeOf<string>(
|
|
134
|
-
expectTypeNotAny(a)
|
|
124
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
135
125
|
return parseInt(a)
|
|
136
126
|
},
|
|
137
127
|
},
|
|
138
128
|
],
|
|
139
129
|
}))
|
|
140
|
-
expectTypeOf
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
expectTypeOf
|
|
130
|
+
expectTypeOf(result2[0]).toEqualTypeOf<
|
|
131
|
+
CreateQueryResult<string, unknown>
|
|
132
|
+
>()
|
|
133
|
+
expectTypeOf(result2[1]).toEqualTypeOf<
|
|
134
|
+
CreateQueryResult<number, unknown>
|
|
135
|
+
>()
|
|
136
|
+
expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
|
|
137
|
+
expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
|
|
144
138
|
|
|
145
139
|
// types should be enforced
|
|
146
140
|
createQueries<[[string, unknown, string], [string, boolean, number]]>(
|
|
@@ -150,8 +144,7 @@ describe('useQueries', () => {
|
|
|
150
144
|
queryKey: key1,
|
|
151
145
|
queryFn: () => 'string',
|
|
152
146
|
select: (a) => {
|
|
153
|
-
expectTypeOf<string>(
|
|
154
|
-
expectTypeNotAny(a)
|
|
147
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
155
148
|
return a.toLowerCase()
|
|
156
149
|
},
|
|
157
150
|
placeholderData: 'string',
|
|
@@ -162,8 +155,7 @@ describe('useQueries', () => {
|
|
|
162
155
|
queryKey: key2,
|
|
163
156
|
queryFn: () => 'string',
|
|
164
157
|
select: (a) => {
|
|
165
|
-
expectTypeOf<string>(
|
|
166
|
-
expectTypeNotAny(a)
|
|
158
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
167
159
|
return parseInt(a)
|
|
168
160
|
},
|
|
169
161
|
placeholderData: 'string',
|
|
@@ -215,13 +207,19 @@ describe('useQueries', () => {
|
|
|
215
207
|
},
|
|
216
208
|
],
|
|
217
209
|
}))
|
|
218
|
-
expectTypeOf
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
expectTypeOf
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
expectTypeOf
|
|
210
|
+
expectTypeOf(result1[0]).toEqualTypeOf<
|
|
211
|
+
CreateQueryResult<number, unknown>
|
|
212
|
+
>()
|
|
213
|
+
expectTypeOf(result1[1]).toEqualTypeOf<
|
|
214
|
+
CreateQueryResult<string, unknown>
|
|
215
|
+
>()
|
|
216
|
+
expectTypeOf(result1[2]).toEqualTypeOf<
|
|
217
|
+
CreateQueryResult<Array<string>, boolean>
|
|
218
|
+
>()
|
|
219
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
220
|
+
expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
|
|
221
|
+
expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
|
|
222
|
+
expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
|
|
225
223
|
|
|
226
224
|
// TData (data prop) takes precedence over TQueryFnData (queryFnData prop)
|
|
227
225
|
const result2 = createQueries<
|
|
@@ -235,8 +233,7 @@ describe('useQueries', () => {
|
|
|
235
233
|
queryKey: key1,
|
|
236
234
|
queryFn: () => 'string',
|
|
237
235
|
select: (a) => {
|
|
238
|
-
expectTypeOf<string>(
|
|
239
|
-
expectTypeNotAny(a)
|
|
236
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
240
237
|
return a.toLowerCase()
|
|
241
238
|
},
|
|
242
239
|
},
|
|
@@ -244,17 +241,20 @@ describe('useQueries', () => {
|
|
|
244
241
|
queryKey: key2,
|
|
245
242
|
queryFn: () => 'string',
|
|
246
243
|
select: (a) => {
|
|
247
|
-
expectTypeOf<string>(
|
|
248
|
-
expectTypeNotAny(a)
|
|
244
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
249
245
|
return parseInt(a)
|
|
250
246
|
},
|
|
251
247
|
},
|
|
252
248
|
],
|
|
253
249
|
}))
|
|
254
|
-
expectTypeOf
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
expectTypeOf
|
|
250
|
+
expectTypeOf(result2[0]).toEqualTypeOf<
|
|
251
|
+
CreateQueryResult<string, unknown>
|
|
252
|
+
>()
|
|
253
|
+
expectTypeOf(result2[1]).toEqualTypeOf<
|
|
254
|
+
CreateQueryResult<number, unknown>
|
|
255
|
+
>()
|
|
256
|
+
expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
|
|
257
|
+
expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
|
|
258
258
|
|
|
259
259
|
// can pass only TData (data prop) although TQueryFnData will be left unknown
|
|
260
260
|
const result3 = createQueries<[{ data: string }, { data: number }]>(
|
|
@@ -264,8 +264,7 @@ describe('useQueries', () => {
|
|
|
264
264
|
queryKey: key1,
|
|
265
265
|
queryFn: () => 'string',
|
|
266
266
|
select: (a) => {
|
|
267
|
-
expectTypeOf<unknown>(
|
|
268
|
-
expectTypeNotAny(a)
|
|
267
|
+
expectTypeOf(a).toEqualTypeOf<unknown>()
|
|
269
268
|
return a as string
|
|
270
269
|
},
|
|
271
270
|
},
|
|
@@ -273,18 +272,21 @@ describe('useQueries', () => {
|
|
|
273
272
|
queryKey: key2,
|
|
274
273
|
queryFn: () => 'string',
|
|
275
274
|
select: (a) => {
|
|
276
|
-
expectTypeOf<unknown>(
|
|
277
|
-
expectTypeNotAny(a)
|
|
275
|
+
expectTypeOf(a).toEqualTypeOf<unknown>()
|
|
278
276
|
return a as number
|
|
279
277
|
},
|
|
280
278
|
},
|
|
281
279
|
],
|
|
282
280
|
}),
|
|
283
281
|
)
|
|
284
|
-
expectTypeOf
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
expectTypeOf
|
|
282
|
+
expectTypeOf(result3[0]).toEqualTypeOf<
|
|
283
|
+
CreateQueryResult<string, unknown>
|
|
284
|
+
>()
|
|
285
|
+
expectTypeOf(result3[1]).toEqualTypeOf<
|
|
286
|
+
CreateQueryResult<number, unknown>
|
|
287
|
+
>()
|
|
288
|
+
expectTypeOf(result3[0].data).toEqualTypeOf<string | undefined>()
|
|
289
|
+
expectTypeOf(result3[1].data).toEqualTypeOf<number | undefined>()
|
|
288
290
|
|
|
289
291
|
// types should be enforced
|
|
290
292
|
createQueries<
|
|
@@ -298,8 +300,7 @@ describe('useQueries', () => {
|
|
|
298
300
|
queryKey: key1,
|
|
299
301
|
queryFn: () => 'string',
|
|
300
302
|
select: (a) => {
|
|
301
|
-
expectTypeOf<string>(
|
|
302
|
-
expectTypeNotAny(a)
|
|
303
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
303
304
|
return a.toLowerCase()
|
|
304
305
|
},
|
|
305
306
|
placeholderData: 'string',
|
|
@@ -310,8 +311,7 @@ describe('useQueries', () => {
|
|
|
310
311
|
queryKey: key2,
|
|
311
312
|
queryFn: () => 'string',
|
|
312
313
|
select: (a) => {
|
|
313
|
-
expectTypeOf<string>(
|
|
314
|
-
expectTypeNotAny(a)
|
|
314
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
315
315
|
return parseInt(a)
|
|
316
316
|
},
|
|
317
317
|
placeholderData: 'string',
|
|
@@ -348,8 +348,12 @@ describe('useQueries', () => {
|
|
|
348
348
|
queryFn: () => i + 10,
|
|
349
349
|
})),
|
|
350
350
|
}))
|
|
351
|
-
expectTypeOf
|
|
352
|
-
|
|
351
|
+
expectTypeOf(result1).toEqualTypeOf<
|
|
352
|
+
Array<CreateQueryResult<number, Error>>
|
|
353
|
+
>()
|
|
354
|
+
if (result1[0]) {
|
|
355
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
356
|
+
}
|
|
353
357
|
|
|
354
358
|
// Array.map preserves TData
|
|
355
359
|
const result2 = createQueries(() => ({
|
|
@@ -359,7 +363,9 @@ describe('useQueries', () => {
|
|
|
359
363
|
select: (data: number) => data.toString(),
|
|
360
364
|
})),
|
|
361
365
|
}))
|
|
362
|
-
expectTypeOf
|
|
366
|
+
expectTypeOf(result2).toEqualTypeOf<
|
|
367
|
+
Array<CreateQueryResult<string, Error>>
|
|
368
|
+
>()
|
|
363
369
|
|
|
364
370
|
const result3 = createQueries(() => ({
|
|
365
371
|
queries: [
|
|
@@ -378,13 +384,13 @@ describe('useQueries', () => {
|
|
|
378
384
|
},
|
|
379
385
|
],
|
|
380
386
|
}))
|
|
381
|
-
expectTypeOf<
|
|
382
|
-
expectTypeOf<
|
|
383
|
-
expectTypeOf<
|
|
384
|
-
expectTypeOf<number | undefined>(
|
|
385
|
-
expectTypeOf<string | undefined>(
|
|
387
|
+
expectTypeOf(result3[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
388
|
+
expectTypeOf(result3[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
389
|
+
expectTypeOf(result3[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
390
|
+
expectTypeOf(result3[0].data).toEqualTypeOf<number | undefined>()
|
|
391
|
+
expectTypeOf(result3[1].data).toEqualTypeOf<string | undefined>()
|
|
386
392
|
// select takes precedence over queryFn
|
|
387
|
-
expectTypeOf<number | undefined>(
|
|
393
|
+
expectTypeOf(result3[2].data).toEqualTypeOf<number | undefined>()
|
|
388
394
|
|
|
389
395
|
// initialData/placeholderData are enforced
|
|
390
396
|
createQueries(() => ({
|
|
@@ -467,9 +473,9 @@ describe('useQueries', () => {
|
|
|
467
473
|
},
|
|
468
474
|
],
|
|
469
475
|
}))
|
|
470
|
-
expectTypeOf<
|
|
471
|
-
expectTypeOf<
|
|
472
|
-
expectTypeOf<
|
|
476
|
+
expectTypeOf(result4[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
477
|
+
expectTypeOf(result4[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
478
|
+
expectTypeOf(result4[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
473
479
|
|
|
474
480
|
// handles when queryFn returns a Promise
|
|
475
481
|
const result5 = createQueries(() => ({
|
|
@@ -480,7 +486,7 @@ describe('useQueries', () => {
|
|
|
480
486
|
},
|
|
481
487
|
],
|
|
482
488
|
}))
|
|
483
|
-
expectTypeOf<
|
|
489
|
+
expectTypeOf(result5[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
484
490
|
|
|
485
491
|
// Array as const does not throw error
|
|
486
492
|
const result6 = createQueries(
|
|
@@ -498,8 +504,8 @@ describe('useQueries', () => {
|
|
|
498
504
|
],
|
|
499
505
|
}) as const,
|
|
500
506
|
)
|
|
501
|
-
expectTypeOf<
|
|
502
|
-
expectTypeOf<
|
|
507
|
+
expectTypeOf(result6[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
508
|
+
expectTypeOf(result6[1]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
503
509
|
|
|
504
510
|
// field names should be enforced - array literal
|
|
505
511
|
createQueries(() => ({
|
|
@@ -585,9 +591,11 @@ describe('useQueries', () => {
|
|
|
585
591
|
// no need to type the mapped query
|
|
586
592
|
(query) => {
|
|
587
593
|
const { queryFn: fn, queryKey: key } = query
|
|
588
|
-
expectTypeOf<
|
|
589
|
-
|
|
590
|
-
|
|
594
|
+
expectTypeOf(fn).toEqualTypeOf<
|
|
595
|
+
| typeof QueryCore.skipToken
|
|
596
|
+
| QueryCore.QueryFunction<TQueryFnData, TQueryKey, never>
|
|
597
|
+
| undefined
|
|
598
|
+
>()
|
|
591
599
|
return {
|
|
592
600
|
queryKey: key,
|
|
593
601
|
queryFn: fn
|
|
@@ -619,8 +627,8 @@ describe('useQueries', () => {
|
|
|
619
627
|
},
|
|
620
628
|
],
|
|
621
629
|
}))
|
|
622
|
-
expectTypeOf<
|
|
623
|
-
expectTypeOf<
|
|
630
|
+
expectTypeOf(result[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
631
|
+
expectTypeOf(result[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
624
632
|
|
|
625
633
|
const withSelector = createQueries(() => ({
|
|
626
634
|
queries: [
|
|
@@ -636,12 +644,12 @@ describe('useQueries', () => {
|
|
|
636
644
|
},
|
|
637
645
|
],
|
|
638
646
|
}))
|
|
639
|
-
expectTypeOf
|
|
640
|
-
|
|
641
|
-
)
|
|
642
|
-
expectTypeOf
|
|
643
|
-
|
|
644
|
-
)
|
|
647
|
+
expectTypeOf(withSelector[0]).toEqualTypeOf<
|
|
648
|
+
CreateQueryResult<[number, string], Error>
|
|
649
|
+
>()
|
|
650
|
+
expectTypeOf(withSelector[1]).toEqualTypeOf<
|
|
651
|
+
CreateQueryResult<[string, number], Error>
|
|
652
|
+
>()
|
|
645
653
|
|
|
646
654
|
const withWrappedQueries = useWrappedQueries(
|
|
647
655
|
Array(10).map(() => ({
|
|
@@ -651,9 +659,9 @@ describe('useQueries', () => {
|
|
|
651
659
|
})),
|
|
652
660
|
)
|
|
653
661
|
|
|
654
|
-
expectTypeOf<
|
|
655
|
-
|
|
656
|
-
)
|
|
662
|
+
expectTypeOf(withWrappedQueries).toEqualTypeOf<
|
|
663
|
+
Array<CreateQueryResult<number, Error>>
|
|
664
|
+
>()
|
|
657
665
|
}
|
|
658
666
|
})
|
|
659
667
|
|
|
@@ -46,32 +46,32 @@ describe('createQuery', () => {
|
|
|
46
46
|
function Page() {
|
|
47
47
|
// unspecified query function should default to unknown
|
|
48
48
|
const noQueryFn = createQuery(() => ({ queryKey: key }))
|
|
49
|
-
expectTypeOf
|
|
50
|
-
expectTypeOf
|
|
49
|
+
expectTypeOf(noQueryFn.data).toEqualTypeOf<unknown>()
|
|
50
|
+
expectTypeOf(noQueryFn.error).toEqualTypeOf<Error | null>()
|
|
51
51
|
|
|
52
52
|
// it should infer the result type from the query function
|
|
53
53
|
const fromQueryFn = createQuery(() => ({
|
|
54
54
|
queryKey: key,
|
|
55
55
|
queryFn: () => 'test',
|
|
56
56
|
}))
|
|
57
|
-
expectTypeOf<string | undefined>(
|
|
58
|
-
expectTypeOf
|
|
57
|
+
expectTypeOf(fromQueryFn.data).toEqualTypeOf<string | undefined>()
|
|
58
|
+
expectTypeOf(fromQueryFn.error).toEqualTypeOf<Error | null>()
|
|
59
59
|
|
|
60
60
|
// it should be possible to specify the result type
|
|
61
61
|
const withResult = createQuery<string>(() => ({
|
|
62
62
|
queryKey: key,
|
|
63
63
|
queryFn: () => 'test',
|
|
64
64
|
}))
|
|
65
|
-
expectTypeOf<string | undefined>(
|
|
66
|
-
expectTypeOf<
|
|
65
|
+
expectTypeOf(withResult.data).toEqualTypeOf<string | undefined>()
|
|
66
|
+
expectTypeOf(withResult.error).toEqualTypeOf<Error | null>()
|
|
67
67
|
|
|
68
68
|
// it should be possible to specify the error type
|
|
69
69
|
const withError = createQuery<string, Error>(() => ({
|
|
70
70
|
queryKey: key,
|
|
71
71
|
queryFn: () => 'test',
|
|
72
72
|
}))
|
|
73
|
-
expectTypeOf<string | undefined>(
|
|
74
|
-
expectTypeOf<Error | null>(
|
|
73
|
+
expectTypeOf(withError.data).toEqualTypeOf<string | undefined>()
|
|
74
|
+
expectTypeOf(withError.error).toEqualTypeOf<Error | null>()
|
|
75
75
|
|
|
76
76
|
// it should provide the result type in the configuration
|
|
77
77
|
createQuery(() => ({
|
|
@@ -82,14 +82,14 @@ describe('createQuery', () => {
|
|
|
82
82
|
// it should be possible to specify a union type as result type
|
|
83
83
|
const unionTypeSync = createQuery(() => ({
|
|
84
84
|
queryKey: key,
|
|
85
|
-
queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
85
|
+
queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)),
|
|
86
86
|
}))
|
|
87
|
-
expectTypeOf<'a' | 'b' | undefined>(
|
|
87
|
+
expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
88
88
|
const unionTypeAsync = createQuery<'a' | 'b'>(() => ({
|
|
89
89
|
queryKey: key,
|
|
90
90
|
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
91
91
|
}))
|
|
92
|
-
expectTypeOf<'a' | 'b' | undefined>(
|
|
92
|
+
expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
93
93
|
|
|
94
94
|
// should error when the query function result does not match with the specified type
|
|
95
95
|
// @ts-expect-error
|
|
@@ -104,15 +104,19 @@ describe('createQuery', () => {
|
|
|
104
104
|
queryKey: key,
|
|
105
105
|
queryFn: () => queryFn(),
|
|
106
106
|
}))
|
|
107
|
-
expectTypeOf<string | undefined>(
|
|
108
|
-
expectTypeOf
|
|
107
|
+
expectTypeOf(fromGenericQueryFn.data).toEqualTypeOf<string | undefined>()
|
|
108
|
+
expectTypeOf(fromGenericQueryFn.error).toEqualTypeOf<Error | null>()
|
|
109
109
|
|
|
110
110
|
const fromGenericOptionsQueryFn = createQuery(() => ({
|
|
111
111
|
queryKey: key,
|
|
112
112
|
queryFn: () => queryFn(),
|
|
113
113
|
}))
|
|
114
|
-
expectTypeOf
|
|
115
|
-
|
|
114
|
+
expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf<
|
|
115
|
+
string | undefined
|
|
116
|
+
>()
|
|
117
|
+
expectTypeOf(
|
|
118
|
+
fromGenericOptionsQueryFn.error,
|
|
119
|
+
).toEqualTypeOf<Error | null>()
|
|
116
120
|
|
|
117
121
|
type MyData = number
|
|
118
122
|
type MyQueryKey = readonly ['my-data', number]
|
|
@@ -131,7 +135,7 @@ describe('createQuery', () => {
|
|
|
131
135
|
const getMyDataStringKey: QueryFunction<MyData, ['1']> = async (
|
|
132
136
|
context,
|
|
133
137
|
) => {
|
|
134
|
-
expectTypeOf<['1']>(
|
|
138
|
+
expectTypeOf(context.queryKey).toEqualTypeOf<['1']>()
|
|
135
139
|
return Number(context.queryKey[0]) + 42
|
|
136
140
|
}
|
|
137
141
|
|
|
@@ -170,7 +174,7 @@ describe('createQuery', () => {
|
|
|
170
174
|
...options,
|
|
171
175
|
}))
|
|
172
176
|
const test = useWrappedQuery([''], async () => '1')
|
|
173
|
-
expectTypeOf<string | undefined>(
|
|
177
|
+
expectTypeOf(test.data).toEqualTypeOf<string | undefined>()
|
|
174
178
|
|
|
175
179
|
// handles wrapped queries with custom fetcher passed directly to createQuery
|
|
176
180
|
const useWrappedFuncStyleQuery = <
|
|
@@ -187,7 +191,7 @@ describe('createQuery', () => {
|
|
|
187
191
|
>,
|
|
188
192
|
) => createQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options }))
|
|
189
193
|
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
|
|
190
|
-
expectTypeOf<boolean | undefined>(
|
|
194
|
+
expectTypeOf(testFuncStyle.data).toEqualTypeOf<boolean | undefined>()
|
|
191
195
|
}
|
|
192
196
|
})
|
|
193
197
|
|
|
@@ -240,14 +244,14 @@ describe('createQuery', () => {
|
|
|
240
244
|
})
|
|
241
245
|
|
|
242
246
|
if (state.isPending) {
|
|
243
|
-
expectTypeOf
|
|
244
|
-
expectTypeOf
|
|
247
|
+
expectTypeOf(state.data).toEqualTypeOf<undefined>()
|
|
248
|
+
expectTypeOf(state.error).toEqualTypeOf<null>()
|
|
245
249
|
} else if (state.isLoadingError) {
|
|
246
|
-
expectTypeOf
|
|
247
|
-
expectTypeOf
|
|
250
|
+
expectTypeOf(state.data).toEqualTypeOf<undefined>()
|
|
251
|
+
expectTypeOf(state.error).toEqualTypeOf<Error>()
|
|
248
252
|
} else {
|
|
249
|
-
expectTypeOf
|
|
250
|
-
expectTypeOf<Error | null>(
|
|
253
|
+
expectTypeOf(state.data).toEqualTypeOf<string>()
|
|
254
|
+
expectTypeOf(state.error).toEqualTypeOf<Error | null>()
|
|
251
255
|
}
|
|
252
256
|
|
|
253
257
|
return (
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest'
|
|
2
|
+
import { useMutationState } from '../useMutationState'
|
|
3
|
+
import type { MutationState, MutationStatus } from '@tanstack/query-core'
|
|
4
|
+
|
|
5
|
+
describe('useMutationState', () => {
|
|
6
|
+
it('should default to QueryState', () => {
|
|
7
|
+
const result = useMutationState(() => ({
|
|
8
|
+
filters: { status: 'pending' },
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
expectTypeOf(result()).toEqualTypeOf<
|
|
12
|
+
Array<MutationState<unknown, Error, void, unknown>>
|
|
13
|
+
>()
|
|
14
|
+
})
|
|
15
|
+
it('should infer with select', () => {
|
|
16
|
+
const result = useMutationState(() => ({
|
|
17
|
+
filters: { status: 'pending' },
|
|
18
|
+
select: (mutation) => mutation.state.status,
|
|
19
|
+
}))
|
|
20
|
+
|
|
21
|
+
expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
|
|
22
|
+
})
|
|
23
|
+
})
|
|
@@ -1,34 +1,12 @@
|
|
|
1
|
-
import { describe, expect,
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
|
|
3
3
|
import { createEffect } from 'solid-js'
|
|
4
4
|
import { useMutationState } from '../useMutationState'
|
|
5
5
|
import { createMutation } from '../createMutation'
|
|
6
6
|
import { QueryClientProvider } from '../QueryClientProvider'
|
|
7
|
-
import { createQueryClient,
|
|
8
|
-
import type { MutationState, MutationStatus } from '@tanstack/query-core'
|
|
7
|
+
import { createQueryClient, sleep } from './utils'
|
|
9
8
|
|
|
10
9
|
describe('useMutationState', () => {
|
|
11
|
-
describe('types', () => {
|
|
12
|
-
it('should default to QueryState', () => {
|
|
13
|
-
doNotExecute(() => {
|
|
14
|
-
const result = useMutationState(() => ({
|
|
15
|
-
filters: { status: 'pending' },
|
|
16
|
-
}))
|
|
17
|
-
|
|
18
|
-
expectTypeOf<Array<MutationState>>(result())
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
it('should infer with select', () => {
|
|
22
|
-
doNotExecute(() => {
|
|
23
|
-
const result = useMutationState(() => ({
|
|
24
|
-
filters: { status: 'pending' },
|
|
25
|
-
select: (mutation) => mutation.state.status,
|
|
26
|
-
}))
|
|
27
|
-
|
|
28
|
-
expectTypeOf<Array<MutationStatus>>(result())
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
})
|
|
32
10
|
it('should return variables after calling mutate', async () => {
|
|
33
11
|
const queryClient = createQueryClient()
|
|
34
12
|
const variables: Array<Array<unknown>> = []
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -59,12 +59,3 @@ export function setActTimeout(fn: () => void, ms?: number) {
|
|
|
59
59
|
fn()
|
|
60
60
|
}, ms)
|
|
61
61
|
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Assert the parameter is not typed as `any`
|
|
65
|
-
*/
|
|
66
|
-
export function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void {
|
|
67
|
-
return undefined
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const doNotExecute = (_func: () => void) => true
|