@tanstack/react-query 5.0.0-beta.9 → 5.0.0-rc.0
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/codemods/coverage/clover.xml +2 -2
- package/build/codemods/coverage/index.html +1 -1
- package/build/legacy/HydrationBoundary.cjs +37 -2
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.d.cts +3 -1
- package/build/legacy/HydrationBoundary.d.ts +3 -1
- package/build/legacy/HydrationBoundary.js +37 -2
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/index.cjs +3 -0
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +1 -0
- package/build/legacy/index.d.ts +1 -0
- package/build/legacy/index.js +2 -0
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +7 -3
- package/build/legacy/queryOptions.d.ts +7 -3
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/suspense.cjs +3 -0
- package/build/legacy/suspense.cjs.map +1 -1
- package/build/legacy/suspense.d.cts +3 -2
- package/build/legacy/suspense.d.ts +3 -2
- package/build/legacy/suspense.js +2 -0
- package/build/legacy/suspense.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +4 -4
- package/build/legacy/types.d.ts +4 -4
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +4 -4
- package/build/legacy/useQueries.d.ts +4 -4
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs +47 -0
- package/build/legacy/useSuspenseQueries.cjs.map +1 -0
- package/build/legacy/useSuspenseQueries.d.cts +66 -0
- package/build/legacy/useSuspenseQueries.d.ts +66 -0
- package/build/legacy/useSuspenseQueries.js +23 -0
- package/build/legacy/useSuspenseQueries.js.map +1 -0
- package/build/legacy/useSuspenseQuery.cjs +2 -1
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.js +2 -1
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +37 -2
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.d.cts +3 -1
- package/build/modern/HydrationBoundary.d.ts +3 -1
- package/build/modern/HydrationBoundary.js +37 -2
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/index.cjs +3 -0
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +1 -0
- package/build/modern/index.d.ts +1 -0
- package/build/modern/index.js +2 -0
- package/build/modern/index.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +7 -3
- package/build/modern/queryOptions.d.ts +7 -3
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/suspense.cjs +3 -0
- package/build/modern/suspense.cjs.map +1 -1
- package/build/modern/suspense.d.cts +3 -2
- package/build/modern/suspense.d.ts +3 -2
- package/build/modern/suspense.js +2 -0
- package/build/modern/suspense.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +4 -4
- package/build/modern/types.d.ts +4 -4
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +4 -4
- package/build/modern/useQueries.d.ts +4 -4
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs +47 -0
- package/build/modern/useSuspenseQueries.cjs.map +1 -0
- package/build/modern/useSuspenseQueries.d.cts +66 -0
- package/build/modern/useSuspenseQueries.d.ts +66 -0
- package/build/modern/useSuspenseQueries.js +23 -0
- package/build/modern/useSuspenseQueries.js.map +1 -0
- package/build/modern/useSuspenseQuery.cjs +2 -1
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.js +2 -1
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js.map +1 -1
- package/package.json +7 -3
- package/src/HydrationBoundary.tsx +78 -8
- package/src/__tests__/HydrationBoundary.test.tsx +111 -7
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +10 -6
- package/src/__tests__/fine-grained-persister.test.tsx +163 -0
- package/src/__tests__/queryOptions.types.test.tsx +31 -0
- package/src/__tests__/ssr.test.tsx +2 -2
- package/src/__tests__/suspense.test.tsx +94 -386
- package/src/__tests__/useInfiniteQuery.test.tsx +44 -44
- package/src/__tests__/useInfiniteQuery.type.test.tsx +10 -10
- package/src/__tests__/useIsFetching.test.tsx +2 -2
- package/src/__tests__/useMutation.test.tsx +4 -4
- package/src/__tests__/useMutationState.test.tsx +4 -4
- package/src/__tests__/useQueries.test.tsx +65 -64
- package/src/__tests__/useQuery.test.tsx +142 -141
- package/src/__tests__/useQuery.types.test.tsx +34 -0
- package/src/__tests__/utils.tsx +0 -7
- package/src/index.ts +5 -0
- package/src/queryOptions.ts +11 -3
- package/src/suspense.ts +12 -0
- package/src/types.ts +18 -12
- package/src/useQueries.ts +34 -28
- package/src/useSuspenseQueries.ts +164 -0
- package/src/useSuspenseQuery.ts +2 -1
- package/src/utils.ts +1 -1
|
@@ -7,7 +7,6 @@ import { QueryCache, keepPreviousData, useQuery } from '..'
|
|
|
7
7
|
import {
|
|
8
8
|
Blink,
|
|
9
9
|
createQueryClient,
|
|
10
|
-
expectType,
|
|
11
10
|
mockOnlineManagerIsOnline,
|
|
12
11
|
mockVisibilityState,
|
|
13
12
|
queryKey,
|
|
@@ -35,29 +34,29 @@ describe('useQuery', () => {
|
|
|
35
34
|
function Page() {
|
|
36
35
|
// unspecified query function should default to unknown
|
|
37
36
|
const noQueryFn = useQuery({ queryKey: key })
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
expectTypeOf<unknown>(noQueryFn.data)
|
|
38
|
+
expectTypeOf<unknown>(noQueryFn.error)
|
|
40
39
|
|
|
41
40
|
// it should infer the result type from the query function
|
|
42
41
|
const fromQueryFn = useQuery({ queryKey: key, queryFn: () => 'test' })
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
expectTypeOf<string | undefined>(fromQueryFn.data)
|
|
43
|
+
expectTypeOf<unknown>(fromQueryFn.error)
|
|
45
44
|
|
|
46
45
|
// it should be possible to specify the result type
|
|
47
46
|
const withResult = useQuery<string>({
|
|
48
47
|
queryKey: key,
|
|
49
48
|
queryFn: () => 'test',
|
|
50
49
|
})
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
expectTypeOf<string | undefined>(withResult.data)
|
|
51
|
+
expectTypeOf<unknown | null>(withResult.error)
|
|
53
52
|
|
|
54
53
|
// it should be possible to specify the error type
|
|
55
54
|
const withError = useQuery<string, Error>({
|
|
56
55
|
queryKey: key,
|
|
57
56
|
queryFn: () => 'test',
|
|
58
57
|
})
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
expectTypeOf<string | undefined>(withError.data)
|
|
59
|
+
expectTypeOf<Error | null>(withError.error)
|
|
61
60
|
|
|
62
61
|
// it should provide the result type in the configuration
|
|
63
62
|
useQuery({
|
|
@@ -70,12 +69,12 @@ describe('useQuery', () => {
|
|
|
70
69
|
queryKey: key,
|
|
71
70
|
queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
72
71
|
})
|
|
73
|
-
|
|
72
|
+
expectTypeOf<'a' | 'b' | undefined>(unionTypeSync.data)
|
|
74
73
|
const unionTypeAsync = useQuery<'a' | 'b'>({
|
|
75
74
|
queryKey: key,
|
|
76
75
|
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
77
76
|
})
|
|
78
|
-
|
|
77
|
+
expectTypeOf<'a' | 'b' | undefined>(unionTypeAsync.data)
|
|
79
78
|
|
|
80
79
|
// should error when the query function result does not match with the specified type
|
|
81
80
|
// @ts-expect-error
|
|
@@ -90,15 +89,15 @@ describe('useQuery', () => {
|
|
|
90
89
|
queryKey: key,
|
|
91
90
|
queryFn: () => queryFn(),
|
|
92
91
|
})
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
expectTypeOf<string | undefined>(fromGenericQueryFn.data)
|
|
93
|
+
expectTypeOf<unknown>(fromGenericQueryFn.error)
|
|
95
94
|
|
|
96
95
|
const fromGenericOptionsQueryFn = useQuery({
|
|
97
96
|
queryKey: key,
|
|
98
97
|
queryFn: () => queryFn(),
|
|
99
98
|
})
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
expectTypeOf<string | undefined>(fromGenericOptionsQueryFn.data)
|
|
100
|
+
expectTypeOf<unknown>(fromGenericOptionsQueryFn.error)
|
|
102
101
|
|
|
103
102
|
type MyData = number
|
|
104
103
|
type MyQueryKey = readonly ['my-data', number]
|
|
@@ -117,7 +116,7 @@ describe('useQuery', () => {
|
|
|
117
116
|
const getMyDataStringKey: QueryFunction<MyData, ['1']> = async (
|
|
118
117
|
context,
|
|
119
118
|
) => {
|
|
120
|
-
|
|
119
|
+
expectTypeOf<['1']>(context.queryKey)
|
|
121
120
|
return Number(context.queryKey[0]) + 42
|
|
122
121
|
}
|
|
123
122
|
|
|
@@ -156,7 +155,7 @@ describe('useQuery', () => {
|
|
|
156
155
|
...options,
|
|
157
156
|
})
|
|
158
157
|
const test = useWrappedQuery([''], async () => '1')
|
|
159
|
-
|
|
158
|
+
expectTypeOf<string | undefined>(test.data)
|
|
160
159
|
|
|
161
160
|
// handles wrapped queries with custom fetcher passed directly to useQuery
|
|
162
161
|
const useWrappedFuncStyleQuery = <
|
|
@@ -173,7 +172,7 @@ describe('useQuery', () => {
|
|
|
173
172
|
>,
|
|
174
173
|
) => useQuery({ queryKey: qk, queryFn: fetcher, ...options })
|
|
175
174
|
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
|
|
176
|
-
|
|
175
|
+
expectTypeOf<boolean | undefined>(testFuncStyle.data)
|
|
177
176
|
}
|
|
178
177
|
})
|
|
179
178
|
|
|
@@ -206,7 +205,7 @@ describe('useQuery', () => {
|
|
|
206
205
|
|
|
207
206
|
it('should return the correct states for a successful query', async () => {
|
|
208
207
|
const key = queryKey()
|
|
209
|
-
const states: UseQueryResult<string
|
|
208
|
+
const states: Array<UseQueryResult<string>> = []
|
|
210
209
|
|
|
211
210
|
function Page() {
|
|
212
211
|
const state = useQuery<string, Error>({
|
|
@@ -220,19 +219,19 @@ describe('useQuery', () => {
|
|
|
220
219
|
states.push(state)
|
|
221
220
|
|
|
222
221
|
if (state.isPending) {
|
|
223
|
-
|
|
224
|
-
|
|
222
|
+
expectTypeOf<undefined>(state.data)
|
|
223
|
+
expectTypeOf<null>(state.error)
|
|
225
224
|
return <span>pending</span>
|
|
226
225
|
}
|
|
227
226
|
|
|
228
227
|
if (state.isLoadingError) {
|
|
229
|
-
|
|
230
|
-
|
|
228
|
+
expectTypeOf<undefined>(state.data)
|
|
229
|
+
expectTypeOf<Error>(state.error)
|
|
231
230
|
return <span>{state.error.message}</span>
|
|
232
231
|
}
|
|
233
232
|
|
|
234
|
-
|
|
235
|
-
|
|
233
|
+
expectTypeOf<string>(state.data)
|
|
234
|
+
expectTypeOf<Error | null>(state.error)
|
|
236
235
|
return <span>{state.data}</span>
|
|
237
236
|
}
|
|
238
237
|
|
|
@@ -300,7 +299,7 @@ describe('useQuery', () => {
|
|
|
300
299
|
it('should return the correct states for an unsuccessful query', async () => {
|
|
301
300
|
const key = queryKey()
|
|
302
301
|
|
|
303
|
-
const states: UseQueryResult
|
|
302
|
+
const states: Array<UseQueryResult> = []
|
|
304
303
|
|
|
305
304
|
function Page() {
|
|
306
305
|
const state = useQuery({
|
|
@@ -549,7 +548,7 @@ describe('useQuery', () => {
|
|
|
549
548
|
|
|
550
549
|
it('should be able to watch a query without providing a query function', async () => {
|
|
551
550
|
const key = queryKey()
|
|
552
|
-
const states: UseQueryResult<string
|
|
551
|
+
const states: Array<UseQueryResult<string>> = []
|
|
553
552
|
|
|
554
553
|
queryClient.setQueryDefaults(key, { queryFn: () => 'data' })
|
|
555
554
|
|
|
@@ -570,7 +569,7 @@ describe('useQuery', () => {
|
|
|
570
569
|
|
|
571
570
|
it('should pick up a query when re-mounting with gcTime 0', async () => {
|
|
572
571
|
const key = queryKey()
|
|
573
|
-
const states: UseQueryResult<string
|
|
572
|
+
const states: Array<UseQueryResult<string>> = []
|
|
574
573
|
|
|
575
574
|
function Page() {
|
|
576
575
|
const [toggle, setToggle] = React.useState(false)
|
|
@@ -643,7 +642,7 @@ describe('useQuery', () => {
|
|
|
643
642
|
|
|
644
643
|
it('should not get into an infinite loop when removing a query with gcTime 0 and rerendering', async () => {
|
|
645
644
|
const key = queryKey()
|
|
646
|
-
const states: UseQueryResult<string
|
|
645
|
+
const states: Array<UseQueryResult<string>> = []
|
|
647
646
|
|
|
648
647
|
function Page() {
|
|
649
648
|
const [, rerender] = React.useState({})
|
|
@@ -705,7 +704,7 @@ describe('useQuery', () => {
|
|
|
705
704
|
|
|
706
705
|
it('should fetch when refetchOnMount is false and nothing has been fetched yet', async () => {
|
|
707
706
|
const key = queryKey()
|
|
708
|
-
const states: UseQueryResult<string
|
|
707
|
+
const states: Array<UseQueryResult<string>> = []
|
|
709
708
|
|
|
710
709
|
function Page() {
|
|
711
710
|
const state = useQuery({
|
|
@@ -728,7 +727,7 @@ describe('useQuery', () => {
|
|
|
728
727
|
|
|
729
728
|
it('should not fetch when refetchOnMount is false and data has been fetched already', async () => {
|
|
730
729
|
const key = queryKey()
|
|
731
|
-
const states: UseQueryResult<string
|
|
730
|
+
const states: Array<UseQueryResult<string>> = []
|
|
732
731
|
|
|
733
732
|
queryClient.setQueryData(key, 'prefetched')
|
|
734
733
|
|
|
@@ -752,7 +751,7 @@ describe('useQuery', () => {
|
|
|
752
751
|
|
|
753
752
|
it('should be able to select a part of the data with select', async () => {
|
|
754
753
|
const key = queryKey()
|
|
755
|
-
const states: UseQueryResult<string
|
|
754
|
+
const states: Array<UseQueryResult<string>> = []
|
|
756
755
|
|
|
757
756
|
function Page() {
|
|
758
757
|
const state = useQuery({
|
|
@@ -778,7 +777,7 @@ describe('useQuery', () => {
|
|
|
778
777
|
|
|
779
778
|
it('should be able to select a part of the data with select in object syntax', async () => {
|
|
780
779
|
const key = queryKey()
|
|
781
|
-
const states: UseQueryResult<string
|
|
780
|
+
const states: Array<UseQueryResult<string>> = []
|
|
782
781
|
|
|
783
782
|
function Page() {
|
|
784
783
|
const state = useQuery({
|
|
@@ -804,7 +803,7 @@ describe('useQuery', () => {
|
|
|
804
803
|
|
|
805
804
|
it('should throw an error when a selector throws', async () => {
|
|
806
805
|
const key = queryKey()
|
|
807
|
-
const states: UseQueryResult<string
|
|
806
|
+
const states: Array<UseQueryResult<string>> = []
|
|
808
807
|
const error = new Error('Select Error')
|
|
809
808
|
|
|
810
809
|
function Page() {
|
|
@@ -871,7 +870,7 @@ describe('useQuery', () => {
|
|
|
871
870
|
|
|
872
871
|
it('should track properties and only re-render when a tracked property changes', async () => {
|
|
873
872
|
const key = queryKey()
|
|
874
|
-
const states: UseQueryResult<string
|
|
873
|
+
const states: Array<UseQueryResult<string>> = []
|
|
875
874
|
|
|
876
875
|
function Page() {
|
|
877
876
|
const state = useQuery({
|
|
@@ -913,7 +912,7 @@ describe('useQuery', () => {
|
|
|
913
912
|
it('should always re-render if we are tracking props but not using any', async () => {
|
|
914
913
|
const key = queryKey()
|
|
915
914
|
let renderCount = 0
|
|
916
|
-
const states: UseQueryResult<string
|
|
915
|
+
const states: Array<UseQueryResult<string>> = []
|
|
917
916
|
|
|
918
917
|
function Page() {
|
|
919
918
|
const state = useQuery({ queryKey: key, queryFn: () => 'test' })
|
|
@@ -948,7 +947,7 @@ describe('useQuery', () => {
|
|
|
948
947
|
|
|
949
948
|
it('should be able to remove a query', async () => {
|
|
950
949
|
const key = queryKey()
|
|
951
|
-
const states: UseQueryResult<number
|
|
950
|
+
const states: Array<UseQueryResult<number>> = []
|
|
952
951
|
let count = 0
|
|
953
952
|
|
|
954
953
|
function Page() {
|
|
@@ -994,7 +993,7 @@ describe('useQuery', () => {
|
|
|
994
993
|
|
|
995
994
|
it('should create a new query when refetching a removed query', async () => {
|
|
996
995
|
const key = queryKey()
|
|
997
|
-
const states: UseQueryResult<number
|
|
996
|
+
const states: Array<UseQueryResult<number>> = []
|
|
998
997
|
let count = 0
|
|
999
998
|
|
|
1000
999
|
function Page() {
|
|
@@ -1055,7 +1054,7 @@ describe('useQuery', () => {
|
|
|
1055
1054
|
{ id: '2', done: true },
|
|
1056
1055
|
]
|
|
1057
1056
|
|
|
1058
|
-
const states: UseQueryResult<typeof result1
|
|
1057
|
+
const states: Array<UseQueryResult<typeof result1>> = []
|
|
1059
1058
|
|
|
1060
1059
|
let count = 0
|
|
1061
1060
|
|
|
@@ -1110,7 +1109,7 @@ describe('useQuery', () => {
|
|
|
1110
1109
|
|
|
1111
1110
|
it('should use query function from hook when the existing query does not have a query function', async () => {
|
|
1112
1111
|
const key = queryKey()
|
|
1113
|
-
const results: DefinedUseQueryResult<string
|
|
1112
|
+
const results: Array<DefinedUseQueryResult<string>> = []
|
|
1114
1113
|
|
|
1115
1114
|
queryClient.setQueryData(key, 'set')
|
|
1116
1115
|
|
|
@@ -1154,7 +1153,6 @@ describe('useQuery', () => {
|
|
|
1154
1153
|
|
|
1155
1154
|
it('should update query stale state and refetch when invalidated with invalidateQueries', async () => {
|
|
1156
1155
|
const key = queryKey()
|
|
1157
|
-
const states: UseQueryResult<number>[] = []
|
|
1158
1156
|
let count = 0
|
|
1159
1157
|
|
|
1160
1158
|
function Page() {
|
|
@@ -1166,11 +1164,8 @@ describe('useQuery', () => {
|
|
|
1166
1164
|
return count
|
|
1167
1165
|
},
|
|
1168
1166
|
staleTime: Infinity,
|
|
1169
|
-
notifyOnChangeProps: 'all',
|
|
1170
1167
|
})
|
|
1171
1168
|
|
|
1172
|
-
states.push(state)
|
|
1173
|
-
|
|
1174
1169
|
return (
|
|
1175
1170
|
<div>
|
|
1176
1171
|
<button
|
|
@@ -1178,52 +1173,29 @@ describe('useQuery', () => {
|
|
|
1178
1173
|
>
|
|
1179
1174
|
invalidate
|
|
1180
1175
|
</button>
|
|
1181
|
-
data: {state.data}
|
|
1176
|
+
data: {state.data}, isStale: {String(state.isStale)}, isFetching:{' '}
|
|
1177
|
+
{String(state.isFetching)}
|
|
1182
1178
|
</div>
|
|
1183
1179
|
)
|
|
1184
1180
|
}
|
|
1185
1181
|
|
|
1186
1182
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
1187
1183
|
|
|
1188
|
-
await waitFor(() =>
|
|
1184
|
+
await waitFor(() =>
|
|
1185
|
+
rendered.getByText('data: 1, isStale: false, isFetching: false'),
|
|
1186
|
+
)
|
|
1189
1187
|
fireEvent.click(rendered.getByRole('button', { name: /invalidate/i }))
|
|
1190
|
-
await waitFor(() =>
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
isFetching: true,
|
|
1197
|
-
isRefetching: false,
|
|
1198
|
-
isSuccess: false,
|
|
1199
|
-
isStale: true,
|
|
1200
|
-
})
|
|
1201
|
-
expect(states[1]).toMatchObject({
|
|
1202
|
-
data: 1,
|
|
1203
|
-
isFetching: false,
|
|
1204
|
-
isRefetching: false,
|
|
1205
|
-
isSuccess: true,
|
|
1206
|
-
isStale: false,
|
|
1207
|
-
})
|
|
1208
|
-
expect(states[2]).toMatchObject({
|
|
1209
|
-
data: 1,
|
|
1210
|
-
isFetching: true,
|
|
1211
|
-
isRefetching: true,
|
|
1212
|
-
isSuccess: true,
|
|
1213
|
-
isStale: true,
|
|
1214
|
-
})
|
|
1215
|
-
expect(states[3]).toMatchObject({
|
|
1216
|
-
data: 2,
|
|
1217
|
-
isFetching: false,
|
|
1218
|
-
isRefetching: false,
|
|
1219
|
-
isSuccess: true,
|
|
1220
|
-
isStale: false,
|
|
1221
|
-
})
|
|
1188
|
+
await waitFor(() =>
|
|
1189
|
+
rendered.getByText('data: 1, isStale: true, isFetching: true'),
|
|
1190
|
+
)
|
|
1191
|
+
await waitFor(() =>
|
|
1192
|
+
rendered.getByText('data: 2, isStale: false, isFetching: false'),
|
|
1193
|
+
)
|
|
1222
1194
|
})
|
|
1223
1195
|
|
|
1224
1196
|
it('should not update disabled query when refetched with refetchQueries', async () => {
|
|
1225
1197
|
const key = queryKey()
|
|
1226
|
-
const states: UseQueryResult<number
|
|
1198
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1227
1199
|
let count = 0
|
|
1228
1200
|
|
|
1229
1201
|
function Page() {
|
|
@@ -1263,7 +1235,7 @@ describe('useQuery', () => {
|
|
|
1263
1235
|
|
|
1264
1236
|
it('should not refetch disabled query when invalidated with invalidateQueries', async () => {
|
|
1265
1237
|
const key = queryKey()
|
|
1266
|
-
const states: UseQueryResult<number
|
|
1238
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1267
1239
|
let count = 0
|
|
1268
1240
|
|
|
1269
1241
|
function Page() {
|
|
@@ -1303,7 +1275,7 @@ describe('useQuery', () => {
|
|
|
1303
1275
|
|
|
1304
1276
|
it('should not fetch when switching to a disabled query', async () => {
|
|
1305
1277
|
const key = queryKey()
|
|
1306
|
-
const states: UseQueryResult<number
|
|
1278
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1307
1279
|
|
|
1308
1280
|
function Page() {
|
|
1309
1281
|
const [count, setCount] = React.useState(0)
|
|
@@ -1366,7 +1338,7 @@ describe('useQuery', () => {
|
|
|
1366
1338
|
|
|
1367
1339
|
it('should keep the previous data when placeholderData is set', async () => {
|
|
1368
1340
|
const key = queryKey()
|
|
1369
|
-
const states: UseQueryResult<number
|
|
1341
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1370
1342
|
|
|
1371
1343
|
function Page() {
|
|
1372
1344
|
const [count, setCount] = React.useState(0)
|
|
@@ -1430,7 +1402,7 @@ describe('useQuery', () => {
|
|
|
1430
1402
|
|
|
1431
1403
|
it('should keep the previous data when placeholderData is set and select fn transform is used', async () => {
|
|
1432
1404
|
const key = queryKey()
|
|
1433
|
-
const states: UseQueryResult<number
|
|
1405
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1434
1406
|
|
|
1435
1407
|
function Page() {
|
|
1436
1408
|
const [count, setCount] = React.useState(0)
|
|
@@ -1498,9 +1470,9 @@ describe('useQuery', () => {
|
|
|
1498
1470
|
})
|
|
1499
1471
|
|
|
1500
1472
|
it('should keep the previous queryKey (from prevQuery) between multiple pending queries when placeholderData is set and select fn transform is used', async () => {
|
|
1501
|
-
const keys: Array<
|
|
1473
|
+
const keys: Array<ReadonlyArray<unknown> | null> = []
|
|
1502
1474
|
const key = queryKey()
|
|
1503
|
-
const states: UseQueryResult<number
|
|
1475
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1504
1476
|
|
|
1505
1477
|
function Page() {
|
|
1506
1478
|
const [count, setCount] = React.useState(0)
|
|
@@ -1553,7 +1525,7 @@ describe('useQuery', () => {
|
|
|
1553
1525
|
|
|
1554
1526
|
it('should show placeholderData between multiple pending queries when select fn transform is used', async () => {
|
|
1555
1527
|
const key = queryKey()
|
|
1556
|
-
const states: UseQueryResult<number
|
|
1528
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1557
1529
|
|
|
1558
1530
|
function Page() {
|
|
1559
1531
|
const [count, setCount] = React.useState(0)
|
|
@@ -1637,7 +1609,7 @@ describe('useQuery', () => {
|
|
|
1637
1609
|
|
|
1638
1610
|
it('should transition to error state when placeholderData is set', async () => {
|
|
1639
1611
|
const key = queryKey()
|
|
1640
|
-
const states: UseQueryResult<number
|
|
1612
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1641
1613
|
|
|
1642
1614
|
function Page({ count }: { count: number }) {
|
|
1643
1615
|
const state = useQuery<number, Error>({
|
|
@@ -1724,7 +1696,7 @@ describe('useQuery', () => {
|
|
|
1724
1696
|
|
|
1725
1697
|
it('should not show initial data from next query if placeholderData is set', async () => {
|
|
1726
1698
|
const key = queryKey()
|
|
1727
|
-
const states: DefinedUseQueryResult<number
|
|
1699
|
+
const states: Array<DefinedUseQueryResult<number>> = []
|
|
1728
1700
|
|
|
1729
1701
|
function Page() {
|
|
1730
1702
|
const [count, setCount] = React.useState(0)
|
|
@@ -1798,7 +1770,7 @@ describe('useQuery', () => {
|
|
|
1798
1770
|
|
|
1799
1771
|
it('should keep the previous data on disabled query when placeholderData is set', async () => {
|
|
1800
1772
|
const key = queryKey()
|
|
1801
|
-
const states: UseQueryResult<number
|
|
1773
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1802
1774
|
|
|
1803
1775
|
function Page() {
|
|
1804
1776
|
const [count, setCount] = React.useState(0)
|
|
@@ -1900,7 +1872,7 @@ describe('useQuery', () => {
|
|
|
1900
1872
|
|
|
1901
1873
|
it('should keep the previous data on disabled query when placeholderData is set and switching query key multiple times', async () => {
|
|
1902
1874
|
const key = queryKey()
|
|
1903
|
-
const states: UseQueryResult<number
|
|
1875
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1904
1876
|
|
|
1905
1877
|
queryClient.setQueryData([key, 10], 10)
|
|
1906
1878
|
|
|
@@ -1984,7 +1956,7 @@ describe('useQuery', () => {
|
|
|
1984
1956
|
|
|
1985
1957
|
it('should use the correct query function when components use different configurations', async () => {
|
|
1986
1958
|
const key = queryKey()
|
|
1987
|
-
const states: UseQueryResult<number
|
|
1959
|
+
const states: Array<UseQueryResult<number>> = []
|
|
1988
1960
|
|
|
1989
1961
|
function FirstComponent() {
|
|
1990
1962
|
const state = useQuery({
|
|
@@ -2045,8 +2017,8 @@ describe('useQuery', () => {
|
|
|
2045
2017
|
|
|
2046
2018
|
it('should be able to set different stale times for a query', async () => {
|
|
2047
2019
|
const key = queryKey()
|
|
2048
|
-
const states1: UseQueryResult<string
|
|
2049
|
-
const states2: UseQueryResult<string
|
|
2020
|
+
const states1: Array<UseQueryResult<string>> = []
|
|
2021
|
+
const states2: Array<UseQueryResult<string>> = []
|
|
2050
2022
|
|
|
2051
2023
|
await queryClient.prefetchQuery({
|
|
2052
2024
|
queryKey: key,
|
|
@@ -2146,7 +2118,7 @@ describe('useQuery', () => {
|
|
|
2146
2118
|
|
|
2147
2119
|
it('should re-render when a query becomes stale', async () => {
|
|
2148
2120
|
const key = queryKey()
|
|
2149
|
-
const states: UseQueryResult<string
|
|
2121
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2150
2122
|
|
|
2151
2123
|
function Page() {
|
|
2152
2124
|
const state = useQuery({
|
|
@@ -2171,7 +2143,7 @@ describe('useQuery', () => {
|
|
|
2171
2143
|
describe('notifyOnChangeProps', () => {
|
|
2172
2144
|
it('should not re-render when it should only re-render only data change and the selected data did not change', async () => {
|
|
2173
2145
|
const key = queryKey()
|
|
2174
|
-
const states: UseQueryResult<string
|
|
2146
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2175
2147
|
|
|
2176
2148
|
function Page() {
|
|
2177
2149
|
const state = useQuery({
|
|
@@ -2212,7 +2184,7 @@ describe('useQuery', () => {
|
|
|
2212
2184
|
})
|
|
2213
2185
|
it('should not re-render when it should only re-render on data changes and the data did not change', async () => {
|
|
2214
2186
|
const key = queryKey()
|
|
2215
|
-
const states: UseQueryResult<string
|
|
2187
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2216
2188
|
|
|
2217
2189
|
function Page() {
|
|
2218
2190
|
const state = useQuery({
|
|
@@ -2270,7 +2242,7 @@ describe('useQuery', () => {
|
|
|
2270
2242
|
describe('function', () => {
|
|
2271
2243
|
it('should not re-render when it should only re-render on data changes and the data did not change', async () => {
|
|
2272
2244
|
const key = queryKey()
|
|
2273
|
-
const states: UseQueryResult<string
|
|
2245
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2274
2246
|
|
|
2275
2247
|
function Page() {
|
|
2276
2248
|
const state = useQuery({
|
|
@@ -2325,7 +2297,7 @@ describe('useQuery', () => {
|
|
|
2325
2297
|
|
|
2326
2298
|
it('should not re-render when change props are not actively being tracked', async () => {
|
|
2327
2299
|
const key = queryKey()
|
|
2328
|
-
const states: UseQueryResult<string
|
|
2300
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2329
2301
|
|
|
2330
2302
|
function Page() {
|
|
2331
2303
|
const fetchCounterRef = React.useRef(0)
|
|
@@ -2410,6 +2382,9 @@ describe('useQuery', () => {
|
|
|
2410
2382
|
)
|
|
2411
2383
|
fireEvent.click(rendered.getByRole('button', { name: 'refetch' }))
|
|
2412
2384
|
|
|
2385
|
+
await waitFor(() => {
|
|
2386
|
+
rendered.getByText('fetch counter: 3')
|
|
2387
|
+
})
|
|
2413
2388
|
// sleep is required to make sure no additional renders happen after click
|
|
2414
2389
|
await sleep(20)
|
|
2415
2390
|
|
|
@@ -2600,7 +2575,7 @@ describe('useQuery', () => {
|
|
|
2600
2575
|
|
|
2601
2576
|
it('should not refetch query on focus when `enabled` is set to `false`', async () => {
|
|
2602
2577
|
const key = queryKey()
|
|
2603
|
-
const queryFn = vi.fn<unknown
|
|
2578
|
+
const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
|
|
2604
2579
|
|
|
2605
2580
|
function Page() {
|
|
2606
2581
|
const { data = 'default' } = useQuery({
|
|
@@ -2629,7 +2604,7 @@ describe('useQuery', () => {
|
|
|
2629
2604
|
|
|
2630
2605
|
it('should not refetch stale query on focus when `refetchOnWindowFocus` is set to `false`', async () => {
|
|
2631
2606
|
const key = queryKey()
|
|
2632
|
-
const states: UseQueryResult<number
|
|
2607
|
+
const states: Array<UseQueryResult<number>> = []
|
|
2633
2608
|
let count = 0
|
|
2634
2609
|
|
|
2635
2610
|
function Page() {
|
|
@@ -2660,7 +2635,7 @@ describe('useQuery', () => {
|
|
|
2660
2635
|
|
|
2661
2636
|
it('should not refetch stale query on focus when `refetchOnWindowFocus` is set to a function that returns `false`', async () => {
|
|
2662
2637
|
const key = queryKey()
|
|
2663
|
-
const states: UseQueryResult<number
|
|
2638
|
+
const states: Array<UseQueryResult<number>> = []
|
|
2664
2639
|
let count = 0
|
|
2665
2640
|
|
|
2666
2641
|
function Page() {
|
|
@@ -2691,7 +2666,7 @@ describe('useQuery', () => {
|
|
|
2691
2666
|
|
|
2692
2667
|
it('should not refetch fresh query on focus when `refetchOnWindowFocus` is set to `true`', async () => {
|
|
2693
2668
|
const key = queryKey()
|
|
2694
|
-
const states: UseQueryResult<number
|
|
2669
|
+
const states: Array<UseQueryResult<number>> = []
|
|
2695
2670
|
let count = 0
|
|
2696
2671
|
|
|
2697
2672
|
function Page() {
|
|
@@ -2722,7 +2697,7 @@ describe('useQuery', () => {
|
|
|
2722
2697
|
|
|
2723
2698
|
it('should refetch fresh query on focus when `refetchOnWindowFocus` is set to `always`', async () => {
|
|
2724
2699
|
const key = queryKey()
|
|
2725
|
-
const states: UseQueryResult<number
|
|
2700
|
+
const states: Array<UseQueryResult<number>> = []
|
|
2726
2701
|
let count = 0
|
|
2727
2702
|
|
|
2728
2703
|
function Page() {
|
|
@@ -2759,7 +2734,7 @@ describe('useQuery', () => {
|
|
|
2759
2734
|
|
|
2760
2735
|
it('should calculate focus behaviour for `refetchOnWindowFocus` depending on function', async () => {
|
|
2761
2736
|
const key = queryKey()
|
|
2762
|
-
const states: UseQueryResult<number
|
|
2737
|
+
const states: Array<UseQueryResult<number>> = []
|
|
2763
2738
|
let count = 0
|
|
2764
2739
|
|
|
2765
2740
|
function Page() {
|
|
@@ -2810,7 +2785,7 @@ describe('useQuery', () => {
|
|
|
2810
2785
|
|
|
2811
2786
|
it('should refetch fresh query when refetchOnMount is set to always', async () => {
|
|
2812
2787
|
const key = queryKey()
|
|
2813
|
-
const states: UseQueryResult<string
|
|
2788
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2814
2789
|
|
|
2815
2790
|
await queryClient.prefetchQuery({
|
|
2816
2791
|
queryKey: key,
|
|
@@ -2847,7 +2822,7 @@ describe('useQuery', () => {
|
|
|
2847
2822
|
|
|
2848
2823
|
it('should refetch stale query when refetchOnMount is set to true', async () => {
|
|
2849
2824
|
const key = queryKey()
|
|
2850
|
-
const states: UseQueryResult<string
|
|
2825
|
+
const states: Array<UseQueryResult<string>> = []
|
|
2851
2826
|
|
|
2852
2827
|
await queryClient.prefetchQuery({
|
|
2853
2828
|
queryKey: key,
|
|
@@ -2885,6 +2860,9 @@ describe('useQuery', () => {
|
|
|
2885
2860
|
})
|
|
2886
2861
|
|
|
2887
2862
|
it('should set status to error if queryFn throws', async () => {
|
|
2863
|
+
const consoleMock = vi
|
|
2864
|
+
.spyOn(console, 'error')
|
|
2865
|
+
.mockImplementation(() => undefined)
|
|
2888
2866
|
const key = queryKey()
|
|
2889
2867
|
|
|
2890
2868
|
function Page() {
|
|
@@ -2908,6 +2886,8 @@ describe('useQuery', () => {
|
|
|
2908
2886
|
|
|
2909
2887
|
await waitFor(() => rendered.getByText('error'))
|
|
2910
2888
|
await waitFor(() => rendered.getByText('Error test jaylen'))
|
|
2889
|
+
|
|
2890
|
+
consoleMock.mockRestore()
|
|
2911
2891
|
})
|
|
2912
2892
|
|
|
2913
2893
|
it('should throw error if queryFn throws and throwOnError is in use', async () => {
|
|
@@ -3151,7 +3131,7 @@ describe('useQuery', () => {
|
|
|
3151
3131
|
|
|
3152
3132
|
it('should always fetch if refetchOnMount is set to always', async () => {
|
|
3153
3133
|
const key = queryKey()
|
|
3154
|
-
const states: UseQueryResult<string
|
|
3134
|
+
const states: Array<UseQueryResult<string>> = []
|
|
3155
3135
|
|
|
3156
3136
|
await queryClient.prefetchQuery({
|
|
3157
3137
|
queryKey: key,
|
|
@@ -3199,7 +3179,7 @@ describe('useQuery', () => {
|
|
|
3199
3179
|
|
|
3200
3180
|
it('should fetch if initial data is set', async () => {
|
|
3201
3181
|
const key = queryKey()
|
|
3202
|
-
const states: DefinedUseQueryResult<string
|
|
3182
|
+
const states: Array<DefinedUseQueryResult<string>> = []
|
|
3203
3183
|
|
|
3204
3184
|
function Page() {
|
|
3205
3185
|
const state = useQuery({
|
|
@@ -3231,7 +3211,7 @@ describe('useQuery', () => {
|
|
|
3231
3211
|
|
|
3232
3212
|
it('should not fetch if initial data is set with a stale time', async () => {
|
|
3233
3213
|
const key = queryKey()
|
|
3234
|
-
const states: DefinedUseQueryResult<string
|
|
3214
|
+
const states: Array<DefinedUseQueryResult<string>> = []
|
|
3235
3215
|
|
|
3236
3216
|
function Page() {
|
|
3237
3217
|
const state = useQuery({
|
|
@@ -3263,7 +3243,7 @@ describe('useQuery', () => {
|
|
|
3263
3243
|
|
|
3264
3244
|
it('should fetch if initial data updated at is older than stale time', async () => {
|
|
3265
3245
|
const key = queryKey()
|
|
3266
|
-
const states: DefinedUseQueryResult<string
|
|
3246
|
+
const states: Array<DefinedUseQueryResult<string>> = []
|
|
3267
3247
|
|
|
3268
3248
|
const oneSecondAgo = Date.now() - 1000
|
|
3269
3249
|
|
|
@@ -3303,7 +3283,7 @@ describe('useQuery', () => {
|
|
|
3303
3283
|
|
|
3304
3284
|
it('should fetch if "initial data updated at" is exactly 0', async () => {
|
|
3305
3285
|
const key = queryKey()
|
|
3306
|
-
const states: DefinedUseQueryResult<string
|
|
3286
|
+
const states: Array<DefinedUseQueryResult<string>> = []
|
|
3307
3287
|
|
|
3308
3288
|
function Page() {
|
|
3309
3289
|
const state = useQuery({
|
|
@@ -3336,7 +3316,7 @@ describe('useQuery', () => {
|
|
|
3336
3316
|
|
|
3337
3317
|
it('should keep initial data when the query key changes', async () => {
|
|
3338
3318
|
const key = queryKey()
|
|
3339
|
-
const states: DefinedUseQueryResult<{ count: number }
|
|
3319
|
+
const states: Array<DefinedUseQueryResult<{ count: number }>> = []
|
|
3340
3320
|
|
|
3341
3321
|
function Page() {
|
|
3342
3322
|
const [count, setCount] = React.useState(0)
|
|
@@ -3371,7 +3351,7 @@ describe('useQuery', () => {
|
|
|
3371
3351
|
it('should retry specified number of times', async () => {
|
|
3372
3352
|
const key = queryKey()
|
|
3373
3353
|
|
|
3374
|
-
const queryFn = vi.fn<unknown
|
|
3354
|
+
const queryFn = vi.fn<Array<unknown>, unknown>()
|
|
3375
3355
|
queryFn.mockImplementation(() => {
|
|
3376
3356
|
return Promise.reject(new Error('Error test Barrett'))
|
|
3377
3357
|
})
|
|
@@ -3408,7 +3388,7 @@ describe('useQuery', () => {
|
|
|
3408
3388
|
it('should not retry if retry function `false`', async () => {
|
|
3409
3389
|
const key = queryKey()
|
|
3410
3390
|
|
|
3411
|
-
const queryFn = vi.fn<unknown
|
|
3391
|
+
const queryFn = vi.fn<Array<unknown>, unknown>()
|
|
3412
3392
|
|
|
3413
3393
|
queryFn.mockImplementationOnce(() => {
|
|
3414
3394
|
return Promise.reject(new Error('Error test Tanner'))
|
|
@@ -3454,7 +3434,7 @@ describe('useQuery', () => {
|
|
|
3454
3434
|
|
|
3455
3435
|
type DelayError = { delay: number }
|
|
3456
3436
|
|
|
3457
|
-
const queryFn = vi.fn<unknown
|
|
3437
|
+
const queryFn = vi.fn<Array<unknown>, unknown>()
|
|
3458
3438
|
queryFn.mockImplementation(() => {
|
|
3459
3439
|
return Promise.reject({ delay: 50 })
|
|
3460
3440
|
})
|
|
@@ -3551,7 +3531,7 @@ describe('useQuery', () => {
|
|
|
3551
3531
|
|
|
3552
3532
|
it('should fetch on mount when a query was already created with setQueryData', async () => {
|
|
3553
3533
|
const key = queryKey()
|
|
3554
|
-
const states: UseQueryResult<string
|
|
3534
|
+
const states: Array<UseQueryResult<string>> = []
|
|
3555
3535
|
|
|
3556
3536
|
queryClient.setQueryData(key, 'prefetched')
|
|
3557
3537
|
|
|
@@ -3582,7 +3562,7 @@ describe('useQuery', () => {
|
|
|
3582
3562
|
|
|
3583
3563
|
it('should refetch after focus regain', async () => {
|
|
3584
3564
|
const key = queryKey()
|
|
3585
|
-
const states: UseQueryResult<string
|
|
3565
|
+
const states: Array<UseQueryResult<string>> = []
|
|
3586
3566
|
|
|
3587
3567
|
// make page unfocused
|
|
3588
3568
|
const visibilityMock = mockVisibilityState('hidden')
|
|
@@ -3645,12 +3625,12 @@ describe('useQuery', () => {
|
|
|
3645
3625
|
// See https://github.com/tannerlinsley/react-query/issues/195
|
|
3646
3626
|
it('should refetch if stale after a prefetch', async () => {
|
|
3647
3627
|
const key = queryKey()
|
|
3648
|
-
const states: UseQueryResult<string
|
|
3628
|
+
const states: Array<UseQueryResult<string>> = []
|
|
3649
3629
|
|
|
3650
|
-
const queryFn = vi.fn<unknown
|
|
3630
|
+
const queryFn = vi.fn<Array<unknown>, string>()
|
|
3651
3631
|
queryFn.mockImplementation(() => 'data')
|
|
3652
3632
|
|
|
3653
|
-
const prefetchQueryFn = vi.fn<unknown
|
|
3633
|
+
const prefetchQueryFn = vi.fn<Array<unknown>, string>()
|
|
3654
3634
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3655
3635
|
|
|
3656
3636
|
await queryClient.prefetchQuery({
|
|
@@ -3678,10 +3658,10 @@ describe('useQuery', () => {
|
|
|
3678
3658
|
it('should not refetch if not stale after a prefetch', async () => {
|
|
3679
3659
|
const key = queryKey()
|
|
3680
3660
|
|
|
3681
|
-
const queryFn = vi.fn<unknown
|
|
3661
|
+
const queryFn = vi.fn<Array<unknown>, string>()
|
|
3682
3662
|
queryFn.mockImplementation(() => 'data')
|
|
3683
3663
|
|
|
3684
|
-
const prefetchQueryFn = vi.fn<unknown
|
|
3664
|
+
const prefetchQueryFn = vi.fn<Array<unknown>, Promise<string>>()
|
|
3685
3665
|
prefetchQueryFn.mockImplementation(async () => {
|
|
3686
3666
|
await sleep(10)
|
|
3687
3667
|
return 'not yet...'
|
|
@@ -3833,7 +3813,7 @@ describe('useQuery', () => {
|
|
|
3833
3813
|
|
|
3834
3814
|
it('should mark query as fetching, when using initialData', async () => {
|
|
3835
3815
|
const key = queryKey()
|
|
3836
|
-
const results: DefinedUseQueryResult<string
|
|
3816
|
+
const results: Array<DefinedUseQueryResult<string>> = []
|
|
3837
3817
|
|
|
3838
3818
|
function Page() {
|
|
3839
3819
|
const result = useQuery({
|
|
@@ -3860,7 +3840,7 @@ describe('useQuery', () => {
|
|
|
3860
3840
|
|
|
3861
3841
|
it('should initialize state properly, when initialData is falsy', async () => {
|
|
3862
3842
|
const key = queryKey()
|
|
3863
|
-
const results: DefinedUseQueryResult<number
|
|
3843
|
+
const results: Array<DefinedUseQueryResult<number>> = []
|
|
3864
3844
|
|
|
3865
3845
|
function Page() {
|
|
3866
3846
|
const result = useQuery({
|
|
@@ -3937,7 +3917,7 @@ describe('useQuery', () => {
|
|
|
3937
3917
|
// // See https://github.com/tannerlinsley/react-query/issues/214
|
|
3938
3918
|
it('data should persist when enabled is changed to false', async () => {
|
|
3939
3919
|
const key = queryKey()
|
|
3940
|
-
const results: DefinedUseQueryResult<string
|
|
3920
|
+
const results: Array<DefinedUseQueryResult<string>> = []
|
|
3941
3921
|
|
|
3942
3922
|
function Page() {
|
|
3943
3923
|
const [shouldFetch, setShouldFetch] = React.useState(true)
|
|
@@ -3988,7 +3968,7 @@ describe('useQuery', () => {
|
|
|
3988
3968
|
|
|
3989
3969
|
it('it should support enabled:false in query object syntax', async () => {
|
|
3990
3970
|
const key = queryKey()
|
|
3991
|
-
const queryFn = vi.fn<unknown
|
|
3971
|
+
const queryFn = vi.fn<Array<unknown>, string>()
|
|
3992
3972
|
queryFn.mockImplementation(() => 'data')
|
|
3993
3973
|
|
|
3994
3974
|
function Page() {
|
|
@@ -4082,7 +4062,7 @@ describe('useQuery', () => {
|
|
|
4082
4062
|
|
|
4083
4063
|
it('should not cause memo churn when data does not change', async () => {
|
|
4084
4064
|
const key = queryKey()
|
|
4085
|
-
const queryFn = vi.fn<unknown
|
|
4065
|
+
const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
|
|
4086
4066
|
const memoFn = vi.fn()
|
|
4087
4067
|
|
|
4088
4068
|
function Page() {
|
|
@@ -4157,7 +4137,7 @@ describe('useQuery', () => {
|
|
|
4157
4137
|
it('should refetch in an interval depending on function result', async () => {
|
|
4158
4138
|
const key = queryKey()
|
|
4159
4139
|
let count = 0
|
|
4160
|
-
const states: UseQueryResult<number
|
|
4140
|
+
const states: Array<UseQueryResult<number>> = []
|
|
4161
4141
|
|
|
4162
4142
|
function Page() {
|
|
4163
4143
|
const queryInfo = useQuery({
|
|
@@ -4223,7 +4203,7 @@ describe('useQuery', () => {
|
|
|
4223
4203
|
|
|
4224
4204
|
it('should not interval fetch with a refetchInterval of 0', async () => {
|
|
4225
4205
|
const key = queryKey()
|
|
4226
|
-
const states: UseQueryResult<number
|
|
4206
|
+
const states: Array<UseQueryResult<number>> = []
|
|
4227
4207
|
|
|
4228
4208
|
function Page() {
|
|
4229
4209
|
const queryInfo = useQuery({
|
|
@@ -4290,7 +4270,7 @@ describe('useQuery', () => {
|
|
|
4290
4270
|
it('should refetch if any query instance becomes enabled', async () => {
|
|
4291
4271
|
const key = queryKey()
|
|
4292
4272
|
|
|
4293
|
-
const queryFn = vi.fn<unknown
|
|
4273
|
+
const queryFn = vi.fn<Array<unknown>, string>().mockReturnValue('data')
|
|
4294
4274
|
|
|
4295
4275
|
function Disabled() {
|
|
4296
4276
|
useQuery({ queryKey: key, queryFn, enabled: false })
|
|
@@ -4319,7 +4299,7 @@ describe('useQuery', () => {
|
|
|
4319
4299
|
it('should use placeholder data while the query loads', async () => {
|
|
4320
4300
|
const key1 = queryKey()
|
|
4321
4301
|
|
|
4322
|
-
const states: UseQueryResult<string
|
|
4302
|
+
const states: Array<UseQueryResult<string>> = []
|
|
4323
4303
|
|
|
4324
4304
|
function Page() {
|
|
4325
4305
|
const state = useQuery({
|
|
@@ -4358,7 +4338,7 @@ describe('useQuery', () => {
|
|
|
4358
4338
|
it('should use placeholder data even for disabled queries', async () => {
|
|
4359
4339
|
const key1 = queryKey()
|
|
4360
4340
|
|
|
4361
|
-
const states: { state: UseQueryResult<string>; count: number }
|
|
4341
|
+
const states: Array<{ state: UseQueryResult<string>; count: number }> = []
|
|
4362
4342
|
|
|
4363
4343
|
function Page() {
|
|
4364
4344
|
const [count, setCount] = React.useState(0)
|
|
@@ -4418,7 +4398,7 @@ describe('useQuery', () => {
|
|
|
4418
4398
|
it('placeholder data should run through select', async () => {
|
|
4419
4399
|
const key1 = queryKey()
|
|
4420
4400
|
|
|
4421
|
-
const states: UseQueryResult<string
|
|
4401
|
+
const states: Array<UseQueryResult<string>> = []
|
|
4422
4402
|
|
|
4423
4403
|
function Page() {
|
|
4424
4404
|
const state = useQuery({
|
|
@@ -4458,7 +4438,7 @@ describe('useQuery', () => {
|
|
|
4458
4438
|
it('placeholder data function result should run through select', async () => {
|
|
4459
4439
|
const key1 = queryKey()
|
|
4460
4440
|
|
|
4461
|
-
const states: UseQueryResult<string
|
|
4441
|
+
const states: Array<UseQueryResult<string>> = []
|
|
4462
4442
|
let placeholderFunctionRunCount = 0
|
|
4463
4443
|
|
|
4464
4444
|
function Page() {
|
|
@@ -4684,7 +4664,7 @@ describe('useQuery', () => {
|
|
|
4684
4664
|
|
|
4685
4665
|
it('should cancel the query if the signal was consumed and there are no more subscriptions', async () => {
|
|
4686
4666
|
const key = queryKey()
|
|
4687
|
-
const states: UseQueryResult<string
|
|
4667
|
+
const states: Array<UseQueryResult<string>> = []
|
|
4688
4668
|
|
|
4689
4669
|
const queryFn: QueryFunction<string, [typeof key, number]> = async (
|
|
4690
4670
|
ctx,
|
|
@@ -4749,7 +4729,7 @@ describe('useQuery', () => {
|
|
|
4749
4729
|
|
|
4750
4730
|
it('should refetch when quickly switching to a failed query', async () => {
|
|
4751
4731
|
const key = queryKey()
|
|
4752
|
-
const states: UseQueryResult<string
|
|
4732
|
+
const states: Array<UseQueryResult<string>> = []
|
|
4753
4733
|
|
|
4754
4734
|
const queryFn = async () => {
|
|
4755
4735
|
await sleep(50)
|
|
@@ -4800,7 +4780,7 @@ describe('useQuery', () => {
|
|
|
4800
4780
|
|
|
4801
4781
|
it('should update query state and refetch when reset with resetQueries', async () => {
|
|
4802
4782
|
const key = queryKey()
|
|
4803
|
-
const states: UseQueryResult<number
|
|
4783
|
+
const states: Array<UseQueryResult<number>> = []
|
|
4804
4784
|
let count = 0
|
|
4805
4785
|
|
|
4806
4786
|
function Page() {
|
|
@@ -4870,7 +4850,7 @@ describe('useQuery', () => {
|
|
|
4870
4850
|
|
|
4871
4851
|
it('should update query state and not refetch when resetting a disabled query with resetQueries', async () => {
|
|
4872
4852
|
const key = queryKey()
|
|
4873
|
-
const states: UseQueryResult<number
|
|
4853
|
+
const states: Array<UseQueryResult<number>> = []
|
|
4874
4854
|
let count = 0
|
|
4875
4855
|
|
|
4876
4856
|
function Page() {
|
|
@@ -4972,7 +4952,7 @@ describe('useQuery', () => {
|
|
|
4972
4952
|
})
|
|
4973
4953
|
|
|
4974
4954
|
it('should refetch when changed enabled to true in error state', async () => {
|
|
4975
|
-
const queryFn = vi.fn<unknown
|
|
4955
|
+
const queryFn = vi.fn<Array<unknown>, unknown>()
|
|
4976
4956
|
queryFn.mockImplementation(async () => {
|
|
4977
4957
|
await sleep(10)
|
|
4978
4958
|
return Promise.reject(new Error('Suspense Error Bingo'))
|
|
@@ -5144,7 +5124,7 @@ describe('useQuery', () => {
|
|
|
5144
5124
|
|
|
5145
5125
|
it('should have no error in pending state when refetching after error occurred', async () => {
|
|
5146
5126
|
const key = queryKey()
|
|
5147
|
-
const states: UseQueryResult<number
|
|
5127
|
+
const states: Array<UseQueryResult<number>> = []
|
|
5148
5128
|
const error = new Error('oops')
|
|
5149
5129
|
|
|
5150
5130
|
let count = 0
|
|
@@ -5954,7 +5934,7 @@ describe('useQuery', () => {
|
|
|
5954
5934
|
|
|
5955
5935
|
it('it should have status=error on mount when a query has failed', async () => {
|
|
5956
5936
|
const key = queryKey()
|
|
5957
|
-
const states: UseQueryResult<unknown
|
|
5937
|
+
const states: Array<UseQueryResult<unknown>> = []
|
|
5958
5938
|
const error = new Error('oops')
|
|
5959
5939
|
|
|
5960
5940
|
const queryFn = async (): Promise<unknown> => {
|
|
@@ -6225,4 +6205,25 @@ describe('useQuery', () => {
|
|
|
6225
6205
|
await waitFor(() => rendered.getByText('Rendered Id: 2'))
|
|
6226
6206
|
expect(spy).toHaveBeenCalledTimes(1)
|
|
6227
6207
|
})
|
|
6208
|
+
it('should not cause an infinite render loop when using unstable callback ref', async () => {
|
|
6209
|
+
const key = queryKey()
|
|
6210
|
+
|
|
6211
|
+
function Test() {
|
|
6212
|
+
const [_, setRef] = React.useState<HTMLDivElement | null>()
|
|
6213
|
+
|
|
6214
|
+
const { data } = useQuery({
|
|
6215
|
+
queryKey: [key],
|
|
6216
|
+
queryFn: async () => {
|
|
6217
|
+
await sleep(5)
|
|
6218
|
+
return 'Works'
|
|
6219
|
+
},
|
|
6220
|
+
})
|
|
6221
|
+
|
|
6222
|
+
return <div ref={(value) => setRef(value)}>{data}</div>
|
|
6223
|
+
}
|
|
6224
|
+
|
|
6225
|
+
const rendered = renderWithClient(queryClient, <Test />)
|
|
6226
|
+
|
|
6227
|
+
await waitFor(() => rendered.getByText('Works'))
|
|
6228
|
+
})
|
|
6228
6229
|
})
|