Skip to content

Commit 7e8eb77

Browse files
committed
complete tests for bind as object
1 parent bec51c1 commit 7e8eb77

File tree

2 files changed

+188
-1
lines changed

2 files changed

+188
-1
lines changed

src/vuefire.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function isObject (val) {
1818
*/
1919
function createRecord (snapshot) {
2020
var value = snapshot.val()
21-
var res = value && typeof value === 'object'
21+
var res = isObject(value)
2222
? value
2323
: { '.value': value }
2424
res['.key'] = snapshot.key()

tests/vuefire.spec.js

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,5 +503,192 @@ describe('VueFire', function () {
503503
})
504504
})
505505
})
506+
507+
it('binds to a primitive', function (done) {
508+
var vm = new Vue({
509+
firebase: {
510+
items: {
511+
source: firebaseRef.child('items'),
512+
asObject: true
513+
}
514+
},
515+
template: '<div>{{ items | json }}</div>'
516+
}).$mount()
517+
firebaseRef.child('items').set('foo', function () {
518+
expect(vm.items).to.deep.equal({
519+
'.key': 'items',
520+
'.value': 'foo'
521+
})
522+
Vue.nextTick(function () {
523+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.items, null, 2))
524+
done()
525+
})
526+
})
527+
})
528+
529+
it('binds to Firebase reference with no data', function (done) {
530+
var vm = new Vue({
531+
firebase: {
532+
items: {
533+
source: firebaseRef.child('items'),
534+
asObject: true
535+
}
536+
},
537+
template: '<div>{{ items | json }}</div>'
538+
}).$mount()
539+
firebaseRef.child('items').set(null, function () {
540+
expect(vm.items).to.deep.equal({
541+
'.key': 'items',
542+
'.value': null
543+
})
544+
Vue.nextTick(function () {
545+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.items, null, 2))
546+
done()
547+
})
548+
})
549+
})
550+
551+
it('sets the key as null when bound to the root of the database', function (done) {
552+
var rootRef = firebaseRef.root()
553+
var vm = new Vue({
554+
firebase: {
555+
items: {
556+
source: rootRef,
557+
asObject: true
558+
}
559+
},
560+
template: '<div>{{ items | json }}</div>'
561+
}).$mount()
562+
rootRef.set('foo', function () {
563+
expect(vm.items).to.deep.equal({
564+
'.key': null,
565+
'.value': 'foo'
566+
})
567+
Vue.nextTick(function () {
568+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.items, null, 2))
569+
done()
570+
})
571+
})
572+
})
573+
574+
it('binds with limit queries', function (done) {
575+
var vm = new Vue({
576+
firebase: {
577+
items: {
578+
source: firebaseRef.child('items').limitToLast(2),
579+
asObject: true
580+
}
581+
},
582+
template: '<div>{{ items | json }}</div>'
583+
}).$mount()
584+
firebaseRef.child('items').set({
585+
first: { index: 0 },
586+
second: { index: 1 },
587+
third: { index: 2 }
588+
}, function () {
589+
expect(vm.items).to.deep.equal({
590+
'.key': 'items',
591+
second: { index: 1 },
592+
third: { index: 2 }
593+
})
594+
Vue.nextTick(function () {
595+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.items, null, 2))
596+
done()
597+
})
598+
})
599+
})
600+
601+
it('binds multiple Firebase references to state variables at the same time', function (done) {
602+
var vm = new Vue({
603+
firebase: {
604+
bindVar0: {
605+
source: firebaseRef.child('items0'),
606+
asObject: true
607+
},
608+
bindVar1: {
609+
source: firebaseRef.child('items1'),
610+
asObject: true
611+
}
612+
},
613+
template: '<div>{{ bindVar0 | json }} {{ bindVar1 | json }}</div>'
614+
}).$mount()
615+
616+
var items0 = {
617+
first: { index: 0 },
618+
second: { index: 1 },
619+
third: { index: 2 }
620+
}
621+
622+
var items1 = {
623+
bar: {
624+
foo: 'baz'
625+
},
626+
baz: true,
627+
foo: 100
628+
}
629+
630+
firebaseRef.set({
631+
items0: items0,
632+
items1: items1
633+
}, function () {
634+
items0['.key'] = 'items0'
635+
expect(vm.bindVar0).to.deep.equal(items0)
636+
items1['.key'] = 'items1'
637+
expect(vm.bindVar1).to.deep.equal(items1)
638+
Vue.nextTick(function () {
639+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.bindVar0, null, 2))
640+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.bindVar1, null, 2))
641+
done()
642+
})
643+
})
644+
})
645+
646+
it('binds a mixture of arrays and objects to state variables at the same time', function (done) {
647+
var vm = new Vue({
648+
firebase: {
649+
bindVar0: {
650+
source: firebaseRef.child('items0'),
651+
asObject: true
652+
},
653+
bindVar1: {
654+
source: firebaseRef.child('items1'),
655+
asObject: false
656+
}
657+
},
658+
template: '<div>{{ bindVar0 | json }} {{ bindVar1 | json }}</div>'
659+
}).$mount()
660+
661+
var items0 = {
662+
first: { index: 0 },
663+
second: { index: 1 },
664+
third: { index: 2 }
665+
}
666+
667+
var items1 = {
668+
bar: {
669+
foo: 'baz'
670+
},
671+
baz: true,
672+
foo: 100
673+
}
674+
675+
firebaseRef.set({
676+
items0: items0,
677+
items1: items1
678+
}, function () {
679+
items0['.key'] = 'items0'
680+
expect(vm.bindVar0).to.deep.equal(items0)
681+
expect(vm.bindVar1).to.deep.equal([
682+
{ '.key': 'bar', foo: 'baz' },
683+
{ '.key': 'baz', '.value': true },
684+
{ '.key': 'foo', '.value': 100 }
685+
])
686+
Vue.nextTick(function () {
687+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.bindVar0, null, 2))
688+
expect(vm.$el.textContent).to.contain(JSON.stringify(vm.bindVar1, null, 2))
689+
done()
690+
})
691+
})
692+
})
506693
})
507694
})

0 commit comments

Comments
 (0)