-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useErrorHandler): add new hook for handling errors (#59)
- Loading branch information
1 parent
ded843a
commit 3c93397
Showing
5 changed files
with
236 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React from 'react' | ||
import userEvent from '@testing-library/user-event' | ||
import {render, screen} from '@testing-library/react' | ||
import {ErrorBoundary, useErrorHandler} from '..' | ||
|
||
function ErrorFallback({error, componentStack, resetErrorBoundary}) { | ||
return ( | ||
<div role="alert"> | ||
<p>Something went wrong:</p> | ||
<pre>{error.message}</pre> | ||
<pre>{componentStack}</pre> | ||
<button onClick={resetErrorBoundary}>Try again</button> | ||
</div> | ||
) | ||
} | ||
|
||
const firstLine = str => str.split('\n')[0] | ||
|
||
test('handleError forwards along async errors', async () => { | ||
function AsyncBomb() { | ||
const [explode, setExplode] = React.useState(false) | ||
const handleError = useErrorHandler() | ||
React.useEffect(() => { | ||
if (explode) { | ||
setTimeout(() => { | ||
handleError(new Error('💥 CABOOM 💥')) | ||
}) | ||
} | ||
}) | ||
return <button onClick={() => setExplode(true)}>bomb</button> | ||
} | ||
render( | ||
<ErrorBoundary FallbackComponent={ErrorFallback}> | ||
<AsyncBomb /> | ||
</ErrorBoundary>, | ||
) | ||
|
||
userEvent.click(screen.getByRole('button', {name: /bomb/i})) | ||
|
||
await screen.findByRole('alert') | ||
|
||
const [[actualError], [componentStack]] = console.error.mock.calls | ||
const firstLineOfError = firstLine(actualError) | ||
expect(firstLineOfError).toMatchInlineSnapshot( | ||
`"Error: Uncaught [Error: 💥 CABOOM 💥]"`, | ||
) | ||
expect(componentStack).toMatchInlineSnapshot(` | ||
"The above error occurred in one of your React components: | ||
in Unknown | ||
in ErrorBoundary | ||
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary." | ||
`) | ||
expect(console.error).toHaveBeenCalledTimes(2) | ||
console.error.mockClear() | ||
|
||
// can recover | ||
userEvent.click(screen.getByRole('button', {name: /try again/i})) | ||
expect(console.error).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('can pass an error to useErrorHandler', async () => { | ||
function AsyncBomb() { | ||
const [error, setError] = React.useState(null) | ||
const [explode, setExplode] = React.useState(false) | ||
useErrorHandler(error) | ||
React.useEffect(() => { | ||
if (explode) { | ||
setTimeout(() => { | ||
setError(new Error('💥 CABOOM 💥')) | ||
}) | ||
} | ||
}) | ||
return <button onClick={() => setExplode(true)}>bomb</button> | ||
} | ||
render( | ||
<ErrorBoundary FallbackComponent={ErrorFallback}> | ||
<AsyncBomb /> | ||
</ErrorBoundary>, | ||
) | ||
|
||
userEvent.click(screen.getByRole('button', {name: /bomb/i})) | ||
|
||
await screen.findByRole('alert') | ||
const [[actualError], [componentStack]] = console.error.mock.calls | ||
const firstLineOfError = firstLine(actualError) | ||
expect(firstLineOfError).toMatchInlineSnapshot( | ||
`"Error: Uncaught [Error: 💥 CABOOM 💥]"`, | ||
) | ||
expect(componentStack).toMatchInlineSnapshot(` | ||
"The above error occurred in one of your React components: | ||
in Unknown | ||
in ErrorBoundary | ||
React will try to recreate this component tree from scratch using the error boundary you provided, ErrorBoundary." | ||
`) | ||
expect(console.error).toHaveBeenCalledTimes(2) | ||
console.error.mockClear() | ||
|
||
// can recover | ||
userEvent.click(screen.getByRole('button', {name: /try again/i})) | ||
expect(console.error).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters