Skip to content

Commit 8f786c5

Browse files
committed
refactor(rtdb): expose bind and unbind
1 parent 346764e commit 8f786c5

File tree

3 files changed

+181
-112
lines changed

3 files changed

+181
-112
lines changed

__tests__/vuefire/rtdb/bind.spec.ts

Lines changed: 79 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,170 @@
1-
import { rtdbPlugin } from '../../src'
2-
import { Vue, tick, 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: manual bind', () => {
7-
async function createVm() {
6+
async function factory() {
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-
})
8+
const wrapper = mount(
9+
{
10+
template: 'no',
11+
// purposely set items as null
12+
// but it's a good practice to set it to an empty array
13+
data: () => ({
14+
items: [],
15+
item: null,
16+
}),
17+
},
18+
{
19+
global: {
20+
plugins: [rtdbPlugin],
21+
},
22+
}
23+
)
24+
1725
await tick()
1826

19-
return { vm, source }
27+
return { wrapper, source }
2028
}
2129

2230
it('manually binds as an array', async () => {
23-
const { vm, source } = await createVm()
24-
expect(vm.items).toEqual([])
25-
const promise = vm.$rtdbBind('items', source)
26-
expect(vm.items).toEqual([])
31+
const { wrapper, source } = await factory()
32+
expect(wrapper.vm.items).toEqual([])
33+
const promise = wrapper.vm.$rtdbBind('items', source)
34+
expect(wrapper.vm.items).toEqual([])
2735
source.push({ text: 'foo' })
2836
source.flush()
2937
await promise
30-
expect(vm.items).toEqual([{ text: 'foo' }])
38+
expect(wrapper.vm.items).toEqual([{ text: 'foo' }])
3139
})
3240

3341
it('removes children in arrays', async () => {
34-
const { vm, source } = await createVm()
42+
const { wrapper, source } = await factory()
3543
source.autoFlush()
3644
source.push({ name: 'one' })
3745
source.push({ name: 'two' })
3846

39-
await vm.$rtdbBind('items', source)
40-
source.child(vm.items[1]['.key']).remove()
41-
expect(vm.items).toEqual([{ name: 'one' }])
47+
await wrapper.vm.$rtdbBind('items', source)
48+
source.child(wrapper.vm.items[1]['.key']).remove()
49+
expect(wrapper.vm.items).toEqual([{ name: 'one' }])
4250
})
4351

4452
it('returs a promise', async () => {
45-
const { vm, source } = await createVm()
46-
expect(vm.$rtdbBind('items', source) instanceof Promise).toBe(true)
47-
expect(vm.$rtdbBind('item', source) instanceof Promise).toBe(true)
53+
const { wrapper, source } = await factory()
54+
expect(wrapper.vm.$rtdbBind('items', source) instanceof Promise).toBe(true)
55+
expect(wrapper.vm.$rtdbBind('item', source) instanceof Promise).toBe(true)
4856
})
4957

5058
it('manually binds as an object', async () => {
51-
const { vm, source } = await createVm()
52-
expect(vm.item).toEqual(null)
53-
const promise = vm.$rtdbBind('item', source)
54-
expect(vm.item).toEqual(null)
59+
const { wrapper, source } = await factory()
60+
expect(wrapper.vm.item).toEqual(null)
61+
const promise = wrapper.vm.$rtdbBind('item', source)
62+
expect(wrapper.vm.item).toEqual(null)
5563
source.set({ text: 'foo' })
5664
source.flush()
5765
await promise
58-
expect(vm.item).toEqual({ text: 'foo' })
66+
expect(wrapper.vm.item).toEqual({ text: 'foo' })
5967
})
6068

6169
it('unbinds when overriting existing bindings', async () => {
62-
const { vm, source } = await createVm()
70+
const { wrapper, source } = await factory()
6371
source.autoFlush()
6472
source.set({ name: 'foo' })
65-
await vm.$rtdbBind('item', source)
66-
expect(vm.item).toEqual({ name: 'foo' })
73+
await wrapper.vm.$rtdbBind('item', source)
74+
expect(wrapper.vm.item).toEqual({ name: 'foo' })
6775
const other = new MockFirebase().child('other')
6876
other.autoFlush()
6977
other.set({ name: 'bar' })
70-
await vm.$rtdbBind('item', other)
71-
expect(vm.item).toEqual({ name: 'bar' })
78+
await wrapper.vm.$rtdbBind('item', other)
79+
expect(wrapper.vm.item).toEqual({ name: 'bar' })
7280

7381
source.set({ name: 'new foo' })
74-
expect(vm.item).toEqual({ name: 'bar' })
82+
expect(wrapper.vm.item).toEqual({ name: 'bar' })
7583
})
7684

7785
it('manually unbinds a ref', async () => {
78-
const { vm, source } = await createVm()
86+
const { wrapper, source } = await factory()
7987
source.autoFlush()
8088
source.set({ name: 'foo' })
81-
await vm.$rtdbBind('item', source)
82-
expect(vm.item).toEqual({ name: 'foo' })
83-
vm.$rtdbUnbind('item')
89+
await wrapper.vm.$rtdbBind('item', source)
90+
expect(wrapper.vm.item).toEqual({ name: 'foo' })
91+
wrapper.vm.$rtdbUnbind('item')
8492
source.set({ name: 'bar' })
85-
expect(vm.item).toEqual(null)
93+
expect(wrapper.vm.item).toEqual(null)
8694
})
8795

8896
it('can customize the reset option through $rtdbBind', async () => {
89-
const { vm, source } = await createVm()
97+
const { wrapper, source } = await factory()
9098
const otherSource = new MockFirebase().child('data2')
9199
source.set({ name: 'foo' })
92100
otherSource.set({ name: 'bar' })
93-
let p = vm.$rtdbBind('item', source)
101+
let p = wrapper.vm.$rtdbBind('item', source)
94102
source.flush()
95103
await p
96-
p = vm.$rtdbBind('item', otherSource, { reset: false })
97-
expect(vm.item).toEqual({ name: 'foo' })
104+
p = wrapper.vm.$rtdbBind('item', otherSource, { reset: false })
105+
expect(wrapper.vm.item).toEqual({ name: 'foo' })
98106
otherSource.flush()
99107
await p
100-
expect(vm.item).toEqual({ name: 'bar' })
108+
expect(wrapper.vm.item).toEqual({ name: 'bar' })
101109
// should not apply last used option
102-
p = vm.$rtdbBind('item', source)
103-
expect(vm.item).toEqual(null)
110+
p = wrapper.vm.$rtdbBind('item', source)
111+
expect(wrapper.vm.item).toEqual(null)
104112
source.flush()
105113
})
106114

107115
it('can customize the reset option through $rtdbUnbind', async () => {
108-
const { vm, source } = await createVm()
116+
const { wrapper, source } = await factory()
109117
source.autoFlush()
110118
source.set({ name: 'foo' })
111119
const otherSource = new MockFirebase().child('data2')
112120
otherSource.set({ name: 'bar' })
113121
otherSource.autoFlush()
114-
await vm.$rtdbBind('item', source)
115-
expect(vm.item).toEqual({ name: 'foo' })
116-
vm.$rtdbUnbind('item', false)
117-
expect(vm.item).toEqual({ name: 'foo' })
122+
await wrapper.vm.$rtdbBind('item', source)
123+
expect(wrapper.vm.item).toEqual({ name: 'foo' })
124+
wrapper.vm.$rtdbUnbind('item', false)
125+
expect(wrapper.vm.item).toEqual({ name: 'foo' })
118126
// should not apply the option to the next unbind call
119-
await vm.$rtdbBind('item', otherSource, { reset: false })
120-
expect(vm.item).toEqual({ name: 'bar' })
121-
vm.$rtdbUnbind('item')
122-
expect(vm.item).toEqual(null)
127+
await wrapper.vm.$rtdbBind('item', otherSource, { reset: false })
128+
expect(wrapper.vm.item).toEqual({ name: 'bar' })
129+
wrapper.vm.$rtdbUnbind('item')
130+
expect(wrapper.vm.item).toEqual(null)
123131
})
124132

125133
it('do not reset if wait: true', async () => {
126-
const { vm, source } = await createVm()
134+
const { wrapper, source } = await factory()
127135
const otherSource = new MockFirebase().child('data2')
128136

129137
// source.autoFlush()
130-
let p = vm.$rtdbBind('items', source)
138+
let p = wrapper.vm.$rtdbBind('items', source)
131139
source.push({ name: 'foo' })
132140
source.flush()
133141
await p
134-
p = vm.$rtdbBind('items', otherSource, { wait: true, reset: true })
135-
expect(vm.items).toEqual([{ name: 'foo' }])
142+
p = wrapper.vm.$rtdbBind('items', otherSource, { wait: true, reset: true })
143+
expect(wrapper.vm.items).toEqual([{ name: 'foo' }])
136144
otherSource.push({ name: 'bar' })
137145
otherSource.flush()
138146
await p
139-
expect(vm.items).toEqual([{ name: 'bar' }])
147+
expect(wrapper.vm.items).toEqual([{ name: 'bar' }])
140148
})
141149

142150
it('wait + reset can be overriden with a function', async () => {
143-
const { vm, source } = await createVm()
151+
const { wrapper, source } = await factory()
144152
const otherSource = new MockFirebase().child('data2')
145153

146154
// source.autoFlush()
147-
let p = vm.$rtdbBind('items', source)
155+
let p = wrapper.vm.$rtdbBind('items', source)
148156
source.push({ name: 'foo' })
149157
source.flush()
150158
await p
151159
// using an array is important as we use that to choose between bindAsObject and bindAsArray
152-
p = vm.$rtdbBind('items', otherSource, { wait: true, reset: () => ['foo'] })
153-
expect(vm.items).toEqual(['foo'])
160+
p = wrapper.vm.$rtdbBind('items', otherSource, {
161+
wait: true,
162+
reset: () => ['foo'],
163+
})
164+
expect(wrapper.vm.items).toEqual(['foo'])
154165
otherSource.push({ name: 'bar' })
155166
otherSource.flush()
156167
await p
157-
expect(vm.items).toEqual([{ name: 'bar' }])
168+
expect(wrapper.vm.items).toEqual([{ name: 'bar' }])
158169
})
159170
})

src/vuefire/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// TODO: add binds
2-
// export { rtdbPlugin } from './rtdb'
1+
export { rtdbPlugin, bind as rtdbBind, unbind as rtdbUnbind } from './rtdb'
32
export {
43
firestorePlugin,
54
bind as firestoreBind,

0 commit comments

Comments
 (0)