@@ -14,33 +14,30 @@ import {
14
14
} from 'firebase/firestore'
15
15
import { expectType , setupFirestoreRefs , tds , firestore } from '../utils'
16
16
import { type Ref } from 'vue'
17
+ import {
18
+ VueFireQueryData ,
19
+ _InferReferenceType ,
20
+ _RefFirestore ,
21
+ } from '../../src/firestore'
17
22
18
23
describe ( 'Firestore collections' , ( ) => {
19
24
const { collection, query } = setupFirestoreRefs ( )
20
25
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
+
28
31
const wrapper = mount ( {
29
32
template : 'no' ,
30
33
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 }
40
37
} ,
41
38
} )
42
39
43
- return { wrapper, listRef }
40
+ return { wrapper, listRef, ... data }
44
41
}
45
42
46
43
function sortedList <
@@ -56,9 +53,12 @@ describe('Firestore collections', () => {
56
53
} )
57
54
}
58
55
56
+ // TODO: factory by default returns an unknown, but even then it should include the id
57
+
59
58
it ( 'starts the collection as an empty array' , async ( ) => {
60
- const { wrapper } = factory ( )
59
+ const { wrapper, data } = factory ( )
61
60
expect ( wrapper . vm . list ) . toEqual ( [ ] )
61
+ expect ( data . value ) . toEqual ( [ ] )
62
62
} )
63
63
64
64
it ( 'add items to the collection' , async ( ) => {
@@ -77,11 +77,11 @@ describe('Firestore collections', () => {
77
77
const { wrapper, listRef } = factory < { name : string } > ( )
78
78
79
79
const aRef = doc ( listRef )
80
- const a = await setDoc ( aRef , { name : 'a' } )
80
+ await setDoc ( aRef , { name : 'a' } )
81
81
const bRef = doc ( listRef )
82
- const b = await setDoc ( bRef , { name : 'b' } )
82
+ await setDoc ( bRef , { name : 'b' } )
83
83
const cRef = doc ( listRef )
84
- const c = await setDoc ( cRef , { name : 'c' } )
84
+ await setDoc ( cRef , { name : 'c' } )
85
85
86
86
await deleteDoc ( aRef )
87
87
expect ( wrapper . vm . list ) . toHaveLength ( 2 )
@@ -97,11 +97,11 @@ describe('Firestore collections', () => {
97
97
const { wrapper, listRef } = factory < { name : string } > ( )
98
98
99
99
const aRef = doc ( listRef )
100
- const a = await setDoc ( aRef , { name : 'a' } )
100
+ await setDoc ( aRef , { name : 'a' } )
101
101
const bRef = doc ( listRef )
102
- const b = await setDoc ( bRef , { name : 'b' } )
102
+ await setDoc ( bRef , { name : 'b' } )
103
103
const cRef = doc ( listRef )
104
- const c = await setDoc ( cRef , { name : 'c' } )
104
+ await setDoc ( cRef , { name : 'c' } )
105
105
106
106
await setDoc ( aRef , { name : 'aa' } )
107
107
await updateDoc ( cRef , { name : 'cc' } )
@@ -111,6 +111,23 @@ describe('Firestore collections', () => {
111
111
expect ( wrapper . vm . list ) . toContainEqual ( { name : 'cc' } )
112
112
} )
113
113
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
+
114
131
tds ( ( ) => {
115
132
interface TodoI {
116
133
text : string
@@ -123,6 +140,25 @@ describe('Firestore collections', () => {
123
140
// @ts -expect-error: document data by default
124
141
expectType < Ref < number [ ] > > ( useCollection ( collection ( db , 'todos' ) ) )
125
142
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
+
126
162
expectType < Ref < TodoI [ ] > > ( useCollection < TodoI > ( collection ( db , 'todos' ) ) )
127
163
expectType < Ref < TodoI [ ] > > ( useCollection < TodoI > ( collection ( db , 'todos' ) ) . data )
128
164
expectType < string > (
0 commit comments