@tanstack/react-query 5.0.0-alpha.4 → 5.0.0-alpha.6
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/lib/__tests__/ssr.test.d.ts +0 -3
- package/build/lib/__tests__/utils.d.ts +2 -3
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -5
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/useQueries.esm.js +10 -7
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +10 -7
- package/build/lib/useQueries.js.map +1 -1
- package/build/umd/index.development.js +52 -51
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/HydrationBoundary.test.tsx +4 -3
- package/src/__tests__/QueryClientProvider.test.tsx +2 -1
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +12 -11
- package/src/__tests__/ssr-hydration.test.tsx +11 -10
- package/src/__tests__/ssr.test.tsx +4 -7
- package/src/__tests__/suspense.test.tsx +14 -13
- package/src/__tests__/useInfiniteQuery.test.tsx +18 -16
- package/src/__tests__/useInfiniteQuery.type.test.tsx +110 -2
- package/src/__tests__/useMutation.test.tsx +21 -20
- package/src/__tests__/useMutationState.test.tsx +1 -58
- package/src/__tests__/useQueries.test.tsx +13 -73
- package/src/__tests__/useQuery.test.tsx +30 -36
- package/src/__tests__/utils.tsx +3 -2
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
setActTimeout,
|
|
14
14
|
sleep,
|
|
15
15
|
} from './utils'
|
|
16
|
+
import { vi } from 'vitest'
|
|
16
17
|
|
|
17
18
|
describe('useMutation', () => {
|
|
18
19
|
const queryCache = new QueryCache()
|
|
@@ -95,8 +96,8 @@ describe('useMutation', () => {
|
|
|
95
96
|
|
|
96
97
|
it('should be able to call `onSuccess` and `onSettled` after each successful mutate', async () => {
|
|
97
98
|
let count = 0
|
|
98
|
-
const onSuccessMock =
|
|
99
|
-
const onSettledMock =
|
|
99
|
+
const onSuccessMock = vi.fn()
|
|
100
|
+
const onSettledMock = vi.fn()
|
|
100
101
|
|
|
101
102
|
function Page() {
|
|
102
103
|
const { mutate } = useMutation({
|
|
@@ -151,7 +152,7 @@ describe('useMutation', () => {
|
|
|
151
152
|
let count = 0
|
|
152
153
|
type Value = { count: number }
|
|
153
154
|
|
|
154
|
-
const mutateFn =
|
|
155
|
+
const mutateFn = vi.fn<[value: Value], Promise<Value>>()
|
|
155
156
|
|
|
156
157
|
mutateFn.mockImplementationOnce(() => {
|
|
157
158
|
return Promise.reject(new Error('Error test Jonas'))
|
|
@@ -197,8 +198,8 @@ describe('useMutation', () => {
|
|
|
197
198
|
})
|
|
198
199
|
|
|
199
200
|
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
|
|
200
|
-
const onErrorMock =
|
|
201
|
-
const onSettledMock =
|
|
201
|
+
const onErrorMock = vi.fn()
|
|
202
|
+
const onSettledMock = vi.fn()
|
|
202
203
|
let count = 0
|
|
203
204
|
|
|
204
205
|
function Page() {
|
|
@@ -488,7 +489,7 @@ describe('useMutation', () => {
|
|
|
488
489
|
|
|
489
490
|
it('should call onMutate even if paused', async () => {
|
|
490
491
|
const onlineMock = mockNavigatorOnLine(false)
|
|
491
|
-
const onMutate =
|
|
492
|
+
const onMutate = vi.fn()
|
|
492
493
|
let count = 0
|
|
493
494
|
|
|
494
495
|
function Page() {
|
|
@@ -688,7 +689,7 @@ describe('useMutation', () => {
|
|
|
688
689
|
})
|
|
689
690
|
|
|
690
691
|
it('should be able to throw an error when throwErrors is set to true', async () => {
|
|
691
|
-
const consoleMock =
|
|
692
|
+
const consoleMock = vi
|
|
692
693
|
.spyOn(console, 'error')
|
|
693
694
|
.mockImplementation(() => undefined)
|
|
694
695
|
function Page() {
|
|
@@ -735,7 +736,7 @@ describe('useMutation', () => {
|
|
|
735
736
|
})
|
|
736
737
|
|
|
737
738
|
it('should be able to throw an error when throwErrors is a function that returns true', async () => {
|
|
738
|
-
const consoleMock =
|
|
739
|
+
const consoleMock = vi
|
|
739
740
|
.spyOn(console, 'error')
|
|
740
741
|
.mockImplementation(() => undefined)
|
|
741
742
|
let boundary = false
|
|
@@ -788,8 +789,8 @@ describe('useMutation', () => {
|
|
|
788
789
|
})
|
|
789
790
|
|
|
790
791
|
it('should pass meta to mutation', async () => {
|
|
791
|
-
const errorMock =
|
|
792
|
-
const successMock =
|
|
792
|
+
const errorMock = vi.fn()
|
|
793
|
+
const successMock = vi.fn()
|
|
793
794
|
|
|
794
795
|
const queryClientMutationMeta = createQueryClient({
|
|
795
796
|
mutationCache: new MutationCache({
|
|
@@ -847,10 +848,10 @@ describe('useMutation', () => {
|
|
|
847
848
|
})
|
|
848
849
|
|
|
849
850
|
it('should call cache callbacks when unmounted', async () => {
|
|
850
|
-
const onSuccess =
|
|
851
|
-
const onSuccessMutate =
|
|
852
|
-
const onSettled =
|
|
853
|
-
const onSettledMutate =
|
|
851
|
+
const onSuccess = vi.fn()
|
|
852
|
+
const onSuccessMutate = vi.fn()
|
|
853
|
+
const onSettled = vi.fn()
|
|
854
|
+
const onSettledMutate = vi.fn()
|
|
854
855
|
const mutationKey = queryKey()
|
|
855
856
|
let count = 0
|
|
856
857
|
|
|
@@ -919,10 +920,10 @@ describe('useMutation', () => {
|
|
|
919
920
|
})
|
|
920
921
|
|
|
921
922
|
it('should call mutate callbacks only for the last observer', async () => {
|
|
922
|
-
const onSuccess =
|
|
923
|
-
const onSuccessMutate =
|
|
924
|
-
const onSettled =
|
|
925
|
-
const onSettledMutate =
|
|
923
|
+
const onSuccess = vi.fn()
|
|
924
|
+
const onSuccessMutate = vi.fn()
|
|
925
|
+
const onSettled = vi.fn()
|
|
926
|
+
const onSettledMutate = vi.fn()
|
|
926
927
|
let count = 0
|
|
927
928
|
|
|
928
929
|
function Page() {
|
|
@@ -1008,7 +1009,7 @@ describe('useMutation', () => {
|
|
|
1008
1009
|
|
|
1009
1010
|
it('should go to error state if onSuccess callback errors', async () => {
|
|
1010
1011
|
const error = new Error('error from onSuccess')
|
|
1011
|
-
const onError =
|
|
1012
|
+
const onError = vi.fn()
|
|
1012
1013
|
|
|
1013
1014
|
function Page() {
|
|
1014
1015
|
const mutation = useMutation({
|
|
@@ -1076,7 +1077,7 @@ describe('useMutation', () => {
|
|
|
1076
1077
|
it('should go to error state if onSettled callback errors', async () => {
|
|
1077
1078
|
const error = new Error('error from onSettled')
|
|
1078
1079
|
const mutateFnError = new Error('mutateFnError')
|
|
1079
|
-
const onError =
|
|
1080
|
+
const onError = vi.fn()
|
|
1080
1081
|
|
|
1081
1082
|
function Page() {
|
|
1082
1083
|
const mutation = useMutation({
|
|
@@ -8,8 +8,6 @@ import {
|
|
|
8
8
|
setActTimeout,
|
|
9
9
|
sleep,
|
|
10
10
|
} from './utils'
|
|
11
|
-
import * as MutationCacheModule from '../../../query-core/src/mutationCache'
|
|
12
|
-
import { screen } from 'solid-testing-library'
|
|
13
11
|
|
|
14
12
|
describe('useIsMutating', () => {
|
|
15
13
|
it('should return the number of fetching mutations', async () => {
|
|
@@ -140,61 +138,6 @@ describe('useIsMutating', () => {
|
|
|
140
138
|
await waitFor(() => expect(isMutatings).toEqual([0, 1, 0]))
|
|
141
139
|
})
|
|
142
140
|
|
|
143
|
-
it('should not change state if unmounted', async () => {
|
|
144
|
-
// We have to mock the MutationCache to not unsubscribe
|
|
145
|
-
// the listener when the component is unmounted
|
|
146
|
-
class MutationCacheMock extends MutationCacheModule.MutationCache {
|
|
147
|
-
subscribe(listener: any) {
|
|
148
|
-
super.subscribe(listener)
|
|
149
|
-
return () => void 0
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const MutationCacheSpy = jest
|
|
154
|
-
.spyOn(MutationCacheModule, 'MutationCache')
|
|
155
|
-
.mockImplementation((fn) => {
|
|
156
|
-
return new MutationCacheMock(fn)
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
const queryClient = createQueryClient()
|
|
160
|
-
|
|
161
|
-
function IsMutating() {
|
|
162
|
-
useIsMutating()
|
|
163
|
-
return null
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function Page() {
|
|
167
|
-
const [mounted, setMounted] = React.useState(true)
|
|
168
|
-
const { mutate: mutate1 } = useMutation({
|
|
169
|
-
mutationKey: ['mutation1'],
|
|
170
|
-
mutationFn: async () => {
|
|
171
|
-
await sleep(10)
|
|
172
|
-
return 'data'
|
|
173
|
-
},
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
React.useEffect(() => {
|
|
177
|
-
mutate1()
|
|
178
|
-
}, [mutate1])
|
|
179
|
-
|
|
180
|
-
return (
|
|
181
|
-
<div>
|
|
182
|
-
<button onClick={() => setMounted(false)}>unmount</button>
|
|
183
|
-
{mounted && <IsMutating />}
|
|
184
|
-
</div>
|
|
185
|
-
)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const { getByText } = renderWithClient(queryClient, <Page />)
|
|
189
|
-
fireEvent.click(getByText('unmount'))
|
|
190
|
-
|
|
191
|
-
// Should not display the console error
|
|
192
|
-
// "Warning: Can't perform a React state update on an unmounted component"
|
|
193
|
-
|
|
194
|
-
await sleep(20)
|
|
195
|
-
MutationCacheSpy.mockRestore()
|
|
196
|
-
})
|
|
197
|
-
|
|
198
141
|
it('should use provided custom queryClient', async () => {
|
|
199
142
|
const queryClient = createQueryClient()
|
|
200
143
|
|
|
@@ -275,7 +218,7 @@ describe('useMutationState', () => {
|
|
|
275
218
|
|
|
276
219
|
await waitFor(() => rendered.getByText('data: null'))
|
|
277
220
|
|
|
278
|
-
fireEvent.click(
|
|
221
|
+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
|
|
279
222
|
|
|
280
223
|
await waitFor(() => rendered.getByText('data: data1'))
|
|
281
224
|
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { render, waitFor } from '@testing-library/react'
|
|
2
2
|
import * as React from 'react'
|
|
3
3
|
import { ErrorBoundary } from 'react-error-boundary'
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
|
|
5
|
+
import type { QueryFunctionContext } from '@tanstack/query-core'
|
|
6
|
+
import { vi } from 'vitest'
|
|
7
|
+
import type {
|
|
8
|
+
QueryFunction,
|
|
9
|
+
QueryKey,
|
|
10
|
+
QueryObserverResult,
|
|
11
|
+
UseQueryOptions,
|
|
12
|
+
UseQueryResult,
|
|
13
|
+
} from '..'
|
|
14
|
+
import { QueryCache, useQueries } from '..'
|
|
7
15
|
import {
|
|
8
16
|
createQueryClient,
|
|
9
17
|
expectType,
|
|
@@ -12,15 +20,6 @@ import {
|
|
|
12
20
|
renderWithClient,
|
|
13
21
|
sleep,
|
|
14
22
|
} from './utils'
|
|
15
|
-
import type {
|
|
16
|
-
QueryFunction,
|
|
17
|
-
QueryKey,
|
|
18
|
-
QueryObserverResult,
|
|
19
|
-
UseQueryOptions,
|
|
20
|
-
UseQueryResult,
|
|
21
|
-
} from '..'
|
|
22
|
-
import { QueriesObserver, QueryCache, useQueries } from '..'
|
|
23
|
-
import type { QueryFunctionContext } from '@tanstack/query-core'
|
|
24
23
|
|
|
25
24
|
describe('useQueries', () => {
|
|
26
25
|
const queryCache = new QueryCache()
|
|
@@ -716,67 +715,8 @@ describe('useQueries', () => {
|
|
|
716
715
|
}
|
|
717
716
|
})
|
|
718
717
|
|
|
719
|
-
it('should not change state if unmounted', async () => {
|
|
720
|
-
const key1 = queryKey()
|
|
721
|
-
|
|
722
|
-
// We have to mock the QueriesObserver to not unsubscribe
|
|
723
|
-
// the listener when the component is unmounted
|
|
724
|
-
class QueriesObserverMock extends QueriesObserver {
|
|
725
|
-
subscribe(listener: any) {
|
|
726
|
-
super.subscribe(listener)
|
|
727
|
-
return () => void 0
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
const QueriesObserverSpy = jest
|
|
732
|
-
.spyOn(QueriesObserverModule, 'QueriesObserver')
|
|
733
|
-
.mockImplementation((fn) => {
|
|
734
|
-
return new QueriesObserverMock(fn)
|
|
735
|
-
})
|
|
736
|
-
|
|
737
|
-
function Queries() {
|
|
738
|
-
useQueries({
|
|
739
|
-
queries: [
|
|
740
|
-
{
|
|
741
|
-
queryKey: key1,
|
|
742
|
-
queryFn: async () => {
|
|
743
|
-
await sleep(10)
|
|
744
|
-
return 1
|
|
745
|
-
},
|
|
746
|
-
},
|
|
747
|
-
],
|
|
748
|
-
})
|
|
749
|
-
|
|
750
|
-
return (
|
|
751
|
-
<div>
|
|
752
|
-
<span>queries</span>
|
|
753
|
-
</div>
|
|
754
|
-
)
|
|
755
|
-
}
|
|
756
|
-
|
|
757
|
-
function Page() {
|
|
758
|
-
const [mounted, setMounted] = React.useState(true)
|
|
759
|
-
|
|
760
|
-
return (
|
|
761
|
-
<div>
|
|
762
|
-
<button onClick={() => setMounted(false)}>unmount</button>
|
|
763
|
-
{mounted && <Queries />}
|
|
764
|
-
</div>
|
|
765
|
-
)
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
const { getByText } = renderWithClient(queryClient, <Page />)
|
|
769
|
-
fireEvent.click(getByText('unmount'))
|
|
770
|
-
|
|
771
|
-
// Should not display the console error
|
|
772
|
-
// "Warning: Can't perform a React state update on an unmounted component"
|
|
773
|
-
|
|
774
|
-
await sleep(20)
|
|
775
|
-
QueriesObserverSpy.mockRestore()
|
|
776
|
-
})
|
|
777
|
-
|
|
778
718
|
it("should throw error if in one of queries' queryFn throws and throwErrors is in use", async () => {
|
|
779
|
-
const consoleMock =
|
|
719
|
+
const consoleMock = vi
|
|
780
720
|
.spyOn(console, 'error')
|
|
781
721
|
.mockImplementation(() => undefined)
|
|
782
722
|
const key1 = queryKey()
|
|
@@ -841,7 +781,7 @@ describe('useQueries', () => {
|
|
|
841
781
|
})
|
|
842
782
|
|
|
843
783
|
it("should throw error if in one of queries' queryFn throws and throwErrors function resolves to true", async () => {
|
|
844
|
-
const consoleMock =
|
|
784
|
+
const consoleMock = vi
|
|
845
785
|
.spyOn(console, 'error')
|
|
846
786
|
.mockImplementation(() => undefined)
|
|
847
787
|
const key1 = queryKey()
|
|
@@ -21,6 +21,8 @@ import type {
|
|
|
21
21
|
} from '..'
|
|
22
22
|
import { QueryCache, useQuery, keepPreviousData } from '..'
|
|
23
23
|
import { ErrorBoundary } from 'react-error-boundary'
|
|
24
|
+
import { vi } from 'vitest'
|
|
25
|
+
import type { Mock } from 'vitest'
|
|
24
26
|
|
|
25
27
|
describe('useQuery', () => {
|
|
26
28
|
const queryCache = new QueryCache()
|
|
@@ -451,7 +453,7 @@ describe('useQuery', () => {
|
|
|
451
453
|
it('should call onSuccess after a query has been fetched', async () => {
|
|
452
454
|
const key = queryKey()
|
|
453
455
|
const states: UseQueryResult<string>[] = []
|
|
454
|
-
const onSuccess =
|
|
456
|
+
const onSuccess = vi.fn()
|
|
455
457
|
|
|
456
458
|
function Page() {
|
|
457
459
|
const state = useQuery({
|
|
@@ -477,7 +479,7 @@ describe('useQuery', () => {
|
|
|
477
479
|
it('should call onSuccess after a query has been refetched', async () => {
|
|
478
480
|
const key = queryKey()
|
|
479
481
|
const states: UseQueryResult<string>[] = []
|
|
480
|
-
const onSuccess =
|
|
482
|
+
const onSuccess = vi.fn()
|
|
481
483
|
let count = 0
|
|
482
484
|
|
|
483
485
|
function Page() {
|
|
@@ -515,7 +517,7 @@ describe('useQuery', () => {
|
|
|
515
517
|
it('should call onSuccess after a disabled query has been fetched', async () => {
|
|
516
518
|
const key = queryKey()
|
|
517
519
|
const states: UseQueryResult<string>[] = []
|
|
518
|
-
const onSuccess =
|
|
520
|
+
const onSuccess = vi.fn()
|
|
519
521
|
|
|
520
522
|
function Page() {
|
|
521
523
|
const state = useQuery({
|
|
@@ -554,7 +556,7 @@ describe('useQuery', () => {
|
|
|
554
556
|
it('should not call onSuccess if a component has unmounted', async () => {
|
|
555
557
|
const key = queryKey()
|
|
556
558
|
const states: UseQueryResult<string>[] = []
|
|
557
|
-
const onSuccess =
|
|
559
|
+
const onSuccess = vi.fn()
|
|
558
560
|
|
|
559
561
|
function Page() {
|
|
560
562
|
const [show, setShow] = React.useState(true)
|
|
@@ -589,7 +591,7 @@ describe('useQuery', () => {
|
|
|
589
591
|
it('should call onError after a query has been fetched with an error', async () => {
|
|
590
592
|
const key = queryKey()
|
|
591
593
|
const states: UseQueryResult<unknown>[] = []
|
|
592
|
-
const onError =
|
|
594
|
+
const onError = vi.fn()
|
|
593
595
|
|
|
594
596
|
function Page() {
|
|
595
597
|
const state = useQuery<unknown>({
|
|
@@ -612,7 +614,7 @@ describe('useQuery', () => {
|
|
|
612
614
|
|
|
613
615
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
614
616
|
const key = queryKey()
|
|
615
|
-
const onError =
|
|
617
|
+
const onError = vi.fn()
|
|
616
618
|
|
|
617
619
|
function Page() {
|
|
618
620
|
const { status, fetchStatus } = useQuery({
|
|
@@ -646,7 +648,7 @@ describe('useQuery', () => {
|
|
|
646
648
|
it('should call onSettled after a query has been fetched', async () => {
|
|
647
649
|
const key = queryKey()
|
|
648
650
|
const states: UseQueryResult<string>[] = []
|
|
649
|
-
const onSettled =
|
|
651
|
+
const onSettled = vi.fn()
|
|
650
652
|
|
|
651
653
|
function Page() {
|
|
652
654
|
const state = useQuery({
|
|
@@ -672,7 +674,7 @@ describe('useQuery', () => {
|
|
|
672
674
|
|
|
673
675
|
it('should call onSettled after a query has been fetched with an error', async () => {
|
|
674
676
|
const key = queryKey()
|
|
675
|
-
const onSettled =
|
|
677
|
+
const onSettled = vi.fn()
|
|
676
678
|
const error = new Error('error')
|
|
677
679
|
|
|
678
680
|
function Page() {
|
|
@@ -2584,7 +2586,7 @@ describe('useQuery', () => {
|
|
|
2584
2586
|
|
|
2585
2587
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
2586
2588
|
const key = queryKey()
|
|
2587
|
-
const queryFn =
|
|
2589
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
2588
2590
|
|
|
2589
2591
|
function Page() {
|
|
2590
2592
|
const { data = 'default' } = useQuery({
|
|
@@ -2895,7 +2897,7 @@ describe('useQuery', () => {
|
|
|
2895
2897
|
})
|
|
2896
2898
|
|
|
2897
2899
|
it('should throw error if queryFn throws and throwErrors is in use', async () => {
|
|
2898
|
-
const consoleMock =
|
|
2900
|
+
const consoleMock = vi
|
|
2899
2901
|
.spyOn(console, 'error')
|
|
2900
2902
|
.mockImplementation(() => undefined)
|
|
2901
2903
|
const key = queryKey()
|
|
@@ -2987,7 +2989,7 @@ describe('useQuery', () => {
|
|
|
2987
2989
|
})
|
|
2988
2990
|
|
|
2989
2991
|
it('should throw error instead of setting status when error should be thrown', async () => {
|
|
2990
|
-
const consoleMock =
|
|
2992
|
+
const consoleMock = vi
|
|
2991
2993
|
.spyOn(console, 'error')
|
|
2992
2994
|
.mockImplementation(() => undefined)
|
|
2993
2995
|
|
|
@@ -3355,7 +3357,7 @@ describe('useQuery', () => {
|
|
|
3355
3357
|
it('should retry specified number of times', async () => {
|
|
3356
3358
|
const key = queryKey()
|
|
3357
3359
|
|
|
3358
|
-
const queryFn =
|
|
3360
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3359
3361
|
queryFn.mockImplementation(() => {
|
|
3360
3362
|
return Promise.reject(new Error('Error test Barrett'))
|
|
3361
3363
|
})
|
|
@@ -3392,7 +3394,7 @@ describe('useQuery', () => {
|
|
|
3392
3394
|
it('should not retry if retry function `false`', async () => {
|
|
3393
3395
|
const key = queryKey()
|
|
3394
3396
|
|
|
3395
|
-
const queryFn =
|
|
3397
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3396
3398
|
|
|
3397
3399
|
queryFn.mockImplementationOnce(() => {
|
|
3398
3400
|
return Promise.reject(new Error('Error test Tanner'))
|
|
@@ -3438,7 +3440,7 @@ describe('useQuery', () => {
|
|
|
3438
3440
|
|
|
3439
3441
|
type DelayError = { delay: number }
|
|
3440
3442
|
|
|
3441
|
-
const queryFn =
|
|
3443
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
3442
3444
|
queryFn.mockImplementation(() => {
|
|
3443
3445
|
return Promise.reject({ delay: 50 })
|
|
3444
3446
|
})
|
|
@@ -3631,10 +3633,10 @@ describe('useQuery', () => {
|
|
|
3631
3633
|
const key = queryKey()
|
|
3632
3634
|
const states: UseQueryResult<string>[] = []
|
|
3633
3635
|
|
|
3634
|
-
const queryFn =
|
|
3636
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3635
3637
|
queryFn.mockImplementation(() => 'data')
|
|
3636
3638
|
|
|
3637
|
-
const prefetchQueryFn =
|
|
3639
|
+
const prefetchQueryFn = vi.fn<unknown[], string>()
|
|
3638
3640
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3639
3641
|
|
|
3640
3642
|
await queryClient.prefetchQuery({
|
|
@@ -3662,10 +3664,10 @@ describe('useQuery', () => {
|
|
|
3662
3664
|
it('should not refetch if not stale after a prefetch', async () => {
|
|
3663
3665
|
const key = queryKey()
|
|
3664
3666
|
|
|
3665
|
-
const queryFn =
|
|
3667
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3666
3668
|
queryFn.mockImplementation(() => 'data')
|
|
3667
3669
|
|
|
3668
|
-
const prefetchQueryFn =
|
|
3670
|
+
const prefetchQueryFn = vi.fn<unknown[], Promise<string>>()
|
|
3669
3671
|
prefetchQueryFn.mockImplementation(async () => {
|
|
3670
3672
|
await sleep(10)
|
|
3671
3673
|
return 'not yet...'
|
|
@@ -3971,7 +3973,7 @@ describe('useQuery', () => {
|
|
|
3971
3973
|
|
|
3972
3974
|
it('it should support enabled:false in query object syntax', async () => {
|
|
3973
3975
|
const key = queryKey()
|
|
3974
|
-
const queryFn =
|
|
3976
|
+
const queryFn = vi.fn<unknown[], string>()
|
|
3975
3977
|
queryFn.mockImplementation(() => 'data')
|
|
3976
3978
|
|
|
3977
3979
|
function Page() {
|
|
@@ -4030,15 +4032,11 @@ describe('useQuery', () => {
|
|
|
4030
4032
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
4031
4033
|
|
|
4032
4034
|
await waitFor(() => rendered.getByText('fetched data'))
|
|
4033
|
-
|
|
4034
|
-
legacyFakeTimers: true,
|
|
4035
|
-
})
|
|
4036
|
-
const setTimeoutSpy = jest.spyOn(globalThis.window, 'setTimeout')
|
|
4035
|
+
const setTimeoutSpy = vi.spyOn(globalThis.window, 'setTimeout')
|
|
4037
4036
|
|
|
4038
4037
|
rendered.unmount()
|
|
4039
4038
|
|
|
4040
4039
|
expect(setTimeoutSpy).not.toHaveBeenCalled()
|
|
4041
|
-
jest.useRealTimers()
|
|
4042
4040
|
})
|
|
4043
4041
|
|
|
4044
4042
|
test('should schedule garbage collection, if gcTimeout is not set to infinity', async () => {
|
|
@@ -4057,10 +4055,7 @@ describe('useQuery', () => {
|
|
|
4057
4055
|
|
|
4058
4056
|
await waitFor(() => rendered.getByText('fetched data'))
|
|
4059
4057
|
|
|
4060
|
-
|
|
4061
|
-
legacyFakeTimers: true,
|
|
4062
|
-
})
|
|
4063
|
-
const setTimeoutSpy = jest.spyOn(globalThis.window, 'setTimeout')
|
|
4058
|
+
const setTimeoutSpy = vi.spyOn(globalThis.window, 'setTimeout')
|
|
4064
4059
|
|
|
4065
4060
|
rendered.unmount()
|
|
4066
4061
|
|
|
@@ -4068,13 +4063,12 @@ describe('useQuery', () => {
|
|
|
4068
4063
|
expect.any(Function),
|
|
4069
4064
|
1000 * 60 * 10,
|
|
4070
4065
|
)
|
|
4071
|
-
jest.useRealTimers()
|
|
4072
4066
|
})
|
|
4073
4067
|
|
|
4074
4068
|
it('should not cause memo churn when data does not change', async () => {
|
|
4075
4069
|
const key = queryKey()
|
|
4076
|
-
const queryFn =
|
|
4077
|
-
const memoFn =
|
|
4070
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4071
|
+
const memoFn = vi.fn()
|
|
4078
4072
|
|
|
4079
4073
|
function Page() {
|
|
4080
4074
|
const result = useQuery({
|
|
@@ -4281,7 +4275,7 @@ describe('useQuery', () => {
|
|
|
4281
4275
|
it('should refetch if any query instance becomes enabled', async () => {
|
|
4282
4276
|
const key = queryKey()
|
|
4283
4277
|
|
|
4284
|
-
const queryFn =
|
|
4278
|
+
const queryFn = vi.fn<unknown[], string>().mockReturnValue('data')
|
|
4285
4279
|
|
|
4286
4280
|
function Disabled() {
|
|
4287
4281
|
useQuery({ queryKey: key, queryFn, enabled: false })
|
|
@@ -4640,11 +4634,11 @@ describe('useQuery', () => {
|
|
|
4640
4634
|
|
|
4641
4635
|
it('should cancel the query function when there are no more subscriptions', async () => {
|
|
4642
4636
|
const key = queryKey()
|
|
4643
|
-
let cancelFn:
|
|
4637
|
+
let cancelFn: Mock = vi.fn()
|
|
4644
4638
|
|
|
4645
4639
|
const queryFn = ({ signal }: { signal?: AbortSignal }) => {
|
|
4646
4640
|
const promise = new Promise<string>((resolve, reject) => {
|
|
4647
|
-
cancelFn =
|
|
4641
|
+
cancelFn = vi.fn(() => reject('Cancelled'))
|
|
4648
4642
|
signal?.addEventListener('abort', cancelFn)
|
|
4649
4643
|
sleep(20).then(() => resolve('OK'))
|
|
4650
4644
|
})
|
|
@@ -4963,7 +4957,7 @@ describe('useQuery', () => {
|
|
|
4963
4957
|
})
|
|
4964
4958
|
|
|
4965
4959
|
it('should refetch when changed enabled to true in error state', async () => {
|
|
4966
|
-
const queryFn =
|
|
4960
|
+
const queryFn = vi.fn<unknown[], unknown>()
|
|
4967
4961
|
queryFn.mockImplementation(async () => {
|
|
4968
4962
|
await sleep(10)
|
|
4969
4963
|
return Promise.reject(new Error('Suspense Error Bingo'))
|
|
@@ -5981,7 +5975,7 @@ describe('useQuery', () => {
|
|
|
5981
5975
|
|
|
5982
5976
|
it('setQueryData - should not call onSuccess callback of active observers', async () => {
|
|
5983
5977
|
const key = queryKey()
|
|
5984
|
-
const onSuccess =
|
|
5978
|
+
const onSuccess = vi.fn()
|
|
5985
5979
|
|
|
5986
5980
|
function Page() {
|
|
5987
5981
|
const state = useQuery({
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -3,6 +3,7 @@ import { act, render } from '@testing-library/react'
|
|
|
3
3
|
import type { QueryClientConfig } from '..'
|
|
4
4
|
import { QueryClient, QueryClientProvider } from '..'
|
|
5
5
|
import * as utils from '@tanstack/query-core'
|
|
6
|
+
import { vi } from 'vitest'
|
|
6
7
|
|
|
7
8
|
export function renderWithClient(
|
|
8
9
|
client: QueryClient,
|
|
@@ -45,11 +46,11 @@ export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
export function mockVisibilityState(value: DocumentVisibilityState) {
|
|
48
|
-
return
|
|
49
|
+
return vi.spyOn(document, 'visibilityState', 'get').mockReturnValue(value)
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
export function mockNavigatorOnLine(value: boolean) {
|
|
52
|
-
return
|
|
53
|
+
return vi.spyOn(navigator, 'onLine', 'get').mockReturnValue(value)
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
let queryKeyCount = 0
|