|
| 1 | +import Vuefire from '../src' |
| 2 | +import { |
| 3 | + db, |
| 4 | + tick, |
| 5 | + delay, |
| 6 | + Vue |
| 7 | +} from './helpers' |
| 8 | + |
| 9 | +Vue.use(Vuefire) |
| 10 | + |
| 11 | +let vm, collection, a, b, c, d |
| 12 | +beforeEach(async () => { |
| 13 | + collection = db.collection() |
| 14 | + a = db.collection().doc() |
| 15 | + b = db.collection().doc() |
| 16 | + c = collection.doc() |
| 17 | + d = collection.doc() |
| 18 | + await c.update({ foo: 'foo' }) |
| 19 | + await d.update({ ref: c }) |
| 20 | + |
| 21 | + vm = new Vue({ |
| 22 | + render (h) { |
| 23 | + return h('ul', this.items && this.items.map( |
| 24 | + item => h('li', [item]) |
| 25 | + )) |
| 26 | + }, |
| 27 | + data: () => ({ |
| 28 | + a: null, |
| 29 | + b: null, |
| 30 | + c: null, |
| 31 | + d: null |
| 32 | + }), |
| 33 | + |
| 34 | + firestore: { |
| 35 | + a, |
| 36 | + b, |
| 37 | + c, |
| 38 | + d |
| 39 | + } |
| 40 | + }) |
| 41 | + await tick() |
| 42 | + // XXX dirty hack until $bind resolves when all refs are bound |
| 43 | + // NOTE should add option for it waitForRefs: true (by default) |
| 44 | + await delay(5) |
| 45 | +}) |
| 46 | + |
| 47 | +test('binds refs on documents', async () => { |
| 48 | + // create an empty doc and update using the ref instead of plain data |
| 49 | + const c = collection.doc() |
| 50 | + await c.update({ foo: 'foo' }) |
| 51 | + await a.update({ ref: c }) |
| 52 | + |
| 53 | + // XXX dirty hack until $bind resolves when all refs are bound |
| 54 | + // NOTE should add option for it waitForRefs: true (by default) |
| 55 | + await delay(5) |
| 56 | + |
| 57 | + expect(vm.a).toEqual({ |
| 58 | + ref: { foo: 'foo' } |
| 59 | + }) |
| 60 | +}) |
| 61 | + |
| 62 | +test('update inner ref', async () => { |
| 63 | + expect(vm.d).toEqual({ |
| 64 | + ref: { |
| 65 | + foo: 'foo' |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + await c.update({ foo: 'bar' }) |
| 70 | + |
| 71 | + expect(vm.d).toEqual({ |
| 72 | + ref: { |
| 73 | + foo: 'bar' |
| 74 | + } |
| 75 | + }) |
| 76 | +}) |
| 77 | + |
| 78 | +test('is null if ref does not exist', async () => { |
| 79 | + await d.update({ ref: a }) |
| 80 | + |
| 81 | + await delay(5) |
| 82 | + |
| 83 | + expect(vm.d).toEqual({ |
| 84 | + ref: null |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +test('unbinds previously bound document', async () => { |
| 89 | + const c = collection.doc() |
| 90 | + |
| 91 | + // Mock c onSnapshot to spy when the callback is called |
| 92 | + const spy = jest.fn() |
| 93 | + const onSnapshot = c.onSnapshot.bind(c) |
| 94 | + c.onSnapshot = jest.fn(fn => onSnapshot((...args) => { |
| 95 | + spy() |
| 96 | + fn(...args) |
| 97 | + }) |
| 98 | + ) |
| 99 | + await c.update({ baz: 'baz' }) |
| 100 | + await d.update({ ref: c }) |
| 101 | + await delay(5) |
| 102 | + expect(spy).toHaveBeenCalledTimes(1) |
| 103 | + await c.update({ baz: 'bar' }) |
| 104 | + await delay(5) |
| 105 | + // make sure things are updating correctly |
| 106 | + expect(vm.d).toEqual({ |
| 107 | + ref: { baz: 'bar' } |
| 108 | + }) |
| 109 | + // we call update twice to make sure our mock works |
| 110 | + expect(spy).toHaveBeenCalledTimes(2) |
| 111 | + await d.update({ ref: a }) |
| 112 | + await delay(5) |
| 113 | + |
| 114 | + expect(vm.d).toEqual({ |
| 115 | + ref: null |
| 116 | + }) |
| 117 | + await c.update({ foo: 'bar' }) |
| 118 | + await delay(5) |
| 119 | + |
| 120 | + expect(spy).toHaveBeenCalledTimes(2) |
| 121 | + expect(vm.d).toEqual({ |
| 122 | + ref: null |
| 123 | + }) |
| 124 | + spy.mockRestore() |
| 125 | +}) |
0 commit comments