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 26, 2016
1 parent 38f74bc commit 106008e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,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 @@ -58,10 +60,18 @@ describe('ReactErrorBoundaries', function() {
});

it('renders an error state (ssr)', 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 +80,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 +103,15 @@ describe('ReactErrorBoundaries', function() {
}
}

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

it('will catch exceptions in componentWillUnmount', function() {
Expand All @@ -98,14 +120,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 +161,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 +199,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 106008e

Please sign in to comment.