Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('wait + reset can be overriden with a function', async () => {
await collection.add({ foo: 'foo' })
await vm.$bind('items', collection)
// @ts-ignore
const col2: firestore.CollectionReference = db.collection()
await col2.add({ bar: 'bar' })
const p = vm.$bind('items', col2, { wait: true, reset: () => ['foo'] })
expect(vm.items).toEqual(['foo'])
await p
expect(vm.items).toEqual([{ bar: 'bar' }])
})
})
it('waits for nested refs in document', async () => {
const a = db.collection().doc()
// @ts-ignore
const b: firestore.DocumentReference = db.collection().doc()
// @ts-ignore
const c: firestore.DocumentReference = db.collection().doc()
await b.update({ c })
delayUpdate(b)
delayUpdate(c, 5)
await document.update({ a, b })
await vm.$bind('item', document)
expect(vm.item).toEqual({
a: null,
b: { c: null },
})
})
it('wait + reset can be overriden with a function', async () => {
// @ts-ignore
const col2: firestore.CollectionReference = db.collection()
await setItems(collection)
await collection.add({ text: 'foo' })
const p = setItems(col2, { wait: true, reset: () => ['foo'] })
expect(store.state.items).toEqual(['foo'])
await p
expect(store.state.items).toEqual([])
})
})
it('respects provided maxRefDepth', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
const d = db.collection().doc()
await a.set({ b })
await b.set({ c })
await d.set({ isD: true })
await c.set({ d })
const collection = db.collection()
await collection.add({ a })
// @ts-ignore
await bind('items', collection, { maxRefDepth: 1 })
expect(vm.items).toEqual([
{
a: {
b: b.path,
},
},
])
// @ts-ignore
await bind('items', collection, { maxRefDepth: 3 })
expect(vm.items).toEqual([
{
it('manually unbinds a document', async () => {
document = collection.doc()
await document.update({ foo: 'foo' })
const unbindSpy = spyUnbind(document)
let unbind: () => void = () => {
throw new Error('Promise was not called')
}
await new Promise((resolve, reject) => {
unbind = bindDocument({ vm, document, key: 'item', resolve, reject, ops })
})
expect(unbindSpy).not.toHaveBeenCalled()
expect(vm.item).toEqual({ foo: 'foo' })
unbind()
expect(unbindSpy).toHaveBeenCalled()
// reset data manually
vm.item = null
await document.update({ foo: 'foo' })
expect(vm.item).toEqual(null)
it('unbinds refs when the collection is unbound', async () => {
const items = db.collection()
const spyA = spyUnbind(a)
const spyB = spyUnbind(b)
await items.add({ ref: a })
await items.add({ ref: b })
// @ts-ignore
await bind('items', items)
expect(spyA).toHaveBeenCalledTimes(0)
expect(spyB).toHaveBeenCalledTimes(0)
unbind()
expect(spyA).toHaveBeenCalledTimes(1)
expect(spyB).toHaveBeenCalledTimes(1)
spyA.mockRestore()
spyB.mockRestore()
})
it('unbinds when a ref is replaced', async () => {
const aSpy = spyUnbind(a)
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)
await bind('d', d)
expect(vm.d).toEqual({
ref: {
isC: true,
},
})
await d.update({ ref: a })
// NOTE see #1
await delay(5)
expect(vm.d).toEqual({
ref: {
isA: true,
},
})
it('unbinds when a ref is replaced', async () => {
const aSpy = spyUnbind(a)
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)
await bind('d', d)
expect(vm.d).toEqual({
ref: {
isC: true,
},
})
await d.update({ ref: a })
// NOTE see #1
await delay(5)
expect(vm.d).toEqual({
ref: {
isA: true,
it('unbinds all refs when the document is unbound', async () => {
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)
// rebind to use the spies
await bind('d', d)
expect(vm.d).toEqual({
ref: {
isC: true,
},
})
unbind()
expect(dSpy).toHaveBeenCalledTimes(1)
expect(cSpy).toHaveBeenCalledTimes(1)
cSpy.mockRestore()
dSpy.mockRestore()
})
it('unbinds when a ref is replaced', async () => {
const aSpy = spyUnbind(a)
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)
await bind('d', d)
expect(vm.d).toEqual({
ref: {
isC: true,
},
})
await d.update({ ref: a })
// NOTE see #1
await delay(5)
expect(vm.d).toEqual({
ref: {
isA: true,
},