|
| 1 | +import Vuex from 'vuex' |
| 2 | +import { firebaseMutations, firebaseAction } from '../src' |
| 3 | +import { db, tick, Vue, delayUpdate } from '@posva/vuefire-test-helpers' |
| 4 | + |
| 5 | +Vue.use(Vuex) |
| 6 | + |
| 7 | +describe('firebaseAction', () => { |
| 8 | + const store = new Vuex.Store({ |
| 9 | + mutations: firebaseMutations, |
| 10 | + actions: { |
| 11 | + action: firebaseAction((context, fn) => fn(context)) |
| 12 | + } |
| 13 | + }) |
| 14 | + |
| 15 | + const setItems = collection => |
| 16 | + store.dispatch('action', ({ bindFirebaseRef }) => |
| 17 | + bindFirebaseRef('items', collection) |
| 18 | + ) |
| 19 | + const setItem = document => |
| 20 | + store.dispatch('action', ({ bindFirebaseRef }) => |
| 21 | + bindFirebaseRef('item', document) |
| 22 | + ) |
| 23 | + |
| 24 | + let collection, document |
| 25 | + beforeEach(async () => { |
| 26 | + store.replaceState({ |
| 27 | + items: null, |
| 28 | + item: null |
| 29 | + }) |
| 30 | + collection = db.collection() |
| 31 | + document = db.collection().doc() |
| 32 | + await tick() |
| 33 | + }) |
| 34 | + |
| 35 | + it('binds a collection', async () => { |
| 36 | + expect(store.state.items).toBe(null) |
| 37 | + await setItems(collection) |
| 38 | + expect(store.state.items).toEqual([]) |
| 39 | + await collection.add({ text: 'foo' }) |
| 40 | + expect(store.state.items).toEqual([{ text: 'foo' }]) |
| 41 | + }) |
| 42 | + |
| 43 | + it('binds a document', async () => { |
| 44 | + expect(store.state.item).toBe(null) |
| 45 | + await setItem(document) |
| 46 | + expect(store.state.item).toEqual(null) |
| 47 | + await document.update({ text: 'foo' }) |
| 48 | + expect(store.state.item).toEqual({ text: 'foo' }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('removes items in collection', async () => { |
| 52 | + await setItems(collection) |
| 53 | + await collection.add({ text: 'foo' }) |
| 54 | + expect(store.state.items).toEqual([{ text: 'foo' }]) |
| 55 | + await collection.doc(store.state.items[0].id).delete() |
| 56 | + expect(store.state.items).toEqual([]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('unbinds previously bound refs', async () => { |
| 60 | + await setItem(document) |
| 61 | + expect(store.state.item).toEqual(null) |
| 62 | + const doc2 = db.collection().doc() |
| 63 | + await doc2.update({ bar: 'bar' }) |
| 64 | + await document.update({ foo: 'foo' }) |
| 65 | + expect(store.state.item).toEqual({ foo: 'foo' }) |
| 66 | + await setItem(doc2) |
| 67 | + expect(store.state.item).toEqual({ bar: 'bar' }) |
| 68 | + await document.update({ foo: 'baz' }) |
| 69 | + expect(store.state.item).toEqual({ bar: 'bar' }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('waits for all refs in document', async () => { |
| 73 | + const a = db.collection().doc() |
| 74 | + const b = db.collection().doc() |
| 75 | + delayUpdate(b) |
| 76 | + await document.update({ a, b }) |
| 77 | + |
| 78 | + await setItem(document) |
| 79 | + |
| 80 | + expect(store.state.item).toEqual({ |
| 81 | + a: null, |
| 82 | + b: null |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('waits for all refs in document with interrupting by new ref', async () => { |
| 87 | + const a = db.collection().doc() |
| 88 | + const b = db.collection().doc() |
| 89 | + const c = db.collection().doc() |
| 90 | + delayUpdate(b) |
| 91 | + await document.update({ a, b }) |
| 92 | + |
| 93 | + const promise = setItem(document) |
| 94 | + |
| 95 | + document.update({ c }) |
| 96 | + |
| 97 | + await promise |
| 98 | + |
| 99 | + expect(store.state.item).toEqual({ |
| 100 | + a: null, |
| 101 | + b: null, |
| 102 | + c: null |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('waits for nested refs with data in collections', async () => { |
| 107 | + const a = db.collection().doc() |
| 108 | + const b = db.collection().doc() |
| 109 | + const c = db.collection().doc() |
| 110 | + await a.update({ isA: true }) |
| 111 | + await c.update({ isC: true }) |
| 112 | + await b.update({ c }) |
| 113 | + delayUpdate(b) |
| 114 | + delayUpdate(c, 5) |
| 115 | + await collection.add({ a }) |
| 116 | + await collection.add({ b }) |
| 117 | + |
| 118 | + await setItems(collection) |
| 119 | + |
| 120 | + expect(store.state.items).toEqual([ |
| 121 | + { a: { isA: true }}, |
| 122 | + { b: { c: { isC: true }}} |
| 123 | + ]) |
| 124 | + }) |
| 125 | + |
| 126 | + it('can unbind a reference', async () => { |
| 127 | + await setItems(collection) |
| 128 | + await collection.add({ text: 'foo' }) |
| 129 | + await store.dispatch('action', ({ unbindFirebaseRef }) => |
| 130 | + unbindFirebaseRef('items') |
| 131 | + ) |
| 132 | + |
| 133 | + expect(store.state.items).toEqual([{ text: 'foo' }]) |
| 134 | + await collection.add({ text: 'foo' }) |
| 135 | + expect(store.state.items).toEqual([{ text: 'foo' }]) |
| 136 | + await setItems(collection) |
| 137 | + expect(store.state.items).toEqual([{ text: 'foo' }, { text: 'foo' }]) |
| 138 | + }) |
| 139 | +}) |
0 commit comments