@tanstack/react-query 4.24.9 → 5.0.0-alpha.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/lib/HydrationBoundary.d.ts +9 -0
- package/build/lib/{Hydrate.esm.js → HydrationBoundary.esm.js} +14 -17
- package/build/lib/HydrationBoundary.esm.js.map +1 -0
- package/build/lib/{Hydrate.js → HydrationBoundary.js} +17 -24
- package/build/lib/HydrationBoundary.js.map +1 -0
- package/build/lib/{Hydrate.mjs → HydrationBoundary.mjs} +14 -17
- package/build/lib/HydrationBoundary.mjs.map +1 -0
- package/build/lib/QueryClientProvider.d.ts +4 -19
- package/build/lib/QueryClientProvider.esm.js +11 -46
- package/build/lib/QueryClientProvider.esm.js.map +1 -1
- package/build/lib/QueryClientProvider.js +14 -52
- package/build/lib/QueryClientProvider.js.map +1 -1
- package/build/lib/QueryClientProvider.mjs +11 -46
- package/build/lib/QueryClientProvider.mjs.map +1 -1
- package/build/lib/QueryErrorResetBoundary.esm.js +7 -2
- package/build/lib/QueryErrorResetBoundary.esm.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.js +10 -8
- package/build/lib/QueryErrorResetBoundary.js.map +1 -1
- package/build/lib/QueryErrorResetBoundary.mjs +7 -2
- package/build/lib/QueryErrorResetBoundary.mjs.map +1 -1
- package/build/lib/__tests__/useQuery.types.test.d.ts +1 -2
- package/build/lib/__tests__/utils.d.ts +7 -10
- package/build/lib/errorBoundaryUtils.d.ts +3 -3
- package/build/lib/errorBoundaryUtils.esm.js +3 -3
- package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
- package/build/lib/errorBoundaryUtils.js +6 -9
- package/build/lib/errorBoundaryUtils.js.map +1 -1
- package/build/lib/errorBoundaryUtils.mjs +3 -3
- package/build/lib/errorBoundaryUtils.mjs.map +1 -1
- package/build/lib/index.d.ts +4 -5
- package/build/lib/index.esm.js +3 -4
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +6 -9
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +3 -4
- package/build/lib/index.mjs.map +1 -1
- package/build/lib/isRestoring.esm.js.map +1 -1
- package/build/lib/isRestoring.js +3 -6
- package/build/lib/isRestoring.js.map +1 -1
- package/build/lib/isRestoring.mjs.map +1 -1
- package/build/lib/suspense.esm.js +5 -5
- package/build/lib/suspense.esm.js.map +1 -1
- package/build/lib/suspense.js +5 -7
- package/build/lib/suspense.js.map +1 -1
- package/build/lib/suspense.mjs +5 -5
- package/build/lib/suspense.mjs.map +1 -1
- package/build/lib/types.d.ts +14 -22
- package/build/lib/useBaseQuery.d.ts +2 -2
- package/build/lib/useBaseQuery.esm.js +15 -18
- package/build/lib/useBaseQuery.esm.js.map +1 -1
- package/build/lib/useBaseQuery.js +18 -24
- package/build/lib/useBaseQuery.js.map +1 -1
- package/build/lib/useBaseQuery.mjs +15 -18
- package/build/lib/useBaseQuery.mjs.map +1 -1
- package/build/lib/useInfiniteQuery.d.ts +2 -4
- package/build/lib/useInfiniteQuery.esm.js +4 -4
- package/build/lib/useInfiniteQuery.esm.js.map +1 -1
- package/build/lib/useInfiniteQuery.js +3 -5
- package/build/lib/useInfiniteQuery.js.map +1 -1
- package/build/lib/useInfiniteQuery.mjs +4 -4
- package/build/lib/useInfiniteQuery.mjs.map +1 -1
- package/build/lib/useIsFetching.d.ts +2 -7
- package/build/lib/useIsFetching.esm.js +5 -9
- package/build/lib/useIsFetching.esm.js.map +1 -1
- package/build/lib/useIsFetching.js +7 -14
- package/build/lib/useIsFetching.js.map +1 -1
- package/build/lib/useIsFetching.mjs +5 -9
- package/build/lib/useIsFetching.mjs.map +1 -1
- package/build/lib/useMutation.d.ts +2 -5
- package/build/lib/useMutation.esm.js +12 -14
- package/build/lib/useMutation.esm.js.map +1 -1
- package/build/lib/useMutation.js +14 -19
- package/build/lib/useMutation.js.map +1 -1
- package/build/lib/useMutation.mjs +12 -14
- package/build/lib/useMutation.mjs.map +1 -1
- package/build/lib/useMutationState.d.ts +8 -0
- package/build/lib/useMutationState.esm.js +37 -0
- package/build/lib/useMutationState.esm.js.map +1 -0
- package/build/lib/useMutationState.js +59 -0
- package/build/lib/useMutationState.js.map +1 -0
- package/build/lib/useMutationState.mjs +37 -0
- package/build/lib/useMutationState.mjs.map +1 -0
- package/build/lib/useQueries.d.ts +9 -7
- package/build/lib/useQueries.esm.js +16 -27
- package/build/lib/useQueries.esm.js.map +1 -1
- package/build/lib/useQueries.js +19 -33
- package/build/lib/useQueries.js.map +1 -1
- package/build/lib/useQueries.mjs +16 -27
- package/build/lib/useQueries.mjs.map +1 -1
- package/build/lib/useQuery.d.ts +9 -21
- package/build/lib/useQuery.esm.js +5 -4
- package/build/lib/useQuery.esm.js.map +1 -1
- package/build/lib/useQuery.js +4 -5
- package/build/lib/useQuery.js.map +1 -1
- package/build/lib/useQuery.mjs +5 -4
- package/build/lib/useQuery.mjs.map +1 -1
- package/build/lib/utils.d.ts +1 -1
- package/build/lib/utils.esm.js +5 -6
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +5 -8
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +5 -6
- package/build/lib/utils.mjs.map +1 -1
- package/build/umd/index.development.js +1007 -1826
- 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 +6 -18
- package/src/{Hydrate.tsx → HydrationBoundary.tsx} +17 -18
- package/src/QueryClientProvider.tsx +11 -65
- package/src/__tests__/{Hydrate.test.tsx → HydrationBoundary.test.tsx} +89 -77
- package/src/__tests__/QueryClientProvider.test.tsx +33 -147
- package/src/__tests__/QueryResetErrorBoundary.test.tsx +118 -85
- package/src/__tests__/ssr-hydration.test.tsx +27 -33
- package/src/__tests__/ssr.test.tsx +23 -9
- package/src/__tests__/suspense.test.tsx +194 -171
- package/src/__tests__/useInfiniteQuery.test.tsx +249 -494
- package/src/__tests__/useInfiniteQuery.type.test.tsx +131 -0
- package/src/__tests__/useIsFetching.test.tsx +65 -108
- package/src/__tests__/useMutation.test.tsx +200 -268
- package/src/__tests__/useMutationState.test.tsx +284 -0
- package/src/__tests__/useQueries.test.tsx +43 -347
- package/src/__tests__/useQuery.test.tsx +890 -686
- package/src/__tests__/useQuery.types.test.tsx +23 -24
- package/src/__tests__/utils.tsx +14 -23
- package/src/errorBoundaryUtils.ts +5 -10
- package/src/index.ts +4 -7
- package/src/types.ts +33 -38
- package/src/useBaseQuery.ts +7 -7
- package/src/useInfiniteQuery.ts +12 -77
- package/src/useIsFetching.ts +8 -20
- package/src/useMutation.ts +8 -66
- package/src/useMutationState.ts +81 -0
- package/src/useQueries.ts +29 -21
- package/src/useQuery.ts +27 -104
- package/src/utils.ts +5 -5
- package/build/lib/Hydrate.d.ts +0 -10
- package/build/lib/Hydrate.esm.js.map +0 -1
- package/build/lib/Hydrate.js.map +0 -1
- package/build/lib/Hydrate.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.esm.js +0 -6
- package/build/lib/reactBatchedUpdates.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.js +0 -30
- package/build/lib/reactBatchedUpdates.js.map +0 -1
- package/build/lib/reactBatchedUpdates.mjs +0 -6
- package/build/lib/reactBatchedUpdates.mjs.map +0 -1
- package/build/lib/reactBatchedUpdates.native.d.ts +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js +0 -2
- package/build/lib/reactBatchedUpdates.native.esm.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.js +0 -13
- package/build/lib/reactBatchedUpdates.native.js.map +0 -1
- package/build/lib/reactBatchedUpdates.native.mjs +0 -2
- package/build/lib/reactBatchedUpdates.native.mjs.map +0 -1
- package/build/lib/setBatchUpdatesFn.esm.js +0 -5
- package/build/lib/setBatchUpdatesFn.esm.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.js +0 -7
- package/build/lib/setBatchUpdatesFn.js.map +0 -1
- package/build/lib/setBatchUpdatesFn.mjs +0 -5
- package/build/lib/setBatchUpdatesFn.mjs.map +0 -1
- package/build/lib/useIsMutating.d.ts +0 -7
- package/build/lib/useIsMutating.esm.js +0 -16
- package/build/lib/useIsMutating.esm.js.map +0 -1
- package/build/lib/useIsMutating.js +0 -40
- package/build/lib/useIsMutating.js.map +0 -1
- package/build/lib/useIsMutating.mjs +0 -16
- package/build/lib/useIsMutating.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.d.ts +0 -2
- package/build/lib/useSyncExternalStore.esm.js +0 -7
- package/build/lib/useSyncExternalStore.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.js +0 -11
- package/build/lib/useSyncExternalStore.js.map +0 -1
- package/build/lib/useSyncExternalStore.mjs +0 -7
- package/build/lib/useSyncExternalStore.mjs.map +0 -1
- package/build/lib/useSyncExternalStore.native.d.ts +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js +0 -2
- package/build/lib/useSyncExternalStore.native.esm.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.js +0 -13
- package/build/lib/useSyncExternalStore.native.js.map +0 -1
- package/build/lib/useSyncExternalStore.native.mjs +0 -2
- package/build/lib/useSyncExternalStore.native.mjs.map +0 -1
- package/codemods/v4/key-transformation.js +0 -138
- package/codemods/v4/replace-import-specifier.js +0 -25
- package/codemods/v4/utils/index.js +0 -166
- package/codemods/v4/utils/replacers/key-replacer.js +0 -160
- package/codemods/v4/utils/transformers/query-cache-transformer.js +0 -115
- package/codemods/v4/utils/transformers/query-client-transformer.js +0 -49
- package/codemods/v4/utils/transformers/use-query-like-transformer.js +0 -32
- package/codemods/v4/utils/unprocessable-key-error.js +0 -8
- package/src/__tests__/useIsMutating.test.tsx +0 -259
- package/src/reactBatchedUpdates.native.ts +0 -4
- package/src/reactBatchedUpdates.ts +0 -2
- package/src/setBatchUpdatesFn.ts +0 -4
- package/src/useIsMutating.ts +0 -39
- package/src/useSyncExternalStore.native.ts +0 -5
- package/src/useSyncExternalStore.ts +0 -4
- /package/build/lib/__tests__/{Hydrate.test.d.ts → HydrationBoundary.test.d.ts} +0 -0
- /package/build/lib/__tests__/{useIsMutating.test.d.ts → useInfiniteQuery.type.test.d.ts} +0 -0
- /package/build/lib/{setBatchUpdatesFn.d.ts → __tests__/useMutationState.test.d.ts} +0 -0
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { act, fireEvent, waitFor } from '@testing-library/react'
|
|
1
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react'
|
|
2
2
|
import '@testing-library/jest-dom'
|
|
3
3
|
import * as React from 'react'
|
|
4
4
|
import {
|
|
5
5
|
Blink,
|
|
6
6
|
createQueryClient,
|
|
7
7
|
expectType,
|
|
8
|
-
mockLogger,
|
|
9
8
|
mockNavigatorOnLine,
|
|
10
9
|
mockVisibilityState,
|
|
11
10
|
queryKey,
|
|
@@ -20,7 +19,7 @@ import type {
|
|
|
20
19
|
UseQueryOptions,
|
|
21
20
|
UseQueryResult,
|
|
22
21
|
} from '..'
|
|
23
|
-
import { QueryCache, useQuery } from '..'
|
|
22
|
+
import { QueryCache, useQuery, keepPreviousData } from '..'
|
|
24
23
|
import { ErrorBoundary } from 'react-error-boundary'
|
|
25
24
|
|
|
26
25
|
describe('useQuery', () => {
|
|
@@ -34,59 +33,68 @@ describe('useQuery', () => {
|
|
|
34
33
|
// eslint-disable-next-line
|
|
35
34
|
function Page() {
|
|
36
35
|
// unspecified query function should default to unknown
|
|
37
|
-
const noQueryFn = useQuery(key)
|
|
36
|
+
const noQueryFn = useQuery({ queryKey: key })
|
|
38
37
|
expectType<unknown>(noQueryFn.data)
|
|
39
38
|
expectType<unknown>(noQueryFn.error)
|
|
40
39
|
|
|
41
40
|
// it should infer the result type from the query function
|
|
42
|
-
const fromQueryFn = useQuery(key, () => 'test')
|
|
41
|
+
const fromQueryFn = useQuery({ queryKey: key, queryFn: () => 'test' })
|
|
43
42
|
expectType<string | undefined>(fromQueryFn.data)
|
|
44
43
|
expectType<unknown>(fromQueryFn.error)
|
|
45
44
|
|
|
46
45
|
// it should be possible to specify the result type
|
|
47
|
-
const withResult = useQuery<string>(
|
|
46
|
+
const withResult = useQuery<string>({
|
|
47
|
+
queryKey: key,
|
|
48
|
+
queryFn: () => 'test',
|
|
49
|
+
})
|
|
48
50
|
expectType<string | undefined>(withResult.data)
|
|
49
51
|
expectType<unknown | null>(withResult.error)
|
|
50
52
|
|
|
51
53
|
// it should be possible to specify the error type
|
|
52
|
-
const withError = useQuery<string, Error>(
|
|
54
|
+
const withError = useQuery<string, Error>({
|
|
55
|
+
queryKey: key,
|
|
56
|
+
queryFn: () => 'test',
|
|
57
|
+
})
|
|
53
58
|
expectType<string | undefined>(withError.data)
|
|
54
59
|
expectType<Error | null>(withError.error)
|
|
55
60
|
|
|
56
61
|
// it should provide the result type in the configuration
|
|
57
|
-
useQuery(
|
|
62
|
+
useQuery({
|
|
63
|
+
queryKey: [key],
|
|
64
|
+
queryFn: async () => true,
|
|
58
65
|
onSuccess: (data) => expectType<boolean>(data),
|
|
59
66
|
onSettled: (data) => expectType<boolean | undefined>(data),
|
|
60
67
|
})
|
|
61
68
|
|
|
62
69
|
// it should be possible to specify a union type as result type
|
|
63
|
-
const unionTypeSync = useQuery(
|
|
64
|
-
key,
|
|
65
|
-
() => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
)
|
|
70
|
+
const unionTypeSync = useQuery({
|
|
71
|
+
queryKey: key,
|
|
72
|
+
queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
73
|
+
|
|
74
|
+
onSuccess: (data) => expectType<'a' | 'b'>(data),
|
|
75
|
+
})
|
|
70
76
|
expectType<'a' | 'b' | undefined>(unionTypeSync.data)
|
|
71
|
-
const unionTypeAsync = useQuery<'a' | 'b'>(
|
|
72
|
-
key,
|
|
73
|
-
() => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
)
|
|
77
|
+
const unionTypeAsync = useQuery<'a' | 'b'>({
|
|
78
|
+
queryKey: key,
|
|
79
|
+
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
80
|
+
|
|
81
|
+
onSuccess: (data) => expectType<'a' | 'b'>(data),
|
|
82
|
+
})
|
|
78
83
|
expectType<'a' | 'b' | undefined>(unionTypeAsync.data)
|
|
79
84
|
|
|
80
85
|
// should error when the query function result does not match with the specified type
|
|
81
86
|
// @ts-expect-error
|
|
82
|
-
useQuery<number>(key, () => 'test')
|
|
87
|
+
useQuery<number>({ queryKey: key, queryFn: () => 'test' })
|
|
83
88
|
|
|
84
89
|
// it should infer the result type from a generic query function
|
|
85
90
|
function queryFn<T = string>(): Promise<T> {
|
|
86
91
|
return Promise.resolve({} as T)
|
|
87
92
|
}
|
|
88
93
|
|
|
89
|
-
const fromGenericQueryFn = useQuery(
|
|
94
|
+
const fromGenericQueryFn = useQuery({
|
|
95
|
+
queryKey: key,
|
|
96
|
+
queryFn: () => queryFn(),
|
|
97
|
+
})
|
|
90
98
|
expectType<string | undefined>(fromGenericQueryFn.data)
|
|
91
99
|
expectType<unknown>(fromGenericQueryFn.error)
|
|
92
100
|
|
|
@@ -124,9 +132,10 @@ describe('useQuery', () => {
|
|
|
124
132
|
})
|
|
125
133
|
|
|
126
134
|
// it should handle query-functions that return Promise<any>
|
|
127
|
-
useQuery(
|
|
128
|
-
|
|
129
|
-
|
|
135
|
+
useQuery({
|
|
136
|
+
queryKey: key,
|
|
137
|
+
queryFn: () => fetch('return Promise<any>').then((resp) => resp.json()),
|
|
138
|
+
})
|
|
130
139
|
|
|
131
140
|
// handles wrapped queries with custom fetcher passed as inline queryFn
|
|
132
141
|
const useWrappedQuery = <
|
|
@@ -145,7 +154,12 @@ describe('useQuery', () => {
|
|
|
145
154
|
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
146
155
|
'queryKey' | 'queryFn' | 'initialData'
|
|
147
156
|
>,
|
|
148
|
-
) =>
|
|
157
|
+
) =>
|
|
158
|
+
useQuery({
|
|
159
|
+
queryKey: qk,
|
|
160
|
+
queryFn: () => fetcher(qk[1], 'token'),
|
|
161
|
+
...options,
|
|
162
|
+
})
|
|
149
163
|
const test = useWrappedQuery([''], async () => '1')
|
|
150
164
|
expectType<string | undefined>(test.data)
|
|
151
165
|
|
|
@@ -162,7 +176,7 @@ describe('useQuery', () => {
|
|
|
162
176
|
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
163
177
|
'queryKey' | 'queryFn' | 'initialData'
|
|
164
178
|
>,
|
|
165
|
-
) => useQuery(qk, fetcher, options)
|
|
179
|
+
) => useQuery({ queryKey: qk, queryFn: fetcher, ...options })
|
|
166
180
|
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
|
|
167
181
|
expectType<boolean | undefined>(testFuncStyle.data)
|
|
168
182
|
}
|
|
@@ -173,9 +187,12 @@ describe('useQuery', () => {
|
|
|
173
187
|
const key = queryKey()
|
|
174
188
|
|
|
175
189
|
function Page() {
|
|
176
|
-
const { data = 'default' } = useQuery(
|
|
177
|
-
|
|
178
|
-
|
|
190
|
+
const { data = 'default' } = useQuery({
|
|
191
|
+
queryKey: key,
|
|
192
|
+
queryFn: async () => {
|
|
193
|
+
await sleep(10)
|
|
194
|
+
return 'test'
|
|
195
|
+
},
|
|
179
196
|
})
|
|
180
197
|
|
|
181
198
|
return (
|
|
@@ -197,17 +214,20 @@ describe('useQuery', () => {
|
|
|
197
214
|
const states: UseQueryResult<string>[] = []
|
|
198
215
|
|
|
199
216
|
function Page() {
|
|
200
|
-
const state = useQuery<string, Error>(
|
|
201
|
-
|
|
202
|
-
|
|
217
|
+
const state = useQuery<string, Error>({
|
|
218
|
+
queryKey: key,
|
|
219
|
+
queryFn: async () => {
|
|
220
|
+
await sleep(10)
|
|
221
|
+
return 'test'
|
|
222
|
+
},
|
|
203
223
|
})
|
|
204
224
|
|
|
205
225
|
states.push(state)
|
|
206
226
|
|
|
207
|
-
if (state.
|
|
227
|
+
if (state.isPending) {
|
|
208
228
|
expectType<undefined>(state.data)
|
|
209
229
|
expectType<null>(state.error)
|
|
210
|
-
return <span>
|
|
230
|
+
return <span>pending</span>
|
|
211
231
|
}
|
|
212
232
|
|
|
213
233
|
if (state.isLoadingError) {
|
|
@@ -240,18 +260,17 @@ describe('useQuery', () => {
|
|
|
240
260
|
isFetchedAfterMount: false,
|
|
241
261
|
isFetching: true,
|
|
242
262
|
isPaused: false,
|
|
243
|
-
|
|
263
|
+
isPending: true,
|
|
244
264
|
isInitialLoading: true,
|
|
265
|
+
isLoading: true,
|
|
245
266
|
isLoadingError: false,
|
|
246
267
|
isPlaceholderData: false,
|
|
247
|
-
isPreviousData: false,
|
|
248
268
|
isRefetchError: false,
|
|
249
269
|
isRefetching: false,
|
|
250
270
|
isStale: true,
|
|
251
271
|
isSuccess: false,
|
|
252
272
|
refetch: expect.any(Function),
|
|
253
|
-
|
|
254
|
-
status: 'loading',
|
|
273
|
+
status: 'pending',
|
|
255
274
|
fetchStatus: 'fetching',
|
|
256
275
|
})
|
|
257
276
|
|
|
@@ -268,17 +287,16 @@ describe('useQuery', () => {
|
|
|
268
287
|
isFetchedAfterMount: true,
|
|
269
288
|
isFetching: false,
|
|
270
289
|
isPaused: false,
|
|
271
|
-
|
|
290
|
+
isPending: false,
|
|
272
291
|
isInitialLoading: false,
|
|
292
|
+
isLoading: false,
|
|
273
293
|
isLoadingError: false,
|
|
274
294
|
isPlaceholderData: false,
|
|
275
|
-
isPreviousData: false,
|
|
276
295
|
isRefetchError: false,
|
|
277
296
|
isRefetching: false,
|
|
278
297
|
isStale: true,
|
|
279
298
|
isSuccess: true,
|
|
280
299
|
refetch: expect.any(Function),
|
|
281
|
-
remove: expect.any(Function),
|
|
282
300
|
status: 'success',
|
|
283
301
|
fetchStatus: 'idle',
|
|
284
302
|
})
|
|
@@ -287,17 +305,16 @@ describe('useQuery', () => {
|
|
|
287
305
|
it('should return the correct states for an unsuccessful query', async () => {
|
|
288
306
|
const key = queryKey()
|
|
289
307
|
|
|
290
|
-
const states: UseQueryResult
|
|
308
|
+
const states: UseQueryResult[] = []
|
|
291
309
|
|
|
292
310
|
function Page() {
|
|
293
|
-
const state = useQuery
|
|
294
|
-
key,
|
|
295
|
-
() => Promise.reject('rejected'),
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
)
|
|
311
|
+
const state = useQuery({
|
|
312
|
+
queryKey: key,
|
|
313
|
+
queryFn: () => Promise.reject(new Error('rejected')),
|
|
314
|
+
|
|
315
|
+
retry: 1,
|
|
316
|
+
retryDelay: 1,
|
|
317
|
+
})
|
|
301
318
|
|
|
302
319
|
states.push(state)
|
|
303
320
|
|
|
@@ -305,7 +322,7 @@ describe('useQuery', () => {
|
|
|
305
322
|
<div>
|
|
306
323
|
<h1>Status: {state.status}</h1>
|
|
307
324
|
<div>Failure Count: {state.failureCount}</div>
|
|
308
|
-
<div>Failure Reason: {state.failureReason}</div>
|
|
325
|
+
<div>Failure Reason: {state.failureReason?.message}</div>
|
|
309
326
|
</div>
|
|
310
327
|
)
|
|
311
328
|
}
|
|
@@ -327,18 +344,17 @@ describe('useQuery', () => {
|
|
|
327
344
|
isFetchedAfterMount: false,
|
|
328
345
|
isFetching: true,
|
|
329
346
|
isPaused: false,
|
|
330
|
-
|
|
347
|
+
isPending: true,
|
|
331
348
|
isInitialLoading: true,
|
|
349
|
+
isLoading: true,
|
|
332
350
|
isLoadingError: false,
|
|
333
351
|
isPlaceholderData: false,
|
|
334
|
-
isPreviousData: false,
|
|
335
352
|
isRefetchError: false,
|
|
336
353
|
isRefetching: false,
|
|
337
354
|
isStale: true,
|
|
338
355
|
isSuccess: false,
|
|
339
356
|
refetch: expect.any(Function),
|
|
340
|
-
|
|
341
|
-
status: 'loading',
|
|
357
|
+
status: 'pending',
|
|
342
358
|
fetchStatus: 'fetching',
|
|
343
359
|
})
|
|
344
360
|
|
|
@@ -348,52 +364,50 @@ describe('useQuery', () => {
|
|
|
348
364
|
error: null,
|
|
349
365
|
errorUpdatedAt: 0,
|
|
350
366
|
failureCount: 1,
|
|
351
|
-
failureReason: 'rejected',
|
|
367
|
+
failureReason: new Error('rejected'),
|
|
352
368
|
errorUpdateCount: 0,
|
|
353
369
|
isError: false,
|
|
354
370
|
isFetched: false,
|
|
355
371
|
isFetchedAfterMount: false,
|
|
356
372
|
isFetching: true,
|
|
357
373
|
isPaused: false,
|
|
358
|
-
|
|
374
|
+
isPending: true,
|
|
359
375
|
isInitialLoading: true,
|
|
376
|
+
isLoading: true,
|
|
360
377
|
isLoadingError: false,
|
|
361
378
|
isPlaceholderData: false,
|
|
362
|
-
isPreviousData: false,
|
|
363
379
|
isRefetchError: false,
|
|
364
380
|
isRefetching: false,
|
|
365
381
|
isStale: true,
|
|
366
382
|
isSuccess: false,
|
|
367
383
|
refetch: expect.any(Function),
|
|
368
|
-
|
|
369
|
-
status: 'loading',
|
|
384
|
+
status: 'pending',
|
|
370
385
|
fetchStatus: 'fetching',
|
|
371
386
|
})
|
|
372
387
|
|
|
373
388
|
expect(states[2]).toEqual({
|
|
374
389
|
data: undefined,
|
|
375
390
|
dataUpdatedAt: 0,
|
|
376
|
-
error: 'rejected',
|
|
391
|
+
error: new Error('rejected'),
|
|
377
392
|
errorUpdatedAt: expect.any(Number),
|
|
378
393
|
failureCount: 2,
|
|
379
|
-
failureReason: 'rejected',
|
|
394
|
+
failureReason: new Error('rejected'),
|
|
380
395
|
errorUpdateCount: 1,
|
|
381
396
|
isError: true,
|
|
382
397
|
isFetched: true,
|
|
383
398
|
isFetchedAfterMount: true,
|
|
384
399
|
isFetching: false,
|
|
385
400
|
isPaused: false,
|
|
386
|
-
|
|
401
|
+
isPending: false,
|
|
387
402
|
isInitialLoading: false,
|
|
403
|
+
isLoading: false,
|
|
388
404
|
isLoadingError: true,
|
|
389
405
|
isPlaceholderData: false,
|
|
390
|
-
isPreviousData: false,
|
|
391
406
|
isRefetchError: false,
|
|
392
407
|
isRefetching: false,
|
|
393
408
|
isStale: true,
|
|
394
409
|
isSuccess: false,
|
|
395
410
|
refetch: expect.any(Function),
|
|
396
|
-
remove: expect.any(Function),
|
|
397
411
|
status: 'error',
|
|
398
412
|
fetchStatus: 'idle',
|
|
399
413
|
})
|
|
@@ -402,10 +416,13 @@ describe('useQuery', () => {
|
|
|
402
416
|
it('should set isFetchedAfterMount to true after a query has been fetched', async () => {
|
|
403
417
|
const key = queryKey()
|
|
404
418
|
|
|
405
|
-
await queryClient.prefetchQuery(
|
|
419
|
+
await queryClient.prefetchQuery({
|
|
420
|
+
queryKey: key,
|
|
421
|
+
queryFn: () => 'prefetched',
|
|
422
|
+
})
|
|
406
423
|
|
|
407
424
|
function Page() {
|
|
408
|
-
const result = useQuery(key, () => 'new data')
|
|
425
|
+
const result = useQuery({ queryKey: key, queryFn: () => 'new data' })
|
|
409
426
|
|
|
410
427
|
return (
|
|
411
428
|
<>
|
|
@@ -437,14 +454,14 @@ describe('useQuery', () => {
|
|
|
437
454
|
const onSuccess = jest.fn()
|
|
438
455
|
|
|
439
456
|
function Page() {
|
|
440
|
-
const state = useQuery(
|
|
441
|
-
key,
|
|
442
|
-
async () => {
|
|
457
|
+
const state = useQuery({
|
|
458
|
+
queryKey: key,
|
|
459
|
+
queryFn: async () => {
|
|
443
460
|
await sleep(10)
|
|
444
461
|
return 'data'
|
|
445
462
|
},
|
|
446
|
-
|
|
447
|
-
)
|
|
463
|
+
onSuccess,
|
|
464
|
+
})
|
|
448
465
|
states.push(state)
|
|
449
466
|
return <div>data: {state.data}</div>
|
|
450
467
|
}
|
|
@@ -464,15 +481,15 @@ describe('useQuery', () => {
|
|
|
464
481
|
let count = 0
|
|
465
482
|
|
|
466
483
|
function Page() {
|
|
467
|
-
const state = useQuery(
|
|
468
|
-
key,
|
|
469
|
-
async () => {
|
|
484
|
+
const state = useQuery({
|
|
485
|
+
queryKey: key,
|
|
486
|
+
queryFn: async () => {
|
|
470
487
|
count++
|
|
471
488
|
await sleep(10)
|
|
472
489
|
return 'data' + count
|
|
473
490
|
},
|
|
474
|
-
|
|
475
|
-
)
|
|
491
|
+
onSuccess,
|
|
492
|
+
})
|
|
476
493
|
|
|
477
494
|
states.push(state)
|
|
478
495
|
|
|
@@ -490,7 +507,7 @@ describe('useQuery', () => {
|
|
|
490
507
|
fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
|
|
491
508
|
await rendered.findByText('data: data2')
|
|
492
509
|
|
|
493
|
-
expect(states.length).toBe(3) //
|
|
510
|
+
expect(states.length).toBe(3) //pending, success, success after refetch
|
|
494
511
|
expect(count).toBe(2)
|
|
495
512
|
expect(onSuccess).toHaveBeenCalledTimes(2)
|
|
496
513
|
})
|
|
@@ -501,7 +518,12 @@ describe('useQuery', () => {
|
|
|
501
518
|
const onSuccess = jest.fn()
|
|
502
519
|
|
|
503
520
|
function Page() {
|
|
504
|
-
const state = useQuery(
|
|
521
|
+
const state = useQuery({
|
|
522
|
+
queryKey: key,
|
|
523
|
+
queryFn: () => 'data',
|
|
524
|
+
enabled: false,
|
|
525
|
+
onSuccess,
|
|
526
|
+
})
|
|
505
527
|
|
|
506
528
|
states.push(state)
|
|
507
529
|
|
|
@@ -545,14 +567,14 @@ describe('useQuery', () => {
|
|
|
545
567
|
}
|
|
546
568
|
|
|
547
569
|
function Component() {
|
|
548
|
-
const state = useQuery(
|
|
549
|
-
key,
|
|
550
|
-
async () => {
|
|
570
|
+
const state = useQuery({
|
|
571
|
+
queryKey: key,
|
|
572
|
+
queryFn: async () => {
|
|
551
573
|
await sleep(10)
|
|
552
574
|
return 'data'
|
|
553
575
|
},
|
|
554
|
-
|
|
555
|
-
)
|
|
576
|
+
onSuccess,
|
|
577
|
+
})
|
|
556
578
|
states.push(state)
|
|
557
579
|
return null
|
|
558
580
|
}
|
|
@@ -570,7 +592,9 @@ describe('useQuery', () => {
|
|
|
570
592
|
const onError = jest.fn()
|
|
571
593
|
|
|
572
594
|
function Page() {
|
|
573
|
-
const state = useQuery<unknown>(
|
|
595
|
+
const state = useQuery<unknown>({
|
|
596
|
+
queryKey: key,
|
|
597
|
+
queryFn: () => Promise.reject(new Error('error')),
|
|
574
598
|
retry: false,
|
|
575
599
|
onError,
|
|
576
600
|
})
|
|
@@ -583,7 +607,7 @@ describe('useQuery', () => {
|
|
|
583
607
|
await sleep(10)
|
|
584
608
|
expect(states.length).toBe(2)
|
|
585
609
|
expect(onError).toHaveBeenCalledTimes(1)
|
|
586
|
-
expect(onError).toHaveBeenCalledWith('error')
|
|
610
|
+
expect(onError).toHaveBeenCalledWith(new Error('error'))
|
|
587
611
|
})
|
|
588
612
|
|
|
589
613
|
it('should not call onError when receiving a CancelledError', async () => {
|
|
@@ -591,16 +615,15 @@ describe('useQuery', () => {
|
|
|
591
615
|
const onError = jest.fn()
|
|
592
616
|
|
|
593
617
|
function Page() {
|
|
594
|
-
const { status, fetchStatus } = useQuery(
|
|
595
|
-
key,
|
|
596
|
-
async () => {
|
|
618
|
+
const { status, fetchStatus } = useQuery({
|
|
619
|
+
queryKey: key,
|
|
620
|
+
queryFn: async () => {
|
|
597
621
|
await sleep(10)
|
|
598
622
|
return 23
|
|
599
623
|
},
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
)
|
|
624
|
+
|
|
625
|
+
onError,
|
|
626
|
+
})
|
|
604
627
|
return (
|
|
605
628
|
<span>
|
|
606
629
|
status: {status}, fetchStatus: {fetchStatus}
|
|
@@ -610,12 +633,12 @@ describe('useQuery', () => {
|
|
|
610
633
|
|
|
611
634
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
612
635
|
|
|
613
|
-
rendered.getByText('status:
|
|
636
|
+
rendered.getByText('status: pending, fetchStatus: fetching')
|
|
614
637
|
|
|
615
|
-
await queryClient.cancelQueries(key)
|
|
638
|
+
await queryClient.cancelQueries({ queryKey: key })
|
|
616
639
|
// query cancellation will reset the query to it's initial state
|
|
617
640
|
await waitFor(() =>
|
|
618
|
-
rendered.getByText('status:
|
|
641
|
+
rendered.getByText('status: pending, fetchStatus: idle'),
|
|
619
642
|
)
|
|
620
643
|
expect(onError).not.toHaveBeenCalled()
|
|
621
644
|
})
|
|
@@ -626,7 +649,11 @@ describe('useQuery', () => {
|
|
|
626
649
|
const onSettled = jest.fn()
|
|
627
650
|
|
|
628
651
|
function Page() {
|
|
629
|
-
const state = useQuery(
|
|
652
|
+
const state = useQuery({
|
|
653
|
+
queryKey: key,
|
|
654
|
+
queryFn: () => 'data',
|
|
655
|
+
onSettled,
|
|
656
|
+
})
|
|
630
657
|
states.push(state)
|
|
631
658
|
|
|
632
659
|
return <div>data: {state.data}</div>
|
|
@@ -645,24 +672,29 @@ describe('useQuery', () => {
|
|
|
645
672
|
|
|
646
673
|
it('should call onSettled after a query has been fetched with an error', async () => {
|
|
647
674
|
const key = queryKey()
|
|
648
|
-
const states: UseQueryResult<string>[] = []
|
|
649
675
|
const onSettled = jest.fn()
|
|
676
|
+
const error = new Error('error')
|
|
650
677
|
|
|
651
678
|
function Page() {
|
|
652
|
-
const state = useQuery(
|
|
679
|
+
const state = useQuery({
|
|
680
|
+
queryKey: key,
|
|
681
|
+
queryFn: async () => {
|
|
682
|
+
await sleep(10)
|
|
683
|
+
return Promise.reject(error)
|
|
684
|
+
},
|
|
653
685
|
retry: false,
|
|
654
686
|
onSettled,
|
|
655
687
|
})
|
|
656
|
-
|
|
657
|
-
return null
|
|
688
|
+
return <div>status: {state.status}</div>
|
|
658
689
|
}
|
|
659
690
|
|
|
660
|
-
renderWithClient(queryClient, <Page />)
|
|
691
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
661
692
|
|
|
662
|
-
await
|
|
663
|
-
|
|
693
|
+
await waitFor(() => {
|
|
694
|
+
rendered.getByText('status: error')
|
|
695
|
+
})
|
|
664
696
|
expect(onSettled).toHaveBeenCalledTimes(1)
|
|
665
|
-
expect(onSettled).toHaveBeenCalledWith(undefined,
|
|
697
|
+
expect(onSettled).toHaveBeenCalledWith(undefined, error)
|
|
666
698
|
})
|
|
667
699
|
|
|
668
700
|
it('should not cancel an ongoing fetch when refetch is called with cancelRefetch=false if we have data already', async () => {
|
|
@@ -670,15 +702,16 @@ describe('useQuery', () => {
|
|
|
670
702
|
let fetchCount = 0
|
|
671
703
|
|
|
672
704
|
function Page() {
|
|
673
|
-
const { refetch } = useQuery(
|
|
674
|
-
key,
|
|
675
|
-
async () => {
|
|
705
|
+
const { refetch } = useQuery({
|
|
706
|
+
queryKey: key,
|
|
707
|
+
queryFn: async () => {
|
|
676
708
|
fetchCount++
|
|
677
709
|
await sleep(10)
|
|
678
710
|
return 'data'
|
|
679
711
|
},
|
|
680
|
-
|
|
681
|
-
|
|
712
|
+
enabled: false,
|
|
713
|
+
initialData: 'initialData',
|
|
714
|
+
})
|
|
682
715
|
|
|
683
716
|
React.useEffect(() => {
|
|
684
717
|
setActTimeout(() => {
|
|
@@ -704,15 +737,16 @@ describe('useQuery', () => {
|
|
|
704
737
|
let fetchCount = 0
|
|
705
738
|
|
|
706
739
|
function Page() {
|
|
707
|
-
const { refetch } = useQuery(
|
|
708
|
-
key,
|
|
709
|
-
async () => {
|
|
740
|
+
const { refetch } = useQuery({
|
|
741
|
+
queryKey: key,
|
|
742
|
+
queryFn: async () => {
|
|
710
743
|
fetchCount++
|
|
711
744
|
await sleep(10)
|
|
712
745
|
return 'data'
|
|
713
746
|
},
|
|
714
|
-
|
|
715
|
-
|
|
747
|
+
enabled: false,
|
|
748
|
+
initialData: 'initialData',
|
|
749
|
+
})
|
|
716
750
|
|
|
717
751
|
React.useEffect(() => {
|
|
718
752
|
setActTimeout(() => {
|
|
@@ -738,15 +772,15 @@ describe('useQuery', () => {
|
|
|
738
772
|
let fetchCount = 0
|
|
739
773
|
|
|
740
774
|
function Page() {
|
|
741
|
-
const { refetch } = useQuery(
|
|
742
|
-
key,
|
|
743
|
-
async () => {
|
|
775
|
+
const { refetch } = useQuery({
|
|
776
|
+
queryKey: key,
|
|
777
|
+
queryFn: async () => {
|
|
744
778
|
fetchCount++
|
|
745
779
|
await sleep(10)
|
|
746
780
|
return 'data'
|
|
747
781
|
},
|
|
748
|
-
|
|
749
|
-
)
|
|
782
|
+
enabled: false,
|
|
783
|
+
})
|
|
750
784
|
|
|
751
785
|
React.useEffect(() => {
|
|
752
786
|
setActTimeout(() => {
|
|
@@ -774,7 +808,7 @@ describe('useQuery', () => {
|
|
|
774
808
|
queryClient.setQueryDefaults(key, { queryFn: () => 'data' })
|
|
775
809
|
|
|
776
810
|
function Page() {
|
|
777
|
-
const state = useQuery<string>(key)
|
|
811
|
+
const state = useQuery<string>({ queryKey: key })
|
|
778
812
|
states.push(state)
|
|
779
813
|
return null
|
|
780
814
|
}
|
|
@@ -788,7 +822,7 @@ describe('useQuery', () => {
|
|
|
788
822
|
expect(states[1]).toMatchObject({ data: 'data' })
|
|
789
823
|
})
|
|
790
824
|
|
|
791
|
-
it('should pick up a query when re-mounting with
|
|
825
|
+
it('should pick up a query when re-mounting with gcTime 0', async () => {
|
|
792
826
|
const key = queryKey()
|
|
793
827
|
const states: UseQueryResult<string>[] = []
|
|
794
828
|
|
|
@@ -808,17 +842,16 @@ describe('useQuery', () => {
|
|
|
808
842
|
}
|
|
809
843
|
|
|
810
844
|
function Component({ value }: { value: string }) {
|
|
811
|
-
const state = useQuery(
|
|
812
|
-
key,
|
|
813
|
-
async () => {
|
|
845
|
+
const state = useQuery({
|
|
846
|
+
queryKey: key,
|
|
847
|
+
queryFn: async () => {
|
|
814
848
|
await sleep(10)
|
|
815
849
|
return 'data: ' + value
|
|
816
850
|
},
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
)
|
|
851
|
+
|
|
852
|
+
gcTime: 0,
|
|
853
|
+
notifyOnChangeProps: 'all',
|
|
854
|
+
})
|
|
822
855
|
states.push(state)
|
|
823
856
|
return (
|
|
824
857
|
<div>
|
|
@@ -838,48 +871,47 @@ describe('useQuery', () => {
|
|
|
838
871
|
expect(states.length).toBe(4)
|
|
839
872
|
// First load
|
|
840
873
|
expect(states[0]).toMatchObject({
|
|
841
|
-
|
|
874
|
+
isPending: true,
|
|
842
875
|
isSuccess: false,
|
|
843
876
|
isFetching: true,
|
|
844
877
|
})
|
|
845
878
|
// First success
|
|
846
879
|
expect(states[1]).toMatchObject({
|
|
847
|
-
|
|
880
|
+
isPending: false,
|
|
848
881
|
isSuccess: true,
|
|
849
882
|
isFetching: false,
|
|
850
883
|
})
|
|
851
884
|
// Switch, goes to fetching
|
|
852
885
|
expect(states[2]).toMatchObject({
|
|
853
|
-
|
|
886
|
+
isPending: false,
|
|
854
887
|
isSuccess: true,
|
|
855
888
|
isFetching: true,
|
|
856
889
|
})
|
|
857
890
|
// Second success
|
|
858
891
|
expect(states[3]).toMatchObject({
|
|
859
|
-
|
|
892
|
+
isPending: false,
|
|
860
893
|
isSuccess: true,
|
|
861
894
|
isFetching: false,
|
|
862
895
|
})
|
|
863
896
|
})
|
|
864
897
|
|
|
865
|
-
it('should not get into an infinite loop when removing a query with
|
|
898
|
+
it('should not get into an infinite loop when removing a query with gcTime 0 and rerendering', async () => {
|
|
866
899
|
const key = queryKey()
|
|
867
900
|
const states: UseQueryResult<string>[] = []
|
|
868
901
|
|
|
869
902
|
function Page() {
|
|
870
903
|
const [, rerender] = React.useState({})
|
|
871
904
|
|
|
872
|
-
const state = useQuery(
|
|
873
|
-
key,
|
|
874
|
-
async () => {
|
|
905
|
+
const state = useQuery({
|
|
906
|
+
queryKey: key,
|
|
907
|
+
queryFn: async () => {
|
|
875
908
|
await sleep(5)
|
|
876
909
|
return 'data'
|
|
877
910
|
},
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
)
|
|
911
|
+
|
|
912
|
+
gcTime: 0,
|
|
913
|
+
notifyOnChangeProps: 'all',
|
|
914
|
+
})
|
|
883
915
|
|
|
884
916
|
states.push(state)
|
|
885
917
|
|
|
@@ -889,7 +921,7 @@ describe('useQuery', () => {
|
|
|
889
921
|
|
|
890
922
|
<button
|
|
891
923
|
onClick={() => {
|
|
892
|
-
|
|
924
|
+
queryClient.removeQueries({ queryKey: key })
|
|
893
925
|
rerender({})
|
|
894
926
|
}}
|
|
895
927
|
>
|
|
@@ -916,15 +948,15 @@ describe('useQuery', () => {
|
|
|
916
948
|
|
|
917
949
|
expect(states.length).toBe(5)
|
|
918
950
|
// First load
|
|
919
|
-
expect(states[0]).toMatchObject({
|
|
951
|
+
expect(states[0]).toMatchObject({ isPending: true, isSuccess: false })
|
|
920
952
|
// First success
|
|
921
|
-
expect(states[1]).toMatchObject({
|
|
953
|
+
expect(states[1]).toMatchObject({ isPending: false, isSuccess: true })
|
|
922
954
|
// Remove
|
|
923
|
-
expect(states[2]).toMatchObject({
|
|
955
|
+
expect(states[2]).toMatchObject({ isPending: true, isSuccess: false })
|
|
924
956
|
// Hook state update
|
|
925
|
-
expect(states[3]).toMatchObject({
|
|
957
|
+
expect(states[3]).toMatchObject({ isPending: true, isSuccess: false })
|
|
926
958
|
// Second success
|
|
927
|
-
expect(states[4]).toMatchObject({
|
|
959
|
+
expect(states[4]).toMatchObject({ isPending: false, isSuccess: true })
|
|
928
960
|
})
|
|
929
961
|
|
|
930
962
|
it('should fetch when refetchOnMount is false and nothing has been fetched yet', async () => {
|
|
@@ -932,7 +964,9 @@ describe('useQuery', () => {
|
|
|
932
964
|
const states: UseQueryResult<string>[] = []
|
|
933
965
|
|
|
934
966
|
function Page() {
|
|
935
|
-
const state = useQuery(
|
|
967
|
+
const state = useQuery({
|
|
968
|
+
queryKey: key,
|
|
969
|
+
queryFn: () => 'test',
|
|
936
970
|
refetchOnMount: false,
|
|
937
971
|
})
|
|
938
972
|
states.push(state)
|
|
@@ -955,7 +989,9 @@ describe('useQuery', () => {
|
|
|
955
989
|
queryClient.setQueryData(key, 'prefetched')
|
|
956
990
|
|
|
957
991
|
function Page() {
|
|
958
|
-
const state = useQuery(
|
|
992
|
+
const state = useQuery({
|
|
993
|
+
queryKey: key,
|
|
994
|
+
queryFn: () => 'test',
|
|
959
995
|
refetchOnMount: false,
|
|
960
996
|
})
|
|
961
997
|
states.push(state)
|
|
@@ -975,7 +1011,9 @@ describe('useQuery', () => {
|
|
|
975
1011
|
const states: UseQueryResult<string>[] = []
|
|
976
1012
|
|
|
977
1013
|
function Page() {
|
|
978
|
-
const state = useQuery(
|
|
1014
|
+
const state = useQuery({
|
|
1015
|
+
queryKey: key,
|
|
1016
|
+
queryFn: () => ({ name: 'test' }),
|
|
979
1017
|
select: (data) => data.name,
|
|
980
1018
|
})
|
|
981
1019
|
states.push(state)
|
|
@@ -1025,7 +1063,9 @@ describe('useQuery', () => {
|
|
|
1025
1063
|
const states: UseQueryResult<string>[] = []
|
|
1026
1064
|
|
|
1027
1065
|
function Page() {
|
|
1028
|
-
const state = useQuery(
|
|
1066
|
+
const state = useQuery({
|
|
1067
|
+
queryKey: key,
|
|
1068
|
+
queryFn: () => ({ name: 'test' }),
|
|
1029
1069
|
select: (data) => data.name,
|
|
1030
1070
|
notifyOnChangeProps: ['data'],
|
|
1031
1071
|
})
|
|
@@ -1066,7 +1106,9 @@ describe('useQuery', () => {
|
|
|
1066
1106
|
const error = new Error('Select Error')
|
|
1067
1107
|
|
|
1068
1108
|
function Page() {
|
|
1069
|
-
const state = useQuery(
|
|
1109
|
+
const state = useQuery({
|
|
1110
|
+
queryKey: key,
|
|
1111
|
+
queryFn: () => ({ name: 'test' }),
|
|
1070
1112
|
select: () => {
|
|
1071
1113
|
throw error
|
|
1072
1114
|
},
|
|
@@ -1082,10 +1124,9 @@ describe('useQuery', () => {
|
|
|
1082
1124
|
rendered.getByText('error')
|
|
1083
1125
|
})
|
|
1084
1126
|
|
|
1085
|
-
expect(mockLogger.error).toHaveBeenCalledWith(error)
|
|
1086
1127
|
expect(states.length).toBe(2)
|
|
1087
1128
|
|
|
1088
|
-
expect(states[0]).toMatchObject({ status: '
|
|
1129
|
+
expect(states[0]).toMatchObject({ status: 'pending', data: undefined })
|
|
1089
1130
|
expect(states[1]).toMatchObject({ status: 'error', error })
|
|
1090
1131
|
})
|
|
1091
1132
|
|
|
@@ -1096,16 +1137,15 @@ describe('useQuery', () => {
|
|
|
1096
1137
|
|
|
1097
1138
|
function Page() {
|
|
1098
1139
|
const [, rerender] = React.useReducer(() => ({}), {})
|
|
1099
|
-
const state = useQuery<string, Error>(
|
|
1100
|
-
key,
|
|
1101
|
-
() => (runs === 0 ? 'test' : 'test2'),
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
)
|
|
1140
|
+
const state = useQuery<string, Error>({
|
|
1141
|
+
queryKey: key,
|
|
1142
|
+
queryFn: () => (runs === 0 ? 'test' : 'test2'),
|
|
1143
|
+
|
|
1144
|
+
select: React.useCallback(() => {
|
|
1145
|
+
runs++
|
|
1146
|
+
throw error
|
|
1147
|
+
}, []),
|
|
1148
|
+
})
|
|
1109
1149
|
return (
|
|
1110
1150
|
<div>
|
|
1111
1151
|
<div>error: {state.error?.message}</div>
|
|
@@ -1132,9 +1172,12 @@ describe('useQuery', () => {
|
|
|
1132
1172
|
const states: UseQueryResult<string>[] = []
|
|
1133
1173
|
|
|
1134
1174
|
function Page() {
|
|
1135
|
-
const state = useQuery(
|
|
1136
|
-
|
|
1137
|
-
|
|
1175
|
+
const state = useQuery({
|
|
1176
|
+
queryKey: key,
|
|
1177
|
+
queryFn: async () => {
|
|
1178
|
+
await sleep(10)
|
|
1179
|
+
return 'test'
|
|
1180
|
+
},
|
|
1138
1181
|
})
|
|
1139
1182
|
|
|
1140
1183
|
states.push(state)
|
|
@@ -1171,7 +1214,7 @@ describe('useQuery', () => {
|
|
|
1171
1214
|
const states: UseQueryResult<string>[] = []
|
|
1172
1215
|
|
|
1173
1216
|
function Page() {
|
|
1174
|
-
const state = useQuery(key, () => 'test')
|
|
1217
|
+
const state = useQuery({ queryKey: key, queryFn: () => 'test' })
|
|
1175
1218
|
|
|
1176
1219
|
states.push(state)
|
|
1177
1220
|
|
|
@@ -1208,15 +1251,19 @@ describe('useQuery', () => {
|
|
|
1208
1251
|
|
|
1209
1252
|
function Page() {
|
|
1210
1253
|
const [, rerender] = React.useState({})
|
|
1211
|
-
const state = useQuery(
|
|
1254
|
+
const state = useQuery({
|
|
1255
|
+
queryKey: key,
|
|
1256
|
+
queryFn: () => ++count,
|
|
1257
|
+
notifyOnChangeProps: 'all',
|
|
1258
|
+
})
|
|
1212
1259
|
|
|
1213
1260
|
states.push(state)
|
|
1214
1261
|
|
|
1215
|
-
const { remove } = state
|
|
1216
|
-
|
|
1217
1262
|
return (
|
|
1218
1263
|
<div>
|
|
1219
|
-
<button onClick={() =>
|
|
1264
|
+
<button onClick={() => queryClient.removeQueries({ queryKey: key })}>
|
|
1265
|
+
remove
|
|
1266
|
+
</button>
|
|
1220
1267
|
<button onClick={() => rerender({})}>rerender</button>
|
|
1221
1268
|
data: {state.data ?? 'null'}
|
|
1222
1269
|
</div>
|
|
@@ -1234,11 +1281,11 @@ describe('useQuery', () => {
|
|
|
1234
1281
|
|
|
1235
1282
|
expect(states.length).toBe(4)
|
|
1236
1283
|
// Initial
|
|
1237
|
-
expect(states[0]).toMatchObject({ status: '
|
|
1284
|
+
expect(states[0]).toMatchObject({ status: 'pending', data: undefined })
|
|
1238
1285
|
// Fetched
|
|
1239
1286
|
expect(states[1]).toMatchObject({ status: 'success', data: 1 })
|
|
1240
1287
|
// Remove + Hook state update, batched
|
|
1241
|
-
expect(states[2]).toMatchObject({ status: '
|
|
1288
|
+
expect(states[2]).toMatchObject({ status: 'pending', data: undefined })
|
|
1242
1289
|
// Fetched
|
|
1243
1290
|
expect(states[3]).toMatchObject({ status: 'success', data: 2 })
|
|
1244
1291
|
})
|
|
@@ -1249,22 +1296,24 @@ describe('useQuery', () => {
|
|
|
1249
1296
|
let count = 0
|
|
1250
1297
|
|
|
1251
1298
|
function Page() {
|
|
1252
|
-
const state = useQuery(
|
|
1253
|
-
key,
|
|
1254
|
-
async () => {
|
|
1299
|
+
const state = useQuery({
|
|
1300
|
+
queryKey: key,
|
|
1301
|
+
queryFn: async () => {
|
|
1255
1302
|
await sleep(10)
|
|
1256
1303
|
return ++count
|
|
1257
1304
|
},
|
|
1258
|
-
|
|
1259
|
-
)
|
|
1305
|
+
notifyOnChangeProps: 'all',
|
|
1306
|
+
})
|
|
1260
1307
|
|
|
1261
1308
|
states.push(state)
|
|
1262
1309
|
|
|
1263
|
-
const {
|
|
1310
|
+
const { refetch } = state
|
|
1264
1311
|
|
|
1265
1312
|
return (
|
|
1266
1313
|
<div>
|
|
1267
|
-
<button onClick={() =>
|
|
1314
|
+
<button onClick={() => queryClient.removeQueries({ queryKey: key })}>
|
|
1315
|
+
remove
|
|
1316
|
+
</button>
|
|
1268
1317
|
<button onClick={() => refetch()}>refetch</button>
|
|
1269
1318
|
data: {state.data ?? 'null'}
|
|
1270
1319
|
</div>
|
|
@@ -1309,15 +1358,15 @@ describe('useQuery', () => {
|
|
|
1309
1358
|
let count = 0
|
|
1310
1359
|
|
|
1311
1360
|
function Page() {
|
|
1312
|
-
const state = useQuery(
|
|
1313
|
-
key,
|
|
1314
|
-
async () => {
|
|
1361
|
+
const state = useQuery({
|
|
1362
|
+
queryKey: key,
|
|
1363
|
+
queryFn: async () => {
|
|
1315
1364
|
await sleep(10)
|
|
1316
1365
|
count++
|
|
1317
1366
|
return count === 1 ? result1 : result2
|
|
1318
1367
|
},
|
|
1319
|
-
|
|
1320
|
-
)
|
|
1368
|
+
notifyOnChangeProps: 'all',
|
|
1369
|
+
})
|
|
1321
1370
|
|
|
1322
1371
|
states.push(state)
|
|
1323
1372
|
|
|
@@ -1364,24 +1413,23 @@ describe('useQuery', () => {
|
|
|
1364
1413
|
queryClient.setQueryData(key, 'set')
|
|
1365
1414
|
|
|
1366
1415
|
function Page() {
|
|
1367
|
-
const result = useQuery(
|
|
1368
|
-
key,
|
|
1369
|
-
async () => {
|
|
1416
|
+
const result = useQuery({
|
|
1417
|
+
queryKey: key,
|
|
1418
|
+
queryFn: async () => {
|
|
1370
1419
|
await sleep(10)
|
|
1371
1420
|
return 'fetched'
|
|
1372
1421
|
},
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
)
|
|
1422
|
+
|
|
1423
|
+
initialData: 'initial',
|
|
1424
|
+
staleTime: Infinity,
|
|
1425
|
+
})
|
|
1378
1426
|
|
|
1379
1427
|
results.push(result)
|
|
1380
1428
|
|
|
1381
1429
|
return (
|
|
1382
1430
|
<div>
|
|
1383
1431
|
<div>isFetching: {result.isFetching}</div>
|
|
1384
|
-
<button onClick={() => queryClient.refetchQueries(key)}>
|
|
1432
|
+
<button onClick={() => queryClient.refetchQueries({ queryKey: key })}>
|
|
1385
1433
|
refetch
|
|
1386
1434
|
</button>
|
|
1387
1435
|
data: {result.data}
|
|
@@ -1408,21 +1456,24 @@ describe('useQuery', () => {
|
|
|
1408
1456
|
let count = 0
|
|
1409
1457
|
|
|
1410
1458
|
function Page() {
|
|
1411
|
-
const state = useQuery(
|
|
1412
|
-
key,
|
|
1413
|
-
async () => {
|
|
1459
|
+
const state = useQuery({
|
|
1460
|
+
queryKey: key,
|
|
1461
|
+
queryFn: async () => {
|
|
1414
1462
|
await sleep(10)
|
|
1415
1463
|
count++
|
|
1416
1464
|
return count
|
|
1417
1465
|
},
|
|
1418
|
-
|
|
1419
|
-
|
|
1466
|
+
staleTime: Infinity,
|
|
1467
|
+
notifyOnChangeProps: 'all',
|
|
1468
|
+
})
|
|
1420
1469
|
|
|
1421
1470
|
states.push(state)
|
|
1422
1471
|
|
|
1423
1472
|
return (
|
|
1424
1473
|
<div>
|
|
1425
|
-
<button
|
|
1474
|
+
<button
|
|
1475
|
+
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
1476
|
+
>
|
|
1426
1477
|
invalidate
|
|
1427
1478
|
</button>
|
|
1428
1479
|
data: {state.data}
|
|
@@ -1474,15 +1525,15 @@ describe('useQuery', () => {
|
|
|
1474
1525
|
let count = 0
|
|
1475
1526
|
|
|
1476
1527
|
function Page() {
|
|
1477
|
-
const state = useQuery(
|
|
1478
|
-
key,
|
|
1479
|
-
async () => {
|
|
1528
|
+
const state = useQuery({
|
|
1529
|
+
queryKey: key,
|
|
1530
|
+
queryFn: async () => {
|
|
1480
1531
|
await sleep(10)
|
|
1481
1532
|
count++
|
|
1482
1533
|
return count
|
|
1483
1534
|
},
|
|
1484
|
-
|
|
1485
|
-
)
|
|
1535
|
+
enabled: false,
|
|
1536
|
+
})
|
|
1486
1537
|
|
|
1487
1538
|
states.push(state)
|
|
1488
1539
|
|
|
@@ -1514,21 +1565,21 @@ describe('useQuery', () => {
|
|
|
1514
1565
|
let count = 0
|
|
1515
1566
|
|
|
1516
1567
|
function Page() {
|
|
1517
|
-
const state = useQuery(
|
|
1518
|
-
key,
|
|
1519
|
-
async () => {
|
|
1568
|
+
const state = useQuery({
|
|
1569
|
+
queryKey: key,
|
|
1570
|
+
queryFn: async () => {
|
|
1520
1571
|
await sleep(10)
|
|
1521
1572
|
count++
|
|
1522
1573
|
return count
|
|
1523
1574
|
},
|
|
1524
|
-
|
|
1525
|
-
)
|
|
1575
|
+
enabled: false,
|
|
1576
|
+
})
|
|
1526
1577
|
|
|
1527
1578
|
states.push(state)
|
|
1528
1579
|
|
|
1529
1580
|
React.useEffect(() => {
|
|
1530
1581
|
setActTimeout(() => {
|
|
1531
|
-
queryClient.invalidateQueries(key)
|
|
1582
|
+
queryClient.invalidateQueries({ queryKey: key })
|
|
1532
1583
|
}, 20)
|
|
1533
1584
|
}, [])
|
|
1534
1585
|
|
|
@@ -1555,14 +1606,14 @@ describe('useQuery', () => {
|
|
|
1555
1606
|
function Page() {
|
|
1556
1607
|
const [count, setCount] = React.useState(0)
|
|
1557
1608
|
|
|
1558
|
-
const state = useQuery(
|
|
1559
|
-
[key, count],
|
|
1560
|
-
async () => {
|
|
1609
|
+
const state = useQuery({
|
|
1610
|
+
queryKey: [key, count],
|
|
1611
|
+
queryFn: async () => {
|
|
1561
1612
|
await sleep(5)
|
|
1562
1613
|
return count
|
|
1563
1614
|
},
|
|
1564
|
-
|
|
1565
|
-
)
|
|
1615
|
+
enabled: count === 0,
|
|
1616
|
+
})
|
|
1566
1617
|
|
|
1567
1618
|
states.push(state)
|
|
1568
1619
|
|
|
@@ -1611,21 +1662,21 @@ describe('useQuery', () => {
|
|
|
1611
1662
|
})
|
|
1612
1663
|
})
|
|
1613
1664
|
|
|
1614
|
-
it('should keep the previous data when
|
|
1665
|
+
it('should keep the previous data when placeholderData is set', async () => {
|
|
1615
1666
|
const key = queryKey()
|
|
1616
1667
|
const states: UseQueryResult<number>[] = []
|
|
1617
1668
|
|
|
1618
1669
|
function Page() {
|
|
1619
1670
|
const [count, setCount] = React.useState(0)
|
|
1620
1671
|
|
|
1621
|
-
const state = useQuery(
|
|
1622
|
-
[key, count],
|
|
1623
|
-
async () => {
|
|
1672
|
+
const state = useQuery({
|
|
1673
|
+
queryKey: [key, count],
|
|
1674
|
+
queryFn: async () => {
|
|
1624
1675
|
await sleep(10)
|
|
1625
1676
|
return count
|
|
1626
1677
|
},
|
|
1627
|
-
|
|
1628
|
-
)
|
|
1678
|
+
placeholderData: keepPreviousData,
|
|
1679
|
+
})
|
|
1629
1680
|
|
|
1630
1681
|
states.push(state)
|
|
1631
1682
|
|
|
@@ -1650,50 +1701,48 @@ describe('useQuery', () => {
|
|
|
1650
1701
|
data: undefined,
|
|
1651
1702
|
isFetching: true,
|
|
1652
1703
|
isSuccess: false,
|
|
1653
|
-
|
|
1704
|
+
isPlaceholderData: false,
|
|
1654
1705
|
})
|
|
1655
1706
|
// Fetched
|
|
1656
1707
|
expect(states[1]).toMatchObject({
|
|
1657
1708
|
data: 0,
|
|
1658
1709
|
isFetching: false,
|
|
1659
1710
|
isSuccess: true,
|
|
1660
|
-
|
|
1711
|
+
isPlaceholderData: false,
|
|
1661
1712
|
})
|
|
1662
1713
|
// Set state
|
|
1663
1714
|
expect(states[2]).toMatchObject({
|
|
1664
1715
|
data: 0,
|
|
1665
1716
|
isFetching: true,
|
|
1666
1717
|
isSuccess: true,
|
|
1667
|
-
|
|
1718
|
+
isPlaceholderData: true,
|
|
1668
1719
|
})
|
|
1669
1720
|
// New data
|
|
1670
1721
|
expect(states[3]).toMatchObject({
|
|
1671
1722
|
data: 1,
|
|
1672
1723
|
isFetching: false,
|
|
1673
1724
|
isSuccess: true,
|
|
1674
|
-
|
|
1725
|
+
isPlaceholderData: false,
|
|
1675
1726
|
})
|
|
1676
1727
|
})
|
|
1677
1728
|
|
|
1678
|
-
it('should transition to error state when
|
|
1729
|
+
it('should transition to error state when placeholderData is set', async () => {
|
|
1679
1730
|
const key = queryKey()
|
|
1680
1731
|
const states: UseQueryResult<number>[] = []
|
|
1681
1732
|
|
|
1682
1733
|
function Page({ count }: { count: number }) {
|
|
1683
|
-
const state = useQuery<number, Error>(
|
|
1684
|
-
[key, count],
|
|
1685
|
-
async () => {
|
|
1734
|
+
const state = useQuery<number, Error>({
|
|
1735
|
+
queryKey: [key, count],
|
|
1736
|
+
queryFn: async () => {
|
|
1686
1737
|
await sleep(10)
|
|
1687
1738
|
if (count === 2) {
|
|
1688
1739
|
throw new Error('Error test')
|
|
1689
1740
|
}
|
|
1690
1741
|
return Promise.resolve(count)
|
|
1691
1742
|
},
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
},
|
|
1696
|
-
)
|
|
1743
|
+
retry: false,
|
|
1744
|
+
placeholderData: keepPreviousData,
|
|
1745
|
+
})
|
|
1697
1746
|
|
|
1698
1747
|
states.push(state)
|
|
1699
1748
|
|
|
@@ -1701,7 +1750,7 @@ describe('useQuery', () => {
|
|
|
1701
1750
|
<div>
|
|
1702
1751
|
<h1>data: {state.data}</h1>
|
|
1703
1752
|
<h2>error: {state.error?.message}</h2>
|
|
1704
|
-
<p>
|
|
1753
|
+
<p>placeholder data: {state.isPlaceholderData}</p>
|
|
1705
1754
|
</div>
|
|
1706
1755
|
)
|
|
1707
1756
|
}
|
|
@@ -1718,9 +1767,9 @@ describe('useQuery', () => {
|
|
|
1718
1767
|
expect(states[0]).toMatchObject({
|
|
1719
1768
|
data: undefined,
|
|
1720
1769
|
isFetching: true,
|
|
1721
|
-
status: '
|
|
1770
|
+
status: 'pending',
|
|
1722
1771
|
error: null,
|
|
1723
|
-
|
|
1772
|
+
isPlaceholderData: false,
|
|
1724
1773
|
})
|
|
1725
1774
|
// Fetched
|
|
1726
1775
|
expect(states[1]).toMatchObject({
|
|
@@ -1728,7 +1777,7 @@ describe('useQuery', () => {
|
|
|
1728
1777
|
isFetching: false,
|
|
1729
1778
|
status: 'success',
|
|
1730
1779
|
error: null,
|
|
1731
|
-
|
|
1780
|
+
isPlaceholderData: false,
|
|
1732
1781
|
})
|
|
1733
1782
|
// rerender Page 1
|
|
1734
1783
|
expect(states[2]).toMatchObject({
|
|
@@ -1736,7 +1785,7 @@ describe('useQuery', () => {
|
|
|
1736
1785
|
isFetching: true,
|
|
1737
1786
|
status: 'success',
|
|
1738
1787
|
error: null,
|
|
1739
|
-
|
|
1788
|
+
isPlaceholderData: true,
|
|
1740
1789
|
})
|
|
1741
1790
|
// Hook state update
|
|
1742
1791
|
expect(states[3]).toMatchObject({
|
|
@@ -1744,7 +1793,7 @@ describe('useQuery', () => {
|
|
|
1744
1793
|
isFetching: true,
|
|
1745
1794
|
status: 'success',
|
|
1746
1795
|
error: null,
|
|
1747
|
-
|
|
1796
|
+
isPlaceholderData: true,
|
|
1748
1797
|
})
|
|
1749
1798
|
// New data
|
|
1750
1799
|
expect(states[4]).toMatchObject({
|
|
@@ -1752,7 +1801,7 @@ describe('useQuery', () => {
|
|
|
1752
1801
|
isFetching: false,
|
|
1753
1802
|
status: 'success',
|
|
1754
1803
|
error: null,
|
|
1755
|
-
|
|
1804
|
+
isPlaceholderData: false,
|
|
1756
1805
|
})
|
|
1757
1806
|
// rerender Page 2
|
|
1758
1807
|
expect(states[5]).toMatchObject({
|
|
@@ -1760,7 +1809,7 @@ describe('useQuery', () => {
|
|
|
1760
1809
|
isFetching: true,
|
|
1761
1810
|
status: 'success',
|
|
1762
1811
|
error: null,
|
|
1763
|
-
|
|
1812
|
+
isPlaceholderData: true,
|
|
1764
1813
|
})
|
|
1765
1814
|
// Hook state update again
|
|
1766
1815
|
expect(states[6]).toMatchObject({
|
|
@@ -1768,33 +1817,34 @@ describe('useQuery', () => {
|
|
|
1768
1817
|
isFetching: true,
|
|
1769
1818
|
status: 'success',
|
|
1770
1819
|
error: null,
|
|
1771
|
-
|
|
1820
|
+
isPlaceholderData: true,
|
|
1772
1821
|
})
|
|
1773
1822
|
// Error
|
|
1774
1823
|
expect(states[7]).toMatchObject({
|
|
1775
1824
|
data: undefined,
|
|
1776
1825
|
isFetching: false,
|
|
1777
1826
|
status: 'error',
|
|
1778
|
-
|
|
1827
|
+
isPlaceholderData: false,
|
|
1779
1828
|
})
|
|
1780
1829
|
expect(states[7]?.error).toHaveProperty('message', 'Error test')
|
|
1781
1830
|
})
|
|
1782
1831
|
|
|
1783
|
-
it('should not show initial data from next query if
|
|
1832
|
+
it('should not show initial data from next query if placeholderData is set', async () => {
|
|
1784
1833
|
const key = queryKey()
|
|
1785
1834
|
const states: DefinedUseQueryResult<number>[] = []
|
|
1786
1835
|
|
|
1787
1836
|
function Page() {
|
|
1788
1837
|
const [count, setCount] = React.useState(0)
|
|
1789
1838
|
|
|
1790
|
-
const state = useQuery(
|
|
1791
|
-
[key, count],
|
|
1792
|
-
async () => {
|
|
1839
|
+
const state = useQuery({
|
|
1840
|
+
queryKey: [key, count],
|
|
1841
|
+
queryFn: async () => {
|
|
1793
1842
|
await sleep(10)
|
|
1794
1843
|
return count
|
|
1795
1844
|
},
|
|
1796
|
-
|
|
1797
|
-
|
|
1845
|
+
initialData: 99,
|
|
1846
|
+
placeholderData: keepPreviousData,
|
|
1847
|
+
})
|
|
1798
1848
|
|
|
1799
1849
|
states.push(state)
|
|
1800
1850
|
|
|
@@ -1828,53 +1878,55 @@ describe('useQuery', () => {
|
|
|
1828
1878
|
data: 99,
|
|
1829
1879
|
isFetching: true,
|
|
1830
1880
|
isSuccess: true,
|
|
1831
|
-
|
|
1881
|
+
isPlaceholderData: false,
|
|
1832
1882
|
})
|
|
1833
1883
|
// Fetched
|
|
1834
1884
|
expect(states[1]).toMatchObject({
|
|
1835
1885
|
data: 0,
|
|
1836
1886
|
isFetching: false,
|
|
1837
1887
|
isSuccess: true,
|
|
1838
|
-
|
|
1888
|
+
isPlaceholderData: false,
|
|
1839
1889
|
})
|
|
1840
1890
|
// Set state
|
|
1841
1891
|
expect(states[2]).toMatchObject({
|
|
1842
1892
|
data: 99,
|
|
1843
1893
|
isFetching: true,
|
|
1844
1894
|
isSuccess: true,
|
|
1845
|
-
|
|
1895
|
+
isPlaceholderData: false,
|
|
1846
1896
|
})
|
|
1847
1897
|
// Hook state update
|
|
1848
1898
|
expect(states[3]).toMatchObject({
|
|
1849
1899
|
data: 99,
|
|
1850
1900
|
isFetching: true,
|
|
1851
1901
|
isSuccess: true,
|
|
1852
|
-
|
|
1902
|
+
isPlaceholderData: false,
|
|
1853
1903
|
})
|
|
1854
1904
|
// New data
|
|
1855
1905
|
expect(states[4]).toMatchObject({
|
|
1856
1906
|
data: 1,
|
|
1857
1907
|
isFetching: false,
|
|
1858
1908
|
isSuccess: true,
|
|
1859
|
-
|
|
1909
|
+
isPlaceholderData: false,
|
|
1860
1910
|
})
|
|
1861
1911
|
})
|
|
1862
1912
|
|
|
1863
|
-
it('should keep the previous data on disabled query when
|
|
1913
|
+
it('should keep the previous data on disabled query when placeholderData is set', async () => {
|
|
1864
1914
|
const key = queryKey()
|
|
1865
1915
|
const states: UseQueryResult<number>[] = []
|
|
1866
1916
|
|
|
1867
1917
|
function Page() {
|
|
1868
1918
|
const [count, setCount] = React.useState(0)
|
|
1869
1919
|
|
|
1870
|
-
const state = useQuery(
|
|
1871
|
-
[key, count],
|
|
1872
|
-
async () => {
|
|
1920
|
+
const state = useQuery({
|
|
1921
|
+
queryKey: [key, count],
|
|
1922
|
+
queryFn: async () => {
|
|
1873
1923
|
await sleep(10)
|
|
1874
1924
|
return count
|
|
1875
1925
|
},
|
|
1876
|
-
|
|
1877
|
-
|
|
1926
|
+
enabled: false,
|
|
1927
|
+
placeholderData: keepPreviousData,
|
|
1928
|
+
notifyOnChangeProps: 'all',
|
|
1929
|
+
})
|
|
1878
1930
|
|
|
1879
1931
|
states.push(state)
|
|
1880
1932
|
|
|
@@ -1921,46 +1973,46 @@ describe('useQuery', () => {
|
|
|
1921
1973
|
data: undefined,
|
|
1922
1974
|
isFetching: false,
|
|
1923
1975
|
isSuccess: false,
|
|
1924
|
-
|
|
1976
|
+
isPlaceholderData: false,
|
|
1925
1977
|
})
|
|
1926
1978
|
// Fetching query
|
|
1927
1979
|
expect(states[1]).toMatchObject({
|
|
1928
1980
|
data: undefined,
|
|
1929
1981
|
isFetching: true,
|
|
1930
1982
|
isSuccess: false,
|
|
1931
|
-
|
|
1983
|
+
isPlaceholderData: false,
|
|
1932
1984
|
})
|
|
1933
1985
|
// Fetched query
|
|
1934
1986
|
expect(states[2]).toMatchObject({
|
|
1935
1987
|
data: 0,
|
|
1936
1988
|
isFetching: false,
|
|
1937
1989
|
isSuccess: true,
|
|
1938
|
-
|
|
1990
|
+
isPlaceholderData: false,
|
|
1939
1991
|
})
|
|
1940
1992
|
// Set state
|
|
1941
1993
|
expect(states[3]).toMatchObject({
|
|
1942
1994
|
data: 0,
|
|
1943
1995
|
isFetching: false,
|
|
1944
1996
|
isSuccess: true,
|
|
1945
|
-
|
|
1997
|
+
isPlaceholderData: true,
|
|
1946
1998
|
})
|
|
1947
1999
|
// Fetching new query
|
|
1948
2000
|
expect(states[4]).toMatchObject({
|
|
1949
2001
|
data: 0,
|
|
1950
2002
|
isFetching: true,
|
|
1951
2003
|
isSuccess: true,
|
|
1952
|
-
|
|
2004
|
+
isPlaceholderData: true,
|
|
1953
2005
|
})
|
|
1954
2006
|
// Fetched new query
|
|
1955
2007
|
expect(states[5]).toMatchObject({
|
|
1956
2008
|
data: 1,
|
|
1957
2009
|
isFetching: false,
|
|
1958
2010
|
isSuccess: true,
|
|
1959
|
-
|
|
2011
|
+
isPlaceholderData: false,
|
|
1960
2012
|
})
|
|
1961
2013
|
})
|
|
1962
2014
|
|
|
1963
|
-
it('should keep the previous data on disabled query when
|
|
2015
|
+
it('should keep the previous data on disabled query when placeholderData is set and switching query key multiple times', async () => {
|
|
1964
2016
|
const key = queryKey()
|
|
1965
2017
|
const states: UseQueryResult<number>[] = []
|
|
1966
2018
|
|
|
@@ -1971,14 +2023,16 @@ describe('useQuery', () => {
|
|
|
1971
2023
|
function Page() {
|
|
1972
2024
|
const [count, setCount] = React.useState(10)
|
|
1973
2025
|
|
|
1974
|
-
const state = useQuery(
|
|
1975
|
-
[key, count],
|
|
1976
|
-
async () => {
|
|
2026
|
+
const state = useQuery({
|
|
2027
|
+
queryKey: [key, count],
|
|
2028
|
+
queryFn: async () => {
|
|
1977
2029
|
await sleep(10)
|
|
1978
2030
|
return count
|
|
1979
2031
|
},
|
|
1980
|
-
|
|
1981
|
-
|
|
2032
|
+
enabled: false,
|
|
2033
|
+
placeholderData: keepPreviousData,
|
|
2034
|
+
notifyOnChangeProps: 'all',
|
|
2035
|
+
})
|
|
1982
2036
|
|
|
1983
2037
|
states.push(state)
|
|
1984
2038
|
|
|
@@ -2010,35 +2064,35 @@ describe('useQuery', () => {
|
|
|
2010
2064
|
data: 10,
|
|
2011
2065
|
isFetching: false,
|
|
2012
2066
|
isSuccess: true,
|
|
2013
|
-
|
|
2067
|
+
isPlaceholderData: false,
|
|
2014
2068
|
})
|
|
2015
2069
|
// Set state
|
|
2016
2070
|
expect(states[1]).toMatchObject({
|
|
2017
2071
|
data: 10,
|
|
2018
2072
|
isFetching: false,
|
|
2019
2073
|
isSuccess: true,
|
|
2020
|
-
|
|
2074
|
+
isPlaceholderData: true,
|
|
2021
2075
|
})
|
|
2022
2076
|
// State update
|
|
2023
2077
|
expect(states[2]).toMatchObject({
|
|
2024
2078
|
data: 10,
|
|
2025
2079
|
isFetching: false,
|
|
2026
2080
|
isSuccess: true,
|
|
2027
|
-
|
|
2081
|
+
isPlaceholderData: true,
|
|
2028
2082
|
})
|
|
2029
2083
|
// Refetch
|
|
2030
2084
|
expect(states[3]).toMatchObject({
|
|
2031
2085
|
data: 10,
|
|
2032
2086
|
isFetching: true,
|
|
2033
2087
|
isSuccess: true,
|
|
2034
|
-
|
|
2088
|
+
isPlaceholderData: true,
|
|
2035
2089
|
})
|
|
2036
2090
|
// Refetch done
|
|
2037
2091
|
expect(states[4]).toMatchObject({
|
|
2038
2092
|
data: 12,
|
|
2039
2093
|
isFetching: false,
|
|
2040
2094
|
isSuccess: true,
|
|
2041
|
-
|
|
2095
|
+
isPlaceholderData: false,
|
|
2042
2096
|
})
|
|
2043
2097
|
})
|
|
2044
2098
|
|
|
@@ -2047,14 +2101,14 @@ describe('useQuery', () => {
|
|
|
2047
2101
|
const states: UseQueryResult<number>[] = []
|
|
2048
2102
|
|
|
2049
2103
|
function FirstComponent() {
|
|
2050
|
-
const state = useQuery(
|
|
2051
|
-
key,
|
|
2052
|
-
async () => {
|
|
2104
|
+
const state = useQuery({
|
|
2105
|
+
queryKey: key,
|
|
2106
|
+
queryFn: async () => {
|
|
2053
2107
|
await sleep(10)
|
|
2054
2108
|
return 1
|
|
2055
2109
|
},
|
|
2056
|
-
|
|
2057
|
-
)
|
|
2110
|
+
notifyOnChangeProps: 'all',
|
|
2111
|
+
})
|
|
2058
2112
|
const refetch = state.refetch
|
|
2059
2113
|
|
|
2060
2114
|
states.push(state)
|
|
@@ -2068,7 +2122,7 @@ describe('useQuery', () => {
|
|
|
2068
2122
|
}
|
|
2069
2123
|
|
|
2070
2124
|
function SecondComponent() {
|
|
2071
|
-
useQuery(key, () => 2,
|
|
2125
|
+
useQuery({ queryKey: key, queryFn: () => 2, notifyOnChangeProps: 'all' })
|
|
2072
2126
|
return null
|
|
2073
2127
|
}
|
|
2074
2128
|
|
|
@@ -2108,39 +2162,40 @@ describe('useQuery', () => {
|
|
|
2108
2162
|
const states1: UseQueryResult<string>[] = []
|
|
2109
2163
|
const states2: UseQueryResult<string>[] = []
|
|
2110
2164
|
|
|
2111
|
-
await queryClient.prefetchQuery(
|
|
2112
|
-
|
|
2113
|
-
|
|
2165
|
+
await queryClient.prefetchQuery({
|
|
2166
|
+
queryKey: key,
|
|
2167
|
+
queryFn: async () => {
|
|
2168
|
+
await sleep(10)
|
|
2169
|
+
return 'prefetch'
|
|
2170
|
+
},
|
|
2114
2171
|
})
|
|
2115
2172
|
|
|
2116
2173
|
await sleep(20)
|
|
2117
2174
|
|
|
2118
2175
|
function FirstComponent() {
|
|
2119
|
-
const state = useQuery(
|
|
2120
|
-
key,
|
|
2121
|
-
async () => {
|
|
2176
|
+
const state = useQuery({
|
|
2177
|
+
queryKey: key,
|
|
2178
|
+
queryFn: async () => {
|
|
2122
2179
|
await sleep(10)
|
|
2123
2180
|
return 'one'
|
|
2124
2181
|
},
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
)
|
|
2182
|
+
|
|
2183
|
+
staleTime: 100,
|
|
2184
|
+
})
|
|
2129
2185
|
states1.push(state)
|
|
2130
2186
|
return null
|
|
2131
2187
|
}
|
|
2132
2188
|
|
|
2133
2189
|
function SecondComponent() {
|
|
2134
|
-
const state = useQuery(
|
|
2135
|
-
key,
|
|
2136
|
-
async () => {
|
|
2190
|
+
const state = useQuery({
|
|
2191
|
+
queryKey: key,
|
|
2192
|
+
queryFn: async () => {
|
|
2137
2193
|
await sleep(10)
|
|
2138
2194
|
return 'two'
|
|
2139
2195
|
},
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
)
|
|
2196
|
+
|
|
2197
|
+
staleTime: 10,
|
|
2198
|
+
})
|
|
2144
2199
|
states2.push(state)
|
|
2145
2200
|
return null
|
|
2146
2201
|
}
|
|
@@ -2208,7 +2263,9 @@ describe('useQuery', () => {
|
|
|
2208
2263
|
const states: UseQueryResult<string>[] = []
|
|
2209
2264
|
|
|
2210
2265
|
function Page() {
|
|
2211
|
-
const state = useQuery(
|
|
2266
|
+
const state = useQuery({
|
|
2267
|
+
queryKey: key,
|
|
2268
|
+
queryFn: () => 'test',
|
|
2212
2269
|
staleTime: 50,
|
|
2213
2270
|
})
|
|
2214
2271
|
states.push(state)
|
|
@@ -2230,16 +2287,15 @@ describe('useQuery', () => {
|
|
|
2230
2287
|
const states: UseQueryResult<string>[] = []
|
|
2231
2288
|
|
|
2232
2289
|
function Page() {
|
|
2233
|
-
const state = useQuery(
|
|
2234
|
-
key,
|
|
2235
|
-
async () => {
|
|
2290
|
+
const state = useQuery({
|
|
2291
|
+
queryKey: key,
|
|
2292
|
+
queryFn: async () => {
|
|
2236
2293
|
await sleep(5)
|
|
2237
2294
|
return 'test'
|
|
2238
2295
|
},
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
)
|
|
2296
|
+
|
|
2297
|
+
notifyOnChangeProps: ['data'],
|
|
2298
|
+
})
|
|
2243
2299
|
|
|
2244
2300
|
states.push(state)
|
|
2245
2301
|
|
|
@@ -2272,7 +2328,7 @@ describe('useQuery', () => {
|
|
|
2272
2328
|
expect(states.length).toBe(2)
|
|
2273
2329
|
expect(states[0]).toMatchObject({
|
|
2274
2330
|
data: undefined,
|
|
2275
|
-
status: '
|
|
2331
|
+
status: 'pending',
|
|
2276
2332
|
isFetching: true,
|
|
2277
2333
|
})
|
|
2278
2334
|
expect(states[1]).toMatchObject({
|
|
@@ -2288,12 +2344,16 @@ describe('useQuery', () => {
|
|
|
2288
2344
|
const key2 = queryKey()
|
|
2289
2345
|
|
|
2290
2346
|
function Page() {
|
|
2291
|
-
const first = useQuery(
|
|
2347
|
+
const first = useQuery({
|
|
2348
|
+
queryKey: key1,
|
|
2349
|
+
queryFn: () => 'data',
|
|
2292
2350
|
enabled: false,
|
|
2293
2351
|
initialData: 'init',
|
|
2294
2352
|
})
|
|
2295
2353
|
|
|
2296
|
-
const second = useQuery(
|
|
2354
|
+
const second = useQuery({
|
|
2355
|
+
queryKey: key2,
|
|
2356
|
+
queryFn: () => 'data',
|
|
2297
2357
|
enabled: false,
|
|
2298
2358
|
initialData: 'init',
|
|
2299
2359
|
})
|
|
@@ -2330,14 +2390,14 @@ describe('useQuery', () => {
|
|
|
2330
2390
|
}
|
|
2331
2391
|
|
|
2332
2392
|
function Page() {
|
|
2333
|
-
useQuery(key, queryFn1)
|
|
2334
|
-
useQuery(key, queryFn2)
|
|
2393
|
+
useQuery({ queryKey: key, queryFn: queryFn1 })
|
|
2394
|
+
useQuery({ queryKey: key, queryFn: queryFn2 })
|
|
2335
2395
|
return null
|
|
2336
2396
|
}
|
|
2337
2397
|
|
|
2338
2398
|
renderWithClient(queryClient, <Page />)
|
|
2339
2399
|
|
|
2340
|
-
expect(queryCache.find(key)!.options.queryFn).toBe(queryFn1)
|
|
2400
|
+
expect(queryCache.find({ queryKey: key })!.options.queryFn).toBe(queryFn1)
|
|
2341
2401
|
})
|
|
2342
2402
|
|
|
2343
2403
|
it('should batch re-renders', async () => {
|
|
@@ -2351,8 +2411,8 @@ describe('useQuery', () => {
|
|
|
2351
2411
|
}
|
|
2352
2412
|
|
|
2353
2413
|
function Page() {
|
|
2354
|
-
const query1 = useQuery(key, queryFn)
|
|
2355
|
-
const query2 = useQuery(key, queryFn)
|
|
2414
|
+
const query1 = useQuery({ queryKey: key, queryFn })
|
|
2415
|
+
const query2 = useQuery({ queryKey: key, queryFn })
|
|
2356
2416
|
renders++
|
|
2357
2417
|
|
|
2358
2418
|
return (
|
|
@@ -2385,12 +2445,16 @@ describe('useQuery', () => {
|
|
|
2385
2445
|
|
|
2386
2446
|
function Page() {
|
|
2387
2447
|
const [count, setCount] = React.useState(0)
|
|
2388
|
-
useQuery(
|
|
2448
|
+
useQuery({
|
|
2449
|
+
queryKey: key,
|
|
2450
|
+
queryFn,
|
|
2389
2451
|
onSuccess: () => {
|
|
2390
2452
|
setCount((x) => x + 1)
|
|
2391
2453
|
},
|
|
2392
2454
|
})
|
|
2393
|
-
useQuery(
|
|
2455
|
+
useQuery({
|
|
2456
|
+
queryKey: key,
|
|
2457
|
+
queryFn,
|
|
2394
2458
|
onSuccess: () => {
|
|
2395
2459
|
setCount((x) => x + 1)
|
|
2396
2460
|
},
|
|
@@ -2408,8 +2472,8 @@ describe('useQuery', () => {
|
|
|
2408
2472
|
|
|
2409
2473
|
await waitFor(() => rendered.getByText('count: 2'))
|
|
2410
2474
|
|
|
2411
|
-
// Should be
|
|
2412
|
-
expect(renders).toBe(
|
|
2475
|
+
// Should be 3 instead of 5
|
|
2476
|
+
expect(renders).toBe(3)
|
|
2413
2477
|
|
|
2414
2478
|
// Both callbacks should have been executed
|
|
2415
2479
|
expect(callbackCount).toBe(2)
|
|
@@ -2420,7 +2484,7 @@ describe('useQuery', () => {
|
|
|
2420
2484
|
|
|
2421
2485
|
function Page() {
|
|
2422
2486
|
const [, setNewState] = React.useState('state')
|
|
2423
|
-
const state = useQuery(key, () => 'data')
|
|
2487
|
+
const state = useQuery({ queryKey: key, queryFn: () => 'data' })
|
|
2424
2488
|
React.useEffect(() => {
|
|
2425
2489
|
setActTimeout(() => {
|
|
2426
2490
|
queryClient.setQueryData(key, 'new')
|
|
@@ -2437,15 +2501,17 @@ describe('useQuery', () => {
|
|
|
2437
2501
|
})
|
|
2438
2502
|
|
|
2439
2503
|
// See https://github.com/tannerlinsley/react-query/issues/170
|
|
2440
|
-
it('should start with status
|
|
2504
|
+
it('should start with status pending, fetchStatus idle if enabled is false', async () => {
|
|
2441
2505
|
const key1 = queryKey()
|
|
2442
2506
|
const key2 = queryKey()
|
|
2443
2507
|
|
|
2444
2508
|
function Page() {
|
|
2445
|
-
const first = useQuery(
|
|
2509
|
+
const first = useQuery({
|
|
2510
|
+
queryKey: key1,
|
|
2511
|
+
queryFn: () => 'data',
|
|
2446
2512
|
enabled: false,
|
|
2447
2513
|
})
|
|
2448
|
-
const second = useQuery(key2, () => 'data')
|
|
2514
|
+
const second = useQuery({ queryKey: key2, queryFn: () => 'data' })
|
|
2449
2515
|
|
|
2450
2516
|
return (
|
|
2451
2517
|
<div>
|
|
@@ -2463,19 +2529,22 @@ describe('useQuery', () => {
|
|
|
2463
2529
|
|
|
2464
2530
|
// use "act" to wait for state update and prevent console warning
|
|
2465
2531
|
|
|
2466
|
-
rendered.getByText('First Status:
|
|
2467
|
-
await waitFor(() => rendered.getByText('Second Status:
|
|
2532
|
+
rendered.getByText('First Status: pending, idle')
|
|
2533
|
+
await waitFor(() => rendered.getByText('Second Status: pending, fetching'))
|
|
2468
2534
|
await waitFor(() => rendered.getByText('Second Status: success, idle'))
|
|
2469
2535
|
})
|
|
2470
2536
|
|
|
2471
2537
|
// See https://github.com/tannerlinsley/react-query/issues/144
|
|
2472
|
-
it('should be in "
|
|
2538
|
+
it('should be in "pending" state by default', async () => {
|
|
2473
2539
|
const key = queryKey()
|
|
2474
2540
|
|
|
2475
2541
|
function Page() {
|
|
2476
|
-
const { status } = useQuery(
|
|
2477
|
-
|
|
2478
|
-
|
|
2542
|
+
const { status } = useQuery({
|
|
2543
|
+
queryKey: key,
|
|
2544
|
+
queryFn: async () => {
|
|
2545
|
+
await sleep(10)
|
|
2546
|
+
return 'test'
|
|
2547
|
+
},
|
|
2479
2548
|
})
|
|
2480
2549
|
|
|
2481
2550
|
return <div>status: {status}</div>
|
|
@@ -2483,7 +2552,7 @@ describe('useQuery', () => {
|
|
|
2483
2552
|
|
|
2484
2553
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
2485
2554
|
|
|
2486
|
-
rendered.getByText('status:
|
|
2555
|
+
rendered.getByText('status: pending')
|
|
2487
2556
|
})
|
|
2488
2557
|
|
|
2489
2558
|
// See https://github.com/tannerlinsley/react-query/issues/147
|
|
@@ -2499,7 +2568,7 @@ describe('useQuery', () => {
|
|
|
2499
2568
|
}
|
|
2500
2569
|
|
|
2501
2570
|
function Page() {
|
|
2502
|
-
const state = useQuery([key, variables], queryFn)
|
|
2571
|
+
const state = useQuery({ queryKey: [key, variables], queryFn })
|
|
2503
2572
|
states.push(state)
|
|
2504
2573
|
return <div>{state.status}</div>
|
|
2505
2574
|
}
|
|
@@ -2518,7 +2587,9 @@ describe('useQuery', () => {
|
|
|
2518
2587
|
const queryFn = jest.fn<string, unknown[]>().mockReturnValue('data')
|
|
2519
2588
|
|
|
2520
2589
|
function Page() {
|
|
2521
|
-
const { data = 'default' } = useQuery(
|
|
2590
|
+
const { data = 'default' } = useQuery({
|
|
2591
|
+
queryKey: key,
|
|
2592
|
+
queryFn,
|
|
2522
2593
|
enabled: false,
|
|
2523
2594
|
})
|
|
2524
2595
|
|
|
@@ -2534,7 +2605,7 @@ describe('useQuery', () => {
|
|
|
2534
2605
|
await waitFor(() => rendered.getByText('default'))
|
|
2535
2606
|
|
|
2536
2607
|
act(() => {
|
|
2537
|
-
window.dispatchEvent(new
|
|
2608
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2538
2609
|
})
|
|
2539
2610
|
|
|
2540
2611
|
expect(queryFn).not.toHaveBeenCalled()
|
|
@@ -2546,7 +2617,9 @@ describe('useQuery', () => {
|
|
|
2546
2617
|
let count = 0
|
|
2547
2618
|
|
|
2548
2619
|
function Page() {
|
|
2549
|
-
const state = useQuery(
|
|
2620
|
+
const state = useQuery({
|
|
2621
|
+
queryKey: key,
|
|
2622
|
+
queryFn: () => count++,
|
|
2550
2623
|
staleTime: 0,
|
|
2551
2624
|
refetchOnWindowFocus: false,
|
|
2552
2625
|
})
|
|
@@ -2559,7 +2632,7 @@ describe('useQuery', () => {
|
|
|
2559
2632
|
await sleep(10)
|
|
2560
2633
|
|
|
2561
2634
|
act(() => {
|
|
2562
|
-
window.dispatchEvent(new
|
|
2635
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2563
2636
|
})
|
|
2564
2637
|
|
|
2565
2638
|
await sleep(10)
|
|
@@ -2575,7 +2648,9 @@ describe('useQuery', () => {
|
|
|
2575
2648
|
let count = 0
|
|
2576
2649
|
|
|
2577
2650
|
function Page() {
|
|
2578
|
-
const state = useQuery(
|
|
2651
|
+
const state = useQuery({
|
|
2652
|
+
queryKey: key,
|
|
2653
|
+
queryFn: () => count++,
|
|
2579
2654
|
staleTime: 0,
|
|
2580
2655
|
refetchOnWindowFocus: () => false,
|
|
2581
2656
|
})
|
|
@@ -2588,7 +2663,7 @@ describe('useQuery', () => {
|
|
|
2588
2663
|
await sleep(10)
|
|
2589
2664
|
|
|
2590
2665
|
act(() => {
|
|
2591
|
-
window.dispatchEvent(new
|
|
2666
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2592
2667
|
})
|
|
2593
2668
|
|
|
2594
2669
|
await sleep(10)
|
|
@@ -2604,7 +2679,9 @@ describe('useQuery', () => {
|
|
|
2604
2679
|
let count = 0
|
|
2605
2680
|
|
|
2606
2681
|
function Page() {
|
|
2607
|
-
const state = useQuery(
|
|
2682
|
+
const state = useQuery({
|
|
2683
|
+
queryKey: key,
|
|
2684
|
+
queryFn: () => count++,
|
|
2608
2685
|
staleTime: Infinity,
|
|
2609
2686
|
refetchOnWindowFocus: true,
|
|
2610
2687
|
})
|
|
@@ -2617,7 +2694,7 @@ describe('useQuery', () => {
|
|
|
2617
2694
|
await sleep(10)
|
|
2618
2695
|
|
|
2619
2696
|
act(() => {
|
|
2620
|
-
window.dispatchEvent(new
|
|
2697
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2621
2698
|
})
|
|
2622
2699
|
|
|
2623
2700
|
await sleep(10)
|
|
@@ -2633,17 +2710,16 @@ describe('useQuery', () => {
|
|
|
2633
2710
|
let count = 0
|
|
2634
2711
|
|
|
2635
2712
|
function Page() {
|
|
2636
|
-
const state = useQuery(
|
|
2637
|
-
key,
|
|
2638
|
-
async () => {
|
|
2713
|
+
const state = useQuery({
|
|
2714
|
+
queryKey: key,
|
|
2715
|
+
queryFn: async () => {
|
|
2639
2716
|
await sleep(10)
|
|
2640
2717
|
return count++
|
|
2641
2718
|
},
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
)
|
|
2719
|
+
|
|
2720
|
+
staleTime: Infinity,
|
|
2721
|
+
refetchOnWindowFocus: 'always',
|
|
2722
|
+
})
|
|
2647
2723
|
states.push(state)
|
|
2648
2724
|
return null
|
|
2649
2725
|
}
|
|
@@ -2653,7 +2729,7 @@ describe('useQuery', () => {
|
|
|
2653
2729
|
await sleep(20)
|
|
2654
2730
|
|
|
2655
2731
|
act(() => {
|
|
2656
|
-
window.dispatchEvent(new
|
|
2732
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2657
2733
|
})
|
|
2658
2734
|
|
|
2659
2735
|
await sleep(20)
|
|
@@ -2671,18 +2747,17 @@ describe('useQuery', () => {
|
|
|
2671
2747
|
let count = 0
|
|
2672
2748
|
|
|
2673
2749
|
function Page() {
|
|
2674
|
-
const state = useQuery(
|
|
2675
|
-
key,
|
|
2676
|
-
async () => {
|
|
2750
|
+
const state = useQuery({
|
|
2751
|
+
queryKey: key,
|
|
2752
|
+
queryFn: async () => {
|
|
2677
2753
|
await sleep(10)
|
|
2678
|
-
return count++
|
|
2679
|
-
},
|
|
2680
|
-
{
|
|
2681
|
-
staleTime: 0,
|
|
2682
|
-
retry: 0,
|
|
2683
|
-
refetchOnWindowFocus: (query) => (query.state.data || 0) < 1,
|
|
2754
|
+
return count++
|
|
2684
2755
|
},
|
|
2685
|
-
|
|
2756
|
+
|
|
2757
|
+
staleTime: 0,
|
|
2758
|
+
retry: 0,
|
|
2759
|
+
refetchOnWindowFocus: (query) => (query.state.data || 0) < 1,
|
|
2760
|
+
})
|
|
2686
2761
|
states.push(state)
|
|
2687
2762
|
return <div>data: {String(state.data)}</div>
|
|
2688
2763
|
}
|
|
@@ -2696,7 +2771,7 @@ describe('useQuery', () => {
|
|
|
2696
2771
|
expect(states[1]).toMatchObject({ data: 0, isFetching: false })
|
|
2697
2772
|
|
|
2698
2773
|
act(() => {
|
|
2699
|
-
window.dispatchEvent(new
|
|
2774
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2700
2775
|
})
|
|
2701
2776
|
|
|
2702
2777
|
await rendered.findByText('data: 1')
|
|
@@ -2708,7 +2783,7 @@ describe('useQuery', () => {
|
|
|
2708
2783
|
expect(states[3]).toMatchObject({ data: 1, isFetching: false })
|
|
2709
2784
|
|
|
2710
2785
|
act(() => {
|
|
2711
|
-
window.dispatchEvent(new
|
|
2786
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
2712
2787
|
})
|
|
2713
2788
|
|
|
2714
2789
|
await sleep(20)
|
|
@@ -2721,10 +2796,15 @@ describe('useQuery', () => {
|
|
|
2721
2796
|
const key = queryKey()
|
|
2722
2797
|
const states: UseQueryResult<string>[] = []
|
|
2723
2798
|
|
|
2724
|
-
await queryClient.prefetchQuery(
|
|
2799
|
+
await queryClient.prefetchQuery({
|
|
2800
|
+
queryKey: key,
|
|
2801
|
+
queryFn: () => 'prefetched',
|
|
2802
|
+
})
|
|
2725
2803
|
|
|
2726
2804
|
function Page() {
|
|
2727
|
-
const state = useQuery(
|
|
2805
|
+
const state = useQuery({
|
|
2806
|
+
queryKey: key,
|
|
2807
|
+
queryFn: () => 'data',
|
|
2728
2808
|
refetchOnMount: 'always',
|
|
2729
2809
|
staleTime: Infinity,
|
|
2730
2810
|
})
|
|
@@ -2753,12 +2833,17 @@ describe('useQuery', () => {
|
|
|
2753
2833
|
const key = queryKey()
|
|
2754
2834
|
const states: UseQueryResult<string>[] = []
|
|
2755
2835
|
|
|
2756
|
-
await queryClient.prefetchQuery(
|
|
2836
|
+
await queryClient.prefetchQuery({
|
|
2837
|
+
queryKey: key,
|
|
2838
|
+
queryFn: () => 'prefetched',
|
|
2839
|
+
})
|
|
2757
2840
|
|
|
2758
2841
|
await sleep(10)
|
|
2759
2842
|
|
|
2760
2843
|
function Page() {
|
|
2761
|
-
const state = useQuery(
|
|
2844
|
+
const state = useQuery({
|
|
2845
|
+
queryKey: key,
|
|
2846
|
+
queryFn: () => 'data',
|
|
2762
2847
|
refetchOnMount: true,
|
|
2763
2848
|
staleTime: 0,
|
|
2764
2849
|
})
|
|
@@ -2787,18 +2872,18 @@ describe('useQuery', () => {
|
|
|
2787
2872
|
const key = queryKey()
|
|
2788
2873
|
|
|
2789
2874
|
function Page() {
|
|
2790
|
-
const { status, error } = useQuery
|
|
2791
|
-
key,
|
|
2792
|
-
() => {
|
|
2793
|
-
return Promise.reject('Error test jaylen')
|
|
2875
|
+
const { status, error } = useQuery({
|
|
2876
|
+
queryKey: key,
|
|
2877
|
+
queryFn: () => {
|
|
2878
|
+
return Promise.reject(new Error('Error test jaylen'))
|
|
2794
2879
|
},
|
|
2795
|
-
|
|
2796
|
-
)
|
|
2880
|
+
retry: false,
|
|
2881
|
+
})
|
|
2797
2882
|
|
|
2798
2883
|
return (
|
|
2799
2884
|
<div>
|
|
2800
2885
|
<h1>{status}</h1>
|
|
2801
|
-
<h2>{error}</h2>
|
|
2886
|
+
<h2>{error?.message}</h2>
|
|
2802
2887
|
</div>
|
|
2803
2888
|
)
|
|
2804
2889
|
}
|
|
@@ -2809,15 +2894,19 @@ describe('useQuery', () => {
|
|
|
2809
2894
|
await waitFor(() => rendered.getByText('Error test jaylen'))
|
|
2810
2895
|
})
|
|
2811
2896
|
|
|
2812
|
-
it('should throw error if queryFn throws and
|
|
2897
|
+
it('should throw error if queryFn throws and throwErrors is in use', async () => {
|
|
2898
|
+
const consoleMock = jest
|
|
2899
|
+
.spyOn(console, 'error')
|
|
2900
|
+
.mockImplementation(() => undefined)
|
|
2813
2901
|
const key = queryKey()
|
|
2814
2902
|
|
|
2815
2903
|
function Page() {
|
|
2816
|
-
const { status, error } = useQuery<unknown, string>(
|
|
2817
|
-
key,
|
|
2818
|
-
() => Promise.reject('Error test jaylen'),
|
|
2819
|
-
|
|
2820
|
-
|
|
2904
|
+
const { status, error } = useQuery<unknown, string>({
|
|
2905
|
+
queryKey: key,
|
|
2906
|
+
queryFn: () => Promise.reject(new Error('Error test jaylen')),
|
|
2907
|
+
retry: false,
|
|
2908
|
+
throwErrors: true,
|
|
2909
|
+
})
|
|
2821
2910
|
|
|
2822
2911
|
return (
|
|
2823
2912
|
<div>
|
|
@@ -2835,16 +2924,19 @@ describe('useQuery', () => {
|
|
|
2835
2924
|
)
|
|
2836
2925
|
|
|
2837
2926
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
2927
|
+
consoleMock.mockRestore()
|
|
2838
2928
|
})
|
|
2839
2929
|
|
|
2840
|
-
it('should update with data if we observe no properties and
|
|
2930
|
+
it('should update with data if we observe no properties and throwErrors', async () => {
|
|
2841
2931
|
const key = queryKey()
|
|
2842
2932
|
|
|
2843
2933
|
let result: UseQueryResult<string> | undefined
|
|
2844
2934
|
|
|
2845
2935
|
function Page() {
|
|
2846
|
-
const query = useQuery(
|
|
2847
|
-
|
|
2936
|
+
const query = useQuery({
|
|
2937
|
+
queryKey: key,
|
|
2938
|
+
queryFn: () => Promise.resolve('data'),
|
|
2939
|
+
throwErrors: true,
|
|
2848
2940
|
})
|
|
2849
2941
|
|
|
2850
2942
|
React.useEffect(() => {
|
|
@@ -2867,19 +2959,18 @@ describe('useQuery', () => {
|
|
|
2867
2959
|
const key = queryKey()
|
|
2868
2960
|
|
|
2869
2961
|
function Page() {
|
|
2870
|
-
const { status, error } = useQuery
|
|
2871
|
-
key,
|
|
2872
|
-
() => Promise.reject('Local Error'),
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
)
|
|
2962
|
+
const { status, error } = useQuery({
|
|
2963
|
+
queryKey: key,
|
|
2964
|
+
queryFn: () => Promise.reject(new Error('Local Error')),
|
|
2965
|
+
|
|
2966
|
+
retry: false,
|
|
2967
|
+
throwErrors: (err) => err.message !== 'Local Error',
|
|
2968
|
+
})
|
|
2878
2969
|
|
|
2879
2970
|
return (
|
|
2880
2971
|
<div>
|
|
2881
2972
|
<h1>{status}</h1>
|
|
2882
|
-
<h2>{error}</h2>
|
|
2973
|
+
<h2>{error?.message}</h2>
|
|
2883
2974
|
</div>
|
|
2884
2975
|
)
|
|
2885
2976
|
}
|
|
@@ -2896,17 +2987,20 @@ describe('useQuery', () => {
|
|
|
2896
2987
|
})
|
|
2897
2988
|
|
|
2898
2989
|
it('should throw error instead of setting status when error should be thrown', async () => {
|
|
2990
|
+
const consoleMock = jest
|
|
2991
|
+
.spyOn(console, 'error')
|
|
2992
|
+
.mockImplementation(() => undefined)
|
|
2993
|
+
|
|
2899
2994
|
const key = queryKey()
|
|
2900
2995
|
|
|
2901
2996
|
function Page() {
|
|
2902
|
-
const { status, error } = useQuery<unknown, Error>(
|
|
2903
|
-
key,
|
|
2904
|
-
() => Promise.reject(new Error('Remote Error')),
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
)
|
|
2997
|
+
const { status, error } = useQuery<unknown, Error>({
|
|
2998
|
+
queryKey: key,
|
|
2999
|
+
queryFn: () => Promise.reject(new Error('Remote Error')),
|
|
3000
|
+
|
|
3001
|
+
retry: false,
|
|
3002
|
+
throwErrors: (err) => err.message !== 'Local Error',
|
|
3003
|
+
})
|
|
2910
3004
|
|
|
2911
3005
|
return (
|
|
2912
3006
|
<div>
|
|
@@ -2933,6 +3027,7 @@ describe('useQuery', () => {
|
|
|
2933
3027
|
|
|
2934
3028
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
2935
3029
|
await waitFor(() => rendered.getByText('Remote Error'))
|
|
3030
|
+
consoleMock.mockRestore()
|
|
2936
3031
|
})
|
|
2937
3032
|
|
|
2938
3033
|
it('should continue retries when observers unmount and remount while waiting for a retry (#3031)', async () => {
|
|
@@ -2940,24 +3035,23 @@ describe('useQuery', () => {
|
|
|
2940
3035
|
let count = 0
|
|
2941
3036
|
|
|
2942
3037
|
function Page() {
|
|
2943
|
-
const result = useQuery
|
|
2944
|
-
key,
|
|
2945
|
-
async () => {
|
|
3038
|
+
const result = useQuery({
|
|
3039
|
+
queryKey: key,
|
|
3040
|
+
queryFn: async () => {
|
|
2946
3041
|
count++
|
|
2947
3042
|
await sleep(10)
|
|
2948
|
-
return Promise.reject('some error')
|
|
2949
|
-
},
|
|
2950
|
-
{
|
|
2951
|
-
retry: 2,
|
|
2952
|
-
retryDelay: 100,
|
|
3043
|
+
return Promise.reject(new Error('some error'))
|
|
2953
3044
|
},
|
|
2954
|
-
|
|
3045
|
+
|
|
3046
|
+
retry: 2,
|
|
3047
|
+
retryDelay: 100,
|
|
3048
|
+
})
|
|
2955
3049
|
|
|
2956
3050
|
return (
|
|
2957
3051
|
<div>
|
|
2958
|
-
<div>error: {result.error ?? 'null'}</div>
|
|
3052
|
+
<div>error: {result.error?.message ?? 'null'}</div>
|
|
2959
3053
|
<div>failureCount: {result.failureCount}</div>
|
|
2960
|
-
<div>failureReason: {result.failureReason}</div>
|
|
3054
|
+
<div>failureReason: {result.failureReason?.message}</div>
|
|
2961
3055
|
</div>
|
|
2962
3056
|
)
|
|
2963
3057
|
}
|
|
@@ -2990,24 +3084,23 @@ describe('useQuery', () => {
|
|
|
2990
3084
|
let count = 0
|
|
2991
3085
|
|
|
2992
3086
|
function Page() {
|
|
2993
|
-
const result = useQuery
|
|
2994
|
-
key,
|
|
2995
|
-
async () => {
|
|
3087
|
+
const result = useQuery({
|
|
3088
|
+
queryKey: key,
|
|
3089
|
+
queryFn: async () => {
|
|
2996
3090
|
count++
|
|
2997
3091
|
await sleep(10)
|
|
2998
|
-
return Promise.reject('some error')
|
|
2999
|
-
},
|
|
3000
|
-
{
|
|
3001
|
-
retry: 2,
|
|
3002
|
-
retryDelay: 100,
|
|
3092
|
+
return Promise.reject(new Error('some error'))
|
|
3003
3093
|
},
|
|
3004
|
-
|
|
3094
|
+
|
|
3095
|
+
retry: 2,
|
|
3096
|
+
retryDelay: 100,
|
|
3097
|
+
})
|
|
3005
3098
|
|
|
3006
3099
|
return (
|
|
3007
3100
|
<div>
|
|
3008
|
-
<div>error: {result.error ?? 'null'}</div>
|
|
3101
|
+
<div>error: {result.error?.message ?? 'null'}</div>
|
|
3009
3102
|
<div>failureCount: {result.failureCount}</div>
|
|
3010
|
-
<div>failureReason: {result.failureReason}</div>
|
|
3103
|
+
<div>failureReason: {result.failureReason?.message}</div>
|
|
3011
3104
|
</div>
|
|
3012
3105
|
)
|
|
3013
3106
|
}
|
|
@@ -3044,10 +3137,15 @@ describe('useQuery', () => {
|
|
|
3044
3137
|
const key = queryKey()
|
|
3045
3138
|
const states: UseQueryResult<string>[] = []
|
|
3046
3139
|
|
|
3047
|
-
await queryClient.prefetchQuery(
|
|
3140
|
+
await queryClient.prefetchQuery({
|
|
3141
|
+
queryKey: key,
|
|
3142
|
+
queryFn: () => 'prefetched',
|
|
3143
|
+
})
|
|
3048
3144
|
|
|
3049
3145
|
function Page() {
|
|
3050
|
-
const state = useQuery(
|
|
3146
|
+
const state = useQuery({
|
|
3147
|
+
queryKey: key,
|
|
3148
|
+
queryFn: () => 'data',
|
|
3051
3149
|
refetchOnMount: 'always',
|
|
3052
3150
|
staleTime: 50,
|
|
3053
3151
|
})
|
|
@@ -3088,7 +3186,9 @@ describe('useQuery', () => {
|
|
|
3088
3186
|
const states: DefinedUseQueryResult<string>[] = []
|
|
3089
3187
|
|
|
3090
3188
|
function Page() {
|
|
3091
|
-
const state = useQuery(
|
|
3189
|
+
const state = useQuery({
|
|
3190
|
+
queryKey: key,
|
|
3191
|
+
queryFn: () => 'data',
|
|
3092
3192
|
initialData: 'initial',
|
|
3093
3193
|
})
|
|
3094
3194
|
states.push(state)
|
|
@@ -3118,7 +3218,9 @@ describe('useQuery', () => {
|
|
|
3118
3218
|
const states: DefinedUseQueryResult<string>[] = []
|
|
3119
3219
|
|
|
3120
3220
|
function Page() {
|
|
3121
|
-
const state = useQuery(
|
|
3221
|
+
const state = useQuery({
|
|
3222
|
+
queryKey: key,
|
|
3223
|
+
queryFn: () => 'data',
|
|
3122
3224
|
staleTime: 50,
|
|
3123
3225
|
initialData: 'initial',
|
|
3124
3226
|
})
|
|
@@ -3150,7 +3252,9 @@ describe('useQuery', () => {
|
|
|
3150
3252
|
const oneSecondAgo = Date.now() - 1000
|
|
3151
3253
|
|
|
3152
3254
|
function Page() {
|
|
3153
|
-
const state = useQuery(
|
|
3255
|
+
const state = useQuery({
|
|
3256
|
+
queryKey: key,
|
|
3257
|
+
queryFn: () => 'data',
|
|
3154
3258
|
staleTime: 50,
|
|
3155
3259
|
initialData: 'initial',
|
|
3156
3260
|
initialDataUpdatedAt: oneSecondAgo,
|
|
@@ -3186,7 +3290,9 @@ describe('useQuery', () => {
|
|
|
3186
3290
|
const states: DefinedUseQueryResult<string>[] = []
|
|
3187
3291
|
|
|
3188
3292
|
function Page() {
|
|
3189
|
-
const state = useQuery(
|
|
3293
|
+
const state = useQuery({
|
|
3294
|
+
queryKey: key,
|
|
3295
|
+
queryFn: () => 'data',
|
|
3190
3296
|
staleTime: 10 * 1000, // 10 seconds
|
|
3191
3297
|
initialData: 'initial',
|
|
3192
3298
|
initialDataUpdatedAt: 0,
|
|
@@ -3218,7 +3324,9 @@ describe('useQuery', () => {
|
|
|
3218
3324
|
|
|
3219
3325
|
function Page() {
|
|
3220
3326
|
const [count, setCount] = React.useState(0)
|
|
3221
|
-
const state = useQuery(
|
|
3327
|
+
const state = useQuery({
|
|
3328
|
+
queryKey: [key, count],
|
|
3329
|
+
queryFn: () => ({ count: 10 }),
|
|
3222
3330
|
staleTime: Infinity,
|
|
3223
3331
|
initialData: () => ({ count }),
|
|
3224
3332
|
})
|
|
@@ -3249,31 +3357,29 @@ describe('useQuery', () => {
|
|
|
3249
3357
|
|
|
3250
3358
|
const queryFn = jest.fn<unknown, unknown[]>()
|
|
3251
3359
|
queryFn.mockImplementation(() => {
|
|
3252
|
-
return Promise.reject('Error test Barrett')
|
|
3360
|
+
return Promise.reject(new Error('Error test Barrett'))
|
|
3253
3361
|
})
|
|
3254
3362
|
|
|
3255
3363
|
function Page() {
|
|
3256
|
-
const { status, failureCount, failureReason } = useQuery
|
|
3257
|
-
key,
|
|
3364
|
+
const { status, failureCount, failureReason } = useQuery({
|
|
3365
|
+
queryKey: key,
|
|
3258
3366
|
queryFn,
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
},
|
|
3263
|
-
)
|
|
3367
|
+
retry: 1,
|
|
3368
|
+
retryDelay: 1,
|
|
3369
|
+
})
|
|
3264
3370
|
|
|
3265
3371
|
return (
|
|
3266
3372
|
<div>
|
|
3267
3373
|
<h1>{status}</h1>
|
|
3268
3374
|
<h2>Failed {failureCount} times</h2>
|
|
3269
|
-
<h2>Failed because {failureReason}</h2>
|
|
3375
|
+
<h2>Failed because {failureReason?.message}</h2>
|
|
3270
3376
|
</div>
|
|
3271
3377
|
)
|
|
3272
3378
|
}
|
|
3273
3379
|
|
|
3274
3380
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3275
3381
|
|
|
3276
|
-
await waitFor(() => rendered.getByText('
|
|
3382
|
+
await waitFor(() => rendered.getByText('pending'))
|
|
3277
3383
|
await waitFor(() => rendered.getByText('error'))
|
|
3278
3384
|
|
|
3279
3385
|
// query should fail `retry + 1` times, since first time isn't a "retry"
|
|
@@ -3289,36 +3395,34 @@ describe('useQuery', () => {
|
|
|
3289
3395
|
const queryFn = jest.fn<unknown, unknown[]>()
|
|
3290
3396
|
|
|
3291
3397
|
queryFn.mockImplementationOnce(() => {
|
|
3292
|
-
return Promise.reject('Error test Tanner')
|
|
3398
|
+
return Promise.reject(new Error('Error test Tanner'))
|
|
3293
3399
|
})
|
|
3294
3400
|
|
|
3295
3401
|
queryFn.mockImplementation(() => {
|
|
3296
|
-
return Promise.reject('NoRetry')
|
|
3402
|
+
return Promise.reject(new Error('NoRetry'))
|
|
3297
3403
|
})
|
|
3298
3404
|
|
|
3299
3405
|
function Page() {
|
|
3300
|
-
const { status, failureCount, failureReason, error } = useQuery
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
[string]
|
|
3304
|
-
>(key, queryFn, {
|
|
3406
|
+
const { status, failureCount, failureReason, error } = useQuery({
|
|
3407
|
+
queryKey: key,
|
|
3408
|
+
queryFn,
|
|
3305
3409
|
retryDelay: 1,
|
|
3306
|
-
retry: (_failureCount, err) => err !== 'NoRetry',
|
|
3410
|
+
retry: (_failureCount, err) => err.message !== 'NoRetry',
|
|
3307
3411
|
})
|
|
3308
3412
|
|
|
3309
3413
|
return (
|
|
3310
3414
|
<div>
|
|
3311
3415
|
<h1>{status}</h1>
|
|
3312
3416
|
<h2>Failed {failureCount} times</h2>
|
|
3313
|
-
<h2>Failed because {failureReason}</h2>
|
|
3314
|
-
<h2>{error}</h2>
|
|
3417
|
+
<h2>Failed because {failureReason?.message}</h2>
|
|
3418
|
+
<h2>{error?.message}</h2>
|
|
3315
3419
|
</div>
|
|
3316
3420
|
)
|
|
3317
3421
|
}
|
|
3318
3422
|
|
|
3319
3423
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3320
3424
|
|
|
3321
|
-
await waitFor(() => rendered.getByText('
|
|
3425
|
+
await waitFor(() => rendered.getByText('pending'))
|
|
3322
3426
|
await waitFor(() => rendered.getByText('error'))
|
|
3323
3427
|
|
|
3324
3428
|
await waitFor(() => rendered.getByText('Failed 2 times'))
|
|
@@ -3340,7 +3444,9 @@ describe('useQuery', () => {
|
|
|
3340
3444
|
})
|
|
3341
3445
|
|
|
3342
3446
|
function Page() {
|
|
3343
|
-
const { status, failureCount, failureReason } = useQuery(
|
|
3447
|
+
const { status, failureCount, failureReason } = useQuery({
|
|
3448
|
+
queryKey: key,
|
|
3449
|
+
queryFn,
|
|
3344
3450
|
retry: 1,
|
|
3345
3451
|
retryDelay: (_, error: DelayError) => error.delay,
|
|
3346
3452
|
})
|
|
@@ -3376,17 +3482,15 @@ describe('useQuery', () => {
|
|
|
3376
3482
|
let count = 0
|
|
3377
3483
|
|
|
3378
3484
|
function Page() {
|
|
3379
|
-
const query = useQuery<unknown, string>(
|
|
3380
|
-
key,
|
|
3381
|
-
() => {
|
|
3485
|
+
const query = useQuery<unknown, string>({
|
|
3486
|
+
queryKey: key,
|
|
3487
|
+
queryFn: () => {
|
|
3382
3488
|
count++
|
|
3383
3489
|
return Promise.reject<unknown>(`fetching error ${count}`)
|
|
3384
3490
|
},
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
},
|
|
3389
|
-
)
|
|
3491
|
+
retry: 3,
|
|
3492
|
+
retryDelay: 1,
|
|
3493
|
+
})
|
|
3390
3494
|
|
|
3391
3495
|
return (
|
|
3392
3496
|
<div>
|
|
@@ -3403,7 +3507,7 @@ describe('useQuery', () => {
|
|
|
3403
3507
|
// The query should display the first error result
|
|
3404
3508
|
await waitFor(() => rendered.getByText('failureCount 1'))
|
|
3405
3509
|
await waitFor(() => rendered.getByText('failureReason fetching error 1'))
|
|
3406
|
-
await waitFor(() => rendered.getByText('status
|
|
3510
|
+
await waitFor(() => rendered.getByText('status pending'))
|
|
3407
3511
|
await waitFor(() => rendered.getByText('error null'))
|
|
3408
3512
|
|
|
3409
3513
|
// Check if the query really paused
|
|
@@ -3414,7 +3518,7 @@ describe('useQuery', () => {
|
|
|
3414
3518
|
act(() => {
|
|
3415
3519
|
// reset visibilityState to original value
|
|
3416
3520
|
visibilityMock.mockRestore()
|
|
3417
|
-
window.dispatchEvent(new
|
|
3521
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
3418
3522
|
})
|
|
3419
3523
|
|
|
3420
3524
|
// Wait for the final result
|
|
@@ -3427,9 +3531,6 @@ describe('useQuery', () => {
|
|
|
3427
3531
|
await sleep(10)
|
|
3428
3532
|
await waitFor(() => rendered.getByText('failureCount 4'))
|
|
3429
3533
|
await waitFor(() => rendered.getByText('failureReason fetching error 4'))
|
|
3430
|
-
|
|
3431
|
-
// Check if the error has been logged in the console
|
|
3432
|
-
expect(mockLogger.error).toHaveBeenCalledWith('fetching error 4')
|
|
3433
3534
|
})
|
|
3434
3535
|
|
|
3435
3536
|
it('should fetch on mount when a query was already created with setQueryData', async () => {
|
|
@@ -3439,7 +3540,7 @@ describe('useQuery', () => {
|
|
|
3439
3540
|
queryClient.setQueryData(key, 'prefetched')
|
|
3440
3541
|
|
|
3441
3542
|
function Page() {
|
|
3442
|
-
const state = useQuery(key, () => 'data')
|
|
3543
|
+
const state = useQuery({ queryKey: key, queryFn: () => 'data' })
|
|
3443
3544
|
states.push(state)
|
|
3444
3545
|
return null
|
|
3445
3546
|
}
|
|
@@ -3474,9 +3575,12 @@ describe('useQuery', () => {
|
|
|
3474
3575
|
queryClient.setQueryData(key, 'prefetched')
|
|
3475
3576
|
|
|
3476
3577
|
function Page() {
|
|
3477
|
-
const state = useQuery(
|
|
3478
|
-
|
|
3479
|
-
|
|
3578
|
+
const state = useQuery({
|
|
3579
|
+
queryKey: key,
|
|
3580
|
+
queryFn: async () => {
|
|
3581
|
+
await sleep(10)
|
|
3582
|
+
return 'data'
|
|
3583
|
+
},
|
|
3480
3584
|
})
|
|
3481
3585
|
states.push(state)
|
|
3482
3586
|
return (
|
|
@@ -3493,7 +3597,7 @@ describe('useQuery', () => {
|
|
|
3493
3597
|
act(() => {
|
|
3494
3598
|
// reset visibilityState to original value
|
|
3495
3599
|
visibilityMock.mockRestore()
|
|
3496
|
-
window.dispatchEvent(new
|
|
3600
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
3497
3601
|
})
|
|
3498
3602
|
|
|
3499
3603
|
await waitFor(() => expect(states.length).toBe(4))
|
|
@@ -3533,14 +3637,16 @@ describe('useQuery', () => {
|
|
|
3533
3637
|
const prefetchQueryFn = jest.fn<string, unknown[]>()
|
|
3534
3638
|
prefetchQueryFn.mockImplementation(() => 'not yet...')
|
|
3535
3639
|
|
|
3536
|
-
await queryClient.prefetchQuery(
|
|
3640
|
+
await queryClient.prefetchQuery({
|
|
3641
|
+
queryKey: key,
|
|
3642
|
+
queryFn: prefetchQueryFn,
|
|
3537
3643
|
staleTime: 10,
|
|
3538
3644
|
})
|
|
3539
3645
|
|
|
3540
3646
|
await sleep(11)
|
|
3541
3647
|
|
|
3542
3648
|
function Page() {
|
|
3543
|
-
const state = useQuery(key, queryFn)
|
|
3649
|
+
const state = useQuery({ queryKey: key, queryFn })
|
|
3544
3650
|
states.push(state)
|
|
3545
3651
|
return null
|
|
3546
3652
|
}
|
|
@@ -3565,16 +3671,16 @@ describe('useQuery', () => {
|
|
|
3565
3671
|
return 'not yet...'
|
|
3566
3672
|
})
|
|
3567
3673
|
|
|
3568
|
-
await queryClient.prefetchQuery(
|
|
3674
|
+
await queryClient.prefetchQuery({
|
|
3675
|
+
queryKey: key,
|
|
3676
|
+
queryFn: prefetchQueryFn,
|
|
3569
3677
|
staleTime: 1000,
|
|
3570
3678
|
})
|
|
3571
3679
|
|
|
3572
3680
|
await sleep(0)
|
|
3573
3681
|
|
|
3574
3682
|
function Page() {
|
|
3575
|
-
useQuery(key, queryFn,
|
|
3576
|
-
staleTime: 1000,
|
|
3577
|
-
})
|
|
3683
|
+
useQuery({ queryKey: key, queryFn, staleTime: 1000 })
|
|
3578
3684
|
return null
|
|
3579
3685
|
}
|
|
3580
3686
|
|
|
@@ -3593,9 +3699,9 @@ describe('useQuery', () => {
|
|
|
3593
3699
|
function Page() {
|
|
3594
3700
|
let counter = 0
|
|
3595
3701
|
|
|
3596
|
-
const query = useQuery<string, Error>(
|
|
3597
|
-
key,
|
|
3598
|
-
async () => {
|
|
3702
|
+
const query = useQuery<string, Error>({
|
|
3703
|
+
queryKey: key,
|
|
3704
|
+
queryFn: async () => {
|
|
3599
3705
|
if (counter < 2) {
|
|
3600
3706
|
counter++
|
|
3601
3707
|
throw new Error('error')
|
|
@@ -3603,8 +3709,8 @@ describe('useQuery', () => {
|
|
|
3603
3709
|
return 'data'
|
|
3604
3710
|
}
|
|
3605
3711
|
},
|
|
3606
|
-
|
|
3607
|
-
)
|
|
3712
|
+
retryDelay: 10,
|
|
3713
|
+
})
|
|
3608
3714
|
|
|
3609
3715
|
return (
|
|
3610
3716
|
<div>
|
|
@@ -3631,23 +3737,23 @@ describe('useQuery', () => {
|
|
|
3631
3737
|
const [enabled, setEnabled] = React.useState(false)
|
|
3632
3738
|
const [isPrefetched, setPrefetched] = React.useState(false)
|
|
3633
3739
|
|
|
3634
|
-
const query = useQuery(
|
|
3635
|
-
key,
|
|
3636
|
-
async () => {
|
|
3740
|
+
const query = useQuery({
|
|
3741
|
+
queryKey: key,
|
|
3742
|
+
queryFn: async () => {
|
|
3637
3743
|
count++
|
|
3638
3744
|
await sleep(10)
|
|
3639
3745
|
return count
|
|
3640
3746
|
},
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
)
|
|
3747
|
+
|
|
3748
|
+
enabled,
|
|
3749
|
+
})
|
|
3645
3750
|
|
|
3646
3751
|
React.useEffect(() => {
|
|
3647
3752
|
async function prefetch() {
|
|
3648
|
-
await queryClient.prefetchQuery(
|
|
3649
|
-
|
|
3650
|
-
|
|
3753
|
+
await queryClient.prefetchQuery({
|
|
3754
|
+
queryKey: key,
|
|
3755
|
+
queryFn: () => Promise.resolve('prefetched data'),
|
|
3756
|
+
})
|
|
3651
3757
|
act(() => setPrefetched(true))
|
|
3652
3758
|
}
|
|
3653
3759
|
prefetch()
|
|
@@ -3677,7 +3783,9 @@ describe('useQuery', () => {
|
|
|
3677
3783
|
function Page() {
|
|
3678
3784
|
const [shouldFetch, setShouldFetch] = React.useState(false)
|
|
3679
3785
|
|
|
3680
|
-
const query = useQuery(
|
|
3786
|
+
const query = useQuery({
|
|
3787
|
+
queryKey: key,
|
|
3788
|
+
queryFn: () => 'data',
|
|
3681
3789
|
enabled: shouldFetch,
|
|
3682
3790
|
})
|
|
3683
3791
|
|
|
@@ -3711,17 +3819,25 @@ describe('useQuery', () => {
|
|
|
3711
3819
|
const results: DefinedUseQueryResult<string>[] = []
|
|
3712
3820
|
|
|
3713
3821
|
function Page() {
|
|
3714
|
-
const result = useQuery(
|
|
3822
|
+
const result = useQuery({
|
|
3823
|
+
queryKey: key,
|
|
3824
|
+
queryFn: async () => {
|
|
3825
|
+
await sleep(10)
|
|
3826
|
+
return 'serverData'
|
|
3827
|
+
},
|
|
3828
|
+
initialData: 'initialData',
|
|
3829
|
+
})
|
|
3715
3830
|
results.push(result)
|
|
3716
|
-
return
|
|
3831
|
+
return <div>data: {result.data}</div>
|
|
3717
3832
|
}
|
|
3718
3833
|
|
|
3719
|
-
renderWithClient(queryClient, <Page />)
|
|
3834
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
3720
3835
|
|
|
3721
|
-
await
|
|
3836
|
+
await waitFor(() => rendered.getByText('data: initialData'))
|
|
3837
|
+
await waitFor(() => rendered.getByText('data: serverData'))
|
|
3722
3838
|
|
|
3723
3839
|
expect(results.length).toBe(2)
|
|
3724
|
-
expect(results[0]).toMatchObject({ data: '
|
|
3840
|
+
expect(results[0]).toMatchObject({ data: 'initialData', isFetching: true })
|
|
3725
3841
|
expect(results[1]).toMatchObject({ data: 'serverData', isFetching: false })
|
|
3726
3842
|
})
|
|
3727
3843
|
|
|
@@ -3730,7 +3846,11 @@ describe('useQuery', () => {
|
|
|
3730
3846
|
const results: DefinedUseQueryResult<number>[] = []
|
|
3731
3847
|
|
|
3732
3848
|
function Page() {
|
|
3733
|
-
const result = useQuery(
|
|
3849
|
+
const result = useQuery({
|
|
3850
|
+
queryKey: key,
|
|
3851
|
+
queryFn: () => 1,
|
|
3852
|
+
initialData: 0,
|
|
3853
|
+
})
|
|
3734
3854
|
results.push(result)
|
|
3735
3855
|
return null
|
|
3736
3856
|
}
|
|
@@ -3744,7 +3864,7 @@ describe('useQuery', () => {
|
|
|
3744
3864
|
expect(results[1]).toMatchObject({ data: 1, isFetching: false })
|
|
3745
3865
|
})
|
|
3746
3866
|
|
|
3747
|
-
it('should show the correct data when switching keys with initialData,
|
|
3867
|
+
it('should show the correct data when switching keys with initialData, placeholderData & staleTime', async () => {
|
|
3748
3868
|
const key = queryKey()
|
|
3749
3869
|
|
|
3750
3870
|
const ALL_TODOS = [
|
|
@@ -3756,21 +3876,19 @@ describe('useQuery', () => {
|
|
|
3756
3876
|
|
|
3757
3877
|
function Page() {
|
|
3758
3878
|
const [filter, setFilter] = React.useState('')
|
|
3759
|
-
const { data: todos } = useQuery(
|
|
3760
|
-
[...key, filter],
|
|
3761
|
-
async () => {
|
|
3879
|
+
const { data: todos } = useQuery({
|
|
3880
|
+
queryKey: [...key, filter],
|
|
3881
|
+
queryFn: async () => {
|
|
3762
3882
|
return ALL_TODOS.filter((todo) =>
|
|
3763
3883
|
filter ? todo.priority === filter : true,
|
|
3764
3884
|
)
|
|
3765
3885
|
},
|
|
3766
|
-
{
|
|
3767
|
-
|
|
3768
|
-
return filter === '' ? initialTodos : undefined
|
|
3769
|
-
},
|
|
3770
|
-
keepPreviousData: true,
|
|
3771
|
-
staleTime: 5000,
|
|
3886
|
+
initialData() {
|
|
3887
|
+
return filter === '' ? initialTodos : undefined
|
|
3772
3888
|
},
|
|
3773
|
-
|
|
3889
|
+
placeholderData: keepPreviousData,
|
|
3890
|
+
staleTime: 5000,
|
|
3891
|
+
})
|
|
3774
3892
|
|
|
3775
3893
|
return (
|
|
3776
3894
|
<div>
|
|
@@ -3807,7 +3925,9 @@ describe('useQuery', () => {
|
|
|
3807
3925
|
function Page() {
|
|
3808
3926
|
const [shouldFetch, setShouldFetch] = React.useState(true)
|
|
3809
3927
|
|
|
3810
|
-
const result = useQuery(
|
|
3928
|
+
const result = useQuery({
|
|
3929
|
+
queryKey: key,
|
|
3930
|
+
queryFn: () => 'fetched data',
|
|
3811
3931
|
enabled: shouldFetch,
|
|
3812
3932
|
initialData: shouldFetch ? 'initial' : 'initial falsy',
|
|
3813
3933
|
})
|
|
@@ -3866,16 +3986,18 @@ describe('useQuery', () => {
|
|
|
3866
3986
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3867
3987
|
|
|
3868
3988
|
expect(queryFn).not.toHaveBeenCalled()
|
|
3869
|
-
expect(queryCache.find(key)).not.toBeUndefined()
|
|
3989
|
+
expect(queryCache.find({ queryKey: key })).not.toBeUndefined()
|
|
3870
3990
|
rendered.getByText('fetchStatus: idle')
|
|
3871
3991
|
})
|
|
3872
3992
|
|
|
3873
3993
|
// See https://github.com/tannerlinsley/react-query/issues/360
|
|
3874
|
-
test('should init to status:
|
|
3994
|
+
test('should init to status:pending, fetchStatus:idle when enabled is false', async () => {
|
|
3875
3995
|
const key = queryKey()
|
|
3876
3996
|
|
|
3877
3997
|
function Page() {
|
|
3878
|
-
const query = useQuery(
|
|
3998
|
+
const query = useQuery({
|
|
3999
|
+
queryKey: key,
|
|
4000
|
+
queryFn: () => 'data',
|
|
3879
4001
|
enabled: false,
|
|
3880
4002
|
})
|
|
3881
4003
|
|
|
@@ -3890,15 +4012,43 @@ describe('useQuery', () => {
|
|
|
3890
4012
|
|
|
3891
4013
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3892
4014
|
|
|
3893
|
-
await waitFor(() => rendered.getByText('status:
|
|
4015
|
+
await waitFor(() => rendered.getByText('status: pending, idle'))
|
|
4016
|
+
})
|
|
4017
|
+
|
|
4018
|
+
test('should not schedule garbage collection, if gcTimeout is set to `Infinity`', async () => {
|
|
4019
|
+
const key = queryKey()
|
|
4020
|
+
|
|
4021
|
+
function Page() {
|
|
4022
|
+
const query = useQuery({
|
|
4023
|
+
queryKey: key,
|
|
4024
|
+
queryFn: () => 'fetched data',
|
|
4025
|
+
gcTime: Infinity,
|
|
4026
|
+
})
|
|
4027
|
+
return <div>{query.data}</div>
|
|
4028
|
+
}
|
|
4029
|
+
|
|
4030
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
4031
|
+
|
|
4032
|
+
await waitFor(() => rendered.getByText('fetched data'))
|
|
4033
|
+
jest.useFakeTimers({
|
|
4034
|
+
legacyFakeTimers: true,
|
|
4035
|
+
})
|
|
4036
|
+
const setTimeoutSpy = jest.spyOn(globalThis.window, 'setTimeout')
|
|
4037
|
+
|
|
4038
|
+
rendered.unmount()
|
|
4039
|
+
|
|
4040
|
+
expect(setTimeoutSpy).not.toHaveBeenCalled()
|
|
4041
|
+
jest.useRealTimers()
|
|
3894
4042
|
})
|
|
3895
4043
|
|
|
3896
|
-
test('should
|
|
4044
|
+
test('should schedule garbage collection, if gcTimeout is not set to infinity', async () => {
|
|
3897
4045
|
const key = queryKey()
|
|
3898
4046
|
|
|
3899
4047
|
function Page() {
|
|
3900
|
-
const query = useQuery(
|
|
3901
|
-
|
|
4048
|
+
const query = useQuery({
|
|
4049
|
+
queryKey: key,
|
|
4050
|
+
queryFn: () => 'fetched data',
|
|
4051
|
+
gcTime: 1000 * 60 * 10, //10 Minutes
|
|
3902
4052
|
})
|
|
3903
4053
|
return <div>{query.data}</div>
|
|
3904
4054
|
}
|
|
@@ -3907,11 +4057,18 @@ describe('useQuery', () => {
|
|
|
3907
4057
|
|
|
3908
4058
|
await waitFor(() => rendered.getByText('fetched data'))
|
|
3909
4059
|
|
|
4060
|
+
jest.useFakeTimers({
|
|
4061
|
+
legacyFakeTimers: true,
|
|
4062
|
+
})
|
|
4063
|
+
const setTimeoutSpy = jest.spyOn(globalThis.window, 'setTimeout')
|
|
4064
|
+
|
|
3910
4065
|
rendered.unmount()
|
|
3911
4066
|
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
4067
|
+
expect(setTimeoutSpy).toHaveBeenLastCalledWith(
|
|
4068
|
+
expect.any(Function),
|
|
4069
|
+
1000 * 60 * 10,
|
|
4070
|
+
)
|
|
4071
|
+
jest.useRealTimers()
|
|
3915
4072
|
})
|
|
3916
4073
|
|
|
3917
4074
|
it('should not cause memo churn when data does not change', async () => {
|
|
@@ -3920,15 +4077,18 @@ describe('useQuery', () => {
|
|
|
3920
4077
|
const memoFn = jest.fn()
|
|
3921
4078
|
|
|
3922
4079
|
function Page() {
|
|
3923
|
-
const result = useQuery(
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
4080
|
+
const result = useQuery({
|
|
4081
|
+
queryKey: key,
|
|
4082
|
+
queryFn: async () => {
|
|
4083
|
+
await sleep(10)
|
|
4084
|
+
return (
|
|
4085
|
+
queryFn() || {
|
|
4086
|
+
data: {
|
|
4087
|
+
nested: true,
|
|
4088
|
+
},
|
|
4089
|
+
}
|
|
4090
|
+
)
|
|
4091
|
+
},
|
|
3932
4092
|
})
|
|
3933
4093
|
|
|
3934
4094
|
React.useMemo(() => {
|
|
@@ -3947,7 +4107,7 @@ describe('useQuery', () => {
|
|
|
3947
4107
|
|
|
3948
4108
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3949
4109
|
|
|
3950
|
-
await waitFor(() => rendered.getByText('status
|
|
4110
|
+
await waitFor(() => rendered.getByText('status pending'))
|
|
3951
4111
|
await waitFor(() => rendered.getByText('status success'))
|
|
3952
4112
|
fireEvent.click(rendered.getByText('refetch'))
|
|
3953
4113
|
await waitFor(() => rendered.getByText('isFetching true'))
|
|
@@ -3962,7 +4122,9 @@ describe('useQuery', () => {
|
|
|
3962
4122
|
|
|
3963
4123
|
function Page() {
|
|
3964
4124
|
const [int, setInt] = React.useState(200)
|
|
3965
|
-
const { data } = useQuery(
|
|
4125
|
+
const { data } = useQuery({
|
|
4126
|
+
queryKey: key,
|
|
4127
|
+
queryFn: () => count++,
|
|
3966
4128
|
refetchInterval: int,
|
|
3967
4129
|
})
|
|
3968
4130
|
|
|
@@ -3989,16 +4151,14 @@ describe('useQuery', () => {
|
|
|
3989
4151
|
const states: UseQueryResult<number>[] = []
|
|
3990
4152
|
|
|
3991
4153
|
function Page() {
|
|
3992
|
-
const queryInfo = useQuery(
|
|
3993
|
-
key,
|
|
3994
|
-
async () => {
|
|
4154
|
+
const queryInfo = useQuery({
|
|
4155
|
+
queryKey: key,
|
|
4156
|
+
queryFn: async () => {
|
|
3995
4157
|
await sleep(10)
|
|
3996
4158
|
return count++
|
|
3997
4159
|
},
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
},
|
|
4001
|
-
)
|
|
4160
|
+
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
|
|
4161
|
+
})
|
|
4002
4162
|
|
|
4003
4163
|
states.push(queryInfo)
|
|
4004
4164
|
|
|
@@ -4020,7 +4180,7 @@ describe('useQuery', () => {
|
|
|
4020
4180
|
|
|
4021
4181
|
expect(states).toMatchObject([
|
|
4022
4182
|
{
|
|
4023
|
-
status: '
|
|
4183
|
+
status: 'pending',
|
|
4024
4184
|
isFetching: true,
|
|
4025
4185
|
data: undefined,
|
|
4026
4186
|
},
|
|
@@ -4057,7 +4217,9 @@ describe('useQuery', () => {
|
|
|
4057
4217
|
const states: UseQueryResult<number>[] = []
|
|
4058
4218
|
|
|
4059
4219
|
function Page() {
|
|
4060
|
-
const queryInfo = useQuery(
|
|
4220
|
+
const queryInfo = useQuery({
|
|
4221
|
+
queryKey: key,
|
|
4222
|
+
queryFn: () => 1,
|
|
4061
4223
|
refetchInterval: 0,
|
|
4062
4224
|
})
|
|
4063
4225
|
|
|
@@ -4076,7 +4238,7 @@ describe('useQuery', () => {
|
|
|
4076
4238
|
|
|
4077
4239
|
expect(states).toMatchObject([
|
|
4078
4240
|
{
|
|
4079
|
-
status: '
|
|
4241
|
+
status: 'pending',
|
|
4080
4242
|
isFetching: true,
|
|
4081
4243
|
data: undefined,
|
|
4082
4244
|
},
|
|
@@ -4090,7 +4252,10 @@ describe('useQuery', () => {
|
|
|
4090
4252
|
|
|
4091
4253
|
it('should accept an empty string as query key', async () => {
|
|
4092
4254
|
function Page() {
|
|
4093
|
-
const result = useQuery(
|
|
4255
|
+
const result = useQuery({
|
|
4256
|
+
queryKey: [''],
|
|
4257
|
+
queryFn: (ctx) => ctx.queryKey,
|
|
4258
|
+
})
|
|
4094
4259
|
return <>{JSON.stringify(result.data)}</>
|
|
4095
4260
|
}
|
|
4096
4261
|
|
|
@@ -4101,7 +4266,10 @@ describe('useQuery', () => {
|
|
|
4101
4266
|
|
|
4102
4267
|
it('should accept an object as query key', async () => {
|
|
4103
4268
|
function Page() {
|
|
4104
|
-
const result = useQuery(
|
|
4269
|
+
const result = useQuery({
|
|
4270
|
+
queryKey: [{ a: 'a' }],
|
|
4271
|
+
queryFn: (ctx) => ctx.queryKey,
|
|
4272
|
+
})
|
|
4105
4273
|
return <>{JSON.stringify(result.data)}</>
|
|
4106
4274
|
}
|
|
4107
4275
|
|
|
@@ -4116,13 +4284,13 @@ describe('useQuery', () => {
|
|
|
4116
4284
|
const queryFn = jest.fn<string, unknown[]>().mockReturnValue('data')
|
|
4117
4285
|
|
|
4118
4286
|
function Disabled() {
|
|
4119
|
-
useQuery(key, queryFn,
|
|
4287
|
+
useQuery({ queryKey: key, queryFn, enabled: false })
|
|
4120
4288
|
return null
|
|
4121
4289
|
}
|
|
4122
4290
|
|
|
4123
4291
|
function Page() {
|
|
4124
4292
|
const [enabled, setEnabled] = React.useState(false)
|
|
4125
|
-
const result = useQuery(key, queryFn,
|
|
4293
|
+
const result = useQuery({ queryKey: key, queryFn, enabled })
|
|
4126
4294
|
return (
|
|
4127
4295
|
<>
|
|
4128
4296
|
<Disabled />
|
|
@@ -4145,7 +4313,9 @@ describe('useQuery', () => {
|
|
|
4145
4313
|
const states: UseQueryResult<string>[] = []
|
|
4146
4314
|
|
|
4147
4315
|
function Page() {
|
|
4148
|
-
const state = useQuery(
|
|
4316
|
+
const state = useQuery({
|
|
4317
|
+
queryKey: key1,
|
|
4318
|
+
queryFn: () => 'data',
|
|
4149
4319
|
placeholderData: 'placeholder',
|
|
4150
4320
|
})
|
|
4151
4321
|
|
|
@@ -4184,7 +4354,9 @@ describe('useQuery', () => {
|
|
|
4184
4354
|
function Page() {
|
|
4185
4355
|
const [count, setCount] = React.useState(0)
|
|
4186
4356
|
|
|
4187
|
-
const state = useQuery(
|
|
4357
|
+
const state = useQuery({
|
|
4358
|
+
queryKey: key1,
|
|
4359
|
+
queryFn: () => 'data',
|
|
4188
4360
|
placeholderData: 'placeholder',
|
|
4189
4361
|
enabled: count === 0,
|
|
4190
4362
|
})
|
|
@@ -4240,7 +4412,9 @@ describe('useQuery', () => {
|
|
|
4240
4412
|
const states: UseQueryResult<string>[] = []
|
|
4241
4413
|
|
|
4242
4414
|
function Page() {
|
|
4243
|
-
const state = useQuery(
|
|
4415
|
+
const state = useQuery({
|
|
4416
|
+
queryKey: key1,
|
|
4417
|
+
queryFn: () => 1,
|
|
4244
4418
|
placeholderData: 23,
|
|
4245
4419
|
select: (data) => String(data * 2),
|
|
4246
4420
|
})
|
|
@@ -4279,7 +4453,9 @@ describe('useQuery', () => {
|
|
|
4279
4453
|
let placeholderFunctionRunCount = 0
|
|
4280
4454
|
|
|
4281
4455
|
function Page() {
|
|
4282
|
-
const state = useQuery(
|
|
4456
|
+
const state = useQuery({
|
|
4457
|
+
queryKey: key1,
|
|
4458
|
+
queryFn: () => 1,
|
|
4283
4459
|
placeholderData: () => {
|
|
4284
4460
|
placeholderFunctionRunCount++
|
|
4285
4461
|
return 23
|
|
@@ -4331,23 +4507,21 @@ describe('useQuery', () => {
|
|
|
4331
4507
|
function Page() {
|
|
4332
4508
|
const [count, inc] = React.useReducer((prev) => prev + 1, 2)
|
|
4333
4509
|
|
|
4334
|
-
const state = useQuery(
|
|
4335
|
-
key1,
|
|
4336
|
-
async () => {
|
|
4510
|
+
const state = useQuery({
|
|
4511
|
+
queryKey: key1,
|
|
4512
|
+
queryFn: async () => {
|
|
4337
4513
|
await sleep(10)
|
|
4338
4514
|
return 0
|
|
4339
4515
|
},
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
},
|
|
4350
|
-
)
|
|
4516
|
+
select: React.useCallback(
|
|
4517
|
+
(data: number) => {
|
|
4518
|
+
selectRun++
|
|
4519
|
+
return `selected ${data + count}`
|
|
4520
|
+
},
|
|
4521
|
+
[count],
|
|
4522
|
+
),
|
|
4523
|
+
placeholderData: 99,
|
|
4524
|
+
})
|
|
4351
4525
|
|
|
4352
4526
|
return (
|
|
4353
4527
|
<div>
|
|
@@ -4377,22 +4551,21 @@ describe('useQuery', () => {
|
|
|
4377
4551
|
const [count, inc] = React.useReducer((prev) => prev + 1, 2)
|
|
4378
4552
|
const [forceValue, forceUpdate] = React.useReducer((prev) => prev + 1, 1)
|
|
4379
4553
|
|
|
4380
|
-
const state = useQuery(
|
|
4381
|
-
key1,
|
|
4382
|
-
async () => {
|
|
4554
|
+
const state = useQuery({
|
|
4555
|
+
queryKey: key1,
|
|
4556
|
+
queryFn: async () => {
|
|
4383
4557
|
await sleep(10)
|
|
4384
4558
|
return 0
|
|
4385
4559
|
},
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
)
|
|
4560
|
+
|
|
4561
|
+
select: React.useCallback(
|
|
4562
|
+
(data: number) => {
|
|
4563
|
+
return `selected ${data + count}`
|
|
4564
|
+
},
|
|
4565
|
+
[count],
|
|
4566
|
+
),
|
|
4567
|
+
placeholderData: 99,
|
|
4568
|
+
})
|
|
4396
4569
|
|
|
4397
4570
|
return (
|
|
4398
4571
|
<div>
|
|
@@ -4427,16 +4600,15 @@ describe('useQuery', () => {
|
|
|
4427
4600
|
function Page() {
|
|
4428
4601
|
const [forceValue, forceUpdate] = React.useReducer((prev) => prev + 1, 1)
|
|
4429
4602
|
|
|
4430
|
-
const state = useQuery(
|
|
4431
|
-
key1,
|
|
4432
|
-
async () => {
|
|
4603
|
+
const state = useQuery({
|
|
4604
|
+
queryKey: key1,
|
|
4605
|
+
queryFn: async () => {
|
|
4433
4606
|
await sleep(10)
|
|
4434
4607
|
return [1, 2]
|
|
4435
4608
|
},
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
)
|
|
4609
|
+
|
|
4610
|
+
select: (res) => res.map((x) => x + 1),
|
|
4611
|
+
})
|
|
4440
4612
|
|
|
4441
4613
|
React.useEffect(() => {
|
|
4442
4614
|
if (state.data) {
|
|
@@ -4481,7 +4653,7 @@ describe('useQuery', () => {
|
|
|
4481
4653
|
}
|
|
4482
4654
|
|
|
4483
4655
|
function Page() {
|
|
4484
|
-
const state = useQuery(key, queryFn)
|
|
4656
|
+
const state = useQuery({ queryKey: key, queryFn })
|
|
4485
4657
|
return (
|
|
4486
4658
|
<div>
|
|
4487
4659
|
<h1>Status: {state.status}</h1>
|
|
@@ -4509,13 +4681,14 @@ describe('useQuery', () => {
|
|
|
4509
4681
|
ctx,
|
|
4510
4682
|
) => {
|
|
4511
4683
|
const [, limit] = ctx.queryKey
|
|
4684
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
4512
4685
|
const value = limit % 2 && ctx.signal ? 'abort' : `data ${limit}`
|
|
4513
4686
|
await sleep(25)
|
|
4514
4687
|
return value
|
|
4515
4688
|
}
|
|
4516
4689
|
|
|
4517
4690
|
function Page(props: { limit: number }) {
|
|
4518
|
-
const state = useQuery([key, props.limit], queryFn)
|
|
4691
|
+
const state = useQuery({ queryKey: [key, props.limit], queryFn })
|
|
4519
4692
|
states[props.limit] = state
|
|
4520
4693
|
return (
|
|
4521
4694
|
<div>
|
|
@@ -4540,27 +4713,27 @@ describe('useQuery', () => {
|
|
|
4540
4713
|
|
|
4541
4714
|
await waitFor(() => expect(states).toHaveLength(4))
|
|
4542
4715
|
|
|
4543
|
-
expect(queryCache.find([key, 0])?.state).toMatchObject({
|
|
4716
|
+
expect(queryCache.find({ queryKey: [key, 0] })?.state).toMatchObject({
|
|
4544
4717
|
data: 'data 0',
|
|
4545
4718
|
status: 'success',
|
|
4546
4719
|
dataUpdateCount: 1,
|
|
4547
4720
|
})
|
|
4548
4721
|
|
|
4549
|
-
expect(queryCache.find([key, 1])?.state).toMatchObject({
|
|
4722
|
+
expect(queryCache.find({ queryKey: [key, 1] })?.state).toMatchObject({
|
|
4550
4723
|
data: undefined,
|
|
4551
|
-
status: '
|
|
4724
|
+
status: 'pending',
|
|
4552
4725
|
fetchStatus: 'idle',
|
|
4553
4726
|
})
|
|
4554
4727
|
|
|
4555
|
-
expect(queryCache.find([key, 2])?.state).toMatchObject({
|
|
4728
|
+
expect(queryCache.find({ queryKey: [key, 2] })?.state).toMatchObject({
|
|
4556
4729
|
data: 'data 2',
|
|
4557
4730
|
status: 'success',
|
|
4558
4731
|
dataUpdateCount: 1,
|
|
4559
4732
|
})
|
|
4560
4733
|
|
|
4561
|
-
expect(queryCache.find([key, 3])?.state).toMatchObject({
|
|
4734
|
+
expect(queryCache.find({ queryKey: [key, 3] })?.state).toMatchObject({
|
|
4562
4735
|
data: undefined,
|
|
4563
|
-
status: '
|
|
4736
|
+
status: 'pending',
|
|
4564
4737
|
fetchStatus: 'idle',
|
|
4565
4738
|
})
|
|
4566
4739
|
})
|
|
@@ -4578,7 +4751,7 @@ describe('useQuery', () => {
|
|
|
4578
4751
|
const [id, setId] = React.useState(1)
|
|
4579
4752
|
const [hasChanged, setHasChanged] = React.useState(false)
|
|
4580
4753
|
|
|
4581
|
-
const state = useQuery([key, id], queryFn)
|
|
4754
|
+
const state = useQuery({ queryKey: [key, id], queryFn })
|
|
4582
4755
|
|
|
4583
4756
|
states.push(state)
|
|
4584
4757
|
|
|
@@ -4596,17 +4769,17 @@ describe('useQuery', () => {
|
|
|
4596
4769
|
expect(states.length).toBe(4)
|
|
4597
4770
|
// Load query 1
|
|
4598
4771
|
expect(states[0]).toMatchObject({
|
|
4599
|
-
status: '
|
|
4772
|
+
status: 'pending',
|
|
4600
4773
|
error: null,
|
|
4601
4774
|
})
|
|
4602
4775
|
// Load query 2
|
|
4603
4776
|
expect(states[1]).toMatchObject({
|
|
4604
|
-
status: '
|
|
4777
|
+
status: 'pending',
|
|
4605
4778
|
error: null,
|
|
4606
4779
|
})
|
|
4607
4780
|
// Load query 1
|
|
4608
4781
|
expect(states[2]).toMatchObject({
|
|
4609
|
-
status: '
|
|
4782
|
+
status: 'pending',
|
|
4610
4783
|
error: null,
|
|
4611
4784
|
})
|
|
4612
4785
|
// Loaded query 1
|
|
@@ -4622,21 +4795,23 @@ describe('useQuery', () => {
|
|
|
4622
4795
|
let count = 0
|
|
4623
4796
|
|
|
4624
4797
|
function Page() {
|
|
4625
|
-
const state = useQuery(
|
|
4626
|
-
key,
|
|
4627
|
-
async () => {
|
|
4798
|
+
const state = useQuery({
|
|
4799
|
+
queryKey: key,
|
|
4800
|
+
queryFn: async () => {
|
|
4628
4801
|
await sleep(10)
|
|
4629
4802
|
count++
|
|
4630
4803
|
return count
|
|
4631
4804
|
},
|
|
4632
|
-
|
|
4633
|
-
)
|
|
4805
|
+
staleTime: Infinity,
|
|
4806
|
+
})
|
|
4634
4807
|
|
|
4635
4808
|
states.push(state)
|
|
4636
4809
|
|
|
4637
4810
|
return (
|
|
4638
4811
|
<div>
|
|
4639
|
-
<button onClick={() => queryClient.resetQueries(key)}>
|
|
4812
|
+
<button onClick={() => queryClient.resetQueries({ queryKey: key })}>
|
|
4813
|
+
reset
|
|
4814
|
+
</button>
|
|
4640
4815
|
<div>data: {state.data ?? 'null'}</div>
|
|
4641
4816
|
<div>isFetching: {state.isFetching}</div>
|
|
4642
4817
|
</div>
|
|
@@ -4656,28 +4831,28 @@ describe('useQuery', () => {
|
|
|
4656
4831
|
|
|
4657
4832
|
expect(states[0]).toMatchObject({
|
|
4658
4833
|
data: undefined,
|
|
4659
|
-
|
|
4834
|
+
isPending: true,
|
|
4660
4835
|
isFetching: true,
|
|
4661
4836
|
isSuccess: false,
|
|
4662
4837
|
isStale: true,
|
|
4663
4838
|
})
|
|
4664
4839
|
expect(states[1]).toMatchObject({
|
|
4665
4840
|
data: 1,
|
|
4666
|
-
|
|
4841
|
+
isPending: false,
|
|
4667
4842
|
isFetching: false,
|
|
4668
4843
|
isSuccess: true,
|
|
4669
4844
|
isStale: false,
|
|
4670
4845
|
})
|
|
4671
4846
|
expect(states[2]).toMatchObject({
|
|
4672
4847
|
data: undefined,
|
|
4673
|
-
|
|
4848
|
+
isPending: true,
|
|
4674
4849
|
isFetching: true,
|
|
4675
4850
|
isSuccess: false,
|
|
4676
4851
|
isStale: true,
|
|
4677
4852
|
})
|
|
4678
4853
|
expect(states[3]).toMatchObject({
|
|
4679
4854
|
data: 2,
|
|
4680
|
-
|
|
4855
|
+
isPending: false,
|
|
4681
4856
|
isFetching: false,
|
|
4682
4857
|
isSuccess: true,
|
|
4683
4858
|
isStale: false,
|
|
@@ -4690,15 +4865,17 @@ describe('useQuery', () => {
|
|
|
4690
4865
|
let count = 0
|
|
4691
4866
|
|
|
4692
4867
|
function Page() {
|
|
4693
|
-
const state = useQuery(
|
|
4694
|
-
key,
|
|
4695
|
-
async () => {
|
|
4868
|
+
const state = useQuery({
|
|
4869
|
+
queryKey: key,
|
|
4870
|
+
queryFn: async () => {
|
|
4696
4871
|
await sleep(10)
|
|
4697
4872
|
count++
|
|
4698
4873
|
return count
|
|
4699
4874
|
},
|
|
4700
|
-
|
|
4701
|
-
|
|
4875
|
+
staleTime: Infinity,
|
|
4876
|
+
enabled: false,
|
|
4877
|
+
notifyOnChangeProps: 'all',
|
|
4878
|
+
})
|
|
4702
4879
|
|
|
4703
4880
|
states.push(state)
|
|
4704
4881
|
|
|
@@ -4707,7 +4884,9 @@ describe('useQuery', () => {
|
|
|
4707
4884
|
return (
|
|
4708
4885
|
<div>
|
|
4709
4886
|
<button onClick={() => refetch()}>refetch</button>
|
|
4710
|
-
<button onClick={() => queryClient.resetQueries(key)}>
|
|
4887
|
+
<button onClick={() => queryClient.resetQueries({ queryKey: key })}>
|
|
4888
|
+
reset
|
|
4889
|
+
</button>
|
|
4711
4890
|
<div>data: {state.data ?? 'null'}</div>
|
|
4712
4891
|
</div>
|
|
4713
4892
|
)
|
|
@@ -4728,28 +4907,28 @@ describe('useQuery', () => {
|
|
|
4728
4907
|
|
|
4729
4908
|
expect(states[0]).toMatchObject({
|
|
4730
4909
|
data: undefined,
|
|
4731
|
-
|
|
4910
|
+
isPending: true,
|
|
4732
4911
|
isFetching: false,
|
|
4733
4912
|
isSuccess: false,
|
|
4734
4913
|
isStale: true,
|
|
4735
4914
|
})
|
|
4736
4915
|
expect(states[1]).toMatchObject({
|
|
4737
4916
|
data: undefined,
|
|
4738
|
-
|
|
4917
|
+
isPending: true,
|
|
4739
4918
|
isFetching: true,
|
|
4740
4919
|
isSuccess: false,
|
|
4741
4920
|
isStale: true,
|
|
4742
4921
|
})
|
|
4743
4922
|
expect(states[2]).toMatchObject({
|
|
4744
4923
|
data: 1,
|
|
4745
|
-
|
|
4924
|
+
isPending: false,
|
|
4746
4925
|
isFetching: false,
|
|
4747
4926
|
isSuccess: true,
|
|
4748
4927
|
isStale: false,
|
|
4749
4928
|
})
|
|
4750
4929
|
expect(states[3]).toMatchObject({
|
|
4751
4930
|
data: undefined,
|
|
4752
|
-
|
|
4931
|
+
isPending: true,
|
|
4753
4932
|
isFetching: false,
|
|
4754
4933
|
isSuccess: false,
|
|
4755
4934
|
isStale: true,
|
|
@@ -4772,7 +4951,7 @@ describe('useQuery', () => {
|
|
|
4772
4951
|
renders++
|
|
4773
4952
|
})
|
|
4774
4953
|
|
|
4775
|
-
useQuery(key, () => 'test',
|
|
4954
|
+
useQuery({ queryKey: key, queryFn: () => 'test', queryKeyHashFn })
|
|
4776
4955
|
return null
|
|
4777
4956
|
}
|
|
4778
4957
|
|
|
@@ -4791,7 +4970,9 @@ describe('useQuery', () => {
|
|
|
4791
4970
|
})
|
|
4792
4971
|
|
|
4793
4972
|
function Page({ enabled }: { enabled: boolean }) {
|
|
4794
|
-
const { error,
|
|
4973
|
+
const { error, isPending } = useQuery({
|
|
4974
|
+
queryKey: ['key'],
|
|
4975
|
+
queryFn,
|
|
4795
4976
|
enabled,
|
|
4796
4977
|
retry: false,
|
|
4797
4978
|
retryOnMount: false,
|
|
@@ -4799,8 +4980,8 @@ describe('useQuery', () => {
|
|
|
4799
4980
|
refetchOnWindowFocus: false,
|
|
4800
4981
|
})
|
|
4801
4982
|
|
|
4802
|
-
if (
|
|
4803
|
-
return <div>status:
|
|
4983
|
+
if (isPending) {
|
|
4984
|
+
return <div>status: pending</div>
|
|
4804
4985
|
}
|
|
4805
4986
|
if (error instanceof Error) {
|
|
4806
4987
|
return <div>error</div>
|
|
@@ -4824,7 +5005,7 @@ describe('useQuery', () => {
|
|
|
4824
5005
|
const rendered = renderWithClient(queryClient, <App />)
|
|
4825
5006
|
|
|
4826
5007
|
// initial state check
|
|
4827
|
-
rendered.getByText('status:
|
|
5008
|
+
rendered.getByText('status: pending')
|
|
4828
5009
|
|
|
4829
5010
|
// // render error state component
|
|
4830
5011
|
await waitFor(() => rendered.getByText('error'))
|
|
@@ -4842,9 +5023,9 @@ describe('useQuery', () => {
|
|
|
4842
5023
|
|
|
4843
5024
|
it('should refetch when query key changed when previous status is error', async () => {
|
|
4844
5025
|
function Page({ id }: { id: number }) {
|
|
4845
|
-
const { error,
|
|
4846
|
-
[id],
|
|
4847
|
-
async () => {
|
|
5026
|
+
const { error, isPending } = useQuery({
|
|
5027
|
+
queryKey: [id],
|
|
5028
|
+
queryFn: async () => {
|
|
4848
5029
|
await sleep(10)
|
|
4849
5030
|
if (id % 2 === 1) {
|
|
4850
5031
|
return Promise.reject(new Error('Error'))
|
|
@@ -4852,16 +5033,14 @@ describe('useQuery', () => {
|
|
|
4852
5033
|
return 'data'
|
|
4853
5034
|
}
|
|
4854
5035
|
},
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
},
|
|
4861
|
-
)
|
|
5036
|
+
retry: false,
|
|
5037
|
+
retryOnMount: false,
|
|
5038
|
+
refetchOnMount: false,
|
|
5039
|
+
refetchOnWindowFocus: false,
|
|
5040
|
+
})
|
|
4862
5041
|
|
|
4863
|
-
if (
|
|
4864
|
-
return <div>status:
|
|
5042
|
+
if (isPending) {
|
|
5043
|
+
return <div>status: pending</div>
|
|
4865
5044
|
}
|
|
4866
5045
|
if (error instanceof Error) {
|
|
4867
5046
|
return <div>error</div>
|
|
@@ -4885,7 +5064,7 @@ describe('useQuery', () => {
|
|
|
4885
5064
|
const rendered = renderWithClient(queryClient, <App />)
|
|
4886
5065
|
|
|
4887
5066
|
// initial state check
|
|
4888
|
-
rendered.getByText('status:
|
|
5067
|
+
rendered.getByText('status: pending')
|
|
4889
5068
|
|
|
4890
5069
|
// render error state component
|
|
4891
5070
|
await waitFor(() => rendered.getByText('error'))
|
|
@@ -4901,19 +5080,17 @@ describe('useQuery', () => {
|
|
|
4901
5080
|
|
|
4902
5081
|
it('should refetch when query key changed when switching between erroneous queries', async () => {
|
|
4903
5082
|
function Page({ id }: { id: boolean }) {
|
|
4904
|
-
const { error, isFetching } = useQuery(
|
|
4905
|
-
[id],
|
|
4906
|
-
async () => {
|
|
5083
|
+
const { error, isFetching } = useQuery({
|
|
5084
|
+
queryKey: [id],
|
|
5085
|
+
queryFn: async () => {
|
|
4907
5086
|
await sleep(10)
|
|
4908
5087
|
return Promise.reject<unknown>(new Error('Error'))
|
|
4909
5088
|
},
|
|
4910
|
-
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
},
|
|
4916
|
-
)
|
|
5089
|
+
retry: false,
|
|
5090
|
+
retryOnMount: false,
|
|
5091
|
+
refetchOnMount: false,
|
|
5092
|
+
refetchOnWindowFocus: false,
|
|
5093
|
+
})
|
|
4917
5094
|
|
|
4918
5095
|
if (isFetching) {
|
|
4919
5096
|
return <div>status: fetching</div>
|
|
@@ -4956,7 +5133,7 @@ describe('useQuery', () => {
|
|
|
4956
5133
|
await waitFor(() => rendered.getByText('error'))
|
|
4957
5134
|
})
|
|
4958
5135
|
|
|
4959
|
-
it('should have no error in
|
|
5136
|
+
it('should have no error in pending state when refetching after error occurred', async () => {
|
|
4960
5137
|
const key = queryKey()
|
|
4961
5138
|
const states: UseQueryResult<number>[] = []
|
|
4962
5139
|
const error = new Error('oops')
|
|
@@ -4964,9 +5141,9 @@ describe('useQuery', () => {
|
|
|
4964
5141
|
let count = 0
|
|
4965
5142
|
|
|
4966
5143
|
function Page() {
|
|
4967
|
-
const state = useQuery(
|
|
4968
|
-
key,
|
|
4969
|
-
async () => {
|
|
5144
|
+
const state = useQuery({
|
|
5145
|
+
queryKey: key,
|
|
5146
|
+
queryFn: async () => {
|
|
4970
5147
|
await sleep(10)
|
|
4971
5148
|
if (count === 0) {
|
|
4972
5149
|
count++
|
|
@@ -4974,15 +5151,13 @@ describe('useQuery', () => {
|
|
|
4974
5151
|
}
|
|
4975
5152
|
return 5
|
|
4976
5153
|
},
|
|
4977
|
-
|
|
4978
|
-
|
|
4979
|
-
},
|
|
4980
|
-
)
|
|
5154
|
+
retry: false,
|
|
5155
|
+
})
|
|
4981
5156
|
|
|
4982
5157
|
states.push(state)
|
|
4983
5158
|
|
|
4984
|
-
if (state.
|
|
4985
|
-
return <div>status:
|
|
5159
|
+
if (state.isPending) {
|
|
5160
|
+
return <div>status: pending</div>
|
|
4986
5161
|
}
|
|
4987
5162
|
if (state.error instanceof Error) {
|
|
4988
5163
|
return (
|
|
@@ -5005,7 +5180,7 @@ describe('useQuery', () => {
|
|
|
5005
5180
|
await waitFor(() => expect(states.length).toBe(4))
|
|
5006
5181
|
|
|
5007
5182
|
expect(states[0]).toMatchObject({
|
|
5008
|
-
status: '
|
|
5183
|
+
status: 'pending',
|
|
5009
5184
|
data: undefined,
|
|
5010
5185
|
error: null,
|
|
5011
5186
|
})
|
|
@@ -5017,7 +5192,7 @@ describe('useQuery', () => {
|
|
|
5017
5192
|
})
|
|
5018
5193
|
|
|
5019
5194
|
expect(states[2]).toMatchObject({
|
|
5020
|
-
status: '
|
|
5195
|
+
status: 'pending',
|
|
5021
5196
|
data: undefined,
|
|
5022
5197
|
error: null,
|
|
5023
5198
|
})
|
|
@@ -5061,7 +5236,7 @@ describe('useQuery', () => {
|
|
|
5061
5236
|
|
|
5062
5237
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
5063
5238
|
|
|
5064
|
-
await waitFor(() => rendered.getByText('status:
|
|
5239
|
+
await waitFor(() => rendered.getByText('status: pending, isPaused: true'))
|
|
5065
5240
|
|
|
5066
5241
|
onlineMock.mockReturnValue(true)
|
|
5067
5242
|
window.dispatchEvent(new Event('online'))
|
|
@@ -5186,7 +5361,7 @@ describe('useQuery', () => {
|
|
|
5186
5361
|
rendered.getByText('status: success, fetchStatus: paused'),
|
|
5187
5362
|
)
|
|
5188
5363
|
|
|
5189
|
-
window.dispatchEvent(new
|
|
5364
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
5190
5365
|
await sleep(15)
|
|
5191
5366
|
|
|
5192
5367
|
await waitFor(() =>
|
|
@@ -5230,7 +5405,7 @@ describe('useQuery', () => {
|
|
|
5230
5405
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
5231
5406
|
|
|
5232
5407
|
await waitFor(() =>
|
|
5233
|
-
rendered.getByText('status:
|
|
5408
|
+
rendered.getByText('status: pending, fetchStatus: paused'),
|
|
5234
5409
|
)
|
|
5235
5410
|
|
|
5236
5411
|
fireEvent.click(rendered.getByRole('button', { name: /invalidate/i }))
|
|
@@ -5239,7 +5414,7 @@ describe('useQuery', () => {
|
|
|
5239
5414
|
|
|
5240
5415
|
// invalidation should not trigger a refetch
|
|
5241
5416
|
await waitFor(() =>
|
|
5242
|
-
rendered.getByText('status:
|
|
5417
|
+
rendered.getByText('status: pending, fetchStatus: paused'),
|
|
5243
5418
|
)
|
|
5244
5419
|
|
|
5245
5420
|
expect(count).toBe(0)
|
|
@@ -5352,7 +5527,7 @@ describe('useQuery', () => {
|
|
|
5352
5527
|
|
|
5353
5528
|
// triggers a second pause
|
|
5354
5529
|
act(() => {
|
|
5355
|
-
window.dispatchEvent(new
|
|
5530
|
+
window.dispatchEvent(new Event('visibilitychange'))
|
|
5356
5531
|
})
|
|
5357
5532
|
|
|
5358
5533
|
onlineMock.mockReturnValue(true)
|
|
@@ -5402,7 +5577,7 @@ describe('useQuery', () => {
|
|
|
5402
5577
|
|
|
5403
5578
|
await waitFor(() =>
|
|
5404
5579
|
rendered.getByText(
|
|
5405
|
-
'status:
|
|
5580
|
+
'status: pending, fetchStatus: fetching, failureCount: 1',
|
|
5406
5581
|
),
|
|
5407
5582
|
)
|
|
5408
5583
|
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
@@ -5413,7 +5588,7 @@ describe('useQuery', () => {
|
|
|
5413
5588
|
|
|
5414
5589
|
await waitFor(() =>
|
|
5415
5590
|
rendered.getByText(
|
|
5416
|
-
'status:
|
|
5591
|
+
'status: pending, fetchStatus: paused, failureCount: 1',
|
|
5417
5592
|
),
|
|
5418
5593
|
)
|
|
5419
5594
|
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
@@ -5473,7 +5648,7 @@ describe('useQuery', () => {
|
|
|
5473
5648
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
5474
5649
|
|
|
5475
5650
|
await waitFor(() =>
|
|
5476
|
-
rendered.getByText('status:
|
|
5651
|
+
rendered.getByText('status: pending, fetchStatus: paused'),
|
|
5477
5652
|
)
|
|
5478
5653
|
|
|
5479
5654
|
fireEvent.click(rendered.getByRole('button', { name: /hide/i }))
|
|
@@ -5530,13 +5705,13 @@ describe('useQuery', () => {
|
|
|
5530
5705
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
5531
5706
|
|
|
5532
5707
|
await waitFor(() =>
|
|
5533
|
-
rendered.getByText('status:
|
|
5708
|
+
rendered.getByText('status: pending, fetchStatus: paused'),
|
|
5534
5709
|
)
|
|
5535
5710
|
|
|
5536
5711
|
fireEvent.click(rendered.getByRole('button', { name: /cancel/i }))
|
|
5537
5712
|
|
|
5538
5713
|
await waitFor(() =>
|
|
5539
|
-
rendered.getByText('status:
|
|
5714
|
+
rendered.getByText('status: pending, fetchStatus: idle'),
|
|
5540
5715
|
)
|
|
5541
5716
|
|
|
5542
5717
|
expect(count).toBe(0)
|
|
@@ -5547,7 +5722,7 @@ describe('useQuery', () => {
|
|
|
5547
5722
|
await sleep(15)
|
|
5548
5723
|
|
|
5549
5724
|
await waitFor(() =>
|
|
5550
|
-
rendered.getByText('status:
|
|
5725
|
+
rendered.getByText('status: pending, fetchStatus: idle'),
|
|
5551
5726
|
)
|
|
5552
5727
|
|
|
5553
5728
|
expect(count).toBe(0)
|
|
@@ -5562,10 +5737,10 @@ describe('useQuery', () => {
|
|
|
5562
5737
|
function Component() {
|
|
5563
5738
|
const state = useQuery({
|
|
5564
5739
|
queryKey: key,
|
|
5565
|
-
queryFn: async ({ signal }) => {
|
|
5740
|
+
queryFn: async ({ signal: _signal }) => {
|
|
5566
5741
|
count++
|
|
5567
5742
|
await sleep(10)
|
|
5568
|
-
return
|
|
5743
|
+
return `signal${count}`
|
|
5569
5744
|
},
|
|
5570
5745
|
})
|
|
5571
5746
|
|
|
@@ -5750,7 +5925,7 @@ describe('useQuery', () => {
|
|
|
5750
5925
|
|
|
5751
5926
|
await waitFor(() =>
|
|
5752
5927
|
rendered.getByText(
|
|
5753
|
-
'status:
|
|
5928
|
+
'status: pending, fetchStatus: paused, failureCount: 1',
|
|
5754
5929
|
),
|
|
5755
5930
|
)
|
|
5756
5931
|
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
@@ -5781,7 +5956,9 @@ describe('useQuery', () => {
|
|
|
5781
5956
|
}
|
|
5782
5957
|
|
|
5783
5958
|
function Page() {
|
|
5784
|
-
const state = useQuery(
|
|
5959
|
+
const state = useQuery({
|
|
5960
|
+
queryKey: key,
|
|
5961
|
+
queryFn,
|
|
5785
5962
|
retry: false,
|
|
5786
5963
|
retryOnMount: false,
|
|
5787
5964
|
})
|
|
@@ -5791,7 +5968,7 @@ describe('useQuery', () => {
|
|
|
5791
5968
|
return <></>
|
|
5792
5969
|
}
|
|
5793
5970
|
|
|
5794
|
-
await queryClient.prefetchQuery(key, queryFn)
|
|
5971
|
+
await queryClient.prefetchQuery({ queryKey: key, queryFn })
|
|
5795
5972
|
renderWithClient(queryClient, <Page />)
|
|
5796
5973
|
|
|
5797
5974
|
await waitFor(() => expect(states).toHaveLength(1))
|
|
@@ -5807,7 +5984,11 @@ describe('useQuery', () => {
|
|
|
5807
5984
|
const onSuccess = jest.fn()
|
|
5808
5985
|
|
|
5809
5986
|
function Page() {
|
|
5810
|
-
const state = useQuery(
|
|
5987
|
+
const state = useQuery({
|
|
5988
|
+
queryKey: key,
|
|
5989
|
+
queryFn: () => 'data',
|
|
5990
|
+
onSuccess,
|
|
5991
|
+
})
|
|
5811
5992
|
return (
|
|
5812
5993
|
<div>
|
|
5813
5994
|
<div>data: {state.data}</div>
|
|
@@ -5832,14 +6013,16 @@ describe('useQuery', () => {
|
|
|
5832
6013
|
const key = queryKey()
|
|
5833
6014
|
|
|
5834
6015
|
function Page() {
|
|
5835
|
-
const state = useQuery(key, () => 'data')
|
|
6016
|
+
const state = useQuery({ queryKey: key, queryFn: () => 'data' })
|
|
5836
6017
|
return (
|
|
5837
6018
|
<div>
|
|
5838
6019
|
<div>data: {state.data}</div>
|
|
5839
6020
|
<div>dataUpdatedAt: {state.dataUpdatedAt}</div>
|
|
5840
6021
|
<button
|
|
5841
6022
|
onClick={() =>
|
|
5842
|
-
queryClient.setQueryData(key, 'newData', {
|
|
6023
|
+
queryClient.setQueryData(key, 'newData', {
|
|
6024
|
+
updatedAt: 100,
|
|
6025
|
+
})
|
|
5843
6026
|
}
|
|
5844
6027
|
>
|
|
5845
6028
|
setQueryData
|
|
@@ -5863,15 +6046,13 @@ describe('useQuery', () => {
|
|
|
5863
6046
|
const error = new Error('oops')
|
|
5864
6047
|
|
|
5865
6048
|
function Page() {
|
|
5866
|
-
const { refetch, errorUpdateCount } = useQuery(
|
|
5867
|
-
key,
|
|
5868
|
-
async (): Promise<unknown> => {
|
|
6049
|
+
const { refetch, errorUpdateCount } = useQuery({
|
|
6050
|
+
queryKey: key,
|
|
6051
|
+
queryFn: async (): Promise<unknown> => {
|
|
5869
6052
|
throw error
|
|
5870
6053
|
},
|
|
5871
|
-
|
|
5872
|
-
|
|
5873
|
-
},
|
|
5874
|
-
)
|
|
6054
|
+
retry: false,
|
|
6055
|
+
})
|
|
5875
6056
|
return (
|
|
5876
6057
|
<div>
|
|
5877
6058
|
<button onClick={() => refetch()}>refetch</button>
|
|
@@ -5887,4 +6068,27 @@ describe('useQuery', () => {
|
|
|
5887
6068
|
fireEvent.click(fetchBtn)
|
|
5888
6069
|
await waitFor(() => rendered.getByText('data: 3'))
|
|
5889
6070
|
})
|
|
6071
|
+
|
|
6072
|
+
it('should use provided custom queryClient', async () => {
|
|
6073
|
+
const key = queryKey()
|
|
6074
|
+
const queryFn = async () => {
|
|
6075
|
+
return Promise.resolve('custom client')
|
|
6076
|
+
}
|
|
6077
|
+
|
|
6078
|
+
function Page() {
|
|
6079
|
+
const { data } = useQuery(
|
|
6080
|
+
{
|
|
6081
|
+
queryKey: key,
|
|
6082
|
+
queryFn,
|
|
6083
|
+
},
|
|
6084
|
+
queryClient,
|
|
6085
|
+
)
|
|
6086
|
+
|
|
6087
|
+
return <div>data: {data}</div>
|
|
6088
|
+
}
|
|
6089
|
+
|
|
6090
|
+
const rendered = render(<Page></Page>)
|
|
6091
|
+
|
|
6092
|
+
await waitFor(() => rendered.getByText('data: custom client'))
|
|
6093
|
+
})
|
|
5890
6094
|
})
|