Skip to content

Commit c4f1cb6

Browse files
committed
test: wip options
1 parent 37c4dd3 commit c4f1cb6

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

tests/firestore/collection.spec.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { mount } from '@vue/test-utils'
22
import { describe, expect, it } from 'vitest'
33
import { useCollection } from '../../src'
4-
import { addDoc, collection, DocumentData } from 'firebase/firestore'
4+
import {
5+
addDoc,
6+
collection as originalCollection,
7+
DocumentData,
8+
orderBy,
9+
} from 'firebase/firestore'
510
import { expectType, setupFirestoreRefs, tds, firestore } from '../utils'
611
import { usePendingPromises } from '../../src/vuefire/firestore'
712
import { type Ref } from 'vue'
813

914
describe('Firestore collections', () => {
10-
const { itemRef, listRef, orderedListRef } = setupFirestoreRefs()
15+
const { collection, query } = setupFirestoreRefs()
16+
17+
const listRef = collection()
18+
const orderedListRef = query(listRef, orderBy('name'))
1119

1220
it('binds a collection as an array', async () => {
1321
const wrapper = mount(
@@ -39,6 +47,7 @@ describe('Firestore collections', () => {
3947

4048
tds(() => {
4149
const db = firestore
50+
const collection = originalCollection
4251
expectType<Ref<DocumentData[]>>(useCollection(collection(db, 'todos')))
4352
// @ts-expect-error
4453
expectType<Ref<number[]>>(useCollection(collection(db, 'todos')))
@@ -48,7 +57,7 @@ describe('Firestore collections', () => {
4857
expectType<Ref<string[]>>(useCollection<number>(collection(db, 'todos')))
4958

5059
const refWithConverter = collection(db, 'todos').withConverter<number>({
51-
toFirestore: data => ({ n: data }),
60+
toFirestore: (data) => ({ n: data }),
5261
fromFirestore: (snap, options) => snap.data(options).n as number,
5362
})
5463
expectType<Ref<number[]>>(useCollection(refWithConverter))

old_tests/vuefire/firestore/options.spec.ts renamed to tests/firestore/options.spec.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { firestorePlugin } from '../../../src'
2-
import { db } from '../../src'
3-
import { mount } from '@vue/test-utils'
4-
import * as firestore from '@firebase/firestore-types'
51
import { defineComponent } from 'vue'
2+
import { mount } from '@vue/test-utils'
3+
import { describe, expect, it, vi } from 'vitest'
4+
import { firestorePlugin, PluginOptions, useCollection } from '../../src'
5+
import { addDoc, DocumentData } from 'firebase/firestore'
6+
import { expectType, setupFirestoreRefs, tds, firestore } from '../utils'
7+
import { usePendingPromises } from '../../src/vuefire/firestore'
8+
import { type Ref } from 'vue'
69

710
const component = defineComponent({ template: 'no' })
811

9-
describe('Firestore: plugin options', () => {
12+
describe.skip('Firestore: Options API', () => {
13+
const { itemRef, listRef, orderedListRef, collection, doc } =
14+
setupFirestoreRefs()
15+
1016
it('allows customizing $rtdbBind', () => {
1117
const wrapper = mount(component, {
1218
global: {
@@ -21,13 +27,16 @@ describe('Firestore: plugin options', () => {
2127
],
2228
},
2329
})
24-
expect(typeof (wrapper.vm as any).$myBind).toBe('function')
25-
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function')
30+
31+
// @ts-expect-error: haven't extended the types
32+
expect(wrapper.vm.$myBind).toBeTypeOf('function')
33+
// @ts-expect-error: haven't extended the types
34+
expect(wrapper.vm.$myUnbind).toBeTypeOf('function')
2635
})
2736

2837
it('calls custom serialize function with collection', async () => {
29-
const pluginOptions = {
30-
serialize: jest.fn(() => ({ foo: 'bar' })),
38+
const pluginOptions: PluginOptions = {
39+
serialize: vi.fn(() => ({ foo: 'bar' })),
3140
}
3241
const wrapper = mount(
3342
{
@@ -41,8 +50,7 @@ describe('Firestore: plugin options', () => {
4150
}
4251
)
4352

44-
// @ts-ignore
45-
const items: firestore.CollectionReference = db.collection()
53+
const items = collection()
4654
await items.add({})
4755

4856
await wrapper.vm.$bind('items', items)
@@ -57,7 +65,7 @@ describe('Firestore: plugin options', () => {
5765

5866
it('can be overridden by local option', async () => {
5967
const pluginOptions = {
60-
serialize: jest.fn(() => ({ foo: 'bar' })),
68+
serialize: vi.fn(() => ({ foo: 'bar' })),
6169
}
6270
const wrapper = mount(
6371
{
@@ -75,7 +83,7 @@ describe('Firestore: plugin options', () => {
7583
const items: firestore.CollectionReference = db.collection()
7684
await items.add({})
7785

78-
const spy = jest.fn(() => ({ bar: 'bar' }))
86+
const spy = vi.fn(() => ({ bar: 'bar' }))
7987

8088
await wrapper.vm.$bind('items', items, { serialize: spy })
8189

tests/utils.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getDocsFromServer,
1919
QueryDocumentSnapshot,
2020
deleteDoc,
21+
DocumentReference,
2122
} from 'firebase/firestore'
2223
import { afterAll, beforeAll } from 'vitest'
2324
import { isCollectionRef, isDocumentRef } from '../src/shared'
@@ -45,15 +46,50 @@ export function setupFirestoreRefs() {
4546
// clean up the tests data
4647
await Promise.all([
4748
deleteDoc(itemRef),
49+
...[...docsToClean].map((doc) => deleteDoc(doc)),
50+
deleteDoc(forItemsRef),
51+
...[...collectionsToClean].map((collection) =>
52+
clearCollection(collection)
53+
),
4854
clearCollection(listRef),
4955
clearCollection(testsCollection),
5056
])
5157
})
5258

53-
return { itemRef, listRef, orderedListRef, testId, col: forItemsRef }
59+
// for automatically generated collections
60+
let collectionId = 0
61+
const collectionsToClean = new Set<CollectionReference<any>>()
62+
function _collection(path?: string, ...pathSegments: string[]) {
63+
path = path || `col_${collectionId++}`
64+
65+
const col = collection(forItemsRef, path, ...pathSegments)
66+
collectionsToClean.add(col)
67+
return col
68+
}
69+
70+
// for automatically generated documents
71+
let docId = 0
72+
const docsToClean = new Set<DocumentReference<any>>()
73+
function _doc(path?: string, ...pathSegments: string[]) {
74+
path = path || `doc_${docId++}`
75+
const d = doc(testsCollection, path, ...pathSegments)
76+
docsToClean.add(d)
77+
return d
78+
}
79+
80+
return {
81+
itemRef,
82+
listRef,
83+
orderedListRef,
84+
testId,
85+
col: forItemsRef,
86+
collection: _collection,
87+
doc: _doc,
88+
query: firestoreQuery,
89+
}
5490
}
5591

56-
async function clearCollection(collection: CollectionReference) {
92+
async function clearCollection(collection: CollectionReference<any>) {
5793
const { docs } = await getDocsFromServer(collection)
5894
await Promise.all(
5995
docs.map((doc) => {
@@ -62,7 +98,7 @@ async function clearCollection(collection: CollectionReference) {
6298
)
6399
}
64100

65-
async function recursiveDeleteDoc(doc: QueryDocumentSnapshot) {
101+
async function recursiveDeleteDoc(doc: QueryDocumentSnapshot<any>) {
66102
const docData = doc.data()
67103
const promises: Promise<any>[] = []
68104
if (docData) {

0 commit comments

Comments
 (0)