@tanstack/react-query 5.28.0 → 5.28.4
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/package.json +2 -2
- package/src/__tests__/useQuery.test.tsx +100 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-query",
|
|
3
|
-
"version": "5.28.
|
|
3
|
+
"version": "5.28.4",
|
|
4
4
|
"description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"!build/codemods/**/__tests__"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@tanstack/query-core": "5.28.
|
|
44
|
+
"@tanstack/query-core": "5.28.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/react": "^18.2.55",
|
|
@@ -6375,4 +6375,104 @@ describe('useQuery', () => {
|
|
|
6375
6375
|
await waitFor(() => rendered.getByText('status: success'))
|
|
6376
6376
|
await waitFor(() => rendered.getByText('data: data'))
|
|
6377
6377
|
})
|
|
6378
|
+
|
|
6379
|
+
it('should return correct optimistic result when fetching after error', async () => {
|
|
6380
|
+
const key = queryKey()
|
|
6381
|
+
const error = new Error('oh no')
|
|
6382
|
+
|
|
6383
|
+
const results: Array<UseQueryResult<string>> = []
|
|
6384
|
+
|
|
6385
|
+
function Page() {
|
|
6386
|
+
const query = useQuery({
|
|
6387
|
+
queryKey: key,
|
|
6388
|
+
queryFn: async () => {
|
|
6389
|
+
await sleep(10)
|
|
6390
|
+
return Promise.reject(error)
|
|
6391
|
+
},
|
|
6392
|
+
retry: false,
|
|
6393
|
+
notifyOnChangeProps: 'all',
|
|
6394
|
+
})
|
|
6395
|
+
|
|
6396
|
+
results.push(query)
|
|
6397
|
+
|
|
6398
|
+
return (
|
|
6399
|
+
<div>
|
|
6400
|
+
<div>
|
|
6401
|
+
status: {query.status}, {query.fetchStatus}
|
|
6402
|
+
</div>
|
|
6403
|
+
<div>error: {query.error?.message}</div>
|
|
6404
|
+
</div>
|
|
6405
|
+
)
|
|
6406
|
+
}
|
|
6407
|
+
|
|
6408
|
+
function App() {
|
|
6409
|
+
const [enabled, setEnabled] = React.useState(true)
|
|
6410
|
+
|
|
6411
|
+
return (
|
|
6412
|
+
<div>
|
|
6413
|
+
<button onClick={() => setEnabled(!enabled)}>toggle</button>
|
|
6414
|
+
{enabled && <Page />}
|
|
6415
|
+
</div>
|
|
6416
|
+
)
|
|
6417
|
+
}
|
|
6418
|
+
|
|
6419
|
+
const rendered = renderWithClient(queryClient, <App />)
|
|
6420
|
+
|
|
6421
|
+
await waitFor(() => rendered.getByText('status: error, idle'))
|
|
6422
|
+
|
|
6423
|
+
fireEvent.click(rendered.getByRole('button', { name: 'toggle' }))
|
|
6424
|
+
fireEvent.click(rendered.getByRole('button', { name: 'toggle' }))
|
|
6425
|
+
|
|
6426
|
+
await waitFor(() => rendered.getByText('status: error, idle'))
|
|
6427
|
+
|
|
6428
|
+
expect(results).toHaveLength(4)
|
|
6429
|
+
|
|
6430
|
+
// initial fetch
|
|
6431
|
+
expect(results[0]).toMatchObject({
|
|
6432
|
+
status: 'pending',
|
|
6433
|
+
fetchStatus: 'fetching',
|
|
6434
|
+
error: null,
|
|
6435
|
+
errorUpdatedAt: 0,
|
|
6436
|
+
errorUpdateCount: 0,
|
|
6437
|
+
isLoading: true,
|
|
6438
|
+
failureCount: 0,
|
|
6439
|
+
failureReason: null,
|
|
6440
|
+
})
|
|
6441
|
+
|
|
6442
|
+
// error state
|
|
6443
|
+
expect(results[1]).toMatchObject({
|
|
6444
|
+
status: 'error',
|
|
6445
|
+
fetchStatus: 'idle',
|
|
6446
|
+
error,
|
|
6447
|
+
errorUpdateCount: 1,
|
|
6448
|
+
isLoading: false,
|
|
6449
|
+
failureCount: 1,
|
|
6450
|
+
failureReason: error,
|
|
6451
|
+
})
|
|
6452
|
+
expect(results[1]?.errorUpdatedAt).toBeGreaterThan(0)
|
|
6453
|
+
|
|
6454
|
+
// refetch, optimistic state, no errors anymore
|
|
6455
|
+
expect(results[2]).toMatchObject({
|
|
6456
|
+
status: 'pending',
|
|
6457
|
+
fetchStatus: 'fetching',
|
|
6458
|
+
error: null,
|
|
6459
|
+
errorUpdateCount: 1,
|
|
6460
|
+
isLoading: true,
|
|
6461
|
+
failureCount: 0,
|
|
6462
|
+
failureReason: null,
|
|
6463
|
+
})
|
|
6464
|
+
expect(results[2]?.errorUpdatedAt).toBeGreaterThan(0)
|
|
6465
|
+
|
|
6466
|
+
// final state
|
|
6467
|
+
expect(results[3]).toMatchObject({
|
|
6468
|
+
status: 'error',
|
|
6469
|
+
fetchStatus: 'idle',
|
|
6470
|
+
error: error,
|
|
6471
|
+
errorUpdateCount: 2,
|
|
6472
|
+
isLoading: false,
|
|
6473
|
+
failureCount: 1,
|
|
6474
|
+
failureReason: error,
|
|
6475
|
+
})
|
|
6476
|
+
expect(results[3]?.errorUpdatedAt).toBeGreaterThan(0)
|
|
6477
|
+
})
|
|
6378
6478
|
})
|