Skip to content

Commit 83ce89e

Browse files
committed
refactor(vuexfire): migrate to ts
1 parent 6dd08a8 commit 83ce89e

24 files changed

+447
-765
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@typescript-eslint/eslint-plugin": "^1.12.0",
1616
"@typescript-eslint/parser": "^1.12.0",
1717
"conventional-changelog-cli": "^2.0.21",
18+
"codecov": "^3.5.0",
1819
"cz-conventional-changelog": "^2.1.0",
1920
"eslint": "^6.0.1",
2021
"eslint-config-prettier": "^6.0.0",

packages/@posva/vuefire-core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
"url": "https://github.com/vuejs/vuefire/issues"
4040
},
4141
"devDependencies": {
42-
"@babel/core": "^7.4.5",
43-
"@babel/preset-env": "^7.4.5",
4442
"@posva/vuefire-test-helpers": "^1.1.0",
4543
"@types/jest": "^24.0.15",
4644
"firebase": "^6.3.0",

packages/vuefire/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"files": [
1212
"dist",
1313
"src",
14-
"types/*.d.ts",
1514
"LICENSE"
1615
],
1716
"scripts": {

packages/vuexfire/test/index.spec.js renamed to packages/vuexfire/__tests__/firestore.spec.ts

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
import Vuex from 'vuex'
22
import { vuexfireMutations, firestoreAction } from '../src'
33
import { db, tick, Vue, delayUpdate } from '@posva/vuefire-test-helpers'
4+
import { firestore } from 'firebase'
45

56
Vue.use(Vuex)
67

78
describe('firestoreAction', () => {
8-
const store = new Vuex.Store({
9+
const item: any = null,
10+
items: any[] = []
11+
const store = new Vuex.Store<{ item: any; items: any[] }>({
12+
state: { item, items },
913
mutations: vuexfireMutations,
1014
actions: {
11-
action: firestoreAction((context, fn) => fn(context))
15+
action: firestoreAction((context, fn) => fn(context)),
1216
},
1317

1418
modules: {
1519
module: {
1620
namespaced: true,
1721
actions: {
18-
action: firestoreAction((context, fn) => fn(context))
19-
}
20-
}
21-
}
22+
action: firestoreAction((context, fn) => fn(context)),
23+
},
24+
},
25+
},
2226
})
2327

24-
const setItems = collection =>
25-
store.dispatch('action', ({ bindFirestoreRef }) =>
26-
bindFirestoreRef('items', collection)
27-
)
28-
const setItem = document =>
29-
store.dispatch('action', ({ bindFirestoreRef }) =>
30-
bindFirestoreRef('item', document)
31-
)
28+
const setItems = (collection: firestore.CollectionReference | firestore.Query) =>
29+
// @ts-ignore
30+
store.dispatch('action', ({ bindFirestoreRef }) => bindFirestoreRef('items', collection))
31+
const setItem = (document: firestore.DocumentReference) =>
32+
// @ts-ignore
33+
store.dispatch('action', ({ bindFirestoreRef }) => bindFirestoreRef('item', document))
3234

33-
let collection, document
35+
let collection: firestore.CollectionReference, document: firestore.DocumentReference
3436
beforeEach(async () => {
3537
store.replaceState({
38+
// @ts-ignore
3639
items: null,
3740
item: null,
3841
module: {
39-
items: null
40-
}
42+
items: [],
43+
},
4144
})
45+
46+
// @ts-ignore
4247
collection = db.collection()
48+
// @ts-ignore
4349
document = db.collection().doc()
4450
await tick()
4551
})
@@ -71,7 +77,8 @@ describe('firestoreAction', () => {
7177
it('unbinds previously bound refs', async () => {
7278
await setItem(document)
7379
expect(store.state.item).toEqual(null)
74-
const doc2 = db.collection().doc()
80+
// @ts-ignore
81+
const doc2: firestore.DocumentReference = db.collection().doc()
7582
await doc2.update({ bar: 'bar' })
7683
await document.update({ foo: 'foo' })
7784
expect(store.state.item).toEqual({ foo: 'foo' })
@@ -83,21 +90,23 @@ describe('firestoreAction', () => {
8390

8491
it('waits for all refs in document', async () => {
8592
const a = db.collection().doc()
86-
const b = db.collection().doc()
93+
// @ts-ignore
94+
const b: firestore.DocumentReference = db.collection().doc()
8795
delayUpdate(b)
8896
await document.update({ a, b })
8997

9098
await setItem(document)
9199

92100
expect(store.state.item).toEqual({
93101
a: null,
94-
b: null
102+
b: null,
95103
})
96104
})
97105

98106
it('waits for all refs in document with interrupting by new ref', async () => {
99107
const a = db.collection().doc()
100-
const b = db.collection().doc()
108+
// @ts-ignore
109+
const b: firestore.DocumentReference = db.collection().doc()
101110
const c = db.collection().doc()
102111
delayUpdate(b)
103112
await document.update({ a, b })
@@ -111,14 +120,16 @@ describe('firestoreAction', () => {
111120
expect(store.state.item).toEqual({
112121
a: null,
113122
b: null,
114-
c: null
123+
c: null,
115124
})
116125
})
117126

118127
it('waits for nested refs with data in collections', async () => {
119128
const a = db.collection().doc()
120-
const b = db.collection().doc()
121-
const c = db.collection().doc()
129+
// @ts-ignore
130+
const b: firestore.DocumentReference = db.collection().doc()
131+
// @ts-ignore
132+
const c: firestore.DocumentReference = db.collection().doc()
122133
await a.update({ isA: true })
123134
await c.update({ isC: true })
124135
await b.update({ c })
@@ -129,17 +140,16 @@ describe('firestoreAction', () => {
129140

130141
await setItems(collection)
131142

132-
expect(store.state.items).toEqual([
133-
{ a: { isA: true }},
134-
{ b: { c: { isC: true }}}
135-
])
143+
expect(store.state.items).toEqual([{ a: { isA: true } }, { b: { c: { isC: true } } }])
136144
})
137145

138146
it('can unbind a reference', async () => {
139147
await setItems(collection)
140148
await collection.add({ text: 'foo' })
141-
await store.dispatch('action', ({ unbindFirestoreRef }) =>
142-
unbindFirestoreRef('items')
149+
await store.dispatch(
150+
'action',
151+
// @ts-ignore
152+
({ unbindFirestoreRef }) => unbindFirestoreRef('items')
143153
)
144154

145155
expect(store.state.items).toEqual([])
@@ -151,18 +161,24 @@ describe('firestoreAction', () => {
151161

152162
it('does not throw there is nothing to unbind', async () => {
153163
await setItems(collection)
154-
await store.dispatch('action', ({ unbindFirestoreRef }) =>
155-
expect(() => {
156-
unbindFirestoreRef('items')
157-
unbindFirestoreRef('items')
158-
}).not.toThrow()
164+
await store.dispatch(
165+
'action',
166+
// @ts-ignore
167+
({ unbindFirestoreRef }) =>
168+
expect(() => {
169+
unbindFirestoreRef('items')
170+
unbindFirestoreRef('items')
171+
}).not.toThrow()
159172
)
160173

161-
await store.dispatch('module/action', ({ unbindFirestoreRef }) =>
162-
expect(() => {
163-
unbindFirestoreRef('items')
164-
unbindFirestoreRef('items')
165-
}).not.toThrow()
174+
await store.dispatch(
175+
'module/action',
176+
// @ts-ignore
177+
({ unbindFirestoreRef }) =>
178+
expect(() => {
179+
unbindFirestoreRef('items')
180+
unbindFirestoreRef('items')
181+
}).not.toThrow()
166182
)
167183
})
168184
})

packages/vuexfire/test/mutations.spec.js renamed to packages/vuexfire/__tests__/mutations.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { vuexfireMutations } from '../src'
22
import {
33
VUEXFIRE_SET_VALUE,
44
VUEXFIRE_ARRAY_ADD,
5-
VUEXFIRE_ARRAY_REMOVE
6-
} from '../src/common/types'
5+
VUEXFIRE_ARRAY_REMOVE,
6+
} from '../src/mutations-types'
77
// import { db, tick, Vue } from '@posva/vuefire-test-helpers'
88

99
describe('mutations', () => {
@@ -12,17 +12,17 @@ describe('mutations', () => {
1212
vuexfireMutations[VUEXFIRE_SET_VALUE](state, {
1313
path: 'foo',
1414
target: state,
15-
data: 'foo'
15+
data: 'foo',
1616
})
1717
expect(state.foo).toBe('foo')
1818
})
1919

2020
it('sets a value with a path', () => {
21-
const state = { foo: { a: { b: null }}}
21+
const state = { foo: { a: { b: null } } }
2222
vuexfireMutations[VUEXFIRE_SET_VALUE](state, {
2323
path: 'foo.a.b',
2424
target: state,
25-
data: 'foo'
25+
data: 'foo',
2626
})
2727
expect(state.foo.a.b).toBe('foo')
2828
})
@@ -32,14 +32,14 @@ describe('mutations', () => {
3232
vuexfireMutations[VUEXFIRE_ARRAY_ADD](state, {
3333
target: state.items,
3434
newIndex: 0,
35-
data: 'foo'
35+
data: 'foo',
3636
})
3737
expect(state.items).toEqual(['foo'])
3838

3939
vuexfireMutations[VUEXFIRE_ARRAY_ADD](state, {
4040
target: state.items,
4141
newIndex: 0,
42-
data: 'bar'
42+
data: 'bar',
4343
})
4444
expect(state.items).toEqual(['bar', 'foo'])
4545
})
@@ -48,13 +48,13 @@ describe('mutations', () => {
4848
const state = { items: ['foo', 'bar'] }
4949
vuexfireMutations[VUEXFIRE_ARRAY_REMOVE](state, {
5050
target: state.items,
51-
oldIndex: 0
51+
oldIndex: 0,
5252
})
5353
expect(state.items).toEqual(['bar'])
5454

5555
vuexfireMutations[VUEXFIRE_ARRAY_REMOVE](state, {
5656
target: state.items,
57-
oldIndex: 0
57+
oldIndex: 0,
5858
})
5959
expect(state.items).toEqual([])
6060
})

packages/vuexfire/test/rtdb/index.spec.js renamed to packages/vuexfire/__tests__/rtdb.spec.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
import Vuex from 'vuex'
2-
import { firebaseAction, vuexfireMutations } from '../../src'
2+
import { firebaseAction, vuexfireMutations } from '../src'
33
import { MockFirebase, tick, Vue } from '@posva/vuefire-test-helpers'
4+
import { database } from 'firebase'
45

56
Vue.use(Vuex)
67

78
const db = new MockFirebase().child('data')
89

910
describe('RTDB: firebaseAction', () => {
10-
const store = new Vuex.Store({
11+
const item: any = null,
12+
items: any[] = []
13+
const store = new Vuex.Store<{ item: any; items: any[] }>({
14+
state: { items, item },
1115
mutations: vuexfireMutations,
1216
actions: {
13-
action: firebaseAction((context, fn) => fn(context))
17+
action: firebaseAction((context, fn) => fn(context)),
1418
},
1519

1620
modules: {
1721
module: {
1822
namespaced: true,
1923
actions: {
20-
action: firebaseAction((context, fn) => fn(context))
21-
}
22-
}
23-
}
24+
action: firebaseAction((context, fn) => fn(context)),
25+
},
26+
},
27+
},
2428
})
2529

26-
const setItems = collection =>
27-
store.dispatch('action', ({ bindFirebaseRef }) =>
28-
bindFirebaseRef('items', collection)
29-
)
30-
const setItem = document =>
31-
store.dispatch('action', ({ bindFirebaseRef }) =>
32-
bindFirebaseRef('item', document)
33-
)
30+
const setItems = (collection: database.Query) =>
31+
// @ts-ignore
32+
store.dispatch('action', ({ bindFirebaseRef }) => bindFirebaseRef('items', collection))
33+
const setItem = (document: database.Reference) =>
34+
// @ts-ignore
35+
store.dispatch('action', ({ bindFirebaseRef }) => bindFirebaseRef('item', document))
3436

35-
let collection, document
37+
let collection: database.Reference, document: database.Reference
3638
beforeEach(async () => {
3739
store.replaceState({
3840
items: [],
39-
item: null
41+
item: null,
4042
})
4143
collection = db.child('data')
4244
document = db.child('item')
45+
// @ts-ignore
4346
collection.autoFlush()
47+
// @ts-ignore
4448
document.autoFlush()
4549
await tick()
4650
})
@@ -71,6 +75,7 @@ describe('RTDB: firebaseAction', () => {
7175
it('unbinds previously bound refs', async () => {
7276
await setItem(document)
7377
const doc2 = db.child('doc2')
78+
// @ts-ignore
7479
doc2.autoFlush()
7580
doc2.set({ bar: 'bar' })
7681
setItem(doc2)
@@ -82,8 +87,10 @@ describe('RTDB: firebaseAction', () => {
8287
it('can unbind a reference', async () => {
8388
await setItems(collection)
8489
collection.push({ text: 'foo' })
85-
await store.dispatch('action', ({ unbindFirebaseRef }) =>
86-
unbindFirebaseRef('items')
90+
await store.dispatch(
91+
'action',
92+
// @ts-ignore
93+
({ unbindFirebaseRef }) => unbindFirebaseRef('items')
8794
)
8895

8996
expect(store.state.items).toEqual([])
@@ -95,18 +102,24 @@ describe('RTDB: firebaseAction', () => {
95102

96103
it('does not throw there is nothing to unbind', async () => {
97104
await setItems(collection)
98-
await store.dispatch('action', ({ unbindFirebaseRef }) =>
99-
expect(() => {
100-
unbindFirebaseRef('items')
101-
unbindFirebaseRef('items')
102-
}).not.toThrow()
105+
await store.dispatch(
106+
'action',
107+
// @ts-ignore
108+
({ unbindFirebaseRef }) =>
109+
expect(() => {
110+
unbindFirebaseRef('items')
111+
unbindFirebaseRef('items')
112+
}).not.toThrow()
103113
)
104114

105-
await store.dispatch('module/action', ({ unbindFirebaseRef }) =>
106-
expect(() => {
107-
unbindFirebaseRef('items')
108-
unbindFirebaseRef('items')
109-
}).not.toThrow()
115+
await store.dispatch(
116+
'module/action',
117+
// @ts-ignore
118+
({ unbindFirebaseRef }) =>
119+
expect(() => {
120+
unbindFirebaseRef('items')
121+
unbindFirebaseRef('items')
122+
}).not.toThrow()
110123
)
111124
})
112125
})

0 commit comments

Comments
 (0)