-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathoptions.spec.ts
75 lines (68 loc) · 2.24 KB
/
options.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { createStore } from 'vuex'
import { vuexfireMutations, firestoreAction } from '../src'
import { db, tick, Vue } from '@posva/vuefire-test-helpers'
import firebase from 'firebase/app'
import { FirestoreOptions } from '@posva/vuefire-core/dist/packages/@posva/vuefire-core/src'
describe('firestoreAction', () => {
const item: any = null,
items: any[] = []
const store = createStore({
state: { item, items },
mutations: vuexfireMutations,
actions: {
action: firestoreAction((context, fn) => fn(context)),
},
modules: {
module: {
namespaced: true,
actions: {
action: firestoreAction((context, fn) => fn(context)),
},
},
},
})
const setItems = (
collection: firebase.firestore.CollectionReference | firebase.firestore.Query,
options?: FirestoreOptions
) =>
// @ts-ignore
store.dispatch('action', ({ bindFirestoreRef }) =>
bindFirestoreRef('items', collection, options)
)
const setItem = (document: firebase.firestore.DocumentReference, options?: FirestoreOptions) =>
// @ts-ignore
store.dispatch('action', ({ bindFirestoreRef }) => bindFirestoreRef('item', document, options))
let collection: firebase.firestore.CollectionReference,
document: firebase.firestore.DocumentReference
beforeEach(async () => {
store.replaceState({
// @ts-ignore
items: null,
item: null,
module: {
items: [],
},
})
// @ts-ignore
collection = db.collection()
// @ts-ignore
document = db.collection().doc()
await tick()
})
it('calls serialize with a collection', async () => {
expect(store.state.items).toBe(null)
const serialize = jest.fn(() => ({ foo: 'bar' }))
await setItems(collection, { serialize })
await collection.add({ text: 'foo' })
expect(serialize).toHaveBeenCalledTimes(1)
expect(store.state.items).toEqual([{ foo: 'bar' }])
})
it('calls serialize with a document', async () => {
expect(store.state.item).toBe(null)
const serialize = jest.fn(() => ({ foo: 'bar' }))
await setItem(document, { serialize })
await document.set({ text: 'foo' })
expect(serialize).toHaveBeenCalledTimes(1)
expect(store.state.item).toEqual({ foo: 'bar' })
})
})