Skip to content

Commit

Permalink
fix(refs): always rebind when updating documents
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 27, 2017
1 parent 39f646b commit ef9aeb2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
13 changes: 2 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@ function subscribeToRefs ({
const sub = subs[refKey]
const ref = refs[refKey]

if (sub) {
if (sub.path !== ref.path) {
sub.unbind()
} else {
// skip it as it's already bound
// NOTE this is valid as long as target is the same
// which is not checked anywhere but should be ok
// because the subs object is created when needed
return
}
}
// documents are fully replaced so it's necessary to bind things again
if (sub) sub.unbind()

// maybe wrap the unbind function to call unbind on every child
const [innerObj, innerKey] = deepGetSplit(target[key], refKey)
Expand Down
12 changes: 12 additions & 0 deletions test/refs-collections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,15 @@ test('unbinds refs when items are modified', async () => {

spyA.mockRestore()
})

test('updates when modifying an item', async () => {
await vm.$bind('items', collection)

await first.update({ newThing: true })
await delay(5)

expect(vm.items).toEqual([
{ ref: { isA: true }, newThing: true },
{ ref: { isB: true }}
])
})
39 changes: 22 additions & 17 deletions test/refs-documents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,6 @@ test('unbinds previously bound document when overwriting a bound', async () => {
spy.mockRestore()
})

test('does not rebind if it is the same ref', async () => {
const c = collection.doc()

const spy = spyOnSnapshot(c)
await c.update({ baz: 'baz' })
await d.update({ ref: c })
// NOTE see #1
await delay(5)
expect(spy).toHaveBeenCalledTimes(1)

await d.update({ ref: c })
await delay(5)

expect(spy).toHaveBeenCalledTimes(1)
spy.mockRestore()
})

test('resolves the promise when refs are resolved in a document', async () => {
await a.update({ a: true })
await b.update({ ref: a })
Expand Down Expand Up @@ -383,3 +366,25 @@ test.skip('binds refs on arrays', async () => {
]
})
})

test('properly updates a documen with refs', async () => {
const item = db.collection().doc()
const a = db.collection().doc()
await a.update({ isA: true })
await item.update({ a })
await vm.$bind('item', item)

expect(vm.item).toEqual({
a: { isA: true }
})

await item.update({ newThing: true })

await delay(5)

expect(vm.item).toEqual({
newThing: true,
a: { isA: true }
})

})

0 comments on commit ef9aeb2

Please sign in to comment.