Skip to content

Commit 760d23c

Browse files
committed
test(bind): add test to wait for data to be ready in collections
Previous test was succeeding because of mock being synchronous on updates but in real world scenarios, those are delayed. So I forced delay (1 tick with setTimeout(0)) to make the test always fail. (it was occasionnaly failing before)
1 parent 8d5a91b commit 760d23c

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

test/refs-collections.spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,41 @@ beforeEach(async () => {
2626
await tick()
2727
})
2828

29+
// This makes sure some tests fail by delaying callbacks
30+
function delayUpdate (ref, time = 0) {
31+
const onSnapshot = ref.onSnapshot.bind(ref)
32+
ref.onSnapshot = fn => onSnapshot(async (...args) => {
33+
await delay(time)
34+
console.log('I waited for', time)
35+
fn(...args)
36+
})
37+
}
38+
2939
test('binds refs on collections', async () => {
3040
await vm.$bind('items', collection)
3141

32-
// XXX dirty hack until $bind resolves when all refs are bound
33-
await delay(5)
34-
3542
expect(vm.items).toEqual([
3643
{ ref: { isA: true }},
3744
{ ref: { isB: true }}
3845
])
3946
})
4047

48+
test('waits for array to be fully populated', async () => {
49+
const c = db.collection().doc()
50+
await c.update({ isC: true })
51+
await collection.add({ ref: c })
52+
// force callback delay
53+
delayUpdate(c)
54+
const data = await vm.$bind('items', collection)
55+
56+
expect(data).toEqual(vm.items)
57+
expect(vm.items).toEqual([
58+
{ ref: { isA: true }},
59+
{ ref: { isB: true }},
60+
{ ref: { isC: true }}
61+
])
62+
})
63+
4164
test('binds refs when adding to collection', async () => {
4265
await vm.$bind('items', collection)
4366
const c = db.collection().doc()

0 commit comments

Comments
 (0)