Skip to content

Commit

Permalink
test(refs): test refs in arrays
Browse files Browse the repository at this point in the history
The test seems to fail right now while it shouldn't. Testing with the example works fine so I
suspect there's something really weird going on
  • Loading branch information
posva committed Dec 27, 2017
1 parent d153bf7 commit 3cf549c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function subscribeToRefs ({
// TODO check if no ref is missing
// TODO max depth param, default to 1?
if (++depth > 3) throw new Error('more than 5 nested refs')

refKeys.forEach(refKey => {
// check if already bound to the same ref -> skip
// TODO reuse if already bound?
Expand Down
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function extractRefs (doc, path = '', result = [{}, {}]) {
tot[1][path + key] = ref
} else if (Array.isArray(ref)) {
// TODO handle array
tot[0][key] = ref
tot[0][key] = Array(ref.length).fill(null)
extractRefs(ref, path + key + '.', [tot[0][key], tot[1]])
} else if (isObject(ref)) {
tot[0][key] = {}
extractRefs(ref, path + key + '.', [tot[0][key], tot[1]])
Expand Down
28 changes: 28 additions & 0 deletions test/refs-documents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,31 @@ test('unbinds removed properties', async () => {
callbackSpy.mockRestore()
onSnapshotSpy.mockRestore()
})

// XXX seems to bug on jest but works on real example...
// could be the mock but don't see how
// the key variable changes for no reason inside of
// subscribeToDocument callback passed to onSnapshot
test.skip('binds refs on arrays', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
const item = db.collection().doc()
await a.update({ isA: true })
await b.update({ isB: true })
await c.update({ isC: true })

await item.update({
arr: [a, b, a]
})

await vm.$bind('item', item)

expect(vm.item).toEqual({
arr: [
{ isA: true },
{ isB: true },
{ isA: true }
]
})
})
24 changes: 24 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ test('extract deep object nested refs from document', () => {
'obj.nested.ref': docRef
})
})

test('extracts refs from array', async () => {
const docRef2 = new DocumentReference({
collection,
id: new Key(),
data: {},
index: 0
})
const [noRefsDoc, refs] = extractRefs({
arr: [
docRef,
docRef2,
docRef
]
})
expect(noRefsDoc.arr[0]).toEqual(docRef.path)
expect(noRefsDoc.arr[1]).toEqual(docRef2.path)
expect(noRefsDoc.arr[2]).toEqual(docRef.path)
expect(refs).toEqual({
'arr.0': docRef,
'arr.1': docRef2,
'arr.2': docRef
})
})

0 comments on commit 3cf549c

Please sign in to comment.