@tanstack/react-query 4.10.3 → 4.13.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/LICENSE +1 -1
- package/build/umd/index.development.js +27 -10
- 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 +10 -4
- package/src/__tests__/useInfiniteQuery.test.tsx +2 -0
- package/src/__tests__/useMutation.test.tsx +58 -1
- package/src/__tests__/useQuery.test.tsx +51 -45
- package/src/__tests__/utils.tsx +6 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-query",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.0",
|
|
4
4
|
"description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,12 +35,18 @@
|
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/jscodeshift": "^0.11.3",
|
|
38
|
+
"@types/react": "^18.0.14",
|
|
39
|
+
"@types/react-dom": "^18.0.5",
|
|
38
40
|
"@types/use-sync-external-store": "^0.0.3",
|
|
41
|
+
"react": "^18.2.0",
|
|
42
|
+
"react-17": "npm:react@^17.0.2",
|
|
43
|
+
"react-dom": "^18.2.0",
|
|
44
|
+
"react-dom-17": "npm:react-dom@^17.0.2",
|
|
39
45
|
"jscodeshift": "^0.13.1",
|
|
40
46
|
"react-error-boundary": "^3.1.4"
|
|
41
47
|
},
|
|
42
48
|
"dependencies": {
|
|
43
|
-
"@tanstack/query-core": "4.
|
|
49
|
+
"@tanstack/query-core": "4.13.0",
|
|
44
50
|
"use-sync-external-store": "^1.2.0"
|
|
45
51
|
},
|
|
46
52
|
"peerDependencies": {
|
|
@@ -60,7 +66,7 @@
|
|
|
60
66
|
"clean": "rm -rf ./build",
|
|
61
67
|
"test:codemods": "../../node_modules/.bin/jest --config codemods/jest.config.js",
|
|
62
68
|
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src",
|
|
63
|
-
"test:jest": "pnpm test:codemods && ../../node_modules/.bin/jest --config jest.config.
|
|
64
|
-
"test:
|
|
69
|
+
"test:jest": "pnpm run test:codemods && ../../node_modules/.bin/jest --config ./jest.config.ts",
|
|
70
|
+
"test:dev": "pnpm run test:jest --watch"
|
|
65
71
|
}
|
|
66
72
|
}
|
|
@@ -71,6 +71,7 @@ describe('useInfiniteQuery', () => {
|
|
|
71
71
|
error: null,
|
|
72
72
|
errorUpdatedAt: 0,
|
|
73
73
|
failureCount: 0,
|
|
74
|
+
failureReason: null,
|
|
74
75
|
errorUpdateCount: 0,
|
|
75
76
|
fetchNextPage: expect.any(Function),
|
|
76
77
|
fetchPreviousPage: expect.any(Function),
|
|
@@ -104,6 +105,7 @@ describe('useInfiniteQuery', () => {
|
|
|
104
105
|
error: null,
|
|
105
106
|
errorUpdatedAt: 0,
|
|
106
107
|
failureCount: 0,
|
|
108
|
+
failureReason: null,
|
|
107
109
|
errorUpdateCount: 0,
|
|
108
110
|
fetchNextPage: expect.any(Function),
|
|
109
111
|
fetchPreviousPage: expect.any(Function),
|
|
@@ -147,6 +147,57 @@ describe('useMutation', () => {
|
|
|
147
147
|
expect(onSettledMock).toHaveBeenCalledWith(3)
|
|
148
148
|
})
|
|
149
149
|
|
|
150
|
+
it('should set correct values for `failureReason` and `failureCount` on multiple mutate calls', async () => {
|
|
151
|
+
let count = 0
|
|
152
|
+
type Value = { count: number }
|
|
153
|
+
|
|
154
|
+
const mutateFn = jest.fn<Promise<Value>, [value: Value]>()
|
|
155
|
+
|
|
156
|
+
mutateFn.mockImplementationOnce(() => {
|
|
157
|
+
return Promise.reject('Error test Jonas')
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
mutateFn.mockImplementation(async (value) => {
|
|
161
|
+
await sleep(10)
|
|
162
|
+
return Promise.resolve(value)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
function Page() {
|
|
166
|
+
const { mutate, failureCount, failureReason, data, status } = useMutation<
|
|
167
|
+
Value,
|
|
168
|
+
string,
|
|
169
|
+
Value
|
|
170
|
+
>(mutateFn)
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<div>
|
|
174
|
+
<h1>Data {data?.count}</h1>
|
|
175
|
+
<h2>Status {status}</h2>
|
|
176
|
+
<h2>Failed {failureCount} times</h2>
|
|
177
|
+
<h2>Failed because {failureReason ?? 'null'}</h2>
|
|
178
|
+
<button onClick={() => mutate({ count: ++count })}>mutate</button>
|
|
179
|
+
</div>
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const rendered = renderWithClient(queryClient, <Page />)
|
|
184
|
+
|
|
185
|
+
await waitFor(() => rendered.getByText('Data'))
|
|
186
|
+
|
|
187
|
+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
|
|
188
|
+
await waitFor(() => rendered.getByText('Data'))
|
|
189
|
+
await waitFor(() => rendered.getByText('Status error'))
|
|
190
|
+
await waitFor(() => rendered.getByText('Failed 1 times'))
|
|
191
|
+
await waitFor(() => rendered.getByText('Failed because Error test Jonas'))
|
|
192
|
+
|
|
193
|
+
fireEvent.click(rendered.getByRole('button', { name: /mutate/i }))
|
|
194
|
+
await waitFor(() => rendered.getByText('Status loading'))
|
|
195
|
+
await waitFor(() => rendered.getByText('Status success'))
|
|
196
|
+
await waitFor(() => rendered.getByText('Data 2'))
|
|
197
|
+
await waitFor(() => rendered.getByText('Failed 0 times'))
|
|
198
|
+
await waitFor(() => rendered.getByText('Failed because null'))
|
|
199
|
+
})
|
|
200
|
+
|
|
150
201
|
it('should be able to call `onError` and `onSettled` after each failed mutate', async () => {
|
|
151
202
|
const onErrorMock = jest.fn()
|
|
152
203
|
const onSettledMock = jest.fn()
|
|
@@ -581,21 +632,25 @@ describe('useMutation', () => {
|
|
|
581
632
|
isLoading: false,
|
|
582
633
|
isPaused: false,
|
|
583
634
|
failureCount: 0,
|
|
635
|
+
failureReason: null,
|
|
584
636
|
})
|
|
585
637
|
expect(states[1]).toMatchObject({
|
|
586
638
|
isLoading: true,
|
|
587
639
|
isPaused: false,
|
|
588
640
|
failureCount: 0,
|
|
641
|
+
failureReason: null,
|
|
589
642
|
})
|
|
590
643
|
expect(states[2]).toMatchObject({
|
|
591
644
|
isLoading: true,
|
|
592
645
|
isPaused: false,
|
|
593
646
|
failureCount: 1,
|
|
647
|
+
failureReason: 'oops',
|
|
594
648
|
})
|
|
595
649
|
expect(states[3]).toMatchObject({
|
|
596
650
|
isLoading: true,
|
|
597
651
|
isPaused: true,
|
|
598
652
|
failureCount: 1,
|
|
653
|
+
failureReason: 'oops',
|
|
599
654
|
})
|
|
600
655
|
|
|
601
656
|
onlineMock.mockReturnValue(true)
|
|
@@ -608,11 +663,13 @@ describe('useMutation', () => {
|
|
|
608
663
|
isLoading: true,
|
|
609
664
|
isPaused: false,
|
|
610
665
|
failureCount: 1,
|
|
666
|
+
failureReason: 'oops',
|
|
611
667
|
})
|
|
612
668
|
expect(states[5]).toMatchObject({
|
|
613
669
|
isLoading: false,
|
|
614
670
|
isPaused: false,
|
|
615
|
-
failureCount:
|
|
671
|
+
failureCount: 0,
|
|
672
|
+
failureReason: null,
|
|
616
673
|
data: 'data',
|
|
617
674
|
})
|
|
618
675
|
|
|
@@ -233,6 +233,7 @@ describe('useQuery', () => {
|
|
|
233
233
|
error: null,
|
|
234
234
|
errorUpdatedAt: 0,
|
|
235
235
|
failureCount: 0,
|
|
236
|
+
failureReason: null,
|
|
236
237
|
errorUpdateCount: 0,
|
|
237
238
|
isError: false,
|
|
238
239
|
isFetched: false,
|
|
@@ -260,6 +261,7 @@ describe('useQuery', () => {
|
|
|
260
261
|
error: null,
|
|
261
262
|
errorUpdatedAt: 0,
|
|
262
263
|
failureCount: 0,
|
|
264
|
+
failureReason: null,
|
|
263
265
|
errorUpdateCount: 0,
|
|
264
266
|
isError: false,
|
|
265
267
|
isFetched: true,
|
|
@@ -303,6 +305,7 @@ describe('useQuery', () => {
|
|
|
303
305
|
<div>
|
|
304
306
|
<h1>Status: {state.status}</h1>
|
|
305
307
|
<div>Failure Count: {state.failureCount}</div>
|
|
308
|
+
<div>Failure Reason: {state.failureReason}</div>
|
|
306
309
|
</div>
|
|
307
310
|
)
|
|
308
311
|
}
|
|
@@ -317,6 +320,7 @@ describe('useQuery', () => {
|
|
|
317
320
|
error: null,
|
|
318
321
|
errorUpdatedAt: 0,
|
|
319
322
|
failureCount: 0,
|
|
323
|
+
failureReason: null,
|
|
320
324
|
errorUpdateCount: 0,
|
|
321
325
|
isError: false,
|
|
322
326
|
isFetched: false,
|
|
@@ -344,6 +348,7 @@ describe('useQuery', () => {
|
|
|
344
348
|
error: null,
|
|
345
349
|
errorUpdatedAt: 0,
|
|
346
350
|
failureCount: 1,
|
|
351
|
+
failureReason: 'rejected',
|
|
347
352
|
errorUpdateCount: 0,
|
|
348
353
|
isError: false,
|
|
349
354
|
isFetched: false,
|
|
@@ -371,6 +376,7 @@ describe('useQuery', () => {
|
|
|
371
376
|
error: 'rejected',
|
|
372
377
|
errorUpdatedAt: expect.any(Number),
|
|
373
378
|
failureCount: 2,
|
|
379
|
+
failureReason: 'rejected',
|
|
374
380
|
errorUpdateCount: 1,
|
|
375
381
|
isError: true,
|
|
376
382
|
isFetched: true,
|
|
@@ -2269,40 +2275,6 @@ describe('useQuery', () => {
|
|
|
2269
2275
|
expect(queryCache.find(key)!.options.queryFn).toBe(queryFn1)
|
|
2270
2276
|
})
|
|
2271
2277
|
|
|
2272
|
-
it('should render correct states even in case of useEffect triggering delays', async () => {
|
|
2273
|
-
const key = queryKey()
|
|
2274
|
-
const states: UseQueryResult<string>[] = []
|
|
2275
|
-
|
|
2276
|
-
const originalUseEffect = React.useEffect
|
|
2277
|
-
|
|
2278
|
-
// Try to simulate useEffect timing delay
|
|
2279
|
-
// @ts-ignore
|
|
2280
|
-
React.useEffect = (...args: any[]) => {
|
|
2281
|
-
originalUseEffect(() => {
|
|
2282
|
-
setTimeout(() => {
|
|
2283
|
-
args[0]()
|
|
2284
|
-
}, 10)
|
|
2285
|
-
}, args[1])
|
|
2286
|
-
}
|
|
2287
|
-
|
|
2288
|
-
function Page() {
|
|
2289
|
-
const state = useQuery(key, () => 'data', { staleTime: Infinity })
|
|
2290
|
-
states.push(state)
|
|
2291
|
-
return null
|
|
2292
|
-
}
|
|
2293
|
-
|
|
2294
|
-
renderWithClient(queryClient, <Page />)
|
|
2295
|
-
queryClient.setQueryData(key, 'data')
|
|
2296
|
-
await sleep(50)
|
|
2297
|
-
|
|
2298
|
-
// @ts-ignore
|
|
2299
|
-
React.useEffect = originalUseEffect
|
|
2300
|
-
|
|
2301
|
-
expect(states.length).toBe(2)
|
|
2302
|
-
expect(states[0]).toMatchObject({ status: 'loading' })
|
|
2303
|
-
expect(states[1]).toMatchObject({ status: 'success' })
|
|
2304
|
-
})
|
|
2305
|
-
|
|
2306
2278
|
it('should batch re-renders', async () => {
|
|
2307
2279
|
const key = queryKey()
|
|
2308
2280
|
|
|
@@ -2911,6 +2883,7 @@ describe('useQuery', () => {
|
|
|
2911
2883
|
<div>
|
|
2912
2884
|
<div>error: {result.error ?? 'null'}</div>
|
|
2913
2885
|
<div>failureCount: {result.failureCount}</div>
|
|
2886
|
+
<div>failureReason: {result.failureReason}</div>
|
|
2914
2887
|
</div>
|
|
2915
2888
|
)
|
|
2916
2889
|
}
|
|
@@ -2929,6 +2902,7 @@ describe('useQuery', () => {
|
|
|
2929
2902
|
const rendered = renderWithClient(queryClient, <App />)
|
|
2930
2903
|
|
|
2931
2904
|
await waitFor(() => rendered.getByText('failureCount: 1'))
|
|
2905
|
+
await waitFor(() => rendered.getByText('failureReason: some error'))
|
|
2932
2906
|
fireEvent.click(rendered.getByRole('button', { name: /hide/i }))
|
|
2933
2907
|
await waitFor(() => rendered.getByRole('button', { name: /show/i }))
|
|
2934
2908
|
fireEvent.click(rendered.getByRole('button', { name: /show/i }))
|
|
@@ -2959,6 +2933,7 @@ describe('useQuery', () => {
|
|
|
2959
2933
|
<div>
|
|
2960
2934
|
<div>error: {result.error ?? 'null'}</div>
|
|
2961
2935
|
<div>failureCount: {result.failureCount}</div>
|
|
2936
|
+
<div>failureReason: {result.failureReason}</div>
|
|
2962
2937
|
</div>
|
|
2963
2938
|
)
|
|
2964
2939
|
}
|
|
@@ -2980,6 +2955,7 @@ describe('useQuery', () => {
|
|
|
2980
2955
|
const rendered = renderWithClient(queryClient, <App />)
|
|
2981
2956
|
|
|
2982
2957
|
await waitFor(() => rendered.getByText('failureCount: 1'))
|
|
2958
|
+
await waitFor(() => rendered.getByText('failureReason: some error'))
|
|
2983
2959
|
fireEvent.click(rendered.getByRole('button', { name: /hide/i }))
|
|
2984
2960
|
fireEvent.click(rendered.getByRole('button', { name: /cancel/i }))
|
|
2985
2961
|
await waitFor(() => rendered.getByRole('button', { name: /show/i }))
|
|
@@ -3203,15 +3179,20 @@ describe('useQuery', () => {
|
|
|
3203
3179
|
})
|
|
3204
3180
|
|
|
3205
3181
|
function Page() {
|
|
3206
|
-
const { status, failureCount } = useQuery
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3182
|
+
const { status, failureCount, failureReason } = useQuery<unknown, string>(
|
|
3183
|
+
key,
|
|
3184
|
+
queryFn,
|
|
3185
|
+
{
|
|
3186
|
+
retry: 1,
|
|
3187
|
+
retryDelay: 1,
|
|
3188
|
+
},
|
|
3189
|
+
)
|
|
3210
3190
|
|
|
3211
3191
|
return (
|
|
3212
3192
|
<div>
|
|
3213
3193
|
<h1>{status}</h1>
|
|
3214
3194
|
<h2>Failed {failureCount} times</h2>
|
|
3195
|
+
<h2>Failed because {failureReason}</h2>
|
|
3215
3196
|
</div>
|
|
3216
3197
|
)
|
|
3217
3198
|
}
|
|
@@ -3223,6 +3204,7 @@ describe('useQuery', () => {
|
|
|
3223
3204
|
|
|
3224
3205
|
// query should fail `retry + 1` times, since first time isn't a "retry"
|
|
3225
3206
|
await waitFor(() => rendered.getByText('Failed 2 times'))
|
|
3207
|
+
await waitFor(() => rendered.getByText('Failed because Error test Barrett'))
|
|
3226
3208
|
|
|
3227
3209
|
expect(queryFn).toHaveBeenCalledTimes(2)
|
|
3228
3210
|
})
|
|
@@ -3241,7 +3223,7 @@ describe('useQuery', () => {
|
|
|
3241
3223
|
})
|
|
3242
3224
|
|
|
3243
3225
|
function Page() {
|
|
3244
|
-
const { status, failureCount, error } = useQuery<
|
|
3226
|
+
const { status, failureCount, failureReason, error } = useQuery<
|
|
3245
3227
|
unknown,
|
|
3246
3228
|
string,
|
|
3247
3229
|
[string]
|
|
@@ -3254,6 +3236,7 @@ describe('useQuery', () => {
|
|
|
3254
3236
|
<div>
|
|
3255
3237
|
<h1>{status}</h1>
|
|
3256
3238
|
<h2>Failed {failureCount} times</h2>
|
|
3239
|
+
<h2>Failed because {failureReason}</h2>
|
|
3257
3240
|
<h2>{error}</h2>
|
|
3258
3241
|
</div>
|
|
3259
3242
|
)
|
|
@@ -3265,6 +3248,8 @@ describe('useQuery', () => {
|
|
|
3265
3248
|
await waitFor(() => rendered.getByText('error'))
|
|
3266
3249
|
|
|
3267
3250
|
await waitFor(() => rendered.getByText('Failed 2 times'))
|
|
3251
|
+
await waitFor(() => rendered.getByText('Failed because NoRetry'))
|
|
3252
|
+
|
|
3268
3253
|
await waitFor(() => rendered.getByText('NoRetry'))
|
|
3269
3254
|
|
|
3270
3255
|
expect(queryFn).toHaveBeenCalledTimes(2)
|
|
@@ -3281,7 +3266,7 @@ describe('useQuery', () => {
|
|
|
3281
3266
|
})
|
|
3282
3267
|
|
|
3283
3268
|
function Page() {
|
|
3284
|
-
const { status, failureCount } = useQuery(key, queryFn, {
|
|
3269
|
+
const { status, failureCount, failureReason } = useQuery(key, queryFn, {
|
|
3285
3270
|
retry: 1,
|
|
3286
3271
|
retryDelay: (_, error: DelayError) => error.delay,
|
|
3287
3272
|
})
|
|
@@ -3290,6 +3275,7 @@ describe('useQuery', () => {
|
|
|
3290
3275
|
<div>
|
|
3291
3276
|
<h1>{status}</h1>
|
|
3292
3277
|
<h2>Failed {failureCount} times</h2>
|
|
3278
|
+
<h2>Failed because DelayError: {failureReason?.delay}ms</h2>
|
|
3293
3279
|
</div>
|
|
3294
3280
|
)
|
|
3295
3281
|
}
|
|
@@ -3300,6 +3286,7 @@ describe('useQuery', () => {
|
|
|
3300
3286
|
|
|
3301
3287
|
expect(queryFn).toHaveBeenCalledTimes(1)
|
|
3302
3288
|
|
|
3289
|
+
await waitFor(() => rendered.getByText('Failed because DelayError: 50ms'))
|
|
3303
3290
|
await waitFor(() => rendered.getByText('Failed 2 times'))
|
|
3304
3291
|
|
|
3305
3292
|
expect(queryFn).toHaveBeenCalledTimes(2)
|
|
@@ -3315,7 +3302,7 @@ describe('useQuery', () => {
|
|
|
3315
3302
|
let count = 0
|
|
3316
3303
|
|
|
3317
3304
|
function Page() {
|
|
3318
|
-
const query = useQuery(
|
|
3305
|
+
const query = useQuery<unknown, string>(
|
|
3319
3306
|
key,
|
|
3320
3307
|
() => {
|
|
3321
3308
|
count++
|
|
@@ -3332,6 +3319,7 @@ describe('useQuery', () => {
|
|
|
3332
3319
|
<div>error {String(query.error)}</div>
|
|
3333
3320
|
<div>status {query.status}</div>
|
|
3334
3321
|
<div>failureCount {query.failureCount}</div>
|
|
3322
|
+
<div>failureReason {query.failureReason}</div>
|
|
3335
3323
|
</div>
|
|
3336
3324
|
)
|
|
3337
3325
|
}
|
|
@@ -3340,12 +3328,14 @@ describe('useQuery', () => {
|
|
|
3340
3328
|
|
|
3341
3329
|
// The query should display the first error result
|
|
3342
3330
|
await waitFor(() => rendered.getByText('failureCount 1'))
|
|
3331
|
+
await waitFor(() => rendered.getByText('failureReason fetching error 1'))
|
|
3343
3332
|
await waitFor(() => rendered.getByText('status loading'))
|
|
3344
3333
|
await waitFor(() => rendered.getByText('error null'))
|
|
3345
3334
|
|
|
3346
3335
|
// Check if the query really paused
|
|
3347
3336
|
await sleep(10)
|
|
3348
3337
|
await waitFor(() => rendered.getByText('failureCount 1'))
|
|
3338
|
+
await waitFor(() => rendered.getByText('failureReason fetching error 1'))
|
|
3349
3339
|
|
|
3350
3340
|
act(() => {
|
|
3351
3341
|
// reset visibilityState to original value
|
|
@@ -3355,12 +3345,14 @@ describe('useQuery', () => {
|
|
|
3355
3345
|
|
|
3356
3346
|
// Wait for the final result
|
|
3357
3347
|
await waitFor(() => rendered.getByText('failureCount 4'))
|
|
3348
|
+
await waitFor(() => rendered.getByText('failureReason fetching error 4'))
|
|
3358
3349
|
await waitFor(() => rendered.getByText('status error'))
|
|
3359
3350
|
await waitFor(() => rendered.getByText('error fetching error 4'))
|
|
3360
3351
|
|
|
3361
3352
|
// Check if the query really stopped
|
|
3362
3353
|
await sleep(10)
|
|
3363
3354
|
await waitFor(() => rendered.getByText('failureCount 4'))
|
|
3355
|
+
await waitFor(() => rendered.getByText('failureReason fetching error 4'))
|
|
3364
3356
|
|
|
3365
3357
|
// Check if the error has been logged in the console
|
|
3366
3358
|
expect(mockLogger.error).toHaveBeenCalledWith('fetching error 4')
|
|
@@ -3527,7 +3519,7 @@ describe('useQuery', () => {
|
|
|
3527
3519
|
function Page() {
|
|
3528
3520
|
let counter = 0
|
|
3529
3521
|
|
|
3530
|
-
const query = useQuery(
|
|
3522
|
+
const query = useQuery<string, Error>(
|
|
3531
3523
|
key,
|
|
3532
3524
|
async () => {
|
|
3533
3525
|
if (counter < 2) {
|
|
@@ -3543,6 +3535,7 @@ describe('useQuery', () => {
|
|
|
3543
3535
|
return (
|
|
3544
3536
|
<div>
|
|
3545
3537
|
<div>failureCount {query.failureCount}</div>
|
|
3538
|
+
<div>failureReason {query.failureReason?.message ?? 'null'}</div>
|
|
3546
3539
|
</div>
|
|
3547
3540
|
)
|
|
3548
3541
|
}
|
|
@@ -3550,7 +3543,9 @@ describe('useQuery', () => {
|
|
|
3550
3543
|
const rendered = renderWithClient(queryClient, <Page />)
|
|
3551
3544
|
|
|
3552
3545
|
await waitFor(() => rendered.getByText('failureCount 2'))
|
|
3546
|
+
await waitFor(() => rendered.getByText('failureReason error'))
|
|
3553
3547
|
await waitFor(() => rendered.getByText('failureCount 0'))
|
|
3548
|
+
await waitFor(() => rendered.getByText('failureReason null'))
|
|
3554
3549
|
})
|
|
3555
3550
|
|
|
3556
3551
|
// See https://github.com/tannerlinsley/react-query/issues/199
|
|
@@ -4943,7 +4938,7 @@ describe('useQuery', () => {
|
|
|
4943
4938
|
let count = 0
|
|
4944
4939
|
|
|
4945
4940
|
function Page() {
|
|
4946
|
-
const state = useQuery({
|
|
4941
|
+
const state = useQuery<string, string>({
|
|
4947
4942
|
queryKey: key,
|
|
4948
4943
|
queryFn: async () => {
|
|
4949
4944
|
count++
|
|
@@ -4958,6 +4953,7 @@ describe('useQuery', () => {
|
|
|
4958
4953
|
status: {state.status}, fetchStatus: {state.fetchStatus},
|
|
4959
4954
|
failureCount: {state.failureCount}
|
|
4960
4955
|
</div>
|
|
4956
|
+
<div>failureReason: {state.failureReason ?? 'null'}</div>
|
|
4961
4957
|
<div>data: {state.data}</div>
|
|
4962
4958
|
<button
|
|
4963
4959
|
onClick={() => queryClient.invalidateQueries({ queryKey: key })}
|
|
@@ -4980,6 +4976,7 @@ describe('useQuery', () => {
|
|
|
4980
4976
|
'status: success, fetchStatus: paused, failureCount: 0',
|
|
4981
4977
|
),
|
|
4982
4978
|
)
|
|
4979
|
+
await waitFor(() => rendered.getByText('failureReason: null'))
|
|
4983
4980
|
|
|
4984
4981
|
onlineMock.mockReturnValue(true)
|
|
4985
4982
|
window.dispatchEvent(new Event('online'))
|
|
@@ -4989,11 +4986,13 @@ describe('useQuery', () => {
|
|
|
4989
4986
|
'status: success, fetchStatus: fetching, failureCount: 0',
|
|
4990
4987
|
),
|
|
4991
4988
|
)
|
|
4989
|
+
await waitFor(() => rendered.getByText('failureReason: null'))
|
|
4992
4990
|
await waitFor(() =>
|
|
4993
4991
|
rendered.getByText(
|
|
4994
4992
|
'status: success, fetchStatus: idle, failureCount: 0',
|
|
4995
4993
|
),
|
|
4996
4994
|
)
|
|
4995
|
+
await waitFor(() => rendered.getByText('failureReason: null'))
|
|
4997
4996
|
|
|
4998
4997
|
await waitFor(() => {
|
|
4999
4998
|
expect(rendered.getByText('data: data2')).toBeInTheDocument()
|
|
@@ -5232,7 +5231,7 @@ describe('useQuery', () => {
|
|
|
5232
5231
|
let count = 0
|
|
5233
5232
|
|
|
5234
5233
|
function Page() {
|
|
5235
|
-
const state = useQuery({
|
|
5234
|
+
const state = useQuery<unknown, Error>({
|
|
5236
5235
|
queryKey: key,
|
|
5237
5236
|
queryFn: async (): Promise<unknown> => {
|
|
5238
5237
|
count++
|
|
@@ -5249,6 +5248,7 @@ describe('useQuery', () => {
|
|
|
5249
5248
|
status: {state.status}, fetchStatus: {state.fetchStatus},
|
|
5250
5249
|
failureCount: {state.failureCount}
|
|
5251
5250
|
</div>
|
|
5251
|
+
<div>failureReason: {state.failureReason?.message ?? 'null'}</div>
|
|
5252
5252
|
</div>
|
|
5253
5253
|
)
|
|
5254
5254
|
}
|
|
@@ -5260,6 +5260,7 @@ describe('useQuery', () => {
|
|
|
5260
5260
|
'status: loading, fetchStatus: fetching, failureCount: 1',
|
|
5261
5261
|
),
|
|
5262
5262
|
)
|
|
5263
|
+
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
5263
5264
|
|
|
5264
5265
|
const onlineMock = mockNavigatorOnLine(false)
|
|
5265
5266
|
|
|
@@ -5270,6 +5271,7 @@ describe('useQuery', () => {
|
|
|
5270
5271
|
'status: loading, fetchStatus: paused, failureCount: 1',
|
|
5271
5272
|
),
|
|
5272
5273
|
)
|
|
5274
|
+
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
5273
5275
|
|
|
5274
5276
|
expect(count).toBe(1)
|
|
5275
5277
|
|
|
@@ -5279,6 +5281,7 @@ describe('useQuery', () => {
|
|
|
5279
5281
|
await waitFor(() =>
|
|
5280
5282
|
rendered.getByText('status: error, fetchStatus: idle, failureCount: 3'),
|
|
5281
5283
|
)
|
|
5284
|
+
await waitFor(() => rendered.getByText('failureReason: failed3'))
|
|
5282
5285
|
|
|
5283
5286
|
expect(count).toBe(3)
|
|
5284
5287
|
|
|
@@ -5573,7 +5576,7 @@ describe('useQuery', () => {
|
|
|
5573
5576
|
let count = 0
|
|
5574
5577
|
|
|
5575
5578
|
function Page() {
|
|
5576
|
-
const state = useQuery({
|
|
5579
|
+
const state = useQuery<unknown, Error>({
|
|
5577
5580
|
queryKey: key,
|
|
5578
5581
|
queryFn: async (): Promise<unknown> => {
|
|
5579
5582
|
count++
|
|
@@ -5591,6 +5594,7 @@ describe('useQuery', () => {
|
|
|
5591
5594
|
status: {state.status}, fetchStatus: {state.fetchStatus},
|
|
5592
5595
|
failureCount: {state.failureCount}
|
|
5593
5596
|
</div>
|
|
5597
|
+
<div>failureReason: {state.failureReason?.message ?? 'null'}</div>
|
|
5594
5598
|
</div>
|
|
5595
5599
|
)
|
|
5596
5600
|
}
|
|
@@ -5602,6 +5606,7 @@ describe('useQuery', () => {
|
|
|
5602
5606
|
'status: loading, fetchStatus: paused, failureCount: 1',
|
|
5603
5607
|
),
|
|
5604
5608
|
)
|
|
5609
|
+
await waitFor(() => rendered.getByText('failureReason: failed1'))
|
|
5605
5610
|
|
|
5606
5611
|
expect(count).toBe(1)
|
|
5607
5612
|
|
|
@@ -5611,6 +5616,7 @@ describe('useQuery', () => {
|
|
|
5611
5616
|
await waitFor(() =>
|
|
5612
5617
|
rendered.getByText('status: error, fetchStatus: idle, failureCount: 3'),
|
|
5613
5618
|
)
|
|
5619
|
+
await waitFor(() => rendered.getByText('failureReason: failed3'))
|
|
5614
5620
|
|
|
5615
5621
|
expect(count).toBe(3)
|
|
5616
5622
|
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -109,11 +109,13 @@ export function executeMutation(
|
|
|
109
109
|
// so that we can pretend to be in a server environment
|
|
110
110
|
export function setIsServer(isServer: boolean) {
|
|
111
111
|
const original = utils.isServer
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
Object.defineProperty(utils, 'isServer', {
|
|
113
|
+
get: () => isServer,
|
|
114
|
+
})
|
|
114
115
|
|
|
115
116
|
return () => {
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
Object.defineProperty(utils, 'isServer', {
|
|
118
|
+
get: () => original,
|
|
119
|
+
})
|
|
118
120
|
}
|
|
119
121
|
}
|