Skip to content

Commit cb72361

Browse files
committed
test(firestore): add option tests
1 parent f8b2cb9 commit cb72361

File tree

4 files changed

+47
-172
lines changed

4 files changed

+47
-172
lines changed

Diff for: old_tests/vuefire/firestore/index.spec.ts

-74
This file was deleted.

Diff for: old_tests/vuefire/firestore/merging.spec.ts

-92
This file was deleted.

Diff for: src/firestore/optionsApi.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import {
1616
import { internalUnbind, _useFirestoreRef } from '.'
1717
import { ResetOption, UnbindWithReset } from '../shared'
1818

19-
export type FirestoreOption = VueFirestoreObject | (() => VueFirestoreObject)
20-
2119
export type VueFirestoreObject = Record<
2220
string,
23-
DocumentReference | Query | CollectionReference
21+
DocumentReference<unknown> | Query<unknown> | CollectionReference<unknown>
2422
>
2523

24+
export type FirestoreOption = VueFirestoreObject | (() => VueFirestoreObject)
25+
2626
// TODO: this should be an entry point to generate the corresponding .d.ts file that only gets included if the plugin is imported
2727

2828
export const firestoreUnbinds = new WeakMap<
@@ -84,7 +84,10 @@ export const firestorePlugin = function firestorePlugin(
8484
GlobalTarget[bindName] = function firestoreBind(
8585
this: ComponentPublicInstance,
8686
key: string,
87-
docOrCollectionRef: Query | CollectionReference | DocumentReference,
87+
docOrCollectionRef:
88+
| Query<unknown>
89+
| CollectionReference<unknown>
90+
| DocumentReference<unknown>,
8891
userOptions?: FirestoreRefOptions
8992
) {
9093
const options = Object.assign({}, globalOptions, userOptions)
@@ -191,7 +194,7 @@ declare module '@vue/runtime-core' {
191194

192195
export interface ComponentCustomOptions {
193196
/**
194-
* Calls `$firestoreBind` at created
197+
* Calls `$firestoreBind` before mounting the component
195198
*/
196199
firestore?: FirestoreOption
197200
}

Diff for: tests/firestore/options.spec.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { defineComponent } from 'vue'
22
import { mount } from '@vue/test-utils'
33
import { describe, expect, it, vi } from 'vitest'
4-
import { firestorePlugin, FirestorePluginOptions } from '../../src'
4+
import {
5+
FirestoreOption,
6+
firestorePlugin,
7+
FirestorePluginOptions,
8+
} from '../../src'
59
import { DocumentData } from 'firebase/firestore'
610
import { setupFirestoreRefs } from '../utils'
711

@@ -94,4 +98,38 @@ describe('Firestore: Options API', () => {
9498
expect(wrapper.vm.itemList).toEqual([{ bar: 'bar' }])
9599
})
96100
})
101+
102+
describe('firestore option', () => {
103+
function factory(
104+
firestore: FirestoreOption,
105+
pluginOptions?: FirestorePluginOptions
106+
) {
107+
return mount(component, {
108+
firestore,
109+
global: {
110+
plugins: [[firestorePlugin, pluginOptions]],
111+
},
112+
})
113+
}
114+
115+
it('setups $firestoreRefs', async () => {
116+
const itemSource = doc()
117+
const itemListSource = collection()
118+
const { vm } = factory({ item: itemSource, itemList: itemListSource })
119+
expect(Object.keys(vm.$firestoreRefs).sort()).toEqual([
120+
'item',
121+
'itemList',
122+
])
123+
expect(vm.$firestoreRefs.item.path).toBe(itemSource.path)
124+
expect(vm.$firestoreRefs.itemList.path).toBe(itemListSource.path)
125+
})
126+
127+
it('clears $firestoreRefs on unmount', async () => {
128+
const itemSource = doc()
129+
const itemListSource = collection()
130+
const wrapper = factory({ item: itemSource, itemList: itemListSource })
131+
wrapper.unmount()
132+
expect(wrapper.vm.$firestoreRefs).toEqual(null)
133+
})
134+
})
97135
})

0 commit comments

Comments
 (0)