Skip to content

Commit ce7412c

Browse files
committed
Putting together tests using the emulator
1 parent 5ab4f64 commit ce7412c

30 files changed

+509
-186
lines changed

.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "angularfire2-test"
4+
}
5+
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ yarn-error.log
1515
*.bak
1616
package-lock.json
1717
tools/build.js
18-
coverage
18+
coverage
19+
*.log

firebase.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"database": {
3+
"rules": "test/database.rules.json"
4+
},
5+
"firestore": {
6+
"rules": "test/firestore.rules",
7+
"indexes": "test/firestore.indexes.json"
8+
},
9+
"storage": {
10+
"rules": "test/storage.rules"
11+
}
12+
}

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"description": "The official library of Firebase and Angular.",
55
"private": true,
66
"scripts": {
7-
"test": "npx ng test",
8-
"test:headless": "npx ng test --watch=false --browsers=ChromeHeadless",
7+
"test": "npm run test:node",
8+
"test:chrome": "npx firebase emulators:exec --project=angularfire2-test \"npx ng test --watch=false --browsers=Chrome\"",
9+
"test:chrome-headless": "npx firebase emulators:exec --project=angularfire2-test \"npx ng test --watch=false --browsers=ChromeHeadless\"",
910
"lint": "npx ng lint",
10-
"test:node": "npx tsc -p tsconfig.jasmine.json && node -r tsconfig-paths/register ./tools/jasmine.js",
11+
"test:node": "npx tsc -p tsconfig.jasmine.json; cp ./dist/packages-dist/schematics/versions.json ./dist/out-tsc/jasmine/schematics && npx firebase emulators:exec --project=angularfire2-test \"node -r tsconfig-paths/register ./tools/jasmine.js\"",
1112
"test:typings": "node ./tools/run-typings-test.js",
1213
"test:build": "bash ./test/ng-build/build.sh",
1314
"test:universal": "cp -R dist/packages-dist test/universal-test/node_modules/angularfire2 && cd test/universal-test && npm run prerender",
14-
"test:all": "npm run test:headless && npm run test:node && npm run test:typings && npm run test:build",
15+
"test:all": "npm run test:node && npm run test:chrome-headless && npm run test:typings && npm run test:build",
1516
"build": "tsc tools/build.ts; node ./tools/build.js",
1617
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
1718
},
@@ -63,10 +64,12 @@
6364
"@angular/cli": "^8.0.3",
6465
"@angular/compiler-cli": ">=6.0.0 <9 || ^9.0.0-0",
6566
"@angular/platform-server": ">=6.0.0 <9 || ^9.0.0-0",
67+
"@firebase/testing": "^0.11.5",
6668
"@types/fs-extra": "^7.0.0",
6769
"@types/gzip-size": "^5.1.1",
6870
"@types/inquirer": "^0.0.44",
6971
"@types/jasmine": "^3.3.13",
72+
"@types/node": "^12.6.2",
7073
"@types/request": "0.0.30",
7174
"codelyzer": "^5.0.0",
7275
"concurrently": "^2.2.0",

src/auth-guard/auth-guard.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('AngularFireAuthGuard', () => {
1313
beforeEach(() => {
1414
TestBed.configureTestingModule({
1515
imports: [
16-
AngularFireModule.initializeApp(COMMON_CONFIG),
16+
AngularFireModule.initializeTestApp(COMMON_CONFIG),
1717
AngularFireAuthModule,
1818
AngularFireAuthGuardModule,
1919
RouterModule.forRoot([

src/auth/auth.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('AngularFireAuth', () => {
2828
beforeEach(() => {
2929
TestBed.configureTestingModule({
3030
imports: [
31-
AngularFireModule.initializeApp(COMMON_CONFIG),
31+
AngularFireModule.initializeTestApp(COMMON_CONFIG),
3232
AngularFireAuthModule
3333
]
3434
});
@@ -83,7 +83,6 @@ describe('AngularFireAuth', () => {
8383

8484
it('should have an initialized Firebase app', () => {
8585
expect(afAuth.auth.app).toBeDefined();
86-
expect(<any>afAuth.auth.app).toEqual(app);
8786
});
8887

8988
it('should emit auth updates through authState', (done: any) => {
@@ -133,7 +132,7 @@ describe('AngularFireAuth with different app', () => {
133132
beforeEach(() => {
134133
TestBed.configureTestingModule({
135134
imports: [
136-
AngularFireModule.initializeApp(COMMON_CONFIG),
135+
AngularFireModule.initializeTestApp(COMMON_CONFIG),
137136
AngularFireAuthModule
138137
],
139138
providers: [
@@ -159,7 +158,6 @@ describe('AngularFireAuth with different app', () => {
159158

160159
it('should have an initialized Firebase app', () => {
161160
expect(afAuth.auth.app).toBeDefined();
162-
expect(<any>afAuth.auth.app).toEqual(app);
163161
});
164162

165163
it('should have an initialized Firebase app instance member', () => {

src/core/angularfire2.spec.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('angularfire', () => {
2020
beforeEach(() => {
2121

2222
TestBed.configureTestingModule({
23-
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, APP_NAME)]
23+
imports: [AngularFireModule.initializeTestApp(COMMON_CONFIG, APP_NAME)]
2424
});
2525

2626
inject([FirebaseApp, PlatformRef], (_app: FirebaseApp, _platform: PlatformRef) => {
@@ -42,34 +42,41 @@ describe('angularfire', () => {
4242
});
4343

4444
describe('FirebaseApp', () => {
45+
4546
it('should provide a FirebaseApp for the FirebaseApp binding', () => {
4647
expect(typeof app.delete).toBe('function');
4748
});
48-
it('should have the provided name', () => {
49-
expect(app.name).toBe(APP_NAME);
50-
})
51-
it('should use an already intialized firebase app if it exists', done => {
52-
@NgModule({
53-
imports: [
54-
AngularFireModule.initializeApp(COMMON_CONFIG, APP_NAME),
55-
BrowserModule
56-
]})
57-
class MyModule {
58-
ngDoBootstrap() {}
59-
}
6049

61-
const compilerFactory: CompilerFactory =
62-
defaultPlatform.injector.get(CompilerFactory, null);
63-
const moduleFactory = compilerFactory.createCompiler().compileModuleSync(MyModule);
50+
if (typeof window !== 'undefined') {
51+
52+
it('should have the provided name', () => {
53+
expect(app.name).toBe(APP_NAME);
54+
});
55+
56+
it('should use an already intialized firebase app if it exists', done => {
57+
@NgModule({
58+
imports: [
59+
AngularFireModule.initializeTestApp(COMMON_CONFIG, APP_NAME),
60+
BrowserModule
61+
]})
62+
class MyModule {
63+
ngDoBootstrap() {}
64+
}
6465

65-
defaultPlatform.bootstrapModuleFactory(moduleFactory)
66-
.then(moduleRef => {
67-
const ref = moduleRef.injector.get(FirebaseApp);
68-
expect(ref.name).toEqual(app.name);
69-
}).then(done, e => {
70-
fail(e);
71-
done()
72-
});
73-
})
66+
const compilerFactory: CompilerFactory =
67+
defaultPlatform.injector.get(CompilerFactory, null);
68+
const moduleFactory = compilerFactory.createCompiler().compileModuleSync(MyModule);
69+
70+
defaultPlatform.bootstrapModuleFactory(moduleFactory)
71+
.then(moduleRef => {
72+
const ref = moduleRef.injector.get(FirebaseApp);
73+
expect(ref.name).toEqual(app.name);
74+
}).then(done, e => {
75+
fail(e);
76+
done()
77+
});
78+
})
79+
80+
}
7481
});
7582
});

src/core/firebase.app.module.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export type FirebaseOptions = {[key:string]: any};
88
export type FirebaseAppConfig = {[key:string]: any};
99

1010
export const FirebaseOptionsToken = new InjectionToken<FirebaseOptions>('angularfire2.app.options');
11-
export const FirebaseNameOrConfigToken = new InjectionToken<string|FirebaseAppConfig|undefined>('angularfire2.app.nameOrConfig')
11+
export const FirebaseNameOrConfigToken = new InjectionToken<string|FirebaseAppConfig|undefined>('angularfire2.app.nameOrConfig');
12+
export const InitializeAsTestApp = new InjectionToken<boolean>('angularfire2.app.test');
1213

1314
export type FirebaseDatabase = database.Database;
1415
export type FirebaseAuth = auth.Auth;
@@ -33,23 +34,29 @@ export class FirebaseApp {
3334
installations: () => any; // SEMVER: drop once we can target
3435
}
3536

36-
export function _firebaseAppFactory(options: FirebaseOptions, nameOrConfig?: string|FirebaseAppConfig|null) {
37-
const name = typeof nameOrConfig === 'string' && nameOrConfig || '[DEFAULT]';
38-
const config = typeof nameOrConfig === 'object' && nameOrConfig || {};
39-
config.name = config.name || name;
37+
export function _firebaseAppFactory(options: FirebaseOptions, nameOrConfig?: string|FirebaseAppConfig|null, initializeAsTestApp?: boolean|null) {
4038
// Added any due to some inconsistency between @firebase/app and firebase types
41-
const existingApp = firebase.apps.filter(app => app && app.name === config.name)[0] as any;
42-
// We support FirebaseConfig, initializeApp's public type only accepts string; need to cast as any
43-
// Could be solved with https://github.com/firebase/firebase-js-sdk/pull/1206
44-
return (existingApp || firebase.initializeApp(options, config as any)) as FirebaseApp;
39+
if (typeof window === 'undefined' && initializeAsTestApp) {
40+
// @ts-ignore
41+
return FirebaseTesting.initializeTestApp(options) as FirebaseApp;
42+
} else {
43+
const name = typeof nameOrConfig === 'string' && nameOrConfig || '[DEFAULT]';
44+
const config = typeof nameOrConfig === 'object' && nameOrConfig || {};
45+
config.name = config.name || name;
46+
const existingApp = <any>firebase.apps.filter(app => app && app.name === config.name)[0];
47+
// We support FirebaseConfig, initializeApp's public type only accepts string; need to cast as any
48+
// Could be solved with https://github.com/firebase/firebase-js-sdk/pull/1206
49+
return (existingApp || <any>firebase.initializeApp(options, config as any)) as FirebaseApp;
50+
}
4551
}
4652

4753
const FirebaseAppProvider = {
4854
provide: FirebaseApp,
4955
useFactory: _firebaseAppFactory,
5056
deps: [
5157
FirebaseOptionsToken,
52-
[new Optional(), FirebaseNameOrConfigToken]
58+
[new Optional(), FirebaseNameOrConfigToken],
59+
[new Optional(), InitializeAsTestApp]
5360
]
5461
};
5562

@@ -62,7 +69,18 @@ export class AngularFireModule {
6269
ngModule: AngularFireModule,
6370
providers: [
6471
{ provide: FirebaseOptionsToken, useValue: options },
65-
{ provide: FirebaseNameOrConfigToken, useValue: nameOrConfig }
72+
{ provide: FirebaseNameOrConfigToken, useValue: nameOrConfig },
73+
{ provide: InitializeAsTestApp, useValue: false }
74+
]
75+
}
76+
}
77+
static initializeTestApp(options: FirebaseOptions, nameOrConfig?: string | FirebaseAppConfig) {
78+
return {
79+
ngModule: AngularFireModule,
80+
providers: [
81+
{ provide: FirebaseOptionsToken, useValue: options },
82+
{ provide: FirebaseNameOrConfigToken, useValue: nameOrConfig },
83+
{ provide: InitializeAsTestApp, useValue: true }
6684
]
6785
}
6886
}

src/database/database.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ describe('AngularFireDatabase', () => {
1515
beforeEach(() => {
1616
TestBed.configureTestingModule({
1717
imports: [
18-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
18+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
1919
AngularFireDatabaseModule
20+
],
21+
providers: [
22+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
2023
]
2124
});
2225
inject([FirebaseApp, AngularFireDatabase, NgZone], (app_: FirebaseApp, _db: AngularFireDatabase, _zone: NgZone) => {
@@ -38,7 +41,6 @@ describe('AngularFireDatabase', () => {
3841

3942
it('should have an initialized Firebase app', () => {
4043
expect(db.database.app).toBeDefined();
41-
expect(<any>db.database.app).toEqual(app);
4244
});
4345

4446
it('should accept a Firebase App in the constructor', () => {
@@ -65,7 +67,7 @@ describe('AngularFireDatabase w/options', () => {
6567
beforeEach(() => {
6668
TestBed.configureTestingModule({
6769
imports: [
68-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
70+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
6971
AngularFireDatabaseModule
7072
],
7173
providers: [
@@ -92,7 +94,6 @@ describe('AngularFireDatabase w/options', () => {
9294

9395
it('should have an initialized Firebase app', () => {
9496
expect(db.database.app).toBeDefined();
95-
expect(<any>db.database.app).toEqual(app);
9697
});
9798

9899
it('should have an initialized Firebase app instance member', () => {

src/database/list/audit-trail.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DatabaseReference } from '../interfaces';
22
import { FirebaseApp, AngularFireModule } from '@angular/fire';
3-
import { AngularFireDatabase, AngularFireDatabaseModule, auditTrail, ChildEvent } from '@angular/fire/database';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, auditTrail, ChildEvent, RealtimeDatabaseURL } from '@angular/fire/database';
44
import { TestBed, inject } from '@angular/core/testing';
55
import { COMMON_CONFIG } from '../../test-config';
66
import { skip } from 'rxjs/operators';
@@ -25,8 +25,11 @@ describe('auditTrail', () => {
2525
beforeEach(() => {
2626
TestBed.configureTestingModule({
2727
imports: [
28-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
28+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
2929
AngularFireDatabaseModule
30+
],
31+
providers: [
32+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
3033
]
3134
});
3235
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {

src/database/list/changes.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { database } from 'firebase/app';
22
import { FirebaseApp, AngularFireModule } from '@angular/fire';
3-
import { AngularFireDatabase, AngularFireDatabaseModule, listChanges } from '@angular/fire/database';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, listChanges, RealtimeDatabaseURL } from '@angular/fire/database';
44
import { TestBed, inject } from '@angular/core/testing';
55
import { COMMON_CONFIG } from '../../test-config';
66
import { skip, take } from 'rxjs/operators';
@@ -25,8 +25,11 @@ describe('listChanges', () => {
2525
beforeEach(() => {
2626
TestBed.configureTestingModule({
2727
imports: [
28-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
28+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
2929
AngularFireDatabaseModule
30+
],
31+
providers: [
32+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
3033
]
3134
});
3235
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {

src/database/list/snapshot-changes.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { database } from 'firebase/app';
22
import { FirebaseApp, AngularFireModule } from '@angular/fire';
3-
import { AngularFireDatabase, AngularFireDatabaseModule, snapshotChanges, ChildEvent } from '@angular/fire/database';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, snapshotChanges, ChildEvent, RealtimeDatabaseURL } from '@angular/fire/database';
44
import { TestBed, inject } from '@angular/core/testing';
55
import { COMMON_CONFIG } from '../../test-config';
66
import { BehaviorSubject } from 'rxjs';
@@ -26,8 +26,11 @@ describe('snapshotChanges', () => {
2626
beforeEach(() => {
2727
TestBed.configureTestingModule({
2828
imports: [
29-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
29+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
3030
AngularFireDatabaseModule
31+
],
32+
providers: [
33+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
3134
]
3235
});
3336
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {

src/database/list/state-changes.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { database } from 'firebase/app';
22
import { FirebaseApp, AngularFireModule } from '@angular/fire';
3-
import { AngularFireDatabase, AngularFireDatabaseModule, stateChanges, ChildEvent } from '@angular/fire/database';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, stateChanges, ChildEvent, RealtimeDatabaseURL } from '@angular/fire/database';
44
import { TestBed, inject } from '@angular/core/testing';
55
import { COMMON_CONFIG } from '../../test-config';
66
import { skip } from 'rxjs/operators';
@@ -25,8 +25,11 @@ describe('stateChanges', () => {
2525
beforeEach(() => {
2626
TestBed.configureTestingModule({
2727
imports: [
28-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
28+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
2929
AngularFireDatabaseModule
30+
],
31+
providers: [
32+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
3033
]
3134
});
3235
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {

src/database/observable/fromRef.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DatabaseReference } from '../interfaces';
22
import { FirebaseApp, AngularFireModule } from '@angular/fire';
3-
import { AngularFireDatabase, AngularFireDatabaseModule, fromRef } from '@angular/fire/database';
3+
import { AngularFireDatabase, AngularFireDatabaseModule, fromRef, RealtimeDatabaseURL } from '@angular/fire/database';
44
import { TestBed, inject } from '@angular/core/testing';
55
import { COMMON_CONFIG } from '../../test-config';
66
import { take } from 'rxjs/operators';
@@ -24,8 +24,11 @@ describe('fromRef', () => {
2424
beforeEach(() => {
2525
TestBed.configureTestingModule({
2626
imports: [
27-
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
27+
AngularFireModule.initializeTestApp(COMMON_CONFIG, FIREBASE_APP_NAME),
2828
AngularFireDatabaseModule
29+
],
30+
providers: [
31+
{ provide: RealtimeDatabaseURL, useValue: 'http://localhost:9000?ns=angularfire2-test'}
2932
]
3033
});
3134
inject([FirebaseApp], (app_: FirebaseApp) => {

0 commit comments

Comments
 (0)