mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Improve error boundaries tests
This commit is contained in:
@@ -207,10 +207,18 @@ describe('ReactTestRenderer', function() {
|
||||
});
|
||||
|
||||
it('supports error boundaries', function() {
|
||||
var log = [];
|
||||
class Angry extends React.Component {
|
||||
render() {
|
||||
log.push('Angry render');
|
||||
throw new Error('Please, do not render me.');
|
||||
}
|
||||
componentDidMount() {
|
||||
log.push('Angry componentDidMount');
|
||||
}
|
||||
componentWillUnmount() {
|
||||
log.push('Angry componentWillUnmount');
|
||||
}
|
||||
}
|
||||
|
||||
class Boundary extends React.Component {
|
||||
@@ -219,12 +227,21 @@ describe('ReactTestRenderer', function() {
|
||||
this.state = {error: false};
|
||||
}
|
||||
render() {
|
||||
log.push('Boundary render');
|
||||
if (!this.state.error) {
|
||||
return (<div><button onClick={this.onClick}>ClickMe</button><Angry /></div>);
|
||||
return (
|
||||
<div><button onClick={this.onClick}>ClickMe</button><Angry /></div>
|
||||
);
|
||||
} else {
|
||||
return (<div>Happy Birthday!</div>);
|
||||
return <div>Happy Birthday!</div>;
|
||||
}
|
||||
}
|
||||
componentDidMount() {
|
||||
log.push('Boundary componentDidMount');
|
||||
}
|
||||
componentWillUnmount() {
|
||||
log.push('Boundary componentWillUnmount');
|
||||
}
|
||||
onClick() {
|
||||
/* do nothing */
|
||||
}
|
||||
@@ -233,15 +250,18 @@ describe('ReactTestRenderer', function() {
|
||||
}
|
||||
}
|
||||
|
||||
var EventPluginHub = require('EventPluginHub');
|
||||
EventPluginHub.putListener = jest.fn();
|
||||
var renderer = ReactTestRenderer.create(<Boundary />);
|
||||
expect(renderer.toJSON()).toEqual({
|
||||
type: 'div',
|
||||
props: {},
|
||||
children: ['Happy Birthday!'],
|
||||
});
|
||||
expect(EventPluginHub.putListener).not.toBeCalled();
|
||||
expect(log).toEqual([
|
||||
'Boundary render',
|
||||
'Angry render',
|
||||
'Boundary render',
|
||||
'Boundary componentDidMount',
|
||||
]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user