Skip to content

Commit 0c20540

Browse files
committed
test: simpler generics
1 parent c34c9d8 commit 0c20540

File tree

2 files changed

+65
-25
lines changed

2 files changed

+65
-25
lines changed

tests/firestore/collection.spec.ts

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,30 @@ import {
1414
} from 'firebase/firestore'
1515
import { expectType, setupFirestoreRefs, tds, firestore } from '../utils'
1616
import { type Ref } from 'vue'
17+
import {
18+
VueFireQueryData,
19+
_InferReferenceType,
20+
_RefFirestore,
21+
} from '../../src/firestore'
1722

1823
describe('Firestore collections', () => {
1924
const { collection, query } = setupFirestoreRefs()
2025

21-
function factory<
22-
T = unknown,
23-
K extends CollectionReference<T> | Query<T> = CollectionReference<T>
24-
>(
25-
// @ts-expect-error: collection or query
26-
listRef: K = collection()
27-
) {
26+
function factory<T = DocumentData>() {
27+
const listRef = collection()
28+
const useIt = () => useCollection<T>(listRef)
29+
let data!: ReturnType<typeof useIt>
30+
2831
const wrapper = mount({
2932
template: 'no',
3033
setup() {
31-
const {
32-
data: list,
33-
promise,
34-
unbind,
35-
pending,
36-
error,
37-
} = useCollection(listRef)
38-
39-
return { list, promise, unbind, pending, error }
34+
data = useIt()
35+
36+
return { list: data.data, ...data }
4037
},
4138
})
4239

43-
return { wrapper, listRef }
40+
return { wrapper, listRef, ...data }
4441
}
4542

4643
function sortedList<
@@ -56,9 +53,12 @@ describe('Firestore collections', () => {
5653
})
5754
}
5855

56+
// TODO: factory by default returns an unknown, but even then it should include the id
57+
5958
it('starts the collection as an empty array', async () => {
60-
const { wrapper } = factory()
59+
const { wrapper, data } = factory()
6160
expect(wrapper.vm.list).toEqual([])
61+
expect(data.value).toEqual([])
6262
})
6363

6464
it('add items to the collection', async () => {
@@ -77,11 +77,11 @@ describe('Firestore collections', () => {
7777
const { wrapper, listRef } = factory<{ name: string }>()
7878

7979
const aRef = doc(listRef)
80-
const a = await setDoc(aRef, { name: 'a' })
80+
await setDoc(aRef, { name: 'a' })
8181
const bRef = doc(listRef)
82-
const b = await setDoc(bRef, { name: 'b' })
82+
await setDoc(bRef, { name: 'b' })
8383
const cRef = doc(listRef)
84-
const c = await setDoc(cRef, { name: 'c' })
84+
await setDoc(cRef, { name: 'c' })
8585

8686
await deleteDoc(aRef)
8787
expect(wrapper.vm.list).toHaveLength(2)
@@ -97,11 +97,11 @@ describe('Firestore collections', () => {
9797
const { wrapper, listRef } = factory<{ name: string }>()
9898

9999
const aRef = doc(listRef)
100-
const a = await setDoc(aRef, { name: 'a' })
100+
await setDoc(aRef, { name: 'a' })
101101
const bRef = doc(listRef)
102-
const b = await setDoc(bRef, { name: 'b' })
102+
await setDoc(bRef, { name: 'b' })
103103
const cRef = doc(listRef)
104-
const c = await setDoc(cRef, { name: 'c' })
104+
await setDoc(cRef, { name: 'c' })
105105

106106
await setDoc(aRef, { name: 'aa' })
107107
await updateDoc(cRef, { name: 'cc' })
@@ -111,6 +111,23 @@ describe('Firestore collections', () => {
111111
expect(wrapper.vm.list).toContainEqual({ name: 'cc' })
112112
})
113113

114+
it('can add an array with null to the collection', async () => {
115+
const { wrapper, listRef, data } = factory<{ list: Array<number | null> }>()
116+
117+
await addDoc(listRef, { list: [2, null] })
118+
expect(wrapper.vm.list).toHaveLength(1)
119+
expect(wrapper.vm.list).toContainEqual({ list: [2, null] })
120+
})
121+
122+
it('adds a non enumerable id to docs in the collection', async () => {
123+
const { wrapper, listRef, data } = factory<{ name: string }>()
124+
125+
const a = await addDoc(listRef, { name: 'a' })
126+
expect(wrapper.vm.list).toHaveLength(1)
127+
expect(data.value[0].id).toBeTypeOf('string')
128+
expect(data.value[0].id).toEqual(a.id)
129+
})
130+
114131
tds(() => {
115132
interface TodoI {
116133
text: string
@@ -123,6 +140,25 @@ describe('Firestore collections', () => {
123140
// @ts-expect-error: document data by default
124141
expectType<Ref<number[]>>(useCollection(collection(db, 'todos')))
125142

143+
// Adds the id
144+
expectType<string>(useCollection(collection(db, 'todos')).value[0].id)
145+
expectType<string>(
146+
useCollection<TodoI>(collection(db, 'todos')).value[0].id
147+
)
148+
expectType<string>(
149+
useCollection<unknown>(collection(db, 'todos')).value[0].id
150+
)
151+
useCollection(
152+
collection(db, 'todos').withConverter<TodoI>({
153+
fromFirestore: (snapshot) => {
154+
const data = snapshot.data()
155+
return { text: data.text, finished: data.finished }
156+
},
157+
toFirestore: (todo) => todo,
158+
})
159+
// @ts-expect-error: no id with custom converter
160+
).value[0].id
161+
126162
expectType<Ref<TodoI[]>>(useCollection<TodoI>(collection(db, 'todos')))
127163
expectType<Ref<TodoI[]>>(useCollection<TodoI>(collection(db, 'todos')).data)
128164
expectType<string>(

tests/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
QueryDocumentSnapshot,
2121
deleteDoc,
2222
DocumentReference,
23+
DocumentData,
2324
} from 'firebase/firestore'
2425
import { afterAll, beforeAll } from 'vitest'
2526
import { isCollectionRef, isDocumentRef } from '../src/shared'
@@ -64,7 +65,10 @@ export function setupFirestoreRefs() {
6465
// for automatically generated collections
6566
let collectionId = 0
6667
const collectionsToClean = new Set<CollectionReference<any>>()
67-
function _collection<T = unknown>(path?: string, ...pathSegments: string[]) {
68+
function _collection<T = DocumentData>(
69+
path?: string,
70+
...pathSegments: string[]
71+
) {
6872
path = path || `col_${collectionId++}`
6973

7074
const col = collection(forItemsRef, path, ...pathSegments)

0 commit comments

Comments
 (0)