forked from enzymejs/enzyme
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New]
shallow
/mount
: add simulateError
- Loading branch information
Showing
7 changed files
with
566 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# `.simulateError(error) => Self` | ||
|
||
Simulate a component throwing an error as part of its rendering lifecycle. | ||
|
||
This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method). | ||
|
||
|
||
#### Arguments | ||
|
||
1. `error` (`Any`): The error to throw. | ||
|
||
|
||
|
||
#### Returns | ||
|
||
`ReactWrapper`: Returns itself. | ||
|
||
|
||
|
||
#### Example | ||
|
||
```jsx | ||
function Something() { | ||
// this is just a placeholder | ||
return null; | ||
} | ||
|
||
class ErrorBoundary extends React.Component { | ||
componentDidCatch(error, info) { | ||
const { spy } = this.props; | ||
spy(error, info); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return ( | ||
<React.Fragment> | ||
{children} | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
ErrorBoundary.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
spy: PropTypes.func.isRequired, | ||
}; | ||
|
||
const spy = sinon.spy(); | ||
const wrapper = mount(<ErrorBoundary spy={spy}><Something /></ErrorBoundary>); | ||
const error = new Error('hi!'); | ||
wrapper.find(Something).simulateError(error); | ||
|
||
expect(spy).to.have.property('callCount', 1); | ||
expect(spy.args).to.deep.equal([ | ||
error, | ||
{ | ||
componentStack: ` | ||
in Something (created by ErrorBoundary) | ||
in ErrorBoundary (created by WrapperComponent) | ||
in WrapperComponent`, | ||
}, | ||
]); | ||
``` | ||
|
||
|
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,65 @@ | ||
# `.simulateError(error) => Self` | ||
|
||
Simulate a component throwing an error as part of its rendering lifecycle. | ||
|
||
This is particularly useful in combination with React 16 error boundaries (ie, the `componentDidCatch` lifecycle method). | ||
|
||
|
||
#### Arguments | ||
|
||
1. `error` (`Any`): The error to throw. | ||
|
||
|
||
|
||
#### Returns | ||
|
||
`ShallowWrapper`: Returns itself. | ||
|
||
|
||
|
||
#### Example | ||
|
||
```jsx | ||
function Something() { | ||
// this is just a placeholder | ||
return null; | ||
} | ||
|
||
class ErrorBoundary extends React.Component { | ||
componentDidCatch(error, info) { | ||
const { spy } = this.props; | ||
spy(error, info); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return ( | ||
<React.Fragment> | ||
{children} | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
ErrorBoundary.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
spy: PropTypes.func.isRequired, | ||
}; | ||
|
||
const spy = sinon.spy(); | ||
const wrapper = shallow(<ErrorBoundary spy={spy}><Something /></ErrorBoundary>); | ||
const error = new Error('hi!'); | ||
wrapper.find(Something).simulateError(error); | ||
|
||
expect(spy).to.have.property('callCount', 1); | ||
expect(spy.args).to.deep.equal([ | ||
error, | ||
{ | ||
componentStack: ` | ||
in Something (created by ErrorBoundary) | ||
in ErrorBoundary (created by WrapperComponent) | ||
in WrapperComponent`, | ||
}, | ||
]); | ||
``` | ||
|
||
|
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.