Skip to content

Commit 32ed0a5

Browse files
committed
Adding app.module back in and writing tests for app injection
1 parent a77b21d commit 32ed0a5

File tree

12 files changed

+77
-36
lines changed

12 files changed

+77
-36
lines changed

src/auth/auth.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ describe('AngularFireAuth', () => {
8585
expect(afAuth.auth).toBeDefined();
8686
});
8787

88+
it('should have an initialized Firebase app', () => {
89+
expect(afAuth.auth.app).toBeDefined();
90+
expect(afAuth.auth.app).toEqual(app);
91+
});
92+
8893
it('should emit auth updates through authState', (done: any) => {
8994
let count = 0;
9095

@@ -156,6 +161,11 @@ describe('AngularFireAuth with different app', () => {
156161
expect(afAuth instanceof AngularFireAuth).toEqual(true);
157162
});
158163

164+
it('should have an initialized Firebase app', () => {
165+
expect(afAuth.auth.app).toBeDefined();
166+
expect(afAuth.auth.app).toEqual(app);
167+
});
168+
159169
it('should have an initialized Firebase app instance member', () => {
160170
expect(afAuth.auth.app.name).toEqual(FIREBASE_APP_NAME_TOO);
161171
});

src/auth/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Injectable, Inject, Optional, NgZone } from '@angular/core';
44
import { Observable } from 'rxjs/Observable';
55
import { observeOn } from 'rxjs/operator/observeOn';
66

7-
import { FirebaseAppConfig, FirebaseAppName, firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
7+
import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';
88

99
import 'rxjs/add/operator/switchMap';
1010
import 'rxjs/add/observable/of';
@@ -32,7 +32,7 @@ export class AngularFireAuth {
3232
@Inject(FirebaseAppConfig) config:FirebaseOptions,
3333
@Optional() @Inject(FirebaseAppName) name:string
3434
) {
35-
const app = firebaseAppFactory(config, name);
35+
const app = _firebaseAppFactory(config, name);
3636
this.auth = app.auth!();
3737

3838
const authStateZone = new NgZone({});

src/core/angularfire2.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { InjectionToken, NgZone, NgModule } from '@angular/core';
1+
import { InjectionToken, NgZone } from '@angular/core';
22
import { Subscription } from 'rxjs/Subscription';
33
import { Scheduler } from 'rxjs/Scheduler';
44
import { queue } from 'rxjs/scheduler/queue';
55

66
import firebase from '@firebase/app';
77
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';
88

9-
export function firebaseAppFactory(config: FirebaseOptions, name?: string): FirebaseApp {
9+
export function _firebaseAppFactory(config: FirebaseOptions, name?: string): FirebaseApp {
1010
const appName = name || '[DEFAULT]';
1111
const existingApp = firebase.apps.filter(app => app.name == appName)[0];
1212
return existingApp || firebase.initializeApp(config, appName);
@@ -15,30 +15,9 @@ export function firebaseAppFactory(config: FirebaseOptions, name?: string): Fire
1515
export const FirebaseAppName = new InjectionToken<string>('angularfire2.appName');
1616
export const FirebaseAppConfig = new InjectionToken<FirebaseOptions>('angularfire2.config');
1717

18-
// Put in database.ts when we dropped depreciated
18+
// Put in database.ts when we drop database-depreciated
1919
export const RealtimeDatabaseURL = new InjectionToken<string>('angularfire2.realtimeDatabaseURL');
2020

21-
export const FirebaseAppProvider = {
22-
provide: FirebaseApp,
23-
useFactory: firebaseAppFactory,
24-
deps: [ FirebaseAppConfig, FirebaseAppName ]
25-
};
26-
27-
@NgModule({
28-
providers: [ FirebaseAppProvider ],
29-
})
30-
export class AngularFireModule {
31-
static initializeApp(config: FirebaseOptions, appName?: string) {
32-
return {
33-
ngModule: AngularFireModule,
34-
providers: [
35-
{ provide: FirebaseAppConfig, useValue: config },
36-
{ provide: FirebaseAppName, useValue: appName }
37-
]
38-
}
39-
}
40-
}
41-
4221
export class FirebaseZoneScheduler {
4322
constructor(public zone: NgZone) {}
4423
schedule(...args: any[]): Subscription {

src/core/firebase.app.module.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { InjectionToken, NgZone, NgModule } from '@angular/core';
2+
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';
3+
4+
import { _firebaseAppFactory, FirebaseAppConfig, FirebaseAppName } from './angularfire2';
5+
6+
const FirebaseAppProvider = {
7+
provide: FirebaseApp,
8+
useFactory: _firebaseAppFactory,
9+
deps: [ FirebaseAppConfig, FirebaseAppName ]
10+
};
11+
12+
@NgModule({
13+
providers: [ FirebaseAppProvider ],
14+
})
15+
export class AngularFireModule {
16+
static initializeApp(config: FirebaseOptions, appName?: string) {
17+
return {
18+
ngModule: AngularFireModule,
19+
providers: [
20+
{ provide: FirebaseAppConfig, useValue: config },
21+
{ provide: FirebaseAppName, useValue: appName }
22+
]
23+
}
24+
}
25+
}

src/core/public_api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './angularfire2';
1+
export * from './angularfire2';
2+
export * from './firebase.app.module';

src/database-deprecated/database.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { FirebaseObjectFactory } from './firebase_object_factory';
88
import { FirebaseObjectObservable } from './firebase_object_observable';
99
import * as utils from './utils';
1010
import { FirebaseOptions } from '@firebase/app-types';
11-
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, firebaseAppFactory } from 'angularfire2';
11+
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, _firebaseAppFactory } from 'angularfire2';
1212

1313
@Injectable()
1414
export class AngularFireDatabase {
@@ -23,8 +23,8 @@ export class AngularFireDatabase {
2323
@Optional() @Inject(FirebaseAppName) name:string,
2424
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string
2525
) {
26-
const app = firebaseAppFactory(config, name);
27-
this.database = app.database!(databaseURL);
26+
const app = _firebaseAppFactory(config, name);
27+
this.database = app.database!(databaseURL || undefined);
2828
}
2929

3030
list(pathOrRef: PathReference, opts?:FirebaseListFactoryOpts):FirebaseListObservable<any[]> {

src/database/database.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ describe('AngularFireDatabase', () => {
3434
expect(db instanceof AngularFireDatabase).toEqual(true);
3535
});
3636

37+
it('should have an initialized Firebase app', () => {
38+
expect(db.database.app).toBeDefined();
39+
expect(db.database.app).toEqual(app);
40+
});
41+
3742
it('should accept a Firebase App in the constructor', () => {
3843
const __db = new AngularFireDatabase(app.options, app.name, null!);
3944
expect(__db instanceof AngularFireDatabase).toEqual(true);
@@ -83,6 +88,11 @@ describe('AngularFireDatabase w/options', () => {
8388
expect(db instanceof AngularFireDatabase).toEqual(true);
8489
});
8590

91+
it('should have an initialized Firebase app', () => {
92+
expect(db.database.app).toBeDefined();
93+
expect(db.database.app).toEqual(app);
94+
});
95+
8696
it('should have an initialized Firebase app instance member', () => {
8797
expect(db.database.app.name).toEqual(FIREBASE_APP_NAME_TOO);
8898
});

src/database/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { InjectionToken } from '@angular/core';
66
import { FirebaseApp, FirebaseOptions } from '@firebase/app-types';
77
import { createListReference } from './list/create-reference';
88
import { createObjectReference } from './object/create-reference';
9-
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, firebaseAppFactory } from 'angularfire2';
9+
import { FirebaseAppConfig, FirebaseAppName, RealtimeDatabaseURL, _firebaseAppFactory } from 'angularfire2';
1010

1111
@Injectable()
1212
export class AngularFireDatabase {
@@ -17,7 +17,7 @@ export class AngularFireDatabase {
1717
@Optional() @Inject(FirebaseAppName) name:string,
1818
@Optional() @Inject(RealtimeDatabaseURL) databaseURL:string
1919
) {
20-
const app = firebaseAppFactory(config, name);
20+
const app = _firebaseAppFactory(config, name);
2121
this.database = app.database!(databaseURL || undefined);
2222
}
2323

src/firestore/firestore.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('AngularFirestore', () => {
4545

4646
it('should have an initialized Firebase app', () => {
4747
expect(afs.firestore.app).toBeDefined();
48+
expect(afs.firestore.app).toEqual(app);
4849
});
4950

5051
it('should create an AngularFirestoreDocument', () => {
@@ -113,6 +114,11 @@ describe('AngularFirestore with different app', () => {
113114
expect(afs instanceof AngularFirestore).toEqual(true);
114115
});
115116

117+
it('should have an initialized Firebase app', () => {
118+
expect(afs.firestore.app).toBeDefined();
119+
expect(afs.firestore.app).toEqual(app);
120+
});
121+
116122
it('should have an initialized Firebase app instance member', () => {
117123
expect(afs.firestore.app.name).toEqual(FIREBASE_APP_NAME_TOO);
118124
});

src/firestore/firestore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { QueryFn, AssociatedReference } from './interfaces';
1212
import { AngularFirestoreDocument } from './document/document';
1313
import { AngularFirestoreCollection } from './collection/collection';
1414

15-
import { FirebaseAppConfig, FirebaseAppName, firebaseAppFactory } from 'angularfire2';
15+
import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory } from 'angularfire2';
1616

1717
/**
1818
* The value of this token determines whether or not the firestore will have persistance enabled
@@ -109,7 +109,7 @@ export class AngularFirestore {
109109
@Optional() @Inject(FirebaseAppName) name:string,
110110
@Optional() @Inject(EnablePersistenceToken) shouldEnablePersistence: boolean
111111
) {
112-
const app = firebaseAppFactory(config, name);
112+
const app = _firebaseAppFactory(config, name);
113113
this.firestore = app.firestore!();
114114

115115
this.persistenceEnabled$ = shouldEnablePersistence ?

src/storage/storage.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ describe('AngularFireStorage', () => {
3535
expect(afStorage.storage).toBeDefined();
3636
});
3737

38+
it('should have an initialized Firebase app', () => {
39+
expect(afStorage.storage.app).toBeDefined();
40+
expect(afStorage.storage.app).toEqual(app);
41+
});
42+
3843
describe('upload task', () => {
3944

4045
it('should upload and delete a file', (done) => {
@@ -156,6 +161,11 @@ describe('AngularFireStorage w/options', () => {
156161
expect(afStorage.storage).toBeDefined();
157162
});
158163

164+
it('should have an initialized Firebase app', () => {
165+
expect(afStorage.storage.app).toBeDefined();
166+
expect(afStorage.storage.app).toEqual(app);
167+
});
168+
159169
it('should be hooked up the right app', () => {
160170
expect(afStorage.storage.app.name).toEqual(FIREBASE_APP_NAME_TOO);
161171
});

src/storage/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FirebaseStorage, UploadMetadata } from '@firebase/storage-types';
33
import { createStorageRef, AngularFireStorageReference } from './ref';
44
import { createUploadTask, AngularFireUploadTask } from './task';
55
import { Observable } from 'rxjs/Observable';
6-
import { FirebaseAppConfig, FirebaseAppName, firebaseAppFactory } from 'angularfire2';
6+
import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory } from 'angularfire2';
77
import { FirebaseOptions } from '@firebase/app-types';
88

99
export const StorageBucket = new InjectionToken<string>('angularfire2.storageBucket');
@@ -24,7 +24,7 @@ export class AngularFireStorage {
2424
@Optional() @Inject(FirebaseAppName) name:string,
2525
@Optional() @Inject(StorageBucket) storageBucket:string
2626
) {
27-
const app = firebaseAppFactory(config, name);
27+
const app = _firebaseAppFactory(config, name);
2828
this.storage = app.storage!(storageBucket || undefined);
2929
}
3030

0 commit comments

Comments
 (0)