1
1
import { initializeApp } from 'firebase/app'
2
+ import {
3
+ connectDatabaseEmulator ,
4
+ getDatabase ,
5
+ ref ,
6
+ query as databaseQuery ,
7
+ orderByChild ,
8
+ remove ,
9
+ } from 'firebase/database'
2
10
import {
3
11
getFirestore ,
4
12
connectFirestoreEmulator ,
5
13
collection ,
6
14
doc ,
7
- query ,
15
+ query as firestoreQuery ,
8
16
orderBy ,
9
17
CollectionReference ,
10
18
getDocsFromServer ,
11
19
QueryDocumentSnapshot ,
12
20
deleteDoc ,
13
21
} from 'firebase/firestore'
14
- import { afterAll } from 'vitest'
22
+ import { beforeAll } from 'vitest'
15
23
import { isCollectionRef , isDocumentRef } from '../src/shared'
16
24
17
25
export const firebaseApp = initializeApp ( { projectId : 'vue-fire-store' } )
18
26
export const firestore = getFirestore ( firebaseApp )
27
+ export const database = getDatabase ( firebaseApp )
28
+
19
29
connectFirestoreEmulator ( firestore , 'localhost' , 8080 )
30
+ connectDatabaseEmulator ( database , 'localhost' , 8081 )
20
31
21
32
let _id = 0
22
- export function setupRefs ( ) {
33
+
34
+ // Firestore
35
+ export function setupFirestoreRefs ( ) {
23
36
const testId = _id ++
24
37
const testsCollection = collection ( firestore , `__tests` )
25
38
const itemRef = doc ( testsCollection , `item:${ testId } ` )
26
39
const forItemsRef = doc ( testsCollection , `forItems:${ testId } ` )
27
40
28
41
const listRef = collection ( forItemsRef , 'list' )
29
- const orderedListRef = query ( listRef , orderBy ( 'name' ) )
42
+ const orderedListRef = firestoreQuery ( listRef , orderBy ( 'name' ) )
30
43
31
- afterAll ( async ( ) => {
44
+ beforeAll ( async ( ) => {
32
45
// clean up the tests data
33
46
await Promise . all ( [
34
47
deleteDoc ( itemRef ) ,
@@ -40,7 +53,7 @@ export function setupRefs() {
40
53
return { itemRef, listRef, orderedListRef, testId, col : forItemsRef }
41
54
}
42
55
43
- export async function clearCollection ( collection : CollectionReference ) {
56
+ async function clearCollection ( collection : CollectionReference ) {
44
57
const { docs } = await getDocsFromServer ( collection )
45
58
await Promise . all (
46
59
docs . map ( doc => {
@@ -49,7 +62,7 @@ export async function clearCollection(collection: CollectionReference) {
49
62
)
50
63
}
51
64
52
- export async function recursiveDeleteDoc ( doc : QueryDocumentSnapshot ) {
65
+ async function recursiveDeleteDoc ( doc : QueryDocumentSnapshot ) {
53
66
const docData = doc . data ( )
54
67
const promises : Promise < any > [ ] = [ ]
55
68
if ( docData ) {
@@ -65,6 +78,28 @@ export async function recursiveDeleteDoc(doc: QueryDocumentSnapshot) {
65
78
return Promise . all ( promises )
66
79
}
67
80
81
+ // Database
82
+ export function setupDatabaseRefs ( ) {
83
+ const testId = _id ++
84
+ const testsCollection = ref ( database , `__tests_${ testId } ` )
85
+
86
+ const itemRef = ref ( database , testsCollection . key + `/item` )
87
+ const listRef = ref ( database , testsCollection . key + `/items` )
88
+ const orderedListRef = databaseQuery ( listRef , orderByChild ( 'name' ) )
89
+
90
+ beforeAll ( async ( ) => {
91
+ // clean up the tests data
92
+ await remove ( testsCollection )
93
+ } )
94
+
95
+ function databaseRef ( path : string ) {
96
+ return ref ( database , testsCollection . key + '/' + path )
97
+ }
98
+
99
+ return { itemRef, listRef, orderedListRef, testId, databaseRef }
100
+ }
101
+
102
+ // General utils
68
103
export const sleep = ( ms : number ) =>
69
104
new Promise ( resolve => setTimeout ( resolve , ms ) )
70
105
0 commit comments