Skip to content

fix: should throw error when using useErrorBoundary #1016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/react/ReactQueryErrorResetBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ReactQueryErrorResetBoundaryValue {
}

function createValue(): ReactQueryErrorResetBoundaryValue {
let isReset = true
let isReset = false
return {
clearReset: () => {
isReset = false
Expand Down
66 changes: 66 additions & 0 deletions src/react/tests/ReactQueryResetErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { render, waitFor, fireEvent } from '@testing-library/react'
import { ErrorBoundary } from 'react-error-boundary'
import * as React from 'react'

import { sleep, queryKey, mockConsoleError } from './utils'
import { useQuery, ReactQueryErrorResetBoundary } from '../..'

describe('ReactQueryResetErrorBoundary', () => {
it('should retry fetch if the reset error boundary has been reset', async () => {
const key = queryKey()

let succeed = false
const consoleMock = mockConsoleError()

function Page() {
const { data } = useQuery(
key,
async () => {
await sleep(10)
if (!succeed) {
throw new Error('Error')
} else {
return 'data'
}
},
{
retry: false,
useErrorBoundary: true,
}
)
return <div>{data}</div>
}

const rendered = render(
<ReactQueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
onReset={reset}
fallbackRender={({ resetErrorBoundary }) => (
<div>
<div>error boundary</div>
<button
onClick={() => {
resetErrorBoundary()
}}
>
retry
</button>
</div>
)}
>
<Page />
</ErrorBoundary>
)}
</ReactQueryErrorResetBoundary>
)

await waitFor(() => rendered.getByText('error boundary'))
await waitFor(() => rendered.getByText('retry'))
succeed = true
fireEvent.click(rendered.getByText('retry'))
await waitFor(() => rendered.getByText('data'))

consoleMock.mockRestore()
})
})