Skip to content

Commit 01e9535

Browse files
authored
fix(app): fix issue with default instanceIdentifier (#26)
FirebaseApp's contract with the components is that the instanceIdentifier will be undefined if default (not the string '[DEFAULT]'). This was not tested by any existing tests and was broken in the async refactor. This fixes that issue and adds a test.
1 parent 0766643 commit 01e9535

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/app/firebase_app.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,12 @@ class FirebaseAppImpl implements FirebaseApp {
303303
}
304304

305305
if (!this.services_[name][instanceIdentifier]) {
306-
let service = this.firebase_.INTERNAL.factories[name](this, this.extendApp.bind(this), instanceIdentifier);
306+
/**
307+
* If a custom instance has been defined (i.e. not '[DEFAULT]')
308+
* then we will pass that instance on, otherwise we pass `null`
309+
*/
310+
const instanceSpecifier = instanceIdentifier !== DEFAULT_ENTRY_NAME ? instanceIdentifier : undefined;
311+
const service = this.firebase_.INTERNAL.factories[name](this, this.extendApp.bind(this), instanceSpecifier);
307312
this.services_[name][instanceIdentifier] = service;
308313
}
309314

tests/app/unit/firebase_app.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ describe("Firebase App Class", () => {
280280
assert.strictEqual(service.instanceIdentifier, service2.instanceIdentifier, '`instanceIdentifier` is not being set correctly');
281281
assert.strictEqual(service, service2);
282282
});
283+
it(`Should pass null to the factory method if using default instance`, () => {
284+
// Register Multi Instance Service
285+
firebase.INTERNAL.registerService('testService', (...args) => {
286+
const [app,,instanceIdentifier] = args;
287+
assert.isUndefined(instanceIdentifier, '`instanceIdentifier` is not `undefined`');
288+
return new TestService(app, instanceIdentifier);
289+
});
290+
firebase.initializeApp({});
291+
292+
// Capture a given service ref
293+
const serviceIdentifier = 'custom instance identifier';
294+
const service = (firebase.app() as any).testService();
295+
});
283296

284297
describe("Check for bad app names", () => {
285298
let tests = ["", 123, false, null];

0 commit comments

Comments
 (0)