15
15
*/
16
16
17
17
import * as admin from '../../lib/index' ;
18
+ import { App , deleteApp , getApp , initializeApp } from '../../lib/app/index' ;
19
+ import { getAuth } from '../../lib/auth/index' ;
18
20
import { expect } from 'chai' ;
19
21
import {
20
22
defaultApp , nullApp , nonNullApp , databaseUrl , projectId , storageBucket ,
@@ -27,30 +29,56 @@ describe('admin', () => {
27
29
expect ( storageBucket ) . to . be . not . empty ;
28
30
} ) ;
29
31
30
- it ( 'does not load RTDB by default' , ( ) => {
31
- const firebaseRtdb = require . cache [ require . resolve ( '@firebase/database' ) ] ;
32
- expect ( firebaseRtdb ) . to . be . undefined ;
33
- const rtdbInternal = require . cache [ require . resolve ( '../../lib/database/database-internal' ) ] ;
34
- expect ( rtdbInternal ) . to . be . undefined ;
35
- } ) ;
32
+ describe ( 'Dependency lazy loading' , ( ) => {
33
+ const tempCache : { [ key : string ] : any } = { } ;
34
+ const dependencies = [ '@firebase/database' , '@google-cloud/firestore' ] ;
35
+ let lazyLoadingApp : App ;
36
36
37
- it ( 'loads RTDB when calling admin.database' , ( ) => {
38
- const rtdbNamespace = admin . database ;
39
- expect ( rtdbNamespace ) . to . not . be . null ;
40
- const firebaseRtdb = require . cache [ require . resolve ( '@firebase/database' ) ] ;
41
- expect ( firebaseRtdb ) . to . not . be . undefined ;
42
- } ) ;
37
+ before ( ( ) => {
38
+ // Unload dependencies if already loaded. Some of the other test files have imports
39
+ // to firebase-admin/database and firebase-admin/firestore, which cause the corresponding
40
+ // dependencies to get loaded before the tests are executed.
41
+ dependencies . forEach ( ( name ) => {
42
+ const resolvedName = require . resolve ( name ) ;
43
+ tempCache [ name ] = require . cache [ resolvedName ] ;
44
+ delete require . cache [ resolvedName ] ;
45
+ } ) ;
43
46
44
- it ( 'does not load Firestore by default' , ( ) => {
45
- const gcloud = require . cache [ require . resolve ( '@google-cloud/firestore' ) ] ;
46
- expect ( gcloud ) . to . be . undefined ;
47
- } ) ;
47
+ // Initialize the SDK
48
+ lazyLoadingApp = initializeApp ( defaultApp . options , 'lazyLoadingApp' ) ;
49
+ } ) ;
50
+
51
+ it ( 'does not load RTDB by default' , ( ) => {
52
+ const firebaseRtdb = require . cache [ require . resolve ( '@firebase/database' ) ] ;
53
+ expect ( firebaseRtdb ) . to . be . undefined ;
54
+ } ) ;
55
+
56
+ it ( 'loads RTDB when calling admin.database' , ( ) => {
57
+ const rtdbNamespace = admin . database ;
58
+ expect ( rtdbNamespace ) . to . not . be . null ;
59
+ const firebaseRtdb = require . cache [ require . resolve ( '@firebase/database' ) ] ;
60
+ expect ( firebaseRtdb ) . to . not . be . undefined ;
61
+ } ) ;
48
62
49
- it ( 'loads Firestore when calling admin.firestore' , ( ) => {
50
- const firestoreNamespace = admin . firestore ;
51
- expect ( firestoreNamespace ) . to . not . be . null ;
52
- const gcloud = require . cache [ require . resolve ( '@google-cloud/firestore' ) ] ;
53
- expect ( gcloud ) . to . not . be . undefined ;
63
+ it ( 'does not load Firestore by default' , ( ) => {
64
+ const gcloud = require . cache [ require . resolve ( '@google-cloud/firestore' ) ] ;
65
+ expect ( gcloud ) . to . be . undefined ;
66
+ } ) ;
67
+
68
+ it ( 'loads Firestore when calling admin.firestore' , ( ) => {
69
+ const firestoreNamespace = admin . firestore ;
70
+ expect ( firestoreNamespace ) . to . not . be . null ;
71
+ const gcloud = require . cache [ require . resolve ( '@google-cloud/firestore' ) ] ;
72
+ expect ( gcloud ) . to . not . be . undefined ;
73
+ } ) ;
74
+
75
+ after ( ( ) => {
76
+ dependencies . forEach ( ( name ) => {
77
+ const resolvedName = require . resolve ( name ) ;
78
+ require . cache [ resolvedName ] = tempCache [ name ] ;
79
+ } ) ;
80
+ return deleteApp ( lazyLoadingApp ) ;
81
+ } )
54
82
} ) ;
55
83
} ) ;
56
84
@@ -98,3 +126,42 @@ describe('admin.app', () => {
98
126
expect ( admin . storage ( app ) . app ) . to . deep . equal ( app ) ;
99
127
} ) ;
100
128
} ) ;
129
+
130
+ describe ( 'getApp' , ( ) => {
131
+ it ( 'getApp() returns the default App' , ( ) => {
132
+ const app = getApp ( ) ;
133
+ expect ( app ) . to . deep . equal ( defaultApp ) ;
134
+ expect ( app . name ) . to . equal ( '[DEFAULT]' ) ;
135
+ expect ( app . options . databaseURL ) . to . equal ( databaseUrl ) ;
136
+ expect ( app . options . databaseAuthVariableOverride ) . to . be . undefined ;
137
+ expect ( app . options . storageBucket ) . to . equal ( storageBucket ) ;
138
+ } ) ;
139
+
140
+ it ( 'getApp("null") returns the App named "null"' , ( ) => {
141
+ const app = getApp ( 'null' ) ;
142
+ expect ( app ) . to . deep . equal ( nullApp ) ;
143
+ expect ( app . name ) . to . equal ( 'null' ) ;
144
+ expect ( app . options . databaseURL ) . to . equal ( databaseUrl ) ;
145
+ expect ( app . options . databaseAuthVariableOverride ) . to . be . null ;
146
+ expect ( app . options . storageBucket ) . to . equal ( storageBucket ) ;
147
+ } ) ;
148
+
149
+ it ( 'getApp("nonNull") returns the App named "nonNull"' , ( ) => {
150
+ const app = getApp ( 'nonNull' ) ;
151
+ expect ( app ) . to . deep . equal ( nonNullApp ) ;
152
+ expect ( app . name ) . to . equal ( 'nonNull' ) ;
153
+ expect ( app . options . databaseURL ) . to . equal ( databaseUrl ) ;
154
+ expect ( ( app . options . databaseAuthVariableOverride as any ) . uid ) . to . be . ok ;
155
+ expect ( app . options . storageBucket ) . to . equal ( storageBucket ) ;
156
+ } ) ;
157
+
158
+ it ( 'namespace services are attached to the default App' , ( ) => {
159
+ const app = getApp ( ) ;
160
+ expect ( getAuth ( app ) . app ) . to . deep . equal ( app ) ;
161
+ } ) ;
162
+
163
+ it ( 'namespace services are attached to the named App' , ( ) => {
164
+ const app = getApp ( 'null' ) ;
165
+ expect ( getAuth ( app ) . app ) . to . deep . equal ( app ) ;
166
+ } ) ;
167
+ } ) ;
0 commit comments