From c251112d5bbb00f06c12dfe7ef366e99c84c52e3 Mon Sep 17 00:00:00 2001 From: jwngr Date: Tue, 29 Sep 2015 10:56:34 -0700 Subject: [PATCH] Fix bug with unbinding when component is unmounted --- changelog.txt | 1 + src/reactfire.js | 4 ++-- tests/reactfire.spec.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index e69de29b..9e9b596b 100644 --- a/changelog.txt +++ b/changelog.txt @@ -0,0 +1 @@ +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 f7b1bbb2..cbe7ba95 100644 --- a/tests/reactfire.spec.js +++ b/tests/reactfire.spec.js @@ -1105,5 +1105,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)); + }); }); });