Skip to content

Commit

Permalink
feat(bind): rejects the promise if it fails
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 24, 2017
1 parent 4c00b6e commit 1d07e13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ function bindCollection ({
vm,
key,
collection,
resolve
resolve,
reject
}) {
// TODO wait to get all data
const array = vm[key] = []
Expand Down Expand Up @@ -34,16 +35,15 @@ function bindCollection ({
ready = true
resolve(array)
}
}, err => {
console.log('onSnapshot ERR', err)
})
}, reject)
}

function bindDocument ({
vm,
key,
document,
resolve
resolve,
reject
}) {
// TODO warning check if key exists?
// TODO create boundRefs object
Expand All @@ -68,29 +68,29 @@ function bindDocument ({
// console.log('ref snap', doc)
// }, err => console.log('onSnapshot ref ERR', err))
// }
}, err => {
console.log('onSnapshot ERR', err)
})
}, reject)

// TODO return a custom unbind function that unbind all refs
}

function bind ({ vm, key, ref }) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
let unbind
if (ref.where) {
unbind = bindCollection({
vm,
key,
collection: ref,
resolve
resolve,
reject
})
} else {
unbind = bindDocument({
vm,
key,
document: ref,
resolve
resolve,
reject
})
}
vm._firestoreUnbinds[key] = unbind
Expand Down
14 changes: 14 additions & 0 deletions test/bind.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from 'ava'
import sinon from 'sinon'
import Vuefire from '../src'
import {
db,
Expand Down Expand Up @@ -50,3 +51,16 @@ test('returs a promise', t => {
t.true(vm.$bind('items', collection) instanceof Promise)
t.true(vm.$bind('item', document) instanceof Promise)
})

test('rejects the promise when errors', async t => {
const { vm, document, collection } = t.context
const fakeOnSnapshot = (_, fail) => {
fail(new Error('nope'))
}
sinon.stub(document, 'onSnapshot').callsFake(fakeOnSnapshot)
sinon.stub(collection, 'onSnapshot').callsFake(fakeOnSnapshot)
await t.throws(vm.$bind('items', collection))
await t.throws(vm.$bind('item', document))
document.onSnapshot.restore()
collection.onSnapshot.restore()
})

0 comments on commit 1d07e13

Please sign in to comment.