Skip to content

Commit 9164705

Browse files
committed
feat(refs): set nested ref to null if non-existant
1 parent f97f93f commit 9164705

File tree

3 files changed

+127
-16
lines changed

3 files changed

+127
-16
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ function subscribeToDocument ({ ref, obj, key, depth }) {
7474
return ref.onSnapshot(doc => {
7575
if (doc.exists) {
7676
updateDataFromDocumentSnapshot({ snapshot: createSnapshot(doc), obj, key, subs, depth })
77+
} else {
78+
obj[key] = null
7779
}
7880
})
7981
}

test/bind.spec.js

-16
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,3 @@ test('unbinds previously bound refs', async () => {
7474
expect(vm.$firestoreRefs.item).toBe(doc2)
7575
expect(vm.item).toEqual({ bar: 'bar' })
7676
})
77-
78-
test('binds refs on documents', async () => {
79-
// create an empty doc and update using the ref instead of plain data
80-
const a = collection.doc()
81-
a.update({ foo: 'foo' })
82-
await document.update({ ref: a })
83-
await vm.$bind('item', document)
84-
85-
// XXX dirty hack until $bind resolves when all refs are bound
86-
// NOTE should add option for it waitForRefs: true (by default)
87-
await delay(5)
88-
89-
expect(vm.item).toEqual({
90-
ref: { foo: 'foo' }
91-
})
92-
})

test/refs.spec.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import Vuefire from '../src'
2+
import {
3+
db,
4+
tick,
5+
delay,
6+
Vue
7+
} from './helpers'
8+
9+
Vue.use(Vuefire)
10+
11+
let vm, collection, a, b, c, d
12+
beforeEach(async () => {
13+
collection = db.collection()
14+
a = db.collection().doc()
15+
b = db.collection().doc()
16+
c = collection.doc()
17+
d = collection.doc()
18+
await c.update({ foo: 'foo' })
19+
await d.update({ ref: c })
20+
21+
vm = new Vue({
22+
render (h) {
23+
return h('ul', this.items && this.items.map(
24+
item => h('li', [item])
25+
))
26+
},
27+
data: () => ({
28+
a: null,
29+
b: null,
30+
c: null,
31+
d: null
32+
}),
33+
34+
firestore: {
35+
a,
36+
b,
37+
c,
38+
d
39+
}
40+
})
41+
await tick()
42+
// XXX dirty hack until $bind resolves when all refs are bound
43+
// NOTE should add option for it waitForRefs: true (by default)
44+
await delay(5)
45+
})
46+
47+
test('binds refs on documents', async () => {
48+
// create an empty doc and update using the ref instead of plain data
49+
const c = collection.doc()
50+
await c.update({ foo: 'foo' })
51+
await a.update({ ref: c })
52+
53+
// XXX dirty hack until $bind resolves when all refs are bound
54+
// NOTE should add option for it waitForRefs: true (by default)
55+
await delay(5)
56+
57+
expect(vm.a).toEqual({
58+
ref: { foo: 'foo' }
59+
})
60+
})
61+
62+
test('update inner ref', async () => {
63+
expect(vm.d).toEqual({
64+
ref: {
65+
foo: 'foo'
66+
}
67+
})
68+
69+
await c.update({ foo: 'bar' })
70+
71+
expect(vm.d).toEqual({
72+
ref: {
73+
foo: 'bar'
74+
}
75+
})
76+
})
77+
78+
test('is null if ref does not exist', async () => {
79+
await d.update({ ref: a })
80+
81+
await delay(5)
82+
83+
expect(vm.d).toEqual({
84+
ref: null
85+
})
86+
})
87+
88+
test('unbinds previously bound document', async () => {
89+
const c = collection.doc()
90+
91+
// Mock c onSnapshot to spy when the callback is called
92+
const spy = jest.fn()
93+
const onSnapshot = c.onSnapshot.bind(c)
94+
c.onSnapshot = jest.fn(fn => onSnapshot((...args) => {
95+
spy()
96+
fn(...args)
97+
})
98+
)
99+
await c.update({ baz: 'baz' })
100+
await d.update({ ref: c })
101+
await delay(5)
102+
expect(spy).toHaveBeenCalledTimes(1)
103+
await c.update({ baz: 'bar' })
104+
await delay(5)
105+
// make sure things are updating correctly
106+
expect(vm.d).toEqual({
107+
ref: { baz: 'bar' }
108+
})
109+
// we call update twice to make sure our mock works
110+
expect(spy).toHaveBeenCalledTimes(2)
111+
await d.update({ ref: a })
112+
await delay(5)
113+
114+
expect(vm.d).toEqual({
115+
ref: null
116+
})
117+
await c.update({ foo: 'bar' })
118+
await delay(5)
119+
120+
expect(spy).toHaveBeenCalledTimes(2)
121+
expect(vm.d).toEqual({
122+
ref: null
123+
})
124+
spy.mockRestore()
125+
})

0 commit comments

Comments
 (0)