@tanstack/solid-query 5.0.0-alpha.2 → 5.0.0-alpha.21
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/build/cjs/index.js +73 -34
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +75 -37
- package/build/esm/index.js.map +1 -1
- package/build/source/QueryClient.js +6 -0
- package/build/source/__tests__/QueryClientProvider.test.jsx +2 -1
- package/build/source/__tests__/createInfiniteQuery.test.jsx +67 -20
- package/build/source/__tests__/createMutation.test.jsx +19 -18
- package/build/source/__tests__/createQueries.test.jsx +4 -91
- package/build/source/__tests__/createQuery.test.jsx +62 -271
- package/build/source/__tests__/suspense.test.jsx +3 -64
- package/build/source/__tests__/useIsFetching.test.jsx +2 -4
- package/build/source/__tests__/useIsMutating.test.jsx +25 -28
- package/build/source/__tests__/utils.jsx +4 -3
- package/build/source/createBaseQuery.js +45 -19
- package/build/source/createQueries.js +5 -5
- package/build/source/index.js +1 -0
- package/build/source/useIsFetching.js +5 -5
- package/build/source/useIsMutating.js +5 -5
- package/build/types/QueryClient.d.ts +29 -0
- package/build/types/QueryClientProvider.d.ts +1 -1
- package/build/types/__tests__/utils.d.ts +3 -4
- package/build/types/createBaseQuery.d.ts +3 -2
- package/build/types/createInfiniteQuery.d.ts +4 -2
- package/build/types/createMutation.d.ts +4 -2
- package/build/types/createQueries.d.ts +5 -4
- package/build/types/createQuery.d.ts +2 -1
- package/build/types/index.d.ts +2 -0
- package/build/types/types.d.ts +2 -1
- package/build/types/useIsFetching.d.ts +3 -7
- package/build/types/useIsMutating.d.ts +3 -7
- package/build/umd/index.js +1 -1
- package/build/umd/index.js.map +1 -1
- package/package.json +5 -5
- package/src/QueryClient.ts +84 -0
- package/src/QueryClientProvider.tsx +1 -1
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/createInfiniteQuery.test.tsx +95 -34
- package/src/__tests__/createMutation.test.tsx +19 -18
- package/src/__tests__/createQueries.test.tsx +4 -97
- package/src/__tests__/createQuery.test.tsx +78 -344
- package/src/__tests__/suspense.test.tsx +3 -85
- package/src/__tests__/useIsFetching.test.tsx +2 -4
- package/src/__tests__/useIsMutating.test.tsx +32 -40
- package/src/__tests__/utils.tsx +4 -3
- package/src/createBaseQuery.ts +70 -25
- package/src/createInfiniteQuery.ts +3 -2
- package/src/createMutation.ts +4 -2
- package/src/createQueries.ts +9 -8
- package/src/createQuery.ts +4 -2
- package/src/index.ts +7 -0
- package/src/types.ts +4 -2
- package/src/useIsFetching.ts +10 -13
- package/src/useIsMutating.ts +10 -11
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
setActTimeout,
|
|
21
21
|
sleep,
|
|
22
22
|
} from './utils'
|
|
23
|
+
import { vi } from 'vitest'
|
|
23
24
|
|
|
24
25
|
describe('createMutation', () => {
|
|
25
26
|
const queryCache = new QueryCache()
|
|
@@ -108,8 +109,8 @@ describe('createMutation', () => {
|
|
|
108
109
|
|
|
109
110
|
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
|
|
110
111
|
const [count, setCount] = createSignal(0)
|
|
111
|
-
const onSuccessMock =
|
|
112
|
-
const onSettledMock =
|
|
112
|
+
const onSuccessMock = vi.fn()
|
|
113
|
+
const onSettledMock = vi.fn()
|
|
113
114
|
|
|
114
115
|
function Page() {
|
|
115
116
|
const mutation = createMutation(() => ({
|
|
@@ -174,7 +175,7 @@ describe('createMutation', () => {
|
|
|
174
175
|
const [count, setCount] = createSignal(0)
|
|
175
176
|
type Value = { count: number }
|
|
176
177
|
|
|
177
|
-
const mutateFn =
|
|
178
|
+
const mutateFn = vi.fn<[value: Value], Promise<Value>>()
|
|
178
179
|
|
|
179
180
|
mutateFn.mockImplementationOnce(() => {
|
|
180
181
|
return Promise.reject(new Error('Error test Jonas'))
|
|
@@ -231,8 +232,8 @@ describe('createMutation', () => {
|
|
|
231
232
|
})
|
|
232
233
|
|
|
233
234
|
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
|
|
234
|
-
const onErrorMock =
|
|
235
|
-
const onSettledMock =
|
|
235
|
+
const onErrorMock = vi.fn()
|
|
236
|
+
const onSettledMock = vi.fn()
|
|
236
237
|
const [count, setCount] = createSignal(0)
|
|
237
238
|
|
|
238
239
|
function Page() {
|
|
@@ -562,7 +563,7 @@ describe('createMutation', () => {
|
|
|
562
563
|
|
|
563
564
|
it('should call onMutate even if paused', async () => {
|
|
564
565
|
const onlineMock = mockNavigatorOnLine(false)
|
|
565
|
-
const onMutate =
|
|
566
|
+
const onMutate = vi.fn()
|
|
566
567
|
let count = 0
|
|
567
568
|
|
|
568
569
|
function Page() {
|
|
@@ -870,8 +871,8 @@ describe('createMutation', () => {
|
|
|
870
871
|
})
|
|
871
872
|
|
|
872
873
|
it('should pass meta to mutation', async () => {
|
|
873
|
-
const errorMock =
|
|
874
|
-
const successMock =
|
|
874
|
+
const errorMock = vi.fn()
|
|
875
|
+
const successMock = vi.fn()
|
|
875
876
|
|
|
876
877
|
const queryClientMutationMeta = createQueryClient({
|
|
877
878
|
mutationCache: new MutationCache({
|
|
@@ -930,10 +931,10 @@ describe('createMutation', () => {
|
|
|
930
931
|
})
|
|
931
932
|
|
|
932
933
|
it('should call cache callbacks when unmounted', async () => {
|
|
933
|
-
const onSuccess =
|
|
934
|
-
const onSuccessMutate =
|
|
935
|
-
const onSettled =
|
|
936
|
-
const onSettledMutate =
|
|
934
|
+
const onSuccess = vi.fn()
|
|
935
|
+
const onSuccessMutate = vi.fn()
|
|
936
|
+
const onSettled = vi.fn()
|
|
937
|
+
const onSettledMutate = vi.fn()
|
|
937
938
|
const mutationKey = queryKey()
|
|
938
939
|
let count = 0
|
|
939
940
|
|
|
@@ -1006,10 +1007,10 @@ describe('createMutation', () => {
|
|
|
1006
1007
|
})
|
|
1007
1008
|
|
|
1008
1009
|
it('should call mutate callbacks only for the last observer', async () => {
|
|
1009
|
-
const onSuccess =
|
|
1010
|
-
const onSuccessMutate =
|
|
1011
|
-
const onSettled =
|
|
1012
|
-
const onSettledMutate =
|
|
1010
|
+
const onSuccess = vi.fn()
|
|
1011
|
+
const onSuccessMutate = vi.fn()
|
|
1012
|
+
const onSettled = vi.fn()
|
|
1013
|
+
const onSettledMutate = vi.fn()
|
|
1013
1014
|
let count = 0
|
|
1014
1015
|
|
|
1015
1016
|
function Page() {
|
|
@@ -1072,7 +1073,7 @@ describe('createMutation', () => {
|
|
|
1072
1073
|
|
|
1073
1074
|
it('should go to error state if onSuccess callback errors', async () => {
|
|
1074
1075
|
const error = new Error('error from onSuccess')
|
|
1075
|
-
const onError =
|
|
1076
|
+
const onError = vi.fn()
|
|
1076
1077
|
|
|
1077
1078
|
function Page() {
|
|
1078
1079
|
const mutation = createMutation(() => ({
|
|
@@ -1148,7 +1149,7 @@ describe('createMutation', () => {
|
|
|
1148
1149
|
it('should go to error state if onSettled callback errors', async () => {
|
|
1149
1150
|
const error = new Error('error from onSettled')
|
|
1150
1151
|
const mutateFnError = new Error('mutateFnError')
|
|
1151
|
-
const onError =
|
|
1152
|
+
const onError = vi.fn()
|
|
1152
1153
|
|
|
1153
1154
|
function Page() {
|
|
1154
1155
|
const mutation = createMutation(() => ({
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
queryKey,
|
|
24
24
|
sleep,
|
|
25
25
|
} from './utils'
|
|
26
|
+
import { vi } from 'vitest'
|
|
26
27
|
|
|
27
28
|
describe('useQueries', () => {
|
|
28
29
|
const queryCache = new QueryCache()
|
|
@@ -157,10 +158,6 @@ describe('useQueries', () => {
|
|
|
157
158
|
expectTypeNotAny(a)
|
|
158
159
|
return a.toLowerCase()
|
|
159
160
|
},
|
|
160
|
-
onSuccess: (a) => {
|
|
161
|
-
expectType<string>(a)
|
|
162
|
-
expectTypeNotAny(a)
|
|
163
|
-
},
|
|
164
161
|
placeholderData: 'string',
|
|
165
162
|
// @ts-expect-error (initialData: string)
|
|
166
163
|
initialData: 123,
|
|
@@ -173,14 +170,6 @@ describe('useQueries', () => {
|
|
|
173
170
|
expectTypeNotAny(a)
|
|
174
171
|
return parseInt(a)
|
|
175
172
|
},
|
|
176
|
-
onSuccess: (a) => {
|
|
177
|
-
expectType<number>(a)
|
|
178
|
-
expectTypeNotAny(a)
|
|
179
|
-
},
|
|
180
|
-
onError: (e) => {
|
|
181
|
-
expectType<boolean>(e)
|
|
182
|
-
expectTypeNotAny(e)
|
|
183
|
-
},
|
|
184
173
|
placeholderData: 'string',
|
|
185
174
|
// @ts-expect-error (initialData: string)
|
|
186
175
|
initialData: 123,
|
|
@@ -318,10 +307,6 @@ describe('useQueries', () => {
|
|
|
318
307
|
expectTypeNotAny(a)
|
|
319
308
|
return a.toLowerCase()
|
|
320
309
|
},
|
|
321
|
-
onSuccess: (a) => {
|
|
322
|
-
expectType<string>(a)
|
|
323
|
-
expectTypeNotAny(a)
|
|
324
|
-
},
|
|
325
310
|
placeholderData: 'string',
|
|
326
311
|
// @ts-expect-error (initialData: string)
|
|
327
312
|
initialData: 123,
|
|
@@ -334,14 +319,6 @@ describe('useQueries', () => {
|
|
|
334
319
|
expectTypeNotAny(a)
|
|
335
320
|
return parseInt(a)
|
|
336
321
|
},
|
|
337
|
-
onSuccess: (a) => {
|
|
338
|
-
expectType<number>(a)
|
|
339
|
-
expectTypeNotAny(a)
|
|
340
|
-
},
|
|
341
|
-
onError: (e) => {
|
|
342
|
-
expectType<boolean>(e)
|
|
343
|
-
expectTypeNotAny(e)
|
|
344
|
-
},
|
|
345
322
|
placeholderData: 'string',
|
|
346
323
|
// @ts-expect-error (initialData: string)
|
|
347
324
|
initialData: 123,
|
|
@@ -435,60 +412,38 @@ describe('useQueries', () => {
|
|
|
435
412
|
],
|
|
436
413
|
}))
|
|
437
414
|
|
|
438
|
-
// select
|
|
415
|
+
// select params are "indirectly" enforced
|
|
439
416
|
createQueries(() => ({
|
|
440
417
|
queries: [
|
|
441
418
|
// unfortunately TS will not suggest the type for you
|
|
442
419
|
{
|
|
443
420
|
queryKey: key1,
|
|
444
421
|
queryFn: () => 'string',
|
|
445
|
-
// @ts-expect-error (noImplicitAny)
|
|
446
|
-
onSuccess: (a) => null,
|
|
447
|
-
// @ts-expect-error (noImplicitAny)
|
|
448
|
-
onSettled: (a) => null,
|
|
449
422
|
},
|
|
450
423
|
// however you can add a type to the callback
|
|
451
424
|
{
|
|
452
425
|
queryKey: key2,
|
|
453
426
|
queryFn: () => 'string',
|
|
454
|
-
onSuccess: (a: string) => {
|
|
455
|
-
expectType<string>(a)
|
|
456
|
-
expectTypeNotAny(a)
|
|
457
|
-
},
|
|
458
|
-
onSettled: (a: string | undefined) => {
|
|
459
|
-
expectType<string | undefined>(a)
|
|
460
|
-
expectTypeNotAny(a)
|
|
461
|
-
},
|
|
462
427
|
},
|
|
463
428
|
// the type you do pass is enforced
|
|
464
429
|
{
|
|
465
430
|
queryKey: key3,
|
|
466
431
|
queryFn: () => 'string',
|
|
467
|
-
// @ts-expect-error (only accepts string)
|
|
468
|
-
onSuccess: (a: number) => null,
|
|
469
432
|
},
|
|
470
433
|
{
|
|
471
434
|
queryKey: key4,
|
|
472
435
|
queryFn: () => 'string',
|
|
473
436
|
select: (a: string) => parseInt(a),
|
|
474
|
-
// @ts-expect-error (select is defined => only accepts number)
|
|
475
|
-
onSuccess: (a: string) => null,
|
|
476
|
-
onSettled: (a: number | undefined) => {
|
|
477
|
-
expectType<number | undefined>(a)
|
|
478
|
-
expectTypeNotAny(a)
|
|
479
|
-
},
|
|
480
437
|
},
|
|
481
438
|
],
|
|
482
439
|
}))
|
|
483
440
|
|
|
484
441
|
// callbacks are also indirectly enforced with Array.map
|
|
485
442
|
createQueries(() => ({
|
|
486
|
-
// @ts-expect-error (onSuccess only accepts string)
|
|
487
443
|
queries: Array(50).map((_, i) => ({
|
|
488
444
|
queryKey: ['key', i] as const,
|
|
489
445
|
queryFn: () => i + 10,
|
|
490
446
|
select: (data: number) => data.toString(),
|
|
491
|
-
onSuccess: (_data: number) => null,
|
|
492
447
|
})),
|
|
493
448
|
}))
|
|
494
449
|
|
|
@@ -497,7 +452,6 @@ describe('useQueries', () => {
|
|
|
497
452
|
queryKey: ['key', i] as const,
|
|
498
453
|
queryFn: () => i + 10,
|
|
499
454
|
select: (data: number) => data.toString(),
|
|
500
|
-
onSuccess: (_data: string) => null,
|
|
501
455
|
})),
|
|
502
456
|
}))
|
|
503
457
|
|
|
@@ -507,32 +461,15 @@ describe('useQueries', () => {
|
|
|
507
461
|
{
|
|
508
462
|
queryKey: key1,
|
|
509
463
|
queryFn: () => 'string',
|
|
510
|
-
// @ts-expect-error (noImplicitAny)
|
|
511
|
-
onSuccess: (a) => null,
|
|
512
|
-
// @ts-expect-error (noImplicitAny)
|
|
513
|
-
onSettled: (a) => null,
|
|
514
464
|
},
|
|
515
465
|
{
|
|
516
466
|
queryKey: key2,
|
|
517
467
|
queryFn: () => 'string',
|
|
518
|
-
onSuccess: (a: string) => {
|
|
519
|
-
expectType<string>(a)
|
|
520
|
-
expectTypeNotAny(a)
|
|
521
|
-
},
|
|
522
|
-
onSettled: (a: string | undefined) => {
|
|
523
|
-
expectType<string | undefined>(a)
|
|
524
|
-
expectTypeNotAny(a)
|
|
525
|
-
},
|
|
526
468
|
},
|
|
527
469
|
{
|
|
528
470
|
queryKey: key4,
|
|
529
471
|
queryFn: () => 'string',
|
|
530
472
|
select: (a: string) => parseInt(a),
|
|
531
|
-
onSuccess: (_a: number) => null,
|
|
532
|
-
onSettled: (a: number | undefined) => {
|
|
533
|
-
expectType<number | undefined>(a)
|
|
534
|
-
expectTypeNotAny(a)
|
|
535
|
-
},
|
|
536
473
|
},
|
|
537
474
|
],
|
|
538
475
|
}))
|
|
@@ -546,12 +483,6 @@ describe('useQueries', () => {
|
|
|
546
483
|
{
|
|
547
484
|
queryKey: key1,
|
|
548
485
|
queryFn: () => Promise.resolve('string'),
|
|
549
|
-
onSuccess: (a: string) => {
|
|
550
|
-
expectType<string>(a)
|
|
551
|
-
expectTypeNotAny(a)
|
|
552
|
-
},
|
|
553
|
-
// @ts-expect-error (refuses to accept a Promise)
|
|
554
|
-
onSettled: (a: Promise<string>) => null,
|
|
555
486
|
},
|
|
556
487
|
],
|
|
557
488
|
}))
|
|
@@ -657,11 +588,10 @@ describe('useQueries', () => {
|
|
|
657
588
|
queries: queries.map(
|
|
658
589
|
// no need to type the mapped query
|
|
659
590
|
(query) => {
|
|
660
|
-
const { queryFn: fn, queryKey: key
|
|
591
|
+
const { queryFn: fn, queryKey: key } = query
|
|
661
592
|
expectType<QueryFunction<TQueryFnData, TQueryKey> | undefined>(fn)
|
|
662
593
|
return {
|
|
663
594
|
queryKey: key,
|
|
664
|
-
onError: err,
|
|
665
595
|
queryFn: fn
|
|
666
596
|
? (ctx: QueryFunctionContext<TQueryKey>) => {
|
|
667
597
|
expectType<TQueryKey>(ctx.queryKey)
|
|
@@ -739,7 +669,7 @@ describe('useQueries', () => {
|
|
|
739
669
|
}
|
|
740
670
|
}
|
|
741
671
|
|
|
742
|
-
const QueriesObserverSpy =
|
|
672
|
+
const QueriesObserverSpy = vi
|
|
743
673
|
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
744
674
|
.mockImplementation((fn) => {
|
|
745
675
|
return new QueriesObserverMock(fn)
|
|
@@ -789,27 +719,4 @@ describe('useQueries', () => {
|
|
|
789
719
|
await sleep(20)
|
|
790
720
|
QueriesObserverSpy.mockRestore()
|
|
791
721
|
})
|
|
792
|
-
|
|
793
|
-
it('should use provided custom queryClient', async () => {
|
|
794
|
-
const key = queryKey()
|
|
795
|
-
const queryFn = () => {
|
|
796
|
-
return Promise.resolve('custom client')
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
function Page() {
|
|
800
|
-
const state = createQueries(() => ({
|
|
801
|
-
queries: [{ queryKey: key, queryFn }],
|
|
802
|
-
queryClient,
|
|
803
|
-
}))
|
|
804
|
-
return (
|
|
805
|
-
<div>
|
|
806
|
-
<h1>Status: {state[0].data}</h1>
|
|
807
|
-
</div>
|
|
808
|
-
)
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
render(() => <Page />)
|
|
812
|
-
|
|
813
|
-
await waitFor(() => screen.getByText('Status: custom client'))
|
|
814
|
-
})
|
|
815
722
|
})
|