Skip to content

Commit 2be3a26

Browse files
committed
test(vuexfire): add test suit based on vuefire
1 parent d4ee129 commit 2be3a26

File tree

3 files changed

+160
-9
lines changed

3 files changed

+160
-9
lines changed

.circleci/config.yml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
version: 2
66

77
defaults: &defaults
8-
working_directory: ~/project/vuefire
8+
working_directory: ~/project
99
docker:
1010
- image: circleci/node:10
1111

@@ -27,28 +27,35 @@ jobs:
2727
- persist_to_workspace:
2828
root: ~/project
2929
paths:
30-
- vuefire
30+
- ./
3131

3232
vuefire-core:
3333
<<: *defaults
3434
steps:
3535
- attach_workspace:
36-
at: ~/project/vuefire
37-
- run: cd vuefire/packages/@posva/vuefire-core && npm test
36+
at: ~/project
37+
- run: cd packages/@posva/vuefire-core && yarn test
3838

3939
vuefire:
4040
<<: *defaults
4141
steps:
4242
- attach_workspace:
43-
at: ~/project/vuefire
44-
- run: cd vuefire/packages/vuefire && npm test
43+
at: ~/project
44+
- run: cd packages/vuefire && yarn test
4545

4646
vuexfire:
4747
<<: *defaults
4848
steps:
4949
- attach_workspace:
50-
at: ~/project/vuefire
51-
- run: cd vuefire/packages/vuexfire && npm test
50+
at: ~/project
51+
- run: cd packages/vuexfire && yarn test
52+
53+
coverage:
54+
<<: *defaults
55+
steps:
56+
- attach_workspace:
57+
at: ~/project
58+
- run: yarn codecov
5259

5360
workflows:
5461
version: 2
@@ -64,3 +71,8 @@ workflows:
6471
- vuexfire:
6572
requires:
6673
- install
74+
- coverage:
75+
requires:
76+
- vuefire-core
77+
- vuefire
78+
- vuexfire

packages/vuexfire/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"scripts": {
1818
"lint": "eslint --color --ext=js,html src test examples",
1919
"pretest": "npm run lint",
20-
"test": "npm run build && npm run types",
20+
"test": "npm run build && npm run types && npm run test:unit",
2121
"test:unit": "jest",
2222
"dev": "npm run test:unit -- --watchAll",
2323
"types": "tsc -p ./types/test/tsconfig.json",

packages/vuexfire/test/index.spec.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)