Skip to content

Commit

Permalink
Invoke componentWillMount() for stateful components just before `re…
Browse files Browse the repository at this point in the history
…nder()`. Fixes #6. Thanks @mikestead!
  • Loading branch information
developit committed Jul 29, 2016
1 parent d913657 commit ab24bd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export default function renderToString(vnode, context, opts, inner) {
let c = new nodeName(props, context);
c.props = props;
c.context = context;
if (c.componentWillMount) c.componentWillMount();
rendered = c.render(c.props, c.state, c.context);

if (c.getChildContext) {
Expand Down
17 changes: 17 additions & 0 deletions server/test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ describe('render', () => {
expect(render(<Test bar="b" />), 'partial').to.equal('<div foo="default foo" bar="b"></div>');
expect(render(<Test foo="a" bar="b" />), 'overridden').to.equal('<div foo="a" bar="b"></div>');
});

it('should invoke componentWillMount', () => {
class Test extends Component {
componentWillMount() {}
render(props) {
return <div {...props} />;
}
}
spy(Test.prototype, 'componentWillMount');
spy(Test.prototype, 'render');

render(<Test />);

expect(Test.prototype.componentWillMount)
.to.have.been.calledOnce
.and.to.have.been.calledBefore(Test.prototype.render);
});
});

describe('High-order components', () => {
Expand Down

0 comments on commit ab24bd3

Please sign in to comment.