1
1
import * as firebase from 'firebase/app' ;
2
- import { TestBed , inject } from '@angular/core/testing' ;
3
- import { ReflectiveInjector , Provider } from '@angular/core' ;
2
+ import { TestBed , inject , withModule , async } from '@angular/core/testing' ;
3
+ import { ReflectiveInjector , Provider , PlatformRef , NgModule , Compiler , ApplicationRef , CompilerFactory } from '@angular/core' ;
4
4
import { FirebaseApp , FirebaseAppConfig , AngularFireModule } from './angularfire2' ;
5
5
import { Subscription } from 'rxjs/Subscription' ;
6
6
import { COMMON_CONFIG } from './test-config' ;
7
+ import { BrowserModule } from '@angular/platform-browser' ;
7
8
8
9
describe ( 'angularfire' , ( ) => {
9
10
let subscription :Subscription ;
10
11
let app : FirebaseApp ;
11
12
let rootRef : firebase . database . Reference ;
12
13
let questionsRef : firebase . database . Reference ;
13
14
let listOfQuestionsRef : firebase . database . Reference ;
15
+ let defaultPlatform : PlatformRef ;
16
+
14
17
const APP_NAME = 'super-awesome-test-firebase-app-name' ;
15
18
16
19
beforeEach ( ( ) => {
@@ -19,11 +22,12 @@ describe('angularfire', () => {
19
22
imports : [ AngularFireModule . initializeApp ( COMMON_CONFIG , APP_NAME ) ]
20
23
} ) ;
21
24
22
- inject ( [ FirebaseApp ] , ( _app : FirebaseApp ) => {
25
+ inject ( [ FirebaseApp , PlatformRef ] , ( _app : FirebaseApp , _platform : PlatformRef ) => {
23
26
app = _app ;
24
27
rootRef = app . database ( ) . ref ( ) ;
25
28
questionsRef = rootRef . child ( 'questions' ) ;
26
29
listOfQuestionsRef = rootRef . child ( 'list-of-questions' ) ;
30
+ defaultPlatform = _platform ;
27
31
} ) ( ) ;
28
32
29
33
} ) ;
@@ -43,5 +47,28 @@ describe('angularfire', () => {
43
47
it ( 'should have the provided name' , ( ) => {
44
48
expect ( app . name ) . toBe ( APP_NAME ) ;
45
49
} )
50
+ it ( 'should use an already intialized firebase app if it exists' , done => {
51
+ @NgModule ( {
52
+ imports : [
53
+ AngularFireModule . initializeApp ( COMMON_CONFIG , APP_NAME ) ,
54
+ BrowserModule
55
+ ] } )
56
+ class MyModule {
57
+ ngDoBootstrap ( ) { }
58
+ }
59
+
60
+ const compilerFactory : CompilerFactory =
61
+ defaultPlatform . injector . get ( CompilerFactory , null ) ;
62
+ const moduleFactory = compilerFactory . createCompiler ( ) . compileModuleSync ( MyModule ) ;
63
+
64
+ defaultPlatform . bootstrapModuleFactory ( moduleFactory )
65
+ . then ( moduleRef => {
66
+ const ref = moduleRef . injector . get ( FirebaseApp ) ;
67
+ expect ( ref . name ) . toEqual ( app . name ) ;
68
+ } ) . then ( done , e => {
69
+ fail ( e ) ;
70
+ done ( )
71
+ } ) ;
72
+ } )
46
73
} ) ;
47
74
} ) ;
0 commit comments