Skip to content

Commit

Permalink
Basic SSR support for error boundaries (#6694)
Browse files Browse the repository at this point in the history
(cherry picked from commit 96cb8c5)
  • Loading branch information
jimfb authored and zpao committed May 10, 2016
1 parent 743e4c6 commit 0244879
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/core/__tests__/ReactErrorBoundaries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

var React;
var ReactDOM;
var ReactDOMServer;

describe('ReactErrorBoundaries', function() {

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

Expand Down Expand Up @@ -55,6 +57,41 @@ describe('ReactErrorBoundaries', function() {
expect(EventPluginHub.putListener).not.toBeCalled();
});

it('renders an error state (ssr)', function() {
class Angry extends React.Component {
render() {
throw new Error('Please, do not render me.');
}
}

class Boundary extends React.Component {
constructor(props) {
super(props);
this.state = {error: false};
}
render() {
if (!this.state.error) {
return (<div><button onClick={this.onClick}>ClickMe</button><Angry /></div>);
} else {
return (<div>Happy Birthday!</div>);
}
}
onClick() {
/* do nothing */
}
unstable_handleError() {
this.setState({error: true});
}
}

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();
});

it('will catch exceptions in componentWillUnmount', function() {
class ErrorBoundary extends React.Component {
constructor() {
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/dom/server/ReactServerRenderingTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ var Mixin = {
*/
destructor: function() {
},

checkpoint: function() {
},

rollback: function() {
},
};


Expand Down

0 comments on commit 0244879

Please sign in to comment.