Skip to content

Commit 7b44a50

Browse files
authored
fix: should throw error when using useErrorBoundary (#1016)
1 parent c5738db commit 7b44a50

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/react/ReactQueryErrorResetBoundary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface ReactQueryErrorResetBoundaryValue {
99
}
1010

1111
function createValue(): ReactQueryErrorResetBoundaryValue {
12-
let isReset = true
12+
let isReset = false
1313
return {
1414
clearReset: () => {
1515
isReset = false
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { render, waitFor, fireEvent } from '@testing-library/react'
2+
import { ErrorBoundary } from 'react-error-boundary'
3+
import * as React from 'react'
4+
5+
import { sleep, queryKey, mockConsoleError } from './utils'
6+
import { useQuery, ReactQueryErrorResetBoundary } from '../..'
7+
8+
describe('ReactQueryResetErrorBoundary', () => {
9+
it('should retry fetch if the reset error boundary has been reset', async () => {
10+
const key = queryKey()
11+
12+
let succeed = false
13+
const consoleMock = mockConsoleError()
14+
15+
function Page() {
16+
const { data } = useQuery(
17+
key,
18+
async () => {
19+
await sleep(10)
20+
if (!succeed) {
21+
throw new Error('Error')
22+
} else {
23+
return 'data'
24+
}
25+
},
26+
{
27+
retry: false,
28+
useErrorBoundary: true,
29+
}
30+
)
31+
return <div>{data}</div>
32+
}
33+
34+
const rendered = render(
35+
<ReactQueryErrorResetBoundary>
36+
{({ reset }) => (
37+
<ErrorBoundary
38+
onReset={reset}
39+
fallbackRender={({ resetErrorBoundary }) => (
40+
<div>
41+
<div>error boundary</div>
42+
<button
43+
onClick={() => {
44+
resetErrorBoundary()
45+
}}
46+
>
47+
retry
48+
</button>
49+
</div>
50+
)}
51+
>
52+
<Page />
53+
</ErrorBoundary>
54+
)}
55+
</ReactQueryErrorResetBoundary>
56+
)
57+
58+
await waitFor(() => rendered.getByText('error boundary'))
59+
await waitFor(() => rendered.getByText('retry'))
60+
succeed = true
61+
fireEvent.click(rendered.getByText('retry'))
62+
await waitFor(() => rendered.getByText('data'))
63+
64+
consoleMock.mockRestore()
65+
})
66+
})

0 commit comments

Comments
 (0)