Skip to content

Commit f46d51b

Browse files
committed
complete test coverage
1 parent f10a8e7 commit f46d51b

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/vuefire.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function indexForKey (array, key) {
3838
return i
3939
}
4040
}
41+
/* istanbul ignore next */
4142
return -1
4243
}
4344

@@ -157,13 +158,24 @@ function unbind (vm, key) {
157158
vm._firebaseListeners[key] = null
158159
}
159160

161+
/**
162+
* Ensure the related bookkeeping variables on an instance.
163+
*
164+
* @param {Vue} vm
165+
*/
166+
function ensureRefs (vm) {
167+
if (!vm.$firebaseRefs) {
168+
vm.$firebaseRefs = Object.create(null)
169+
vm._firebaseSources = Object.create(null)
170+
vm._firebaseListeners = Object.create(null)
171+
}
172+
}
173+
160174
var VueFireMixin = {
161175
init: function () {
162176
var bindings = this.$options.firebase
163177
if (!bindings) return
164-
this.$firebaseRefs = Object.create(null)
165-
this._firebaseSources = Object.create(null)
166-
this._firebaseListeners = Object.create(null)
178+
ensureRefs(this)
167179
for (var key in bindings) {
168180
bind(this, key, bindings[key])
169181
}
@@ -196,6 +208,7 @@ function install (_Vue) {
196208

197209
// extend instance methods
198210
Vue.prototype.$bindAsObject = function (key, source, cancelCallback) {
211+
ensureRefs(this)
199212
bind(this, key, {
200213
source: source,
201214
asObject: true,
@@ -204,6 +217,7 @@ function install (_Vue) {
204217
}
205218

206219
Vue.prototype.$bindAsArray = function (key, source, cancelCallback) {
220+
ensureRefs(this)
207221
bind(this, key, {
208222
source: source,
209223
cancelCallback: cancelCallback
@@ -216,6 +230,7 @@ function install (_Vue) {
216230
}
217231

218232
// auto install
233+
/* istanbul ignore if */
219234
if (typeof window !== 'undefined' && window.Vue) {
220235
install(window.Vue)
221236
}

tests/vuefire.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ describe('VueFire', function () {
5959
})
6060
})
6161

62+
it('bind using $bindAsArray', function (done) {
63+
var vm = new Vue({
64+
template: '<div><div v-for="item in items">{{ item[".key"] }} {{ item.index }} </div></div>',
65+
created: function () {
66+
this.$bindAsArray('items', firebaseRef)
67+
}
68+
}).$mount()
69+
firebaseRef.set({
70+
first: { index: 0 },
71+
second: { index: 1 },
72+
third: { index: 2 }
73+
}, function () {
74+
expect(vm.items).to.deep.equal([
75+
{ '.key': 'first', index: 0 },
76+
{ '.key': 'second', index: 1 },
77+
{ '.key': 'third', index: 2 }
78+
])
79+
Vue.nextTick(function () {
80+
expect(vm.$el.textContent).to.contain('first 0 second 1 third 2')
81+
done()
82+
})
83+
})
84+
})
85+
6286
it('binds array records which are primitives', function (done) {
6387
var vm = new Vue({
6488
firebase: {
@@ -504,6 +528,28 @@ describe('VueFire', function () {
504528
})
505529
})
506530

531+
it('binds with $bindAsObject', function (done) {
532+
var obj = {
533+
first: { index: 0 },
534+
second: { index: 1 },
535+
third: { index: 2 }
536+
}
537+
var vm = new Vue({
538+
template: '<div>{{ items | json }}</div>',
539+
created: function () {
540+
this.$bindAsObject('items', firebaseRef.child('items'))
541+
}
542+
}).$mount()
543+
firebaseRef.child('items').set(obj, function () {
544+
obj['.key'] = 'items'
545+
expect(vm.items).to.deep.equal(obj)
546+
Vue.nextTick(function () {
547+
expect(vm.$el.textContent).to.contain(JSON.stringify(obj, null, 2))
548+
done()
549+
})
550+
})
551+
})
552+
507553
it('binds to a primitive', function (done) {
508554
var vm = new Vue({
509555
firebase: {
@@ -700,6 +746,13 @@ describe('VueFire', function () {
700746
}).to.throw(/not bound to a Firebase reference/)
701747
})
702748

749+
it('should work properly for instances with no firebase bindings', function () {
750+
expect(function () {
751+
var vm = new Vue()
752+
vm.$destroy()
753+
}).not.to.throw()
754+
})
755+
703756
it('unbinds the state bound to Firebase as an array', function (done) {
704757
var vm = new Vue({
705758
firebase: {

0 commit comments

Comments
 (0)