Skip to content

Commit

Permalink
Fix an issue where nested components would not receive unmount lifecy…
Browse files Browse the repository at this point in the history
…cle hooks (fixes Issue #47)
  • Loading branch information
developit committed Feb 12, 2016
1 parent 8050f46 commit 2136889
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/vdom/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function createComponentFromVNode(vnode, dom, context) {
function unmountComponent(dom, component, remove) {
// console.warn('unmounting mismatched component', component);

hook(component, 'componentWillUnmount');
deepHook(component, 'componentWillUnmount');
if (remove!==false) {
if (dom._component===component) {
delete dom._component;
Expand All @@ -244,6 +244,6 @@ function unmountComponent(dom, component, remove) {
}
}
component._parentComponent = null;
hook(component, 'componentDidUnmount');
deepHook(component, 'componentDidUnmount');
collectComponent(component);
}
22 changes: 21 additions & 1 deletion test/browser/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,17 @@ describe('render()', () => {
});

it('should re-render nested components', () => {
let doRender = null;
let doRender = null,
alt = false;

class Outer extends Component {
componentDidMount() {
let i = 1;
doRender = () => this.setState({ i: ++i });
}
componentWillUnmount() {}
render(props, { i }) {
if (alt) return <div />;
return <Inner i={i} {...props} />;
}
}
Expand All @@ -418,15 +421,19 @@ describe('render()', () => {
this._constructor(...args);
}
_constructor() {}
componentWillMount() {}
componentDidMount() {}
componentWillUnmount() {}
componentDidUnmount() {}
render(props) {
return <div j={ ++j } {...props}>inner</div>;
}
}
sinon.spy(Inner.prototype, '_constructor');
sinon.spy(Inner.prototype, 'render');
sinon.spy(Inner.prototype, 'componentWillMount');
sinon.spy(Inner.prototype, 'componentDidMount');
sinon.spy(Inner.prototype, 'componentDidUnmount');
sinon.spy(Inner.prototype, 'componentWillUnmount');

render(<Outer foo="bar" />, scratch);
Expand All @@ -441,6 +448,8 @@ describe('render()', () => {

expect(Inner.prototype._constructor).to.have.been.calledOnce;
expect(Inner.prototype.componentWillUnmount).not.to.have.been.called;
expect(Inner.prototype.componentDidUnmount).not.to.have.been.called;
expect(Inner.prototype.componentWillMount).to.have.been.calledOnce;
expect(Inner.prototype.componentDidMount).to.have.been.calledOnce;
expect(Inner.prototype.render).to.have.been.calledTwice;

Expand All @@ -461,6 +470,8 @@ describe('render()', () => {
rerender();

expect(Inner.prototype.componentWillUnmount).not.to.have.been.called;
expect(Inner.prototype.componentDidUnmount).not.to.have.been.called;
expect(Inner.prototype.componentWillMount).to.have.been.calledOnce;
expect(Inner.prototype.componentDidMount).to.have.been.calledOnce;
expect(Inner.prototype.render).to.have.been.calledThrice;

Expand All @@ -475,5 +486,14 @@ describe('render()', () => {
}));

expect(scratch.innerHTML).to.equal('<div j="3" foo="bar" i="3">inner</div>');


// update & flush
alt = true;
doRender();
rerender();

expect(Inner.prototype.componentWillUnmount).to.have.been.calledOnce;
expect(Inner.prototype.componentDidUnmount).to.have.been.calledOnce;
});
});

0 comments on commit 2136889

Please sign in to comment.