Skip to content

Commit 83964fd

Browse files
committed
Make tests work again
1 parent 3a77941 commit 83964fd

File tree

11 files changed

+62
-40
lines changed

11 files changed

+62
-40
lines changed

integration/browserify/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/shared/namespaceDefinition.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"INTERNAL": {
4242
"__type": "object",
43-
"registerService": {
43+
"registerComponent": {
4444
"__type": "function"
4545
},
4646
"extendNamespace": {
@@ -55,20 +55,8 @@
5555
"removeApp": {
5656
"__type": "function"
5757
},
58-
"factories": {
59-
"__type": "object",
60-
"storage": {
61-
"__type": "function"
62-
},
63-
"auth": {
64-
"__type": "function"
65-
},
66-
"database": {
67-
"__type": "function"
68-
},
69-
"messaging": {
70-
"__type": "function"
71-
}
58+
"components": {
59+
"__type": "Map"
7260
},
7361
"ErrorFactory": {
7462
"__type": "function"

integration/typescript/test/namespace.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/webpack/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

packages/app-types/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Provider } from '@firebase/component';
2-
31
/**
42
* @license
53
* Copyright 2017 Google Inc.

packages/app-types/private.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
2424
import { Observer, Subscribe } from '@firebase/util';
2525
import { FirebaseError, ErrorFactory } from '@firebase/util';
2626
import { Deferred } from '../firestore/test/util/promise';
27-
import { Component } from '@firebase/component';
27+
import { Component, ComponentContainer } from '@firebase/component';
2828

2929
export interface FirebaseServiceInternals {
3030
/**
@@ -82,7 +82,8 @@ export interface FirebaseAppInternals {
8282
}
8383

8484
export interface _FirebaseApp extends FirebaseApp {
85-
_addComponent(component: Component): void;
85+
container: ComponentContainer;
86+
_addComponent(component: Component, overwrite?: boolean): void;
8687
_removeServiceInstance(name: string, instanceIdentifier?: string): void;
8788
}
8889
export interface _FirebaseNamespace extends FirebaseNamespace {

packages/component/src/provider.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class Provider<T extends Name> {
3636
constructor(
3737
private readonly name: T,
3838
private readonly container: ComponentContainer
39-
) {}
39+
) { }
4040

4141
/**
4242
* @param identifier A provider can provide mulitple instances of a service
@@ -50,9 +50,14 @@ export class Provider<T extends Name> {
5050
const deferred = new Deferred<NameServiceMapping[T]>();
5151
this.instancesDeferred.set(normalizedIdentifier, deferred);
5252
// If the service instance is available, resolve the promise with it immediately
53-
const instance = this.getOrInitializeService(normalizedIdentifier);
54-
if (instance) {
55-
deferred.resolve(instance);
53+
try {
54+
const instance = this.getOrInitializeService(normalizedIdentifier);
55+
if (instance) {
56+
deferred.resolve(instance);
57+
}
58+
} catch (e) {
59+
// when the instance factory throws an exception during get(), it should not cause
60+
// an fatal error. We just return the unresolved promise in this case.
5661
}
5762
}
5863

@@ -86,17 +91,23 @@ export class Provider<T extends Name> {
8691
};
8792
// if multipleInstances is not supported, use the default name
8893
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
94+
try {
95+
const instance = this.getOrInitializeService(normalizedIdentifier);
8996

90-
const instance = this.getOrInitializeService(normalizedIdentifier);
91-
92-
if (!instance) {
93-
if (optional) {
97+
if (!instance) {
98+
if (optional) {
99+
return null;
100+
}
101+
throw Error(`Service ${this.name} is not available`);
102+
}
103+
return instance;
104+
} catch (e) {
105+
if(optional) {
94106
return null;
107+
} else {
108+
throw e;
95109
}
96-
throw Error(`Service ${this.name} is not available`);
97110
}
98-
99-
return instance;
100111
}
101112

102113
setComponent(component: Component<T>): void {
@@ -113,7 +124,14 @@ export class Provider<T extends Name> {
113124
this.component = component;
114125
// if the service is eager, initialize the default instance
115126
if (isComponentEager(component)) {
116-
this.getOrInitializeService(DEFAULT_ENTRY_NAME);
127+
try {
128+
this.getOrInitializeService(DEFAULT_ENTRY_NAME);
129+
} catch (e) {
130+
// when the instance factory for an eager Component throws an exception during the eager
131+
// initialization, it should not cause an fatal error.
132+
// TODO: Investigate if we need to make it configurable, because some component may want to cause
133+
// a fatal error in this case?
134+
}
117135
}
118136

119137
// Create service instances for the pending promises and resolve them
@@ -127,10 +145,15 @@ export class Provider<T extends Name> {
127145
instanceIdentifier
128146
);
129147

130-
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
131-
const instance = this.getOrInitializeService(normalizedIdentifier)!;
148+
try {
149+
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
150+
const instance = this.getOrInitializeService(normalizedIdentifier)!;
151+
instanceDeferred.resolve(instance);
152+
} catch (e) {
153+
// when the instance factory throws an exception, it should not cause
154+
// an fatal error. We just leave the promise unresolved.
155+
}
132156

133-
instanceDeferred.resolve(instance);
134157
}
135158
}
136159

packages/functions/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export class ContextProvider {
3737
authProvider: Provider<FirebaseAuthInternal>,
3838
messagingProvider: Provider<FirebaseMessaging>
3939
) {
40-
this.auth = authProvider.getImmediate(undefined, { optional: true });
41-
this.messaging = messagingProvider.getImmediate(undefined, {
40+
this.auth = authProvider.getImmediate({ optional: true });
41+
this.messaging = messagingProvider.getImmediate({
4242
optional: true
4343
});
4444

packages/messaging-types/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ export class FirebaseMessaging {
4949
useServiceWorker(registration: ServiceWorkerRegistration): void;
5050
usePublicVapidKey(b64PublicKey: string): void;
5151
}
52+
53+
declare module '@firebase/component' {
54+
interface ComponentContainer {
55+
getProvider(name: 'messaging'): Provider<FirebaseMessaging>;
56+
}
57+
58+
interface Provider { }
59+
}

packages/rxfire/test/database.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('RxFire Database', () => {
9292
*/
9393
beforeEach(() => {
9494
app = initializeApp({
95+
apiKey: TEST_PROJECT.apiKey,
9596
projectId: TEST_PROJECT.projectId,
9697
databaseURL: TEST_PROJECT.databaseURL
9798
});

packages/storage/src/implementation/authwrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class AuthWrapper {
8989
}
9090

9191
getAuthToken(): Promise<string | null> {
92-
const auth = this.authProvider_.getImmediate(undefined, { optional: true });
92+
const auth = this.authProvider_.getImmediate({ optional: true });
9393
if (auth) {
9494
return auth.getToken().then(
9595
(response: FirebaseAuthTokenData | null): string | null => {

0 commit comments

Comments
 (0)