Skip to content

Commit a13f418

Browse files
committed
FirebaseApp / Options typeguard. More tests.
1 parent e9f28ed commit a13f418

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

common/api-review/app.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ export function initializeApp(): FirebaseApp;
116116
// @public
117117
export function initializeServerApp(options: FirebaseOptions | FirebaseApp, config: FirebaseServerAppSettings): FirebaseServerApp;
118118

119+
// @internal (undocumented)
120+
export function _isFirebaseApp(obj: FirebaseApp | FirebaseOptions): obj is FirebaseApp;
121+
119122
// @public
120123
export function onLog(logCallback: LogCallback | null, options?: LogOptions): void;
121124

packages/app/src/api.test.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,6 @@ describe('API tests', () => {
162162
expect(app.name).to.equal(appName);
163163
});
164164

165-
it('takes an object as the second parameter to create named App', () => {
166-
const appName = 'MyApp';
167-
const app = initializeApp({}, { name: appName });
168-
expect(app.name).to.equal(appName);
169-
});
170-
171165
it('sets automaticDataCollectionEnabled', () => {
172166
const app = initializeApp({}, { automaticDataCollectionEnabled: true });
173167
expect(app.automaticDataCollectionEnabled).to.be.true;
@@ -189,18 +183,59 @@ describe('API tests', () => {
189183
});
190184

191185
describe('initializeServerApp', () => {
192-
it('creates named App', () => {
186+
it('creates FirebaseServerApp with options', () => {
187+
const options = {
188+
apiKey: 'APIKEY'
189+
};
190+
191+
const serverAppSettings: FirebaseServerAppSettings = {};
192+
193+
const app = initializeServerApp(options, serverAppSettings);
194+
expect(app).to.not.equal(null);
195+
expect(app.automaticDataCollectionEnabled).to.be.false;
196+
});
197+
198+
it('creates FirebaseServerApp with automaticDataCollectionEnabled', () => {
193199
const options = {
194200
apiKey: 'APIKEY'
195201
};
196202

203+
const serverAppSettings: FirebaseServerAppSettings = {
204+
automaticDataCollectionEnabled: true
205+
};
206+
207+
const app = initializeServerApp(options, serverAppSettings);
208+
expect(app).to.not.equal(null);
209+
expect(app.automaticDataCollectionEnabled).to.be.true;
210+
});
211+
212+
it('creates FirebaseServerApp with releaseOnDeref', () => {
213+
const options = { apiKey: 'APIKEY' };
197214
const serverAppSettings: FirebaseServerAppSettings = {
198215
automaticDataCollectionEnabled: false,
199216
releaseOnDeref: options
200217
};
201218

202219
const app = initializeServerApp(options, serverAppSettings);
203220
expect(app).to.not.equal(null);
221+
expect(app.automaticDataCollectionEnabled).to.be.false;
222+
});
223+
224+
it('creates FirebaseServerApp with FirebaseApp', () => {
225+
const options = {
226+
apiKey: 'test1'
227+
};
228+
const standardApp = initializeApp(options);
229+
expect(standardApp.name).to.equal(DEFAULT_ENTRY_NAME);
230+
expect(standardApp.options.apiKey).to.equal('test1');
231+
232+
const serverAppSettings: FirebaseServerAppSettings = {
233+
automaticDataCollectionEnabled: false
234+
};
235+
236+
const serverApp = initializeServerApp(standardApp, serverAppSettings);
237+
expect(serverApp).to.not.equal(null);
238+
expect(serverApp.options.apiKey).to.equal('test1');
204239
});
205240
});
206241

packages/app/src/api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { FirebaseServerAppImpl } from './firebaseServerApp';
3636
import {
3737
_apps,
3838
_components,
39+
_isFirebaseApp,
3940
_registerComponent,
4041
_serverApps
4142
} from './internal';
@@ -234,10 +235,10 @@ export function initializeServerApp(
234235
};
235236

236237
let appOptions: FirebaseOptions;
237-
if ((_options as FirebaseApp).options !== undefined) {
238-
appOptions = (_options as FirebaseApp).options;
238+
if (_isFirebaseApp(_options)) {
239+
appOptions = _options.options;
239240
} else {
240-
appOptions = _options as FirebaseOptions;
241+
appOptions = _options;
241242
}
242243

243244
const nameObj = {

packages/app/src/internal.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseApp, FirebaseServerApp } from './public-types';
18+
import {
19+
FirebaseApp,
20+
FirebaseOptions,
21+
FirebaseServerApp
22+
} from './public-types';
1923
import { Component, Provider, Name } from '@firebase/component';
2024
import { logger } from './logger';
2125
import { DEFAULT_ENTRY_NAME } from './constants';
@@ -137,6 +141,20 @@ export function _removeServiceInstance<T extends Name>(
137141
_getProvider(app, name).clearInstance(instanceIdentifier);
138142
}
139143

144+
/**
145+
*
146+
* @param obj - an object of type FirebaseApp or FirebaseOptions.
147+
*
148+
* @returns true if the provide object is of type FirebaseApp.
149+
*
150+
* @internal
151+
*/
152+
export function _isFirebaseApp(
153+
obj: FirebaseApp | FirebaseOptions
154+
): obj is FirebaseApp {
155+
return (obj as FirebaseApp).options !== undefined;
156+
}
157+
140158
/**
141159
* Test only
142160
*

0 commit comments

Comments
 (0)