Skip to content

Commit 1d07e13

Browse files
committed
feat(bind): rejects the promise if it fails
1 parent 4c00b6e commit 1d07e13

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ function bindCollection ({
44
vm,
55
key,
66
collection,
7-
resolve
7+
resolve,
8+
reject
89
}) {
910
// TODO wait to get all data
1011
const array = vm[key] = []
@@ -34,16 +35,15 @@ function bindCollection ({
3435
ready = true
3536
resolve(array)
3637
}
37-
}, err => {
38-
console.log('onSnapshot ERR', err)
39-
})
38+
}, reject)
4039
}
4140

4241
function bindDocument ({
4342
vm,
4443
key,
4544
document,
46-
resolve
45+
resolve,
46+
reject
4747
}) {
4848
// TODO warning check if key exists?
4949
// TODO create boundRefs object
@@ -68,29 +68,29 @@ function bindDocument ({
6868
// console.log('ref snap', doc)
6969
// }, err => console.log('onSnapshot ref ERR', err))
7070
// }
71-
}, err => {
72-
console.log('onSnapshot ERR', err)
73-
})
71+
}, reject)
7472

7573
// TODO return a custom unbind function that unbind all refs
7674
}
7775

7876
function bind ({ vm, key, ref }) {
79-
return new Promise(resolve => {
77+
return new Promise((resolve, reject) => {
8078
let unbind
8179
if (ref.where) {
8280
unbind = bindCollection({
8381
vm,
8482
key,
8583
collection: ref,
86-
resolve
84+
resolve,
85+
reject
8786
})
8887
} else {
8988
unbind = bindDocument({
9089
vm,
9190
key,
9291
document: ref,
93-
resolve
92+
resolve,
93+
reject
9494
})
9595
}
9696
vm._firestoreUnbinds[key] = unbind

test/bind.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import test from 'ava'
2+
import sinon from 'sinon'
23
import Vuefire from '../src'
34
import {
45
db,
@@ -50,3 +51,16 @@ test('returs a promise', t => {
5051
t.true(vm.$bind('items', collection) instanceof Promise)
5152
t.true(vm.$bind('item', document) instanceof Promise)
5253
})
54+
55+
test('rejects the promise when errors', async t => {
56+
const { vm, document, collection } = t.context
57+
const fakeOnSnapshot = (_, fail) => {
58+
fail(new Error('nope'))
59+
}
60+
sinon.stub(document, 'onSnapshot').callsFake(fakeOnSnapshot)
61+
sinon.stub(collection, 'onSnapshot').callsFake(fakeOnSnapshot)
62+
await t.throws(vm.$bind('items', collection))
63+
await t.throws(vm.$bind('item', document))
64+
document.onSnapshot.restore()
65+
collection.onSnapshot.restore()
66+
})

0 commit comments

Comments
 (0)