|
| 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