Skip to content

Commit 0f90f9b

Browse files
authored
Merge pull request angular#1021 from Kamshak/patch-1
fix(FirebaseApp): Return app if it exists
2 parents ce5297a + d8ba8de commit 0f90f9b

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

rollup-globals.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export default (mod) => {
77

88
if (mod === 'firebase') return 'firebase';
99
if (mod === '@angular/core') return 'ng.core';
10+
if (mod === '@angular/platform-browser') return 'ng.platformBrowser';
1011
if (mod === '@angular/core/testing') return 'ng.core.testing';
1112
}

src/angularfire2.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
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';
44
import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from './angularfire2';
55
import { Subscription } from 'rxjs/Subscription';
66
import { COMMON_CONFIG } from './test-config';
7+
import { BrowserModule } from '@angular/platform-browser';
78

89
describe('angularfire', () => {
910
let subscription:Subscription;
1011
let app: FirebaseApp;
1112
let rootRef: firebase.database.Reference;
1213
let questionsRef: firebase.database.Reference;
1314
let listOfQuestionsRef: firebase.database.Reference;
15+
let defaultPlatform: PlatformRef;
16+
1417
const APP_NAME = 'super-awesome-test-firebase-app-name';
1518

1619
beforeEach(() => {
@@ -19,11 +22,12 @@ describe('angularfire', () => {
1922
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, APP_NAME)]
2023
});
2124

22-
inject([FirebaseApp], (_app: FirebaseApp) => {
25+
inject([FirebaseApp, PlatformRef], (_app: FirebaseApp, _platform: PlatformRef) => {
2326
app = _app;
2427
rootRef = app.database().ref();
2528
questionsRef = rootRef.child('questions');
2629
listOfQuestionsRef = rootRef.child('list-of-questions');
30+
defaultPlatform = _platform;
2731
})();
2832

2933
});
@@ -43,5 +47,28 @@ describe('angularfire', () => {
4347
it('should have the provided name', () => {
4448
expect(app.name).toBe(APP_NAME);
4549
})
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+
})
4673
});
4774
});

src/app/firebase.app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export function _firebaseAppFactory(config: FirebaseAppConfig, appName?: string)
2323
}
2424
}
2525
catch (e) {
26+
if (e.code === "app/duplicate-app") {
27+
return firebase.app(e.name);
28+
}
29+
2630
return firebase.app(null);
2731
}
2832
}

0 commit comments

Comments
 (0)