This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from Bayer-Group/error-boundary
Added ErrorBoundary to Map and context wrapped components
- Loading branch information
Showing
17 changed files
with
290 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,79 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { Button, Container, FloatingBackground, Header, Message } from './styled' | ||
import MappyConcerned from './mappy_concerned.svg' | ||
import MappyDead from './mappy_dead.svg' | ||
|
||
/** | ||
* React ErrorBoundary component | ||
* @component | ||
*/ | ||
class ErrorBoundary extends React.Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.state = { | ||
abandonAllHope: false, | ||
attemptedReset: false, | ||
hasError: false | ||
} | ||
} | ||
|
||
componentDidCatch () { | ||
const { attemptedReset } = this.state | ||
|
||
if (attemptedReset) { | ||
// don't try to reset state after attemptedReset | ||
this.setState({ abandonAllHope: true }) | ||
} | ||
} | ||
|
||
componentDidUpdate (prevProps, prevState) { | ||
const { abandonAllHope, attemptedReset, hasError } = this.state | ||
|
||
// reset the attemptedReset bool whenever successful reset occurs so next error can also be reset instead of abandoned | ||
if (prevState.hasError && !prevState.attemptedReset && !abandonAllHope && attemptedReset && !hasError) { | ||
this.setState({ attemptedReset: false }) | ||
} | ||
} | ||
|
||
static getDerivedStateFromError (error) { // eslint-disable-line handle-callback-err | ||
return { hasError: true } | ||
} | ||
|
||
render () { | ||
const { abandonAllHope, hasError } = this.state | ||
const { floating } = this.props | ||
|
||
return hasError | ||
? ( | ||
<FloatingBackground floating={floating}> | ||
<Container floating={floating}> | ||
<Header>Something went wrong!</Header> | ||
{!abandonAllHope | ||
? <MappyConcerned /> | ||
: <MappyDead />} | ||
<Message>{!abandonAllHope | ||
? 'Have another go?' | ||
: 'This component is beyond recovery...'} | ||
</Message> | ||
{!abandonAllHope | ||
? <Button onClick={() => this.setState({ attemptedReset: true, hasError: false })}>Try Again</Button> | ||
: null} | ||
</Container> | ||
</FloatingBackground> | ||
) : this.props.children | ||
} | ||
} | ||
|
||
ErrorBoundary.propTypes = { | ||
/** pass components as children of ErrorBoundary which are rendered if there is no error */ | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.node | ||
]).isRequired, | ||
/** display error boundary with a modal-like background */ | ||
floating: PropTypes.bool | ||
} | ||
|
||
export default ErrorBoundary |
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,54 @@ | ||
import React from 'react' | ||
import { render, waitFor } from '@testing-library/react' | ||
import { Map } from 'Map' | ||
import { MultiMapManager, FlexMap, FullScreenFlex } from 'MultiMapManager' | ||
|
||
const Bomb = () => { | ||
throw new Error('Blew up during render') | ||
} | ||
|
||
describe('ErrorBoundary', () => { | ||
it('displays mappy on Map render errors', async () => { | ||
const { container, getByText } = render(<Map><Bomb /></Map>) | ||
|
||
// wait for async child render | ||
await waitFor(() => {}, { container }) | ||
|
||
expect(getByText('Something went wrong!')).toBeInTheDocument() | ||
}) | ||
|
||
it.skip('displays mappy on MultiMap render errors', async () => { | ||
const mapKeys = [ | ||
'map0', | ||
'map1', | ||
'map2', | ||
'map3' | ||
] | ||
|
||
const { container, getByText } = render( | ||
<MultiMapManager groups={[['map0', 'map1'], ['map2', 'map3']]}> | ||
<FullScreenFlex> | ||
{mapKeys.map((key, i, array) => { | ||
return ( | ||
<FlexMap | ||
key={key} | ||
index={i} | ||
total={array.length} | ||
numberOfRows={2} | ||
numberOfColumns={2}> | ||
<Map id={key} isMultiMap> | ||
{ i % 2 === 0 && <Bomb />} | ||
</Map> | ||
</FlexMap> | ||
) | ||
})} | ||
</FullScreenFlex> | ||
</MultiMapManager> | ||
) | ||
|
||
// wait for async child render | ||
await waitFor(() => {}, { container }) | ||
|
||
expect(getByText('Something went wrong!')).toBeInTheDocument() | ||
}) | ||
}) |
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 @@ | ||
export { default as ErrorBoundary } from './ErrorBoundary' |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,54 @@ | ||
import styled from 'styled-components' | ||
|
||
export const Button = styled.button` | ||
border: 1px solid #353535; | ||
border-radius: 2px; | ||
padding: 2px 6px; | ||
background: white; | ||
margin-top: 20px; | ||
font-size: 12px; | ||
color: #353535; | ||
` | ||
|
||
export const Container = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
color: #ca2525; | ||
text-align: center; | ||
${props => props.floating ? ` | ||
position: absolute; | ||
top: 0px; | ||
right: 0px; | ||
bottom: 0px; | ||
left: 0px; | ||
margin: auto; | ||
background: white; | ||
width: 250px; | ||
height: 250px; | ||
border-radius: 4px;` | ||
: null | ||
} | ||
` | ||
|
||
export const FloatingBackground = styled.div` | ||
${props => props.floating ? ` | ||
background: #000000ab; | ||
top: 0; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
position: absolute;` | ||
: null | ||
} | ||
` | ||
|
||
export const Header = styled.h1` | ||
font-size: 20px; | ||
padding-top: 20px; | ||
` | ||
|
||
export const Message = styled.em` | ||
display: block; | ||
color: #353535; | ||
font-size: 12px; | ||
` |
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
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
Oops, something went wrong.