Skip to content

Commit

Permalink
feat(plugin-react): Add types for clearError() prop
Browse files Browse the repository at this point in the history
  • Loading branch information
bengourley committed May 14, 2020
1 parent c6a5d93 commit 1442522
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/plugin-react/src/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import Client from '@bugsnag/core/client'
const client = new Client({ apiKey: '123', plugins: [new BugsnagPluginReact(React)] }, undefined)
client._notify = jest.fn()

type FallbackComponentType = React.ComponentType<{
interface FallbackComponentProps {
error: Error
info: React.ErrorInfo
}>
clearError: () => void
}
type FallbackComponentType = React.ComponentType<FallbackComponentProps>

// eslint-disable-next-line
const ErrorBoundary = client.getPlugin('react')!.createErrorBoundary()
Expand All @@ -31,12 +33,6 @@ const BadComponent = () => {
// see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544
const GoodComponent = (): JSX.Element => 'test' as unknown as JSX.Element

const FallbackComponent = ({ clearError }: { clearError: () => void }) => {
return (
<button onClick={() => clearError()}>clearError</button>
)
}

const ComponentWithBadButton = () => {
const [clicked, setClicked] = useState(false)

Expand Down Expand Up @@ -90,6 +86,12 @@ it('passes the props to the FallbackComponent', () => {
})

it('resets the error boundary when the FallbackComponent calls the passed clearError prop', () => {
const FallbackComponent = ({ clearError }: FallbackComponentProps) => {
return (
<button onClick={() => clearError()}>clearError</button>
)
}

const component = create(<ErrorBoundary FallbackComponent={FallbackComponent}><ComponentWithBadButton /></ErrorBoundary>)
const instance = component.root

Expand All @@ -110,7 +112,9 @@ it('resets the error boundary when the FallbackComponent calls the passed clearE
})

it('a bad FallbackComponent implementation does not trigger stack overflow', () => {
const BadFallbackComponentImplementation = ({ clearError }: { clearError: () => void }) => {
const BadFallbackComponentImplementation = ({ error, info, clearError }: FallbackComponentProps) => {
function log (o: any) {}
log(error)
clearError()

return <div>fallback</div>
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react/types/bugsnag-plugin-react.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BugsnagErrorBoundary = React.ComponentType<{
FallbackComponent?: React.ComponentType<{
error: Error
info: React.ErrorInfo
clearError: () => void
}>
}>

Expand Down

0 comments on commit 1442522

Please sign in to comment.