Skip to content

Commit

Permalink
feat(unbind): manually unbind a ref
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 17, 2017
1 parent 250a4f9 commit 7667fe5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ function install (Vue, options) {
}

Vue.prototype.$unbind = function (key) {
unbind({
vm: this,
key,
})
this._firestoreUnbinds[key]()
delete this._firestoreUnbinds[key]
delete this.$firestoreRefs[key]
}
}

Expand Down
48 changes: 48 additions & 0 deletions test/unbind.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from 'ava'
import sinon from 'sinon'
import Vuefire from '../src'
import {
createSnapshot
} from '../src/utils'
import {
db,
tick,
Vue
} from './helpers'

Vue.use(Vuefire)

test.beforeEach(async t => {
t.context.collection = db.collection()
t.context.document = t.context.collection.doc()
t.context.vm = new Vue({
render (h) {
return h('ul', this.items && this.items.map(
item => h('li', [item])
))
},
// purposely set items as null
// but it's a good practice to set it to an empty array
data: () => ({
items: null,
item: null,
}),
firestore: {
items: t.context.collection,
item: t.context.document
}
}).$mount()
await tick()
})

test('manually unbinds a collection', async t => {
const vm = t.context.vm
const spy = sinon.spy(vm._firestoreUnbinds, 'items')
vm.$unbind('items')
t.is(spy.callCount, 1)
t.deepEqual(Object.keys(vm._firestoreUnbinds), ['item'])
t.deepEqual(Object.keys(vm.$firestoreRefs), ['item'])
t.deepEqual(vm.items, [])
await t.context.collection.add({ text: 'foo' })
t.deepEqual(vm.items, [])
})

0 comments on commit 7667fe5

Please sign in to comment.