1
- import {
2
- DocumentSnapshot ,
3
- GeoPoint ,
4
- getDoc ,
5
- Timestamp ,
6
- } from 'firebase/firestore'
1
+ import { GeoPoint , getDoc , Timestamp } from 'firebase/firestore'
7
2
import { beforeEach , describe , expect , it } from 'vitest'
8
3
import {
9
4
extractRefs ,
@@ -12,41 +7,38 @@ import {
12
7
import { setupFirestoreRefs } from '../utils'
13
8
14
9
describe ( 'Firestore and Database utils' , ( ) => {
15
- const { collection, doc, setDoc , updateDoc , addDoc } = setupFirestoreRefs ( )
10
+ const { collection, doc, addDoc } = setupFirestoreRefs ( )
16
11
17
- const docRef = doc ( )
18
12
const collectionRef = collection ( )
19
13
20
- const docData = {
21
- n : 42 ,
22
- is : true ,
23
- items : [ { text : 'foo' } ] ,
24
- ref : docRef ,
25
- }
26
-
27
- let snapshot ! : DocumentSnapshot < any >
14
+ let docRef = doc ( )
28
15
beforeEach ( async ( ) => {
29
- await setDoc ( docRef , {
30
- // TODO: ref of collection
31
- // collection,
32
- data : { } ,
33
- index : 0 ,
34
- } )
35
- const forSnapshot = await addDoc ( collectionRef , { ...docData } )
36
- snapshot = await getDoc (
37
- forSnapshot . withConverter ( firestoreDefaultConverter )
38
- )
16
+ // fresh document for each test
17
+ docRef = doc ( )
39
18
} )
40
19
41
- it ( 'createSnapshot adds an id' , ( ) => {
42
- expect ( snapshot . data ( ) . id ) . not . toBeFalsy ( )
20
+ async function addDocToCollection ( ) {
21
+ const forSnapshot = await addDoc ( collectionRef , {
22
+ n : 42 ,
23
+ is : true ,
24
+ items : [ { text : 'foo' } ] ,
25
+ ref : docRef ,
26
+ } )
27
+ return await getDoc ( forSnapshot . withConverter ( firestoreDefaultConverter ) )
28
+ }
29
+
30
+ it ( 'createSnapshot adds an id' , async ( ) => {
31
+ const snapshot = await addDocToCollection ( )
32
+ expect ( snapshot . data ( ) ?. id ) . not . toBeFalsy ( )
43
33
} )
44
34
45
- it ( 'id is not enumerable' , ( ) => {
46
- expect ( Object . keys ( snapshot . data ( ) ) . includes ( 'id' ) ) . toBe ( false )
35
+ it ( 'id is not enumerable' , async ( ) => {
36
+ const snapshot = await addDocToCollection ( )
37
+ expect ( Object . keys ( snapshot . data ( ) ?? { } ) . includes ( 'id' ) ) . toBe ( false )
47
38
} )
48
39
49
- it ( 'contains all the data' , ( ) => {
40
+ it ( 'contains all the data' , async ( ) => {
41
+ const snapshot = await addDocToCollection ( )
50
42
expect ( snapshot . data ( ) ) . toEqual ( {
51
43
n : 42 ,
52
44
is : true ,
@@ -56,7 +48,16 @@ describe('Firestore and Database utils', () => {
56
48
} )
57
49
58
50
it ( 'extracts refs from documents' , async ( ) => {
59
- const [ noRefsDoc , refs ] = extractRefs ( docData , undefined , { } )
51
+ const [ noRefsDoc , refs ] = extractRefs (
52
+ {
53
+ n : 42 ,
54
+ is : true ,
55
+ items : [ { text : 'foo' } ] ,
56
+ ref : docRef ,
57
+ } ,
58
+ undefined ,
59
+ { }
60
+ )
60
61
expect ( noRefsDoc . ref ) . toBe ( docRef . path )
61
62
expect ( refs ) . toEqual ( {
62
63
ref : docRef ,
0 commit comments