@@ -48,7 +48,7 @@ describe('Database', () => {
48
48
describe ( 'Constructor' , ( ) => {
49
49
const invalidApps = [ null , NaN , 0 , 1 , true , false , '' , 'a' , [ ] , [ 1 , 'a' ] , { } , { a : 1 } , _ . noop ] ;
50
50
invalidApps . forEach ( ( invalidApp ) => {
51
- it ( `should throw given invalid app: ${ JSON . stringify ( invalidApp ) } ` , ( ) => {
51
+ it ( `should throw given invalid app: ${ JSON . stringify ( invalidApp ) } ` , ( ) => {
52
52
expect ( ( ) => {
53
53
const databaseAny : any = DatabaseService ;
54
54
return new databaseAny ( invalidApp ) ;
@@ -154,11 +154,8 @@ describe('Database', () => {
154
154
}` ;
155
155
const rulesPath = '.settings/rules.json' ;
156
156
157
- function callParamsForGet (
158
- strict = false ,
159
- url = `https://databasename.firebaseio.com/${ rulesPath } ` ,
160
- ) : HttpRequestConfig {
161
-
157
+ function callParamsForGet ( options ?: { strict ?: boolean ; url ?: string } ) : HttpRequestConfig {
158
+ const url = options ?. url || `https://databasename.firebaseio.com/${ rulesPath } ` ;
162
159
const params : HttpRequestConfig = {
163
160
method : 'GET' ,
164
161
url,
@@ -167,7 +164,7 @@ describe('Database', () => {
167
164
} ,
168
165
} ;
169
166
170
- if ( strict ) {
167
+ if ( options ?. strict ) {
171
168
params . data = { format : 'strict' } ;
172
169
}
173
170
@@ -215,7 +212,7 @@ describe('Database', () => {
215
212
return db . getRules ( ) . then ( ( result ) => {
216
213
expect ( result ) . to . equal ( rulesString ) ;
217
214
return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
218
- callParamsForGet ( false , `https://custom.firebaseio.com/${ rulesPath } ` ) ) ;
215
+ callParamsForGet ( { url : `https://custom.firebaseio.com/${ rulesPath } ` } ) ) ;
219
216
} ) ;
220
217
} ) ;
221
218
@@ -225,7 +222,7 @@ describe('Database', () => {
225
222
return db . getRules ( ) . then ( ( result ) => {
226
223
expect ( result ) . to . equal ( rulesString ) ;
227
224
return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
228
- callParamsForGet ( false , `http://localhost:9000/${ rulesPath } ?ns=foo` ) ) ;
225
+ callParamsForGet ( { url : `http://localhost:9000/${ rulesPath } ?ns=foo` } ) ) ;
229
226
} ) ;
230
227
} ) ;
231
228
@@ -259,7 +256,7 @@ describe('Database', () => {
259
256
return db . getRulesJSON ( ) . then ( ( result ) => {
260
257
expect ( result ) . to . deep . equal ( rules ) ;
261
258
return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
262
- callParamsForGet ( true ) ) ;
259
+ callParamsForGet ( { strict : true } ) ) ;
263
260
} ) ;
264
261
} ) ;
265
262
@@ -269,7 +266,7 @@ describe('Database', () => {
269
266
return db . getRulesJSON ( ) . then ( ( result ) => {
270
267
expect ( result ) . to . deep . equal ( rules ) ;
271
268
return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
272
- callParamsForGet ( true , `https://custom.firebaseio.com/${ rulesPath } ` ) ) ;
269
+ callParamsForGet ( { strict : true , url : `https://custom.firebaseio.com/${ rulesPath } ` } ) ) ;
273
270
} ) ;
274
271
} ) ;
275
272
@@ -279,7 +276,7 @@ describe('Database', () => {
279
276
return db . getRulesJSON ( ) . then ( ( result ) => {
280
277
expect ( result ) . to . deep . equal ( rules ) ;
281
278
return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
282
- callParamsForGet ( true , `http://localhost:9000/${ rulesPath } ?ns=foo` ) ) ;
279
+ callParamsForGet ( { strict : true , url : `http://localhost:9000/${ rulesPath } ?ns=foo` } ) ) ;
283
280
} ) ;
284
281
} ) ;
285
282
@@ -409,5 +406,101 @@ describe('Database', () => {
409
406
return db . setRules ( rules ) . should . eventually . be . rejectedWith ( 'network error' ) ;
410
407
} ) ;
411
408
} ) ;
409
+
410
+ describe ( 'emulator mode' , ( ) => {
411
+ interface EmulatorTestConfig {
412
+ name : string ;
413
+ setUp : ( ) => FirebaseApp ;
414
+ tearDown ?: ( ) => void ;
415
+ url : string ;
416
+ }
417
+
418
+ const configs : EmulatorTestConfig [ ] = [
419
+ {
420
+ name : 'with environment variable' ,
421
+ setUp : ( ) => {
422
+ process . env . FIREBASE_DATABASE_EMULATOR_HOST = 'localhost:9090' ;
423
+ return mocks . app ( ) ;
424
+ } ,
425
+ tearDown : ( ) => {
426
+ delete process . env . FIREBASE_DATABASE_EMULATOR_HOST ;
427
+ } ,
428
+ url : `http://localhost:9090/${ rulesPath } ?ns=databasename` ,
429
+ } ,
430
+ {
431
+ name : 'with app options' ,
432
+ setUp : ( ) => {
433
+ return mocks . appWithOptions ( {
434
+ databaseURL : 'http://localhost:9091?ns=databasename' ,
435
+ } ) ;
436
+ } ,
437
+ url : `http://localhost:9091/${ rulesPath } ?ns=databasename` ,
438
+ } ,
439
+ {
440
+ name : 'with environment variable overriding app options' ,
441
+ setUp : ( ) => {
442
+ process . env . FIREBASE_DATABASE_EMULATOR_HOST = 'localhost:9090' ;
443
+ return mocks . appWithOptions ( {
444
+ databaseURL : 'http://localhost:9091?ns=databasename' ,
445
+ } ) ;
446
+ } ,
447
+ tearDown : ( ) => {
448
+ delete process . env . FIREBASE_DATABASE_EMULATOR_HOST ;
449
+ } ,
450
+ url : `http://localhost:9090/${ rulesPath } ?ns=databasename` ,
451
+ } ,
452
+ ] ;
453
+
454
+ configs . forEach ( ( config ) => {
455
+ describe ( config . name , ( ) => {
456
+ let emulatorApp : FirebaseApp ;
457
+ let emulatorDatabase : DatabaseService ;
458
+
459
+ before ( ( ) => {
460
+ emulatorApp = config . setUp ( ) ;
461
+ emulatorDatabase = new DatabaseService ( emulatorApp ) ;
462
+ } ) ;
463
+
464
+ after ( ( ) => {
465
+ if ( config . tearDown ) {
466
+ config . tearDown ( ) ;
467
+ }
468
+
469
+ return emulatorDatabase . delete ( ) . then ( ( ) => {
470
+ return emulatorApp . delete ( ) ;
471
+ } ) ;
472
+ } ) ;
473
+
474
+ it ( 'getRules should connect to the emulator' , ( ) => {
475
+ const db : Database = emulatorDatabase . getDatabase ( ) ;
476
+ const stub = stubSuccessfulResponse ( rules ) ;
477
+ return db . getRules ( ) . then ( ( result ) => {
478
+ expect ( result ) . to . equal ( rulesString ) ;
479
+ return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
480
+ callParamsForGet ( { url : config . url } ) ) ;
481
+ } ) ;
482
+ } ) ;
483
+
484
+ it ( 'getRulesJSON should connect to the emulator' , ( ) => {
485
+ const db : Database = emulatorDatabase . getDatabase ( ) ;
486
+ const stub = stubSuccessfulResponse ( rules ) ;
487
+ return db . getRulesJSON ( ) . then ( ( result ) => {
488
+ expect ( result ) . to . equal ( rules ) ;
489
+ return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
490
+ callParamsForGet ( { strict : true , url : config . url } ) ) ;
491
+ } ) ;
492
+ } ) ;
493
+
494
+ it ( 'setRules should connect to the emulator' , ( ) => {
495
+ const db : Database = emulatorDatabase . getDatabase ( ) ;
496
+ const stub = stubSuccessfulResponse ( { } ) ;
497
+ return db . setRules ( rulesString ) . then ( ( ) => {
498
+ return expect ( stub ) . to . have . been . calledOnce . and . calledWith (
499
+ callParamsForPut ( rulesString , config . url ) ) ;
500
+ } ) ;
501
+ } ) ;
502
+ } ) ;
503
+ } ) ;
504
+ } ) ;
412
505
} ) ;
413
506
} ) ;
0 commit comments