Skip to content

Commit

Permalink
test(bind): add test to wait for data to be ready in collections
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
posva committed Dec 26, 2017
1 parent 8d5a91b commit 760d23c
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions test/refs-collections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,41 @@ beforeEach(async () => {
await tick()
})

// This makes sure some tests fail by delaying callbacks
function delayUpdate (ref, time = 0) {
const onSnapshot = ref.onSnapshot.bind(ref)
ref.onSnapshot = fn => onSnapshot(async (...args) => {
await delay(time)
console.log('I waited for', time)
fn(...args)
})
}

test('binds refs on collections', async () => {
await vm.$bind('items', collection)

// XXX dirty hack until $bind resolves when all refs are bound
await delay(5)

expect(vm.items).toEqual([
{ ref: { isA: true }},
{ ref: { isB: true }}
])
})

test('waits for array to be fully populated', async () => {
const c = db.collection().doc()
await c.update({ isC: true })
await collection.add({ ref: c })
// force callback delay
delayUpdate(c)
const data = await vm.$bind('items', collection)

expect(data).toEqual(vm.items)
expect(vm.items).toEqual([
{ ref: { isA: true }},
{ ref: { isB: true }},
{ ref: { isC: true }}
])
})

test('binds refs when adding to collection', async () => {
await vm.$bind('items', collection)
const c = db.collection().doc()
Expand Down

0 comments on commit 760d23c

Please sign in to comment.