@tanstack/react-query 4.24.10 → 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 +1003 -1827
- 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,15 +1,8 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { render, waitFor } from '@testing-library/react'
|
|
3
|
-
import { renderToString } from 'react-dom/server'
|
|
4
3
|
|
|
5
4
|
import { sleep, queryKey, createQueryClient } from './utils'
|
|
6
|
-
import {
|
|
7
|
-
QueryClient,
|
|
8
|
-
QueryClientProvider,
|
|
9
|
-
QueryCache,
|
|
10
|
-
useQuery,
|
|
11
|
-
useQueryClient,
|
|
12
|
-
} from '..'
|
|
5
|
+
import { QueryClientProvider, QueryCache, useQuery, useQueryClient } from '..'
|
|
13
6
|
|
|
14
7
|
describe('QueryClientProvider', () => {
|
|
15
8
|
test('sets a specific cache for all queries to use', async () => {
|
|
@@ -19,9 +12,12 @@ describe('QueryClientProvider', () => {
|
|
|
19
12
|
const queryClient = createQueryClient({ queryCache })
|
|
20
13
|
|
|
21
14
|
function Page() {
|
|
22
|
-
const { data } = useQuery(
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
const { data } = useQuery({
|
|
16
|
+
queryKey: key,
|
|
17
|
+
queryFn: async () => {
|
|
18
|
+
await sleep(10)
|
|
19
|
+
return 'test'
|
|
20
|
+
},
|
|
25
21
|
})
|
|
26
22
|
|
|
27
23
|
return (
|
|
@@ -39,7 +35,7 @@ describe('QueryClientProvider', () => {
|
|
|
39
35
|
|
|
40
36
|
await waitFor(() => rendered.getByText('test'))
|
|
41
37
|
|
|
42
|
-
expect(queryCache.find(key)).toBeDefined()
|
|
38
|
+
expect(queryCache.find({ queryKey: key })).toBeDefined()
|
|
43
39
|
})
|
|
44
40
|
|
|
45
41
|
test('allows multiple caches to be partitioned', async () => {
|
|
@@ -53,9 +49,12 @@ describe('QueryClientProvider', () => {
|
|
|
53
49
|
const queryClient2 = createQueryClient({ queryCache: queryCache2 })
|
|
54
50
|
|
|
55
51
|
function Page1() {
|
|
56
|
-
const { data } = useQuery(
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
const { data } = useQuery({
|
|
53
|
+
queryKey: key1,
|
|
54
|
+
queryFn: async () => {
|
|
55
|
+
await sleep(10)
|
|
56
|
+
return 'test1'
|
|
57
|
+
},
|
|
59
58
|
})
|
|
60
59
|
|
|
61
60
|
return (
|
|
@@ -65,9 +64,12 @@ describe('QueryClientProvider', () => {
|
|
|
65
64
|
)
|
|
66
65
|
}
|
|
67
66
|
function Page2() {
|
|
68
|
-
const { data } = useQuery(
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const { data } = useQuery({
|
|
68
|
+
queryKey: key2,
|
|
69
|
+
queryFn: async () => {
|
|
70
|
+
await sleep(10)
|
|
71
|
+
return 'test2'
|
|
72
|
+
},
|
|
71
73
|
})
|
|
72
74
|
|
|
73
75
|
return (
|
|
@@ -91,10 +93,10 @@ describe('QueryClientProvider', () => {
|
|
|
91
93
|
await waitFor(() => rendered.getByText('test1'))
|
|
92
94
|
await waitFor(() => rendered.getByText('test2'))
|
|
93
95
|
|
|
94
|
-
expect(queryCache1.find(key1)).toBeDefined()
|
|
95
|
-
expect(queryCache1.find(key2)).not.toBeDefined()
|
|
96
|
-
expect(queryCache2.find(key1)).not.toBeDefined()
|
|
97
|
-
expect(queryCache2.find(key2)).toBeDefined()
|
|
96
|
+
expect(queryCache1.find({ queryKey: key1 })).toBeDefined()
|
|
97
|
+
expect(queryCache1.find({ queryKey: key2 })).not.toBeDefined()
|
|
98
|
+
expect(queryCache2.find({ queryKey: key1 })).not.toBeDefined()
|
|
99
|
+
expect(queryCache2.find({ queryKey: key2 })).toBeDefined()
|
|
98
100
|
})
|
|
99
101
|
|
|
100
102
|
test("uses defaultOptions for queries when they don't provide their own config", async () => {
|
|
@@ -105,15 +107,18 @@ describe('QueryClientProvider', () => {
|
|
|
105
107
|
queryCache,
|
|
106
108
|
defaultOptions: {
|
|
107
109
|
queries: {
|
|
108
|
-
|
|
110
|
+
gcTime: Infinity,
|
|
109
111
|
},
|
|
110
112
|
},
|
|
111
113
|
})
|
|
112
114
|
|
|
113
115
|
function Page() {
|
|
114
|
-
const { data } = useQuery(
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
const { data } = useQuery({
|
|
117
|
+
queryKey: key,
|
|
118
|
+
queryFn: async () => {
|
|
119
|
+
await sleep(10)
|
|
120
|
+
return 'test'
|
|
121
|
+
},
|
|
117
122
|
})
|
|
118
123
|
|
|
119
124
|
return (
|
|
@@ -131,73 +136,8 @@ describe('QueryClientProvider', () => {
|
|
|
131
136
|
|
|
132
137
|
await waitFor(() => rendered.getByText('test'))
|
|
133
138
|
|
|
134
|
-
expect(queryCache.find(key)).toBeDefined()
|
|
135
|
-
expect(queryCache.find(key)?.options.
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
describe('with custom context', () => {
|
|
139
|
-
it('uses the correct context', async () => {
|
|
140
|
-
const key = queryKey()
|
|
141
|
-
|
|
142
|
-
const contextOuter = React.createContext<QueryClient | undefined>(
|
|
143
|
-
undefined,
|
|
144
|
-
)
|
|
145
|
-
const contextInner = React.createContext<QueryClient | undefined>(
|
|
146
|
-
undefined,
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
const queryCacheOuter = new QueryCache()
|
|
150
|
-
const queryClientOuter = new QueryClient({ queryCache: queryCacheOuter })
|
|
151
|
-
|
|
152
|
-
const queryCacheInner = new QueryCache()
|
|
153
|
-
const queryClientInner = new QueryClient({ queryCache: queryCacheInner })
|
|
154
|
-
|
|
155
|
-
const queryCacheInnerInner = new QueryCache()
|
|
156
|
-
const queryClientInnerInner = new QueryClient({
|
|
157
|
-
queryCache: queryCacheInnerInner,
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
function Page() {
|
|
161
|
-
const { data: testOuter } = useQuery(key, async () => 'testOuter', {
|
|
162
|
-
context: contextOuter,
|
|
163
|
-
})
|
|
164
|
-
const { data: testInner } = useQuery(key, async () => 'testInner', {
|
|
165
|
-
context: contextInner,
|
|
166
|
-
})
|
|
167
|
-
const { data: testInnerInner } = useQuery(
|
|
168
|
-
key,
|
|
169
|
-
async () => 'testInnerInner',
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
return (
|
|
173
|
-
<div>
|
|
174
|
-
<h1>
|
|
175
|
-
{testOuter} {testInner} {testInnerInner}
|
|
176
|
-
</h1>
|
|
177
|
-
</div>
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// contextSharing should be ignored when passing a custom context.
|
|
182
|
-
const contextSharing = true
|
|
183
|
-
|
|
184
|
-
const rendered = render(
|
|
185
|
-
<QueryClientProvider client={queryClientOuter} context={contextOuter}>
|
|
186
|
-
<QueryClientProvider client={queryClientInner} context={contextInner}>
|
|
187
|
-
<QueryClientProvider
|
|
188
|
-
client={queryClientInnerInner}
|
|
189
|
-
contextSharing={contextSharing}
|
|
190
|
-
>
|
|
191
|
-
<Page />
|
|
192
|
-
</QueryClientProvider>
|
|
193
|
-
</QueryClientProvider>
|
|
194
|
-
</QueryClientProvider>,
|
|
195
|
-
)
|
|
196
|
-
|
|
197
|
-
await waitFor(() =>
|
|
198
|
-
rendered.getByText('testOuter testInner testInnerInner'),
|
|
199
|
-
)
|
|
200
|
-
})
|
|
139
|
+
expect(queryCache.find({ queryKey: key })).toBeDefined()
|
|
140
|
+
expect(queryCache.find({ queryKey: key })?.options.gcTime).toBe(Infinity)
|
|
201
141
|
})
|
|
202
142
|
|
|
203
143
|
describe('useQueryClient', () => {
|
|
@@ -217,59 +157,5 @@ describe('QueryClientProvider', () => {
|
|
|
217
157
|
|
|
218
158
|
consoleMock.mockRestore()
|
|
219
159
|
})
|
|
220
|
-
|
|
221
|
-
test('should use window to get the context when contextSharing is true', () => {
|
|
222
|
-
const queryCache = new QueryCache()
|
|
223
|
-
const queryClient = createQueryClient({ queryCache })
|
|
224
|
-
|
|
225
|
-
let queryClientFromHook: QueryClient | undefined
|
|
226
|
-
let queryClientFromWindow: QueryClient | undefined
|
|
227
|
-
|
|
228
|
-
function Page() {
|
|
229
|
-
queryClientFromHook = useQueryClient()
|
|
230
|
-
queryClientFromWindow = React.useContext(
|
|
231
|
-
window.ReactQueryClientContext as React.Context<
|
|
232
|
-
QueryClient | undefined
|
|
233
|
-
>,
|
|
234
|
-
)
|
|
235
|
-
return null
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
render(
|
|
239
|
-
<QueryClientProvider client={queryClient} contextSharing={true}>
|
|
240
|
-
<Page />
|
|
241
|
-
</QueryClientProvider>,
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
expect(queryClientFromHook).toEqual(queryClient)
|
|
245
|
-
expect(queryClientFromWindow).toEqual(queryClient)
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
test('should not use window to get the context when contextSharing is true and window does not exist', () => {
|
|
249
|
-
const queryCache = new QueryCache()
|
|
250
|
-
const queryClient = createQueryClient({ queryCache })
|
|
251
|
-
|
|
252
|
-
// Mock a non web browser environment
|
|
253
|
-
const windowSpy = jest
|
|
254
|
-
.spyOn(window, 'window', 'get')
|
|
255
|
-
.mockImplementation(undefined)
|
|
256
|
-
|
|
257
|
-
let queryClientFromHook: QueryClient | undefined
|
|
258
|
-
|
|
259
|
-
function Page() {
|
|
260
|
-
queryClientFromHook = useQueryClient()
|
|
261
|
-
return null
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
renderToString(
|
|
265
|
-
<QueryClientProvider client={queryClient} contextSharing={true}>
|
|
266
|
-
<Page />
|
|
267
|
-
</QueryClientProvider>,
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
expect(queryClientFromHook).toEqual(queryClient)
|
|
271
|
-
|
|
272
|
-
windowSpy.mockRestore()
|
|
273
|
-
})
|
|
274
160
|
})
|
|
275
161
|
})
|
|
@@ -17,14 +17,17 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
17
17
|
const queryClient = createQueryClient({ queryCache })
|
|
18
18
|
|
|
19
19
|
it('should retry fetch if the reset error boundary has been reset', async () => {
|
|
20
|
+
const consoleMock = jest
|
|
21
|
+
.spyOn(console, 'error')
|
|
22
|
+
.mockImplementation(() => undefined)
|
|
20
23
|
const key = queryKey()
|
|
21
24
|
|
|
22
25
|
let succeed = false
|
|
23
26
|
|
|
24
27
|
function Page() {
|
|
25
|
-
const { data } = useQuery(
|
|
26
|
-
key,
|
|
27
|
-
async () => {
|
|
28
|
+
const { data } = useQuery({
|
|
29
|
+
queryKey: key,
|
|
30
|
+
queryFn: async () => {
|
|
28
31
|
await sleep(10)
|
|
29
32
|
if (!succeed) {
|
|
30
33
|
throw new Error('Error')
|
|
@@ -32,11 +35,9 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
32
35
|
return 'data'
|
|
33
36
|
}
|
|
34
37
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
)
|
|
38
|
+
retry: false,
|
|
39
|
+
throwErrors: true,
|
|
40
|
+
})
|
|
40
41
|
return <div>{data}</div>
|
|
41
42
|
}
|
|
42
43
|
|
|
@@ -70,17 +71,21 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
70
71
|
succeed = true
|
|
71
72
|
fireEvent.click(rendered.getByText('retry'))
|
|
72
73
|
await waitFor(() => rendered.getByText('data'))
|
|
74
|
+
consoleMock.mockRestore()
|
|
73
75
|
})
|
|
74
76
|
|
|
75
77
|
it('should not throw error if query is disabled', async () => {
|
|
78
|
+
const consoleMock = jest
|
|
79
|
+
.spyOn(console, 'error')
|
|
80
|
+
.mockImplementation(() => undefined)
|
|
76
81
|
const key = queryKey()
|
|
77
82
|
|
|
78
83
|
let succeed = false
|
|
79
84
|
|
|
80
85
|
function Page() {
|
|
81
|
-
const { data, status } = useQuery(
|
|
82
|
-
key,
|
|
83
|
-
async () => {
|
|
86
|
+
const { data, status } = useQuery({
|
|
87
|
+
queryKey: key,
|
|
88
|
+
queryFn: async () => {
|
|
84
89
|
await sleep(10)
|
|
85
90
|
if (!succeed) {
|
|
86
91
|
throw new Error('Error')
|
|
@@ -88,12 +93,10 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
88
93
|
return 'data'
|
|
89
94
|
}
|
|
90
95
|
},
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
},
|
|
96
|
-
)
|
|
96
|
+
retry: false,
|
|
97
|
+
enabled: !succeed,
|
|
98
|
+
throwErrors: true,
|
|
99
|
+
})
|
|
97
100
|
return (
|
|
98
101
|
<div>
|
|
99
102
|
<div>status: {status}</div>
|
|
@@ -132,18 +135,23 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
132
135
|
succeed = true
|
|
133
136
|
fireEvent.click(rendered.getByText('retry'))
|
|
134
137
|
await waitFor(() => rendered.getByText('status: error'))
|
|
138
|
+
consoleMock.mockRestore()
|
|
135
139
|
})
|
|
136
140
|
|
|
137
141
|
it('should not throw error if query is disabled, and refetch if query becomes enabled again', async () => {
|
|
142
|
+
const consoleMock = jest
|
|
143
|
+
.spyOn(console, 'error')
|
|
144
|
+
.mockImplementation(() => undefined)
|
|
145
|
+
|
|
138
146
|
const key = queryKey()
|
|
139
147
|
|
|
140
148
|
let succeed = false
|
|
141
149
|
|
|
142
150
|
function Page() {
|
|
143
151
|
const [enabled, setEnabled] = React.useState(false)
|
|
144
|
-
const { data } = useQuery(
|
|
145
|
-
key,
|
|
146
|
-
async () => {
|
|
152
|
+
const { data } = useQuery({
|
|
153
|
+
queryKey: key,
|
|
154
|
+
queryFn: async () => {
|
|
147
155
|
await sleep(10)
|
|
148
156
|
if (!succeed) {
|
|
149
157
|
throw new Error('Error')
|
|
@@ -151,12 +159,10 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
151
159
|
return 'data'
|
|
152
160
|
}
|
|
153
161
|
},
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
},
|
|
159
|
-
)
|
|
162
|
+
retry: false,
|
|
163
|
+
enabled,
|
|
164
|
+
throwErrors: true,
|
|
165
|
+
})
|
|
160
166
|
|
|
161
167
|
React.useEffect(() => {
|
|
162
168
|
setEnabled(true)
|
|
@@ -195,23 +201,26 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
195
201
|
succeed = true
|
|
196
202
|
fireEvent.click(rendered.getByText('retry'))
|
|
197
203
|
await waitFor(() => rendered.getByText('data'))
|
|
204
|
+
consoleMock.mockRestore()
|
|
198
205
|
})
|
|
199
206
|
|
|
200
207
|
it('should throw error if query is disabled and manually refetched', async () => {
|
|
208
|
+
const consoleMock = jest
|
|
209
|
+
.spyOn(console, 'error')
|
|
210
|
+
.mockImplementation(() => undefined)
|
|
211
|
+
|
|
201
212
|
const key = queryKey()
|
|
202
213
|
|
|
203
214
|
function Page() {
|
|
204
|
-
const { data, refetch, status, fetchStatus } = useQuery<string>(
|
|
205
|
-
key,
|
|
206
|
-
async () => {
|
|
215
|
+
const { data, refetch, status, fetchStatus } = useQuery<string>({
|
|
216
|
+
queryKey: key,
|
|
217
|
+
queryFn: async () => {
|
|
207
218
|
throw new Error('Error')
|
|
208
219
|
},
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
},
|
|
214
|
-
)
|
|
220
|
+
retry: false,
|
|
221
|
+
enabled: false,
|
|
222
|
+
throwErrors: true,
|
|
223
|
+
})
|
|
215
224
|
|
|
216
225
|
return (
|
|
217
226
|
<div>
|
|
@@ -250,21 +259,26 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
250
259
|
)
|
|
251
260
|
|
|
252
261
|
await waitFor(() =>
|
|
253
|
-
rendered.getByText('status:
|
|
262
|
+
rendered.getByText('status: pending, fetchStatus: idle'),
|
|
254
263
|
)
|
|
255
264
|
fireEvent.click(rendered.getByRole('button', { name: /refetch/i }))
|
|
256
265
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
266
|
+
consoleMock.mockRestore()
|
|
257
267
|
})
|
|
258
268
|
|
|
259
269
|
it('should not retry fetch if the reset error boundary has not been reset', async () => {
|
|
270
|
+
const consoleMock = jest
|
|
271
|
+
.spyOn(console, 'error')
|
|
272
|
+
.mockImplementation(() => undefined)
|
|
273
|
+
|
|
260
274
|
const key = queryKey()
|
|
261
275
|
|
|
262
276
|
let succeed = false
|
|
263
277
|
|
|
264
278
|
function Page() {
|
|
265
|
-
const { data } = useQuery(
|
|
266
|
-
key,
|
|
267
|
-
async () => {
|
|
279
|
+
const { data } = useQuery({
|
|
280
|
+
queryKey: key,
|
|
281
|
+
queryFn: async () => {
|
|
268
282
|
await sleep(10)
|
|
269
283
|
if (!succeed) {
|
|
270
284
|
throw new Error('Error')
|
|
@@ -272,11 +286,9 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
272
286
|
return 'data'
|
|
273
287
|
}
|
|
274
288
|
},
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
},
|
|
279
|
-
)
|
|
289
|
+
retry: false,
|
|
290
|
+
throwErrors: true,
|
|
291
|
+
})
|
|
280
292
|
return <div>{data}</div>
|
|
281
293
|
}
|
|
282
294
|
|
|
@@ -309,17 +321,22 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
309
321
|
succeed = true
|
|
310
322
|
fireEvent.click(rendered.getByText('retry'))
|
|
311
323
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
324
|
+
consoleMock.mockRestore()
|
|
312
325
|
})
|
|
313
326
|
|
|
314
327
|
it('should retry fetch if the reset error boundary has been reset and the query contains data from a previous fetch', async () => {
|
|
328
|
+
const consoleMock = jest
|
|
329
|
+
.spyOn(console, 'error')
|
|
330
|
+
.mockImplementation(() => undefined)
|
|
331
|
+
|
|
315
332
|
const key = queryKey()
|
|
316
333
|
|
|
317
334
|
let succeed = false
|
|
318
335
|
|
|
319
336
|
function Page() {
|
|
320
|
-
const { data } = useQuery(
|
|
321
|
-
key,
|
|
322
|
-
async () => {
|
|
337
|
+
const { data } = useQuery({
|
|
338
|
+
queryKey: key,
|
|
339
|
+
queryFn: async () => {
|
|
323
340
|
await sleep(10)
|
|
324
341
|
if (!succeed) {
|
|
325
342
|
throw new Error('Error')
|
|
@@ -327,12 +344,10 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
327
344
|
return 'data'
|
|
328
345
|
}
|
|
329
346
|
},
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
},
|
|
335
|
-
)
|
|
347
|
+
retry: false,
|
|
348
|
+
throwErrors: true,
|
|
349
|
+
initialData: 'initial',
|
|
350
|
+
})
|
|
336
351
|
return <div>{data}</div>
|
|
337
352
|
}
|
|
338
353
|
|
|
@@ -366,18 +381,23 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
366
381
|
succeed = true
|
|
367
382
|
fireEvent.click(rendered.getByText('retry'))
|
|
368
383
|
await waitFor(() => rendered.getByText('data'))
|
|
384
|
+
consoleMock.mockRestore()
|
|
369
385
|
})
|
|
370
386
|
|
|
371
387
|
it('should not retry fetch if the reset error boundary has not been reset after a previous reset', async () => {
|
|
388
|
+
const consoleMock = jest
|
|
389
|
+
.spyOn(console, 'error')
|
|
390
|
+
.mockImplementation(() => undefined)
|
|
391
|
+
|
|
372
392
|
const key = queryKey()
|
|
373
393
|
|
|
374
394
|
let succeed = false
|
|
375
395
|
let shouldReset = true
|
|
376
396
|
|
|
377
397
|
function Page() {
|
|
378
|
-
const { data } = useQuery(
|
|
379
|
-
key,
|
|
380
|
-
async () => {
|
|
398
|
+
const { data } = useQuery({
|
|
399
|
+
queryKey: key,
|
|
400
|
+
queryFn: async () => {
|
|
381
401
|
await sleep(10)
|
|
382
402
|
if (!succeed) {
|
|
383
403
|
throw new Error('Error')
|
|
@@ -385,11 +405,9 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
385
405
|
return 'data'
|
|
386
406
|
}
|
|
387
407
|
},
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
},
|
|
392
|
-
)
|
|
408
|
+
retry: false,
|
|
409
|
+
throwErrors: true,
|
|
410
|
+
})
|
|
393
411
|
return <div>{data}</div>
|
|
394
412
|
}
|
|
395
413
|
|
|
@@ -431,25 +449,28 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
431
449
|
shouldReset = false
|
|
432
450
|
fireEvent.click(rendered.getByText('retry'))
|
|
433
451
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
452
|
+
consoleMock.mockRestore()
|
|
434
453
|
})
|
|
435
454
|
|
|
436
455
|
it('should throw again on error after the reset error boundary has been reset', async () => {
|
|
456
|
+
const consoleMock = jest
|
|
457
|
+
.spyOn(console, 'error')
|
|
458
|
+
.mockImplementation(() => undefined)
|
|
459
|
+
|
|
437
460
|
const key = queryKey()
|
|
438
461
|
let fetchCount = 0
|
|
439
462
|
|
|
440
463
|
function Page() {
|
|
441
|
-
const { data } = useQuery<string>(
|
|
442
|
-
key,
|
|
443
|
-
async () => {
|
|
464
|
+
const { data } = useQuery<string>({
|
|
465
|
+
queryKey: key,
|
|
466
|
+
queryFn: async () => {
|
|
444
467
|
fetchCount++
|
|
445
468
|
await sleep(10)
|
|
446
469
|
throw new Error('Error')
|
|
447
470
|
},
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
},
|
|
452
|
-
)
|
|
471
|
+
retry: false,
|
|
472
|
+
throwErrors: true,
|
|
473
|
+
})
|
|
453
474
|
return <div>{data}</div>
|
|
454
475
|
}
|
|
455
476
|
|
|
@@ -486,17 +507,22 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
486
507
|
fireEvent.click(rendered.getByText('retry'))
|
|
487
508
|
await waitFor(() => rendered.getByText('error boundary'))
|
|
488
509
|
expect(fetchCount).toBe(3)
|
|
510
|
+
consoleMock.mockRestore()
|
|
489
511
|
})
|
|
490
512
|
|
|
491
513
|
it('should never render the component while the query is in error state', async () => {
|
|
514
|
+
const consoleMock = jest
|
|
515
|
+
.spyOn(console, 'error')
|
|
516
|
+
.mockImplementation(() => undefined)
|
|
517
|
+
|
|
492
518
|
const key = queryKey()
|
|
493
519
|
let fetchCount = 0
|
|
494
520
|
let renders = 0
|
|
495
521
|
|
|
496
522
|
function Page() {
|
|
497
|
-
const { data } = useQuery(
|
|
498
|
-
key,
|
|
499
|
-
async () => {
|
|
523
|
+
const { data } = useQuery({
|
|
524
|
+
queryKey: key,
|
|
525
|
+
queryFn: async () => {
|
|
500
526
|
fetchCount++
|
|
501
527
|
await sleep(10)
|
|
502
528
|
if (fetchCount > 2) {
|
|
@@ -505,11 +531,9 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
505
531
|
throw new Error('Error')
|
|
506
532
|
}
|
|
507
533
|
},
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
},
|
|
512
|
-
)
|
|
534
|
+
retry: false,
|
|
535
|
+
suspense: true,
|
|
536
|
+
})
|
|
513
537
|
renders++
|
|
514
538
|
return <div>{data}</div>
|
|
515
539
|
}
|
|
@@ -550,9 +574,14 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
550
574
|
await waitFor(() => rendered.getByText('data'))
|
|
551
575
|
expect(fetchCount).toBe(3)
|
|
552
576
|
expect(renders).toBe(1)
|
|
577
|
+
consoleMock.mockRestore()
|
|
553
578
|
})
|
|
554
579
|
|
|
555
580
|
it('should render children', async () => {
|
|
581
|
+
const consoleMock = jest
|
|
582
|
+
.spyOn(console, 'error')
|
|
583
|
+
.mockImplementation(() => undefined)
|
|
584
|
+
|
|
556
585
|
function Page() {
|
|
557
586
|
return (
|
|
558
587
|
<div>
|
|
@@ -569,17 +598,22 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
569
598
|
)
|
|
570
599
|
|
|
571
600
|
expect(rendered.queryByText('page')).not.toBeNull()
|
|
601
|
+
consoleMock.mockRestore()
|
|
572
602
|
})
|
|
573
603
|
|
|
574
604
|
it('should show error boundary when using tracked queries even though we do not track the error field', async () => {
|
|
605
|
+
const consoleMock = jest
|
|
606
|
+
.spyOn(console, 'error')
|
|
607
|
+
.mockImplementation(() => undefined)
|
|
608
|
+
|
|
575
609
|
const key = queryKey()
|
|
576
610
|
|
|
577
611
|
let succeed = false
|
|
578
612
|
|
|
579
613
|
function Page() {
|
|
580
|
-
const { data } = useQuery(
|
|
581
|
-
key,
|
|
582
|
-
async () => {
|
|
614
|
+
const { data } = useQuery({
|
|
615
|
+
queryKey: key,
|
|
616
|
+
queryFn: async () => {
|
|
583
617
|
await sleep(10)
|
|
584
618
|
if (!succeed) {
|
|
585
619
|
throw new Error('Error')
|
|
@@ -587,11 +621,9 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
587
621
|
return 'data'
|
|
588
622
|
}
|
|
589
623
|
},
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
},
|
|
594
|
-
)
|
|
624
|
+
retry: false,
|
|
625
|
+
throwErrors: true,
|
|
626
|
+
})
|
|
595
627
|
return <div>{data}</div>
|
|
596
628
|
}
|
|
597
629
|
|
|
@@ -625,5 +657,6 @@ describe('QueryErrorResetBoundary', () => {
|
|
|
625
657
|
succeed = true
|
|
626
658
|
fireEvent.click(rendered.getByText('retry'))
|
|
627
659
|
await waitFor(() => rendered.getByText('data'))
|
|
660
|
+
consoleMock.mockRestore()
|
|
628
661
|
})
|
|
629
662
|
})
|