Skip to content

Commit

Permalink
fix: FallbackComponent type (#62)
Browse files Browse the repository at this point in the history
* test: add failing tests

* chore: add react-is as dep

* fix: support all kind of component

Closes #61
To fix the issue I have used the library from facebook 'react-is'.
With this library we can check if the FallbackComponent is a real component.
A component is not alwasy a function, but can be also object

* chore: remove react-is dep

* fix: check FallbackComponent if is defined instead of using react-is

* test: simplify test after review
  • Loading branch information
marcosvega91 committed Jul 23, 2020
1 parent 7b7329d commit f75eda0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/__tests__/index.js
Expand Up @@ -348,3 +348,39 @@ test('supports automatic reset of error boundary when resetKeys change', () => {
expect(screen.queryByRole('alert')).not.toBeInTheDocument()
expect(console.error).not.toHaveBeenCalled()
})

test('should support not only function as FallbackComponent', () => {
const FancyFallback = React.forwardRef(({error}) => (
<div>
<p>Everything is broken. Try again</p>
<pre>{error.message}</pre>
</div>
))
FancyFallback.displayName = 'FancyFallback'

expect(() =>
render(
<ErrorBoundary FallbackComponent={FancyFallback}>
<Bomb />
</ErrorBoundary>,
),
).not.toThrow()

expect(
screen.getByText('Everything is broken. Try again'),
).toBeInTheDocument()

console.error.mockClear()
})

test('should throw error if FallbackComponent is not valid', () => {
expect(() =>
render(
<ErrorBoundary FallbackComponent={{}}>
<Bomb />
</ErrorBoundary>,
),
).toThrowError(/Element type is invalid/i)

console.error.mockClear()
})
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -39,7 +39,7 @@ class ErrorBoundary extends React.Component {
return fallback
} else if (typeof fallbackRender === 'function') {
return fallbackRender(props)
} else if (typeof FallbackComponent === 'function') {
} else if (FallbackComponent) {
return <FallbackComponent {...props} />
} else {
throw new Error(
Expand Down

0 comments on commit f75eda0

Please sign in to comment.