diff --git a/changelog.txt b/changelog.txt index a4e86be5..99b00d18 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1 +1,2 @@ feature - Upgraded Firebase dependency to 2.x.x. +fixed - Fixed bug which caused `unbind()` to be called on a previously unbound reference when a component was unmounted. diff --git a/src/reactfire.js b/src/reactfire.js index a50839f7..2db52ae1 100644 --- a/src/reactfire.js +++ b/src/reactfire.js @@ -351,8 +351,8 @@ this.firebaseRefs[bindVar].off(event, offListener); } } - this.firebaseRefs[bindVar] = undefined; - this.firebaseListeners[bindVar] = undefined; + delete this.firebaseRefs[bindVar]; + delete this.firebaseListeners[bindVar]; // Update state var newState = {}; diff --git a/tests/reactfire.spec.js b/tests/reactfire.spec.js index d4ca1979..9445f959 100644 --- a/tests/reactfire.spec.js +++ b/tests/reactfire.spec.js @@ -1104,5 +1104,40 @@ describe('ReactFire', function() { shallowRenderer.render(React.createElement(TestComponent)); }); + + it('handles already unbound state when the component unmounts', function(done) { + var TestComponent = React.createClass({ + mixins: [ReactFireMixin], + + componentWillMount: function() { + sinon.spy(this, 'unbind'); + + this.bindAsArray(firebaseRef, 'items0'); + this.bindAsObject(firebaseRef, 'items1'); + + firebaseRef.set({ + first: { index: 0 }, + second: { index: 1 }, + third: { index: 2 } + }, function() { + this.unbind('items0'); + + shallowRenderer.unmount(); + + expect(this.unbind).to.have.been.calledTwice; + expect(this.unbind.args[0][0]).to.equal('items0'); + expect(this.unbind.args[1][0]).to.equal('items1'); + + done(); + }.bind(this)); + }, + + render: function() { + return React.DOM.div(null); + } + }); + + shallowRenderer.render(React.createElement(TestComponent)); + }); }); });