Skip to content

Commit 3b2da89

Browse files
committed
Add tests for suspense+useErrorBoundary behaviours
1 parent 9f30130 commit 3b2da89

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

src/react/tests/suspense.test.tsx

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,185 @@ describe("useQuery's in Suspense mode", () => {
550550
consoleMock.mockRestore()
551551
})
552552

553+
it('should throw errors to the error boundary by default', async () => {
554+
const key = queryKey()
555+
556+
const consoleMock = mockConsoleError()
557+
558+
function Page() {
559+
useQuery(
560+
key,
561+
async () => {
562+
await sleep(10)
563+
throw new Error('Suspense Error a1x')
564+
},
565+
{
566+
retry: false,
567+
suspense: true,
568+
}
569+
)
570+
return <div>rendered</div>
571+
}
572+
573+
function App() {
574+
return (
575+
<ErrorBoundary
576+
fallbackRender={() => (
577+
<div>
578+
<div>error boundary</div>
579+
</div>
580+
)}
581+
>
582+
<React.Suspense fallback="Loading...">
583+
<Page />
584+
</React.Suspense>
585+
</ErrorBoundary>
586+
)
587+
}
588+
589+
const rendered = renderWithClient(queryClient, <App />)
590+
591+
await waitFor(() => rendered.getByText('Loading...'))
592+
await waitFor(() => rendered.getByText('error boundary'))
593+
594+
consoleMock.mockRestore()
595+
})
596+
597+
it('should not throw errors to the error boundary when useErrorBoundary: false', async () => {
598+
const key = queryKey()
599+
600+
const consoleMock = mockConsoleError()
601+
602+
function Page() {
603+
useQuery(
604+
key,
605+
async () => {
606+
await sleep(10)
607+
throw new Error('Suspense Error a2x')
608+
},
609+
{
610+
retry: false,
611+
suspense: true,
612+
useErrorBoundary: false,
613+
}
614+
)
615+
return <div>rendered</div>
616+
}
617+
618+
function App() {
619+
return (
620+
<ErrorBoundary
621+
fallbackRender={() => (
622+
<div>
623+
<div>error boundary</div>
624+
</div>
625+
)}
626+
>
627+
<React.Suspense fallback="Loading...">
628+
<Page />
629+
</React.Suspense>
630+
</ErrorBoundary>
631+
)
632+
}
633+
634+
const rendered = renderWithClient(queryClient, <App />)
635+
636+
await waitFor(() => rendered.getByText('Loading...'))
637+
await waitFor(() => rendered.getByText('rendered'))
638+
639+
consoleMock.mockRestore()
640+
})
641+
642+
it('should not throw errors to the error boundary when a useErrorBoundary function returns true', async () => {
643+
const key = queryKey()
644+
645+
const consoleMock = mockConsoleError()
646+
647+
function Page() {
648+
useQuery(
649+
key,
650+
async () => {
651+
await sleep(10)
652+
return Promise.reject('Remote Error')
653+
},
654+
{
655+
retry: false,
656+
suspense: true,
657+
useErrorBoundary: err => err !== 'Local Error',
658+
}
659+
)
660+
return <div>rendered</div>
661+
}
662+
663+
function App() {
664+
return (
665+
<ErrorBoundary
666+
fallbackRender={() => (
667+
<div>
668+
<div>error boundary</div>
669+
</div>
670+
)}
671+
>
672+
<React.Suspense fallback="Loading...">
673+
<Page />
674+
</React.Suspense>
675+
</ErrorBoundary>
676+
)
677+
}
678+
679+
const rendered = renderWithClient(queryClient, <App />)
680+
681+
await waitFor(() => rendered.getByText('Loading...'))
682+
await waitFor(() => rendered.getByText('error boundary'))
683+
684+
consoleMock.mockRestore()
685+
})
686+
687+
it('should not throw errors to the error boundary when a useErrorBoundary function returns false', async () => {
688+
const key = queryKey()
689+
690+
const consoleMock = mockConsoleError()
691+
692+
function Page() {
693+
useQuery(
694+
key,
695+
async () => {
696+
await sleep(10)
697+
return Promise.reject('Local Error')
698+
},
699+
{
700+
retry: false,
701+
suspense: true,
702+
useErrorBoundary: err => err !== 'Local Error',
703+
}
704+
)
705+
return <div>rendered</div>
706+
}
707+
708+
function App() {
709+
return (
710+
<ErrorBoundary
711+
fallbackRender={() => (
712+
<div>
713+
<div>error boundary</div>
714+
</div>
715+
)}
716+
>
717+
<React.Suspense fallback="Loading...">
718+
<Page />
719+
</React.Suspense>
720+
</ErrorBoundary>
721+
)
722+
}
723+
724+
const rendered = renderWithClient(queryClient, <App />)
725+
726+
await waitFor(() => rendered.getByText('Loading...'))
727+
await waitFor(() => rendered.getByText('rendered'))
728+
729+
consoleMock.mockRestore()
730+
})
731+
553732
it('should not call the queryFn when not enabled', async () => {
554733
const key = queryKey()
555734

0 commit comments

Comments
 (0)