Skip to content

Commit 5089c6c

Browse files
committed
test(database): options tests
1 parent 65a72b7 commit 5089c6c

File tree

6 files changed

+44
-284
lines changed

6 files changed

+44
-284
lines changed

old_tests/vuefire/rtdb/index.spec.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

old_tests/vuefire/rtdb/merging.spec.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

old_tests/vuefire/rtdb/options.spec.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

src/database/optionsApi.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@ import {
1010
_GlobalDatabaseRefOptions,
1111
} from './subscribe'
1212

13-
/**
14-
* Returns the original reference of a Firebase reference or query across SDK versions.
15-
*
16-
* @param refOrQuery
17-
*/
18-
function getRef(refOrQuery: DatabaseReference | Query): DatabaseReference {
19-
return refOrQuery.ref
20-
}
21-
2213
/**
2314
* Options for the Firebase Database Plugin that enables the Options API such as `$rtdbBind` and `$rtdbUnbind`.
2415
*/
@@ -78,8 +69,8 @@ declare module '@vue/runtime-core' {
7869
}
7970
}
8071

81-
type VueFirebaseObject = Record<string, Query | DatabaseReference>
82-
type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
72+
export type VueFirebaseObject = Record<string, Query | DatabaseReference>
73+
export type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
8374

8475
export const rtdbUnbinds = new WeakMap<
8576
object,
@@ -151,7 +142,7 @@ export function databasePlugin(
151142
// this._firebaseSources[key] = source
152143
// we make it readonly for the user but we must change it. Maybe there is a way to have an internal type here but expose a readonly type through a d.ts
153144
;(this.$firebaseRefs as Mutable<Record<string, DatabaseReference>>)[key] =
154-
getRef(source)
145+
source.ref
155146

156147
return promise
157148
}

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export type {
1212
} from './database/utils'
1313

1414
export { databasePlugin } from './database/optionsApi'
15-
export type { DatabasePluginOptions } from './database/optionsApi'
15+
export type {
16+
DatabasePluginOptions,
17+
VueFirebaseObject,
18+
FirebaseOption,
19+
} from './database/optionsApi'
1620

1721
export { useCollection, useDocument } from './firestore'
1822
export type {

tests/database/options.spec.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {
55
databasePlugin,
66
DatabasePluginOptions,
77
DatabaseSnapshotSerializer,
8+
FirebaseOption,
89
} from '../../src'
910
import { setupDatabaseRefs } from '../utils'
1011

1112
const component = defineComponent({
1213
template: 'no',
13-
data: () => ({ items: [], item: null }),
14+
data: () => ({ itemList: [], item: null }),
1415
})
1516

1617
describe('RTDB: plugin options', () => {
@@ -40,14 +41,14 @@ describe('RTDB: plugin options', () => {
4041

4142
const itemListRef = databaseRef()
4243

43-
const p = vm.$rtdbBind('items', itemListRef)
44+
const p = vm.$rtdbBind('itemList', itemListRef)
4445
await push(itemListRef, { text: 'foo' })
4546

4647
expect(serialize).toHaveBeenCalledTimes(1)
4748
expect(serialize).toHaveBeenCalledWith(
4849
expect.objectContaining({ val: expect.any(Function) })
4950
)
50-
expect(vm.items).toEqual([{ foo: 'bar', id: '2' }])
51+
expect(vm.itemList).toEqual([{ foo: 'bar', id: '2' }])
5152
})
5253

5354
it('can override serialize with local option', async () => {
@@ -60,17 +61,46 @@ describe('RTDB: plugin options', () => {
6061
bar: 'bar',
6162
}))
6263

63-
vm.$rtdbBind('items', items, { serialize: spy })
64+
vm.$rtdbBind('itemList', items, { serialize: spy })
6465
await push(items, { text: 'foo' })
6566

6667
expect(serialize).not.toHaveBeenCalled()
6768
expect(spy).toHaveBeenCalledTimes(1)
6869
expect(spy).toHaveBeenCalledWith(
6970
expect.objectContaining({ val: expect.any(Function) })
7071
)
71-
expect(vm.items).toEqual([{ bar: 'bar', id: '3' }])
72+
expect(vm.itemList).toEqual([{ bar: 'bar', id: '3' }])
7273
})
7374
})
7475

75-
// describe('firebase option', () => {})
76+
describe('firebase option', () => {
77+
function factory(
78+
firebase: FirebaseOption,
79+
pluginOptions?: DatabasePluginOptions
80+
) {
81+
return mount(component, {
82+
firebase,
83+
global: {
84+
plugins: [[databasePlugin, pluginOptions]],
85+
},
86+
})
87+
}
88+
89+
it('setups $firebaseRefs', async () => {
90+
const itemSource = databaseRef()
91+
const itemListSource = databaseRef()
92+
const { vm } = factory({ item: itemSource, itemList: itemListSource })
93+
expect(Object.keys(vm.$firebaseRefs).sort()).toEqual(['item', 'itemList'])
94+
expect(vm.$firebaseRefs.item.key).toBe(itemSource.key)
95+
expect(vm.$firebaseRefs.itemList.key).toBe(itemListSource.key)
96+
})
97+
98+
it('clears $firebaseRefs on unmount', async () => {
99+
const itemSource = databaseRef()
100+
const itemListSource = databaseRef()
101+
const wrapper = factory({ item: itemSource, itemList: itemListSource })
102+
wrapper.unmount()
103+
expect(wrapper.vm.$firebaseRefs).toEqual(null)
104+
})
105+
})
76106
})

0 commit comments

Comments
 (0)