File tree 2 files changed +25
-11
lines changed
2 files changed +25
-11
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ function bindCollection ({
4
4
vm,
5
5
key,
6
6
collection,
7
- resolve
7
+ resolve,
8
+ reject
8
9
} ) {
9
10
// TODO wait to get all data
10
11
const array = vm [ key ] = [ ]
@@ -34,16 +35,15 @@ function bindCollection ({
34
35
ready = true
35
36
resolve ( array )
36
37
}
37
- } , err => {
38
- console . log ( 'onSnapshot ERR' , err )
39
- } )
38
+ } , reject )
40
39
}
41
40
42
41
function bindDocument ( {
43
42
vm,
44
43
key,
45
44
document,
46
- resolve
45
+ resolve,
46
+ reject
47
47
} ) {
48
48
// TODO warning check if key exists?
49
49
// TODO create boundRefs object
@@ -68,29 +68,29 @@ function bindDocument ({
68
68
// console.log('ref snap', doc)
69
69
// }, err => console.log('onSnapshot ref ERR', err))
70
70
// }
71
- } , err => {
72
- console . log ( 'onSnapshot ERR' , err )
73
- } )
71
+ } , reject )
74
72
75
73
// TODO return a custom unbind function that unbind all refs
76
74
}
77
75
78
76
function bind ( { vm, key, ref } ) {
79
- return new Promise ( resolve => {
77
+ return new Promise ( ( resolve , reject ) => {
80
78
let unbind
81
79
if ( ref . where ) {
82
80
unbind = bindCollection ( {
83
81
vm,
84
82
key,
85
83
collection : ref ,
86
- resolve
84
+ resolve,
85
+ reject
87
86
} )
88
87
} else {
89
88
unbind = bindDocument ( {
90
89
vm,
91
90
key,
92
91
document : ref ,
93
- resolve
92
+ resolve,
93
+ reject
94
94
} )
95
95
}
96
96
vm . _firestoreUnbinds [ key ] = unbind
Original file line number Diff line number Diff line change 1
1
import test from 'ava'
2
+ import sinon from 'sinon'
2
3
import Vuefire from '../src'
3
4
import {
4
5
db ,
@@ -50,3 +51,16 @@ test('returs a promise', t => {
50
51
t . true ( vm . $bind ( 'items' , collection ) instanceof Promise )
51
52
t . true ( vm . $bind ( 'item' , document ) instanceof Promise )
52
53
} )
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
+ } )
You can’t perform that action at this time.
0 commit comments