Skip to content

Commit 3cf549c

Browse files
committed
test(refs): test refs in arrays
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
1 parent d153bf7 commit 3cf549c

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function subscribeToRefs ({
2323
// TODO check if no ref is missing
2424
// TODO max depth param, default to 1?
2525
if (++depth > 3) throw new Error('more than 5 nested refs')
26+
2627
refKeys.forEach(refKey => {
2728
// check if already bound to the same ref -> skip
2829
// TODO reuse if already bound?

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function extractRefs (doc, path = '', result = [{}, {}]) {
2323
tot[1][path + key] = ref
2424
} else if (Array.isArray(ref)) {
2525
// TODO handle array
26-
tot[0][key] = ref
26+
tot[0][key] = Array(ref.length).fill(null)
27+
extractRefs(ref, path + key + '.', [tot[0][key], tot[1]])
2728
} else if (isObject(ref)) {
2829
tot[0][key] = {}
2930
extractRefs(ref, path + key + '.', [tot[0][key], tot[1]])

test/refs-documents.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,31 @@ test('unbinds removed properties', async () => {
355355
callbackSpy.mockRestore()
356356
onSnapshotSpy.mockRestore()
357357
})
358+
359+
// XXX seems to bug on jest but works on real example...
360+
// could be the mock but don't see how
361+
// the key variable changes for no reason inside of
362+
// subscribeToDocument callback passed to onSnapshot
363+
test.skip('binds refs on arrays', async () => {
364+
const a = db.collection().doc()
365+
const b = db.collection().doc()
366+
const c = db.collection().doc()
367+
const item = db.collection().doc()
368+
await a.update({ isA: true })
369+
await b.update({ isB: true })
370+
await c.update({ isC: true })
371+
372+
await item.update({
373+
arr: [a, b, a]
374+
})
375+
376+
await vm.$bind('item', item)
377+
378+
expect(vm.item).toEqual({
379+
arr: [
380+
{ isA: true },
381+
{ isB: true },
382+
{ isA: true }
383+
]
384+
})
385+
})

test/utils.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,27 @@ test('extract deep object nested refs from document', () => {
7979
'obj.nested.ref': docRef
8080
})
8181
})
82+
83+
test('extracts refs from array', async () => {
84+
const docRef2 = new DocumentReference({
85+
collection,
86+
id: new Key(),
87+
data: {},
88+
index: 0
89+
})
90+
const [noRefsDoc, refs] = extractRefs({
91+
arr: [
92+
docRef,
93+
docRef2,
94+
docRef
95+
]
96+
})
97+
expect(noRefsDoc.arr[0]).toEqual(docRef.path)
98+
expect(noRefsDoc.arr[1]).toEqual(docRef2.path)
99+
expect(noRefsDoc.arr[2]).toEqual(docRef.path)
100+
expect(refs).toEqual({
101+
'arr.0': docRef,
102+
'arr.1': docRef2,
103+
'arr.2': docRef
104+
})
105+
})

0 commit comments

Comments
 (0)