Skip to content

Commit

Permalink
Improve error boundaries tests
Browse files Browse the repository at this point in the history
  • Loading branch information
millermedeiros committed Aug 30, 2016
1 parent f7076b7 commit 0c031c5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@

var React;
var ReactDOM;
var ReactDOMServer;

describe('ReactErrorBoundaries', function() {

beforeEach(function() {
ReactDOM = require('ReactDOM');
ReactDOMServer = require('ReactDOMServer');
React = require('React');
});

Expand All @@ -37,9 +35,11 @@ describe('ReactErrorBoundaries', function() {
}
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>;
}
}
onClick() {
Expand All @@ -57,11 +57,19 @@ describe('ReactErrorBoundaries', function() {
expect(EventPluginHub.putListener).not.toBeCalled();
});

it('renders an error state (ssr)', function() {
it('renders an error state', 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 {
Expand All @@ -70,12 +78,21 @@ describe('ReactErrorBoundaries', 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 */
}
Expand All @@ -84,12 +101,15 @@ describe('ReactErrorBoundaries', function() {
}
}

var EventPluginHub = require('EventPluginHub');
var container = document.createElement('div');
EventPluginHub.putListener = jest.fn();
container.innerHTML = ReactDOMServer.renderToString(<Boundary />);
ReactDOM.render(<Boundary />, container);
expect(container.firstChild.innerHTML).toBe('Happy Birthday!');
expect(EventPluginHub.putListener).not.toBeCalled();
expect(log).toEqual([
'Boundary render',
'Angry render',
'Boundary render',
'Boundary componentDidMount',
]);
});

it('will catch exceptions in componentWillUnmount', function() {
Expand All @@ -98,14 +118,14 @@ describe('ReactErrorBoundaries', function() {
super();
this.state = {error: false};
}

render() {
if (!this.state.error) {
return <div>{this.props.children}</div>;
}
return <div>Error has been caught</div>;
}

unstable_handleError() {
this.setState({error: true});
}
Expand Down Expand Up @@ -139,27 +159,36 @@ describe('ReactErrorBoundaries', function() {
});

it('expect uneventful render to succeed', function() {
var log = [];
class Boundary extends React.Component {
constructor(props) {
super(props);
this.state = {error: false};
}
render() {
return (<div><button onClick={this.onClick}>ClickMe</button></div>);
log.push('Boundary render');
return <div><button onClick={this.onClick}>ClickMe</button></div>;
}
onClick() {
/* do nothing */
}
componentDidMount() {
log.push('Boundary componentDidMount');
}
componentWillUnmount() {
log.push('Boundary componentWillUnmount');
}
unstable_handleError() {
this.setState({error: true});
}
}

var EventPluginHub = require('EventPluginHub');
var container = document.createElement('div');
EventPluginHub.putListener = jest.fn();
ReactDOM.render(<Boundary />, container);
expect(EventPluginHub.putListener).toBeCalled();
expect(log).toEqual([
'Boundary render',
'Boundary componentDidMount',
]);
});

it('correctly handles composite siblings', function() {
Expand All @@ -168,14 +197,14 @@ describe('ReactErrorBoundaries', function() {
super();
this.state = {error: false};
}

render() {
if (!this.state.error) {
return <div>{this.props.children}</div>;
}
return <div>Error has been caught</div>;
}

unstable_handleError() {
this.setState({error: true});
}
Expand Down
30 changes: 25 additions & 5 deletions src/renderers/testing/__tests__/ReactTestRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 */
}
Expand All @@ -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',
]);
});

});

0 comments on commit 0c031c5

Please sign in to comment.