@tanstack/solid-query 5.0.0-alpha.1 → 5.0.0-alpha.15
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 +32 -28
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +32 -28
- package/build/esm/index.js.map +1 -1
- package/build/source/__tests__/QueryClientProvider.test.jsx +2 -1
- package/build/source/__tests__/createInfiniteQuery.test.jsx +11 -10
- package/build/source/__tests__/createMutation.test.jsx +19 -18
- package/build/source/__tests__/createQueries.test.jsx +2 -18
- package/build/source/__tests__/createQuery.test.jsx +26 -25
- package/build/source/__tests__/suspense.test.jsx +6 -5
- package/build/source/__tests__/useIsFetching.test.jsx +2 -4
- package/build/source/__tests__/useIsMutating.test.jsx +25 -28
- package/build/source/__tests__/utils.jsx +3 -2
- package/build/source/createBaseQuery.js +20 -12
- package/build/source/createQueries.js +5 -5
- package/build/source/useIsFetching.js +5 -5
- package/build/source/useIsMutating.js +5 -5
- package/build/types/__tests__/utils.d.ts +2 -3
- package/build/types/createBaseQuery.d.ts +1 -1
- package/build/types/createInfiniteQuery.d.ts +3 -2
- package/build/types/createMutation.d.ts +3 -2
- package/build/types/createQueries.d.ts +6 -6
- package/build/types/createQuery.d.ts +5 -5
- package/build/types/types.d.ts +17 -17
- package/build/types/useIsFetching.d.ts +1 -6
- package/build/types/useIsMutating.d.ts +1 -6
- package/build/umd/index.js +1 -1
- package/build/umd/index.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/createInfiniteQuery.test.tsx +20 -18
- package/src/__tests__/createMutation.test.tsx +19 -18
- package/src/__tests__/createQueries.test.tsx +2 -24
- package/src/__tests__/createQuery.test.tsx +27 -25
- package/src/__tests__/suspense.test.tsx +6 -5
- package/src/__tests__/useIsFetching.test.tsx +2 -4
- package/src/__tests__/useIsMutating.test.tsx +32 -40
- package/src/__tests__/utils.tsx +3 -2
- package/src/createBaseQuery.ts +28 -14
- package/src/createInfiniteQuery.ts +4 -3
- package/src/createMutation.ts +4 -3
- package/src/createQueries.ts +11 -10
- package/src/createQuery.ts +8 -11
- package/src/types.ts +17 -17
- package/src/useIsFetching.ts +8 -12
- package/src/useIsMutating.ts +8 -10
|
@@ -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()
|
|
@@ -739,7 +740,7 @@ describe('useQueries', () => {
|
|
|
739
740
|
}
|
|
740
741
|
}
|
|
741
742
|
|
|
742
|
-
const QueriesObserverSpy =
|
|
743
|
+
const QueriesObserverSpy = vi
|
|
743
744
|
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
744
745
|
.mockImplementation((fn) => {
|
|
745
746
|
return new QueriesObserverMock(fn)
|
|
@@ -789,27 +790,4 @@ describe('useQueries', () => {
|
|
|
789
790
|
await sleep(20)
|
|
790
791
|
QueriesObserverSpy.mockRestore()
|
|
791
792
|
})
|
|
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
793
|
})
|
|
@@ -33,6 +33,8 @@ import {
|
|
|
33
33
|
setActTimeout,
|
|
34
34
|
sleep,
|
|
35
35
|
} from './utils'
|
|
36
|
+
import { vi } from 'vitest'
|
|
37
|
+
import type { Mock } from 'vitest'
|
|
36
38
|
|
|
37
39
|
describe('createQuery', () => {
|
|
38
40
|
const queryCache = new QueryCache()
|
|
@@ -491,7 +493,7 @@ describe('createQuery', () => {
|
|
|
491
493
|
it('should call onSuccess after a query has been fetched', async () => {
|
|
492
494
|
const key = queryKey()
|
|
493
495
|
const states: CreateQueryResult<string>[] = []
|
|
494
|
-
const onSuccess =
|
|
496
|
+
const onSuccess = vi.fn()
|
|
495
497
|
|
|
496
498
|
function Page() {
|
|
497
499
|
const state = createQuery(() => ({
|
|
@@ -523,7 +525,7 @@ describe('createQuery', () => {
|
|
|
523
525
|
it('should call onSuccess after a disabled query has been fetched', async () => {
|
|
524
526
|
const key = queryKey()
|
|
525
527
|
const states: CreateQueryResult<string>[] = []
|
|
526
|
-
const onSuccess =
|
|
528
|
+
const onSuccess = vi.fn()
|
|
527
529
|
|
|
528
530
|
function Page() {
|
|
529
531
|
const state = createQuery(() => ({
|
|
@@ -561,7 +563,7 @@ describe('createQuery', () => {
|
|
|
561
563
|
it('should not call onSuccess if a component has unmounted', async () => {
|
|
562
564
|
const key = queryKey()
|
|
563
565
|
const states: CreateQueryResult<string>[] = []
|
|
564
|
-
const onSuccess =
|
|
566
|
+
const onSuccess = vi.fn()
|
|
565
567
|
|
|
566
568
|
function Page() {
|
|
567
569
|
const [show, setShow] = createSignal(true)
|
|
@@ -601,7 +603,7 @@ describe('createQuery', () => {
|
|
|
601
603
|
it('should call onError after a query has been fetched with an error', async () => {
|
|
602
604
|
const key = queryKey()
|
|
603
605
|
const states: CreateQueryResult<unknown>[] = []
|
|
604
|
-
const onError =
|
|
606
|
+
const onError = vi.fn()
|
|
605
607
|
|
|
606
608
|
function Page() {
|
|
607
609
|
const state = createQuery(() => ({
|
|
@@ -632,7 +634,7 @@ describe('createQuery', () => {
|
|
|
632
634
|
|
|
633
635
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
634
636
|
const key = queryKey()
|
|
635
|
-
const onError =
|
|
637
|
+
const onError = vi.fn()
|
|
636
638
|
|
|
637
639
|
function Page() {
|
|
638
640
|
const state = createQuery(() => ({
|
|
@@ -666,7 +668,7 @@ describe('createQuery', () => {
|
|
|
666
668
|
it('should call onSettled after a query has been fetched', async () => {
|
|
667
669
|
const key = queryKey()
|
|
668
670
|
const states: CreateQueryResult<string>[] = []
|
|
669
|
-
const onSettled =
|
|
671
|
+
const onSettled = vi.fn()
|
|
670
672
|
|
|
671
673
|
function Page() {
|
|
672
674
|
const state = createQuery(() => ({
|
|
@@ -696,7 +698,7 @@ describe('createQuery', () => {
|
|
|
696
698
|
it('should call onSettled after a query has been fetched with an error', async () => {
|
|
697
699
|
const key = queryKey()
|
|
698
700
|
const states: CreateQueryResult<string>[] = []
|
|
699
|
-
const onSettled =
|
|
701
|
+
const onSettled = vi.fn()
|
|
700
702
|
|
|
701
703
|
function Page() {
|
|
702
704
|
const state = createQuery(() => ({
|
|
@@ -2452,7 +2454,7 @@ describe('createQuery', () => {
|
|
|
2452
2454
|
|
|
2453
2455
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
2454
2456
|
const key = queryKey()
|
|
2455
|
-
const queryFn =
|
|
2457
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
2456
2458
|
|
|
2457
2459
|
function Page() {
|
|
2458
2460
|
const { data = 'default' } = createQuery(() => ({
|
|
@@ -3296,7 +3298,7 @@ describe('createQuery', () => {
|
|
|
3296
3298
|
it('should retry specified number of times', async () => {
|
|
3297
3299
|
const key = queryKey()
|
|
3298
3300
|
|
|
3299
|
-
const queryFn =
|
|
3301
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3300
3302
|
queryFn.mockImplementation(() => {
|
|
3301
3303
|
return Promise.reject(new Error('Error test Barrett'))
|
|
3302
3304
|
})
|
|
@@ -3337,7 +3339,7 @@ describe('createQuery', () => {
|
|
|
3337
3339
|
it('should not retry if retry function `false`', async () => {
|
|
3338
3340
|
const key = queryKey()
|
|
3339
3341
|
|
|
3340
|
-
const queryFn =
|
|
3342
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3341
3343
|
|
|
3342
3344
|
queryFn.mockImplementationOnce(() => {
|
|
3343
3345
|
return Promise.reject(new Error('Error test Tanner'))
|
|
@@ -3385,7 +3387,7 @@ describe('createQuery', () => {
|
|
|
3385
3387
|
|
|
3386
3388
|
type DelayError = { delay: number }
|
|
3387
3389
|
|
|
3388
|
-
const queryFn =
|
|
3390
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3389
3391
|
queryFn.mockImplementation(() => {
|
|
3390
3392
|
return Promise.reject({ delay: 50 })
|
|
3391
3393
|
})
|
|
@@ -3596,10 +3598,10 @@ describe('createQuery', () => {
|
|
|
3596
3598
|
const key = queryKey()
|
|
3597
3599
|
const states: CreateQueryResult<string>[] = []
|
|
3598
3600
|
|
|
3599
|
-
const queryFn =
|
|
3601
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3600
3602
|
queryFn.mockImplementation(() => 'data')
|
|
3601
3603
|
|
|
3602
|
-
const prefetchQueryFn =
|
|
3604
|
+
const prefetchQueryFn = vi.fn<unknown[], string>()
|
|
3603
3605
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3604
3606
|
|
|
3605
3607
|
await queryClient.prefetchQuery({
|
|
@@ -3633,10 +3635,10 @@ describe('createQuery', () => {
|
|
|
3633
3635
|
it('should not refetch if not stale after a prefetch', async () => {
|
|
3634
3636
|
const key = queryKey()
|
|
3635
3637
|
|
|
3636
|
-
const queryFn =
|
|
3638
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3637
3639
|
queryFn.mockImplementation(() => 'data')
|
|
3638
3640
|
|
|
3639
|
-
const prefetchQueryFn =
|
|
3641
|
+
const prefetchQueryFn = vi.fn<unknown[], Promise<string>>()
|
|
3640
3642
|
prefetchQueryFn.mockImplementation(async () => {
|
|
3641
3643
|
await sleep(10)
|
|
3642
3644
|
return 'not yet...'
|
|
@@ -3909,7 +3911,7 @@ describe('createQuery', () => {
|
|
|
3909
3911
|
|
|
3910
3912
|
it('it should support enabled:false in query object syntax', async () => {
|
|
3911
3913
|
const key = queryKey()
|
|
3912
|
-
const queryFn =
|
|
3914
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3913
3915
|
queryFn.mockImplementation(() => 'data')
|
|
3914
3916
|
|
|
3915
3917
|
function Page() {
|
|
@@ -3981,7 +3983,7 @@ describe('createQuery', () => {
|
|
|
3981
3983
|
))
|
|
3982
3984
|
|
|
3983
3985
|
await waitFor(() => screen.getByText('fetched data'))
|
|
3984
|
-
const setTimeoutSpy =
|
|
3986
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout')
|
|
3985
3987
|
|
|
3986
3988
|
result.unmount()
|
|
3987
3989
|
|
|
@@ -4007,7 +4009,7 @@ describe('createQuery', () => {
|
|
|
4007
4009
|
))
|
|
4008
4010
|
|
|
4009
4011
|
await waitFor(() => screen.getByText('fetched data'))
|
|
4010
|
-
const setTimeoutSpy =
|
|
4012
|
+
const setTimeoutSpy = vi.spyOn(window, 'setTimeout')
|
|
4011
4013
|
|
|
4012
4014
|
result.unmount()
|
|
4013
4015
|
|
|
@@ -4019,8 +4021,8 @@ describe('createQuery', () => {
|
|
|
4019
4021
|
|
|
4020
4022
|
it('should not cause memo churn when data does not change', async () => {
|
|
4021
4023
|
const key = queryKey()
|
|
4022
|
-
const queryFn =
|
|
4023
|
-
const memoFn =
|
|
4024
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4025
|
+
const memoFn = vi.fn()
|
|
4024
4026
|
|
|
4025
4027
|
function Page() {
|
|
4026
4028
|
const result = createQuery(() => ({
|
|
@@ -4255,7 +4257,7 @@ describe('createQuery', () => {
|
|
|
4255
4257
|
it('should refetch if any query instance becomes enabled', async () => {
|
|
4256
4258
|
const key = queryKey()
|
|
4257
4259
|
|
|
4258
|
-
const queryFn =
|
|
4260
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4259
4261
|
|
|
4260
4262
|
function Disabled() {
|
|
4261
4263
|
createQuery(() => ({ queryKey: key, queryFn, enabled: false }))
|
|
@@ -4610,11 +4612,11 @@ describe('createQuery', () => {
|
|
|
4610
4612
|
|
|
4611
4613
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
4612
4614
|
const key = queryKey()
|
|
4613
|
-
let cancelFn:
|
|
4615
|
+
let cancelFn: Mock = vi.fn()
|
|
4614
4616
|
|
|
4615
4617
|
const queryFn = ({ signal }: { signal?: AbortSignal }) => {
|
|
4616
4618
|
const promise = new Promise<string>((resolve, reject) => {
|
|
4617
|
-
cancelFn =
|
|
4619
|
+
cancelFn = vi.fn(() => reject('Cancelled'))
|
|
4618
4620
|
signal?.addEventListener('abort', cancelFn)
|
|
4619
4621
|
sleep(20).then(() => resolve('OK'))
|
|
4620
4622
|
})
|
|
@@ -4958,7 +4960,7 @@ describe('createQuery', () => {
|
|
|
4958
4960
|
})
|
|
4959
4961
|
|
|
4960
4962
|
it('should refetch when changed enabled to true in error state', async () => {
|
|
4961
|
-
const queryFn =
|
|
4963
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
4962
4964
|
queryFn.mockImplementation(async () => {
|
|
4963
4965
|
await sleep(10)
|
|
4964
4966
|
return Promise.reject(new Error('Suspense Error Bingo'))
|
|
@@ -6052,7 +6054,7 @@ describe('createQuery', () => {
|
|
|
6052
6054
|
|
|
6053
6055
|
it('setQueryData - should not call onSuccess callback of active observers', async () => {
|
|
6054
6056
|
const key = queryKey()
|
|
6055
|
-
const onSuccess =
|
|
6057
|
+
const onSuccess = vi.fn()
|
|
6056
6058
|
|
|
6057
6059
|
function Page() {
|
|
6058
6060
|
const state = createQuery(() => ({
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
QueryClientProvider,
|
|
21
21
|
} from '..'
|
|
22
22
|
import { createQueryClient, queryKey, sleep } from './utils'
|
|
23
|
+
import { vi } from 'vitest'
|
|
23
24
|
|
|
24
25
|
describe("useQuery's in Suspense mode", () => {
|
|
25
26
|
const queryCache = new QueryCache()
|
|
@@ -142,7 +143,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
142
143
|
it('should not call the queryFn twice when used in Suspense mode', async () => {
|
|
143
144
|
const key = queryKey()
|
|
144
145
|
|
|
145
|
-
const queryFn =
|
|
146
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
146
147
|
queryFn.mockImplementation(() => {
|
|
147
148
|
sleep(10)
|
|
148
149
|
return 'data'
|
|
@@ -219,7 +220,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
219
220
|
it('should call onSuccess on the first successful call', async () => {
|
|
220
221
|
const key = queryKey()
|
|
221
222
|
|
|
222
|
-
const successFn =
|
|
223
|
+
const successFn = vi.fn()
|
|
223
224
|
|
|
224
225
|
function Page() {
|
|
225
226
|
createQuery(() => ({
|
|
@@ -254,8 +255,8 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
254
255
|
it('should call every onSuccess handler within a suspense boundary', async () => {
|
|
255
256
|
const key = queryKey()
|
|
256
257
|
|
|
257
|
-
const successFn1 =
|
|
258
|
-
const successFn2 =
|
|
258
|
+
const successFn1 = vi.fn()
|
|
259
|
+
const successFn2 = vi.fn()
|
|
259
260
|
|
|
260
261
|
function FirstComponent() {
|
|
261
262
|
createQuery(() => ({
|
|
@@ -733,7 +734,7 @@ describe("useQuery's in Suspense mode", () => {
|
|
|
733
734
|
it('should not call the queryFn when not enabled', async () => {
|
|
734
735
|
const key = queryKey()
|
|
735
736
|
|
|
736
|
-
const queryFn =
|
|
737
|
+
const queryFn = vi.fn<unknown[], Promise<string>>()
|
|
737
738
|
queryFn.mockImplementation(async () => {
|
|
738
739
|
await sleep(10)
|
|
739
740
|
return '23'
|
|
@@ -152,9 +152,7 @@ describe('useIsFetching', () => {
|
|
|
152
152
|
function Page() {
|
|
153
153
|
const [started, setStarted] = createSignal(false)
|
|
154
154
|
const isFetching = useIsFetching(() => ({
|
|
155
|
-
|
|
156
|
-
queryKey: key1,
|
|
157
|
-
},
|
|
155
|
+
queryKey: key1,
|
|
158
156
|
}))
|
|
159
157
|
|
|
160
158
|
createRenderEffect(() => {
|
|
@@ -237,7 +235,7 @@ describe('useIsFetching', () => {
|
|
|
237
235
|
() => queryClient,
|
|
238
236
|
)
|
|
239
237
|
|
|
240
|
-
const isFetching = useIsFetching(() =>
|
|
238
|
+
const isFetching = useIsFetching(undefined, () => queryClient)
|
|
241
239
|
|
|
242
240
|
return (
|
|
243
241
|
<div>
|
|
@@ -6,6 +6,7 @@ import { createEffect, createRenderEffect, createSignal, Show } from 'solid-js'
|
|
|
6
6
|
import { render } from 'solid-testing-library'
|
|
7
7
|
import * as MutationCacheModule from '../../../query-core/src/mutationCache'
|
|
8
8
|
import { setActTimeout } from './utils'
|
|
9
|
+
import { vi } from 'vitest'
|
|
9
10
|
|
|
10
11
|
describe('useIsMutating', () => {
|
|
11
12
|
it('should return the number of fetching mutations', async () => {
|
|
@@ -68,9 +69,7 @@ describe('useIsMutating', () => {
|
|
|
68
69
|
const queryClient = createQueryClient()
|
|
69
70
|
|
|
70
71
|
function IsMutating() {
|
|
71
|
-
const isMutating = useIsMutating(() => ({
|
|
72
|
-
filters: { mutationKey: ['mutation1'] },
|
|
73
|
-
}))
|
|
72
|
+
const isMutating = useIsMutating(() => ({ mutationKey: ['mutation1'] }))
|
|
74
73
|
createRenderEffect(() => {
|
|
75
74
|
isMutatings.push(isMutating())
|
|
76
75
|
})
|
|
@@ -116,10 +115,8 @@ describe('useIsMutating', () => {
|
|
|
116
115
|
|
|
117
116
|
function IsMutating() {
|
|
118
117
|
const isMutating = useIsMutating(() => ({
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
mutation.options.mutationKey?.[0] === 'mutation1',
|
|
122
|
-
},
|
|
118
|
+
predicate: (mutation) =>
|
|
119
|
+
mutation.options.mutationKey?.[0] === 'mutation1',
|
|
123
120
|
}))
|
|
124
121
|
createRenderEffect(() => {
|
|
125
122
|
isMutatings.push(isMutating())
|
|
@@ -161,6 +158,33 @@ describe('useIsMutating', () => {
|
|
|
161
158
|
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]))
|
|
162
159
|
})
|
|
163
160
|
|
|
161
|
+
it('should use provided custom queryClient', async () => {
|
|
162
|
+
const queryClient = createQueryClient()
|
|
163
|
+
function Page() {
|
|
164
|
+
const isMutating = useIsMutating(undefined, () => queryClient)
|
|
165
|
+
const { mutate } = createMutation(
|
|
166
|
+
() => ({
|
|
167
|
+
mutationKey: ['mutation1'],
|
|
168
|
+
mutationFn: async () => {
|
|
169
|
+
await sleep(10)
|
|
170
|
+
return 'data'
|
|
171
|
+
},
|
|
172
|
+
}),
|
|
173
|
+
() => queryClient,
|
|
174
|
+
)
|
|
175
|
+
createEffect(() => {
|
|
176
|
+
mutate()
|
|
177
|
+
})
|
|
178
|
+
return (
|
|
179
|
+
<div>
|
|
180
|
+
<div>mutating: {isMutating}</div>
|
|
181
|
+
</div>
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
render(() => <Page></Page>)
|
|
185
|
+
await waitFor(() => screen.findByText('mutating: 1'))
|
|
186
|
+
})
|
|
187
|
+
|
|
164
188
|
it('should not change state if unmounted', async () => {
|
|
165
189
|
// We have to mock the MutationCache to not unsubscribe
|
|
166
190
|
// the listener when the component is unmounted
|
|
@@ -171,7 +195,7 @@ describe('useIsMutating', () => {
|
|
|
171
195
|
}
|
|
172
196
|
}
|
|
173
197
|
|
|
174
|
-
const MutationCacheSpy =
|
|
198
|
+
const MutationCacheSpy = vi
|
|
175
199
|
.spyOn(MutationCacheModule, 'MutationCache')
|
|
176
200
|
.mockImplementation((fn) => {
|
|
177
201
|
return new MutationCacheMock(fn)
|
|
@@ -221,36 +245,4 @@ describe('useIsMutating', () => {
|
|
|
221
245
|
await sleep(20)
|
|
222
246
|
MutationCacheSpy.mockRestore()
|
|
223
247
|
})
|
|
224
|
-
|
|
225
|
-
it('should use provided custom queryClient', async () => {
|
|
226
|
-
const queryClient = createQueryClient()
|
|
227
|
-
|
|
228
|
-
function Page() {
|
|
229
|
-
const isMutating = useIsMutating(() => ({ queryClient }))
|
|
230
|
-
const { mutate } = createMutation(
|
|
231
|
-
() => ({
|
|
232
|
-
mutationKey: ['mutation1'],
|
|
233
|
-
mutationFn: async () => {
|
|
234
|
-
await sleep(10)
|
|
235
|
-
return 'data'
|
|
236
|
-
},
|
|
237
|
-
}),
|
|
238
|
-
() => queryClient,
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
createEffect(() => {
|
|
242
|
-
mutate()
|
|
243
|
-
})
|
|
244
|
-
|
|
245
|
-
return (
|
|
246
|
-
<div>
|
|
247
|
-
<div>mutating: {isMutating}</div>
|
|
248
|
-
</div>
|
|
249
|
-
)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
render(() => <Page></Page>)
|
|
253
|
-
|
|
254
|
-
await waitFor(() => screen.findByText('mutating: 1'))
|
|
255
|
-
})
|
|
256
248
|
})
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type { QueryClientConfig } from '@tanstack/query-core'
|
|
|
2
2
|
import { QueryClient } from '@tanstack/query-core'
|
|
3
3
|
import type { ParentProps } from 'solid-js'
|
|
4
4
|
import { createEffect, createSignal, onCleanup, Show } from 'solid-js'
|
|
5
|
+
import { vi } from 'vitest'
|
|
5
6
|
|
|
6
7
|
let queryKeyCount = 0
|
|
7
8
|
export function queryKey(): Array<string> {
|
|
@@ -34,11 +35,11 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
37
|
-
return
|
|
38
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export function mockNavigatorOnLine(value: boolean) {
|
|
41
|
-
return
|
|
42
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export function sleep(timeout: number): Promise<void> {
|
package/src/createBaseQuery.ts
CHANGED
|
@@ -18,7 +18,6 @@ import {
|
|
|
18
18
|
createResource,
|
|
19
19
|
on,
|
|
20
20
|
onCleanup,
|
|
21
|
-
onMount,
|
|
22
21
|
} from 'solid-js'
|
|
23
22
|
import { createStore, unwrap } from 'solid-js/store'
|
|
24
23
|
import { useQueryClient } from './QueryClientProvider'
|
|
@@ -37,7 +36,7 @@ export function createBaseQuery<
|
|
|
37
36
|
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
|
|
38
37
|
>,
|
|
39
38
|
Observer: typeof QueryObserver,
|
|
40
|
-
queryClient?:
|
|
39
|
+
queryClient?: Accessor<QueryClient>,
|
|
41
40
|
) {
|
|
42
41
|
const client = createMemo(() => useQueryClient(queryClient?.()))
|
|
43
42
|
|
|
@@ -64,7 +63,20 @@ export function createBaseQuery<
|
|
|
64
63
|
) => {
|
|
65
64
|
return observer.subscribe((result) => {
|
|
66
65
|
notifyManager.batchCalls(() => {
|
|
67
|
-
const
|
|
66
|
+
const query = observer.getCurrentQuery()
|
|
67
|
+
const unwrappedResult = {
|
|
68
|
+
...unwrap(result),
|
|
69
|
+
|
|
70
|
+
// hydrate() expects a QueryState object, which is similar but not
|
|
71
|
+
// quite the same as a QueryObserverResult object. Thus, for now, we're
|
|
72
|
+
// copying over the missing properties from state in order to support hydration
|
|
73
|
+
dataUpdateCount: query.state.dataUpdateCount,
|
|
74
|
+
fetchFailureCount: query.state.fetchFailureCount,
|
|
75
|
+
fetchFailureReason: query.state.fetchFailureReason,
|
|
76
|
+
fetchMeta: query.state.fetchMeta,
|
|
77
|
+
isInvalidated: query.state.isInvalidated,
|
|
78
|
+
}
|
|
79
|
+
|
|
68
80
|
if (unwrappedResult.isError) {
|
|
69
81
|
if (process.env['NODE_ENV'] === 'development') {
|
|
70
82
|
console.error(unwrappedResult.error)
|
|
@@ -178,13 +190,17 @@ export function createBaseQuery<
|
|
|
178
190
|
}
|
|
179
191
|
})
|
|
180
192
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
193
|
+
createComputed(
|
|
194
|
+
on(
|
|
195
|
+
() => client().defaultQueryOptions(options()),
|
|
196
|
+
() => observer.setOptions(client().defaultQueryOptions(options())),
|
|
197
|
+
{
|
|
198
|
+
// Defer because we don't need to trigger on first render
|
|
199
|
+
// This only cares about changes to options after the observer is created
|
|
200
|
+
defer: true,
|
|
201
|
+
},
|
|
202
|
+
),
|
|
203
|
+
)
|
|
188
204
|
|
|
189
205
|
createComputed(
|
|
190
206
|
on(
|
|
@@ -209,10 +225,8 @@ export function createBaseQuery<
|
|
|
209
225
|
target: QueryObserverResult<TData, TError>,
|
|
210
226
|
prop: keyof QueryObserverResult<TData, TError>,
|
|
211
227
|
): any {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
return Reflect.get(target, prop)
|
|
228
|
+
const val = queryResource()?.[prop]
|
|
229
|
+
return val !== undefined ? val : Reflect.get(target, prop)
|
|
216
230
|
},
|
|
217
231
|
}
|
|
218
232
|
|
|
@@ -2,7 +2,7 @@ import type {
|
|
|
2
2
|
QueryObserver,
|
|
3
3
|
QueryKey,
|
|
4
4
|
QueryClient,
|
|
5
|
-
|
|
5
|
+
DefaultError,
|
|
6
6
|
InfiniteData,
|
|
7
7
|
} from '@tanstack/query-core'
|
|
8
8
|
import { InfiniteQueryObserver } from '@tanstack/query-core'
|
|
@@ -12,10 +12,11 @@ import type {
|
|
|
12
12
|
} from './types'
|
|
13
13
|
import { createBaseQuery } from './createBaseQuery'
|
|
14
14
|
import { createMemo } from 'solid-js'
|
|
15
|
+
import type { Accessor } from 'solid-js'
|
|
15
16
|
|
|
16
17
|
export function createInfiniteQuery<
|
|
17
18
|
TQueryFnData,
|
|
18
|
-
TError =
|
|
19
|
+
TError = DefaultError,
|
|
19
20
|
TData = InfiniteData<TQueryFnData>,
|
|
20
21
|
TQueryKey extends QueryKey = QueryKey,
|
|
21
22
|
TPageParam = unknown,
|
|
@@ -27,7 +28,7 @@ export function createInfiniteQuery<
|
|
|
27
28
|
TQueryKey,
|
|
28
29
|
TPageParam
|
|
29
30
|
>,
|
|
30
|
-
queryClient?:
|
|
31
|
+
queryClient?: Accessor<QueryClient>,
|
|
31
32
|
): CreateInfiniteQueryResult<TData, TError> {
|
|
32
33
|
return createBaseQuery(
|
|
33
34
|
createMemo(() => options()),
|