Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit 459f031

Browse files
committed
test(rtdb): add basic bind options
1 parent 8f786c5 commit 459f031

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

__tests__/vuefire/rtdb/index.spec.ts

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1-
import { rtdbPlugin } from '../../src'
2-
import { tick, Vue, MockFirebase } from '@posva/vuefire-test-helpers'
3-
4-
Vue.use(rtdbPlugin)
1+
import { mount } from '@vue/test-utils'
2+
import { rtdbPlugin } from '../../../src'
3+
import { tick, MockFirebase } from '../../src'
54

65
describe('RTDB: firebase option', () => {
76
async function createVm() {
87
const source = new MockFirebase().child('data')
9-
const vm = new Vue({
10-
// purposely set items as null
11-
// but it's a good practice to set it to an empty array
12-
data: () => ({
13-
items: [],
14-
item: null,
15-
}),
16-
firebase: {
17-
items: source,
18-
item: source,
8+
const wrapper = mount(
9+
{
10+
template: 'no',
11+
data: () => ({
12+
items: [],
13+
item: null,
14+
}),
15+
firebase: {
16+
items: source,
17+
item: source,
18+
},
1919
},
20-
})
20+
{
21+
global: {
22+
plugins: [rtdbPlugin],
23+
},
24+
}
25+
)
2126
await tick()
2227

23-
return { vm, source }
28+
return { vm: wrapper.vm, source, wrapper }
2429
}
2530

2631
it('does nothing with no firebase', () => {
27-
const vm = new Vue({
28-
data: () => ({ items: null }),
29-
})
30-
expect(vm.items).toEqual(null)
31-
})
32-
33-
it('setups _firebaseUnbinds', async () => {
34-
const { vm } = await createVm()
35-
expect(vm._firebaseUnbinds).toBeTruthy()
36-
expect(Object.keys(vm._firebaseUnbinds).sort()).toEqual(['item', 'items'])
32+
const wrapper = mount(
33+
{
34+
template: 'no',
35+
data: () => ({ items: null }),
36+
},
37+
{ global: { plugins: [rtdbPlugin] } }
38+
)
39+
expect(wrapper.vm.items).toEqual(null)
3740
})
3841

39-
it('setups _firebaseUnbinds with no firebase options', () => {
40-
const vm = new Vue({
41-
data: () => ({ items: null }),
42-
})
43-
expect(vm._firebaseUnbinds).toBeTruthy()
44-
expect(Object.keys(vm._firebaseUnbinds)).toEqual([])
42+
it('does nothing with empty firebase return', () => {
43+
const wrapper = mount(
44+
{
45+
template: 'no',
46+
data: () => ({ items: null }),
47+
// @ts-ignore
48+
firebase: () => {},
49+
},
50+
{ global: { plugins: [rtdbPlugin] } }
51+
)
52+
// @ts-ignore
53+
expect(wrapper.vm.items).toEqual(null)
4554
})
4655

4756
it('setups $firebaseRefs', async () => {
@@ -52,10 +61,8 @@ describe('RTDB: firebase option', () => {
5261
})
5362

5463
it('clears $firebaseRefs on $destroy', async () => {
55-
const { vm } = await createVm()
56-
vm.$destroy()
64+
const { vm, wrapper } = await createVm()
65+
wrapper.unmount()
5766
expect(vm.$firebaseRefs).toEqual(null)
58-
expect(vm._firebaseUnbinds).toEqual(null)
59-
expect(vm._firebaseSources).toEqual(null)
6067
})
6168
})

src/vuefire/rtdb.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
getCurrentInstance,
1616
onBeforeUnmount,
1717
} from 'vue'
18-
import { rtdbBindAsObject } from 'src/rtdb'
1918

2019
/**
2120
* Returns the original reference of a Firebase reference or query across SDK versions.
@@ -177,8 +176,7 @@ export const rtdbPlugin = function rtdbPlugin(
177176
reset?: RTDBOptions['reset']
178177
) {
179178
internalUnbind(key, rtdbUnbinds.get(this), reset)
180-
// TODO:
181-
// delete this.$firestoreRefs[key]
179+
delete this.$firebaseRefs[key]
182180
}
183181

184182
// add $rtdbBind and $rtdbUnbind methods
@@ -213,19 +211,15 @@ export const rtdbPlugin = function rtdbPlugin(
213211
// TODO:
214212
// @ts-ignore
215213
// this._firebaseSources[key] = source
216-
// @ts-ignore
217-
// this.$firebaseRefs[key] = getRef(source)
214+
this.$firebaseRefs[key] = getRef(source)
218215

219216
return promise
220217
}
221218

222219
// handle firebase option
223220
app.mixin({
224221
beforeCreate(this: ComponentPublicInstance) {
225-
// TODO:
226-
// this.$firebaseRefs = Object.create(null)
227-
// this._firebaseSources = Object.create(null)
228-
// this._firebaseUnbinds = Object.create(null)
222+
this.$firebaseRefs = Object.create(null)
229223
},
230224
created(this: ComponentPublicInstance) {
231225
let bindings = this.$options.firebase
@@ -241,20 +235,15 @@ export const rtdbPlugin = function rtdbPlugin(
241235
}
242236
},
243237

244-
beforeDestroy(this: ComponentPublicInstance) {
238+
beforeUnmount(this: ComponentPublicInstance) {
245239
const unbinds = rtdbUnbinds.get(this)
246240
if (unbinds) {
247241
for (const key in unbinds) {
248242
unbinds[key]()
249243
}
250244
}
251-
// TODO:
252-
// // @ts-ignore
253-
// this._firebaseSources = null
254-
// // @ts-ignore
255-
// this._firebaseUnbinds = null
256-
// // @ts-ignore
257-
// this.$firebaseRefs = null
245+
// @ts-ignore
246+
this.$firebaseRefs = null
258247
},
259248
})
260249
}

0 commit comments

Comments
 (0)