Skip to content

Commit 06189b7

Browse files
committed
Unit tests
1 parent a471e5d commit 06189b7

File tree

1 file changed

+85
-5
lines changed

1 file changed

+85
-5
lines changed

test/unit/app/firebase-namespace.spec.ts

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
'use strict';
1919

20+
import http = require('http');
2021
import path = require('path');
2122

2223
import * as _ from 'lodash';
@@ -89,6 +90,20 @@ const expect = chai.expect;
8990
const DEFAULT_APP_NAME = '[DEFAULT]';
9091
const DEFAULT_APP_NOT_FOUND = 'The default Firebase app does not exist. Make sure you call initializeApp() '
9192
+ 'before using any of the Firebase services.';
93+
const INITIALIZE_APP_CREDENTIAL_EXISTANCE_MISMATCH = 'An existing app named "mock-app-name" already '
94+
+ 'exists with a different options configuration: Credential';
95+
const INITIALIZE_APP_HTTP_AGENT_EXISTANCE_MISMATCH = 'An existing app named "mock-app-name" already '
96+
+ 'exists with a different options configuration: httpAgent';
97+
const INITIALIZE_APP_NOT_IDEMPOTENT_CREDENTIAL = 'Firebase app named "mock-app-name" already exists and '
98+
+ 'initializeApp was invoked with an optional Credential. The SDK cannot confirm the equality '
99+
+ 'of Credential objects with the existing app. Please use getApp or getApps to reuse the '
100+
+ 'existing app instead.'
101+
const INITIALIZE_APP_NOT_IDEMPOTENT_HTTP_AGENT = 'Firebase app named "mock-app-name" already exists and '
102+
+ 'initializeApp was invoked with an optional http.Agent. The SDK cannot confirm the equality '
103+
+ 'of http.Agent objects with the existing app. Please use getApp or getApps to reuse the '
104+
+ 'existing app instead.'
105+
106+
92107

93108
describe('FirebaseNamespace', () => {
94109
let firebaseNamespace: FirebaseNamespace;
@@ -221,15 +236,80 @@ describe('FirebaseNamespace', () => {
221236
let app1: App | undefined;
222237
let app2: App | undefined;
223238
expect(() => {
224-
app1 = firebaseNamespace.initializeApp(mocks.appOptions);
225-
app2 = firebaseNamespace.initializeApp(mocks.appOptions);
239+
app1 = firebaseNamespace.initializeApp(mocks.appOptionsWithoutCredential);
240+
app2 = firebaseNamespace.initializeApp(mocks.appOptionsWithoutCredential);
226241
}).to.not.throw();
227242
expect(app1).to.equal(app2);
243+
});
228244

245+
it('should throw due to the Credential option being not supported by idemopotency', () => {
246+
let app1: App | undefined;
247+
let app2: App | undefined;
229248
expect(() => {
230-
app2 = firebaseNamespace.initializeApp(mocks.appOptions, DEFAULT_APP_NAME);
231-
}).to.not.throw();
232-
expect(app1).to.equal(app2);
249+
app1 = firebaseNamespace.initializeApp(mocks.appOptions, mocks.appName);
250+
app2 = firebaseNamespace.initializeApp(mocks.appOptions, mocks.appName);
251+
}).to.throw(INITIALIZE_APP_NOT_IDEMPOTENT_CREDENTIAL);
252+
expect(app1).to.not.be.undefined;
253+
expect(app2).to.be.undefined;
254+
});
255+
256+
it('should throw due to idempotency check on Credential option on second app.', () => {
257+
let app1: App | undefined;
258+
let app2: App | undefined;
259+
expect(() => {
260+
app1 = firebaseNamespace.initializeApp(mocks.appOptions, mocks.appName);
261+
app2 = firebaseNamespace.initializeApp(mocks.appOptionsWithoutCredential, mocks.appName);
262+
}).to.throw(INITIALIZE_APP_CREDENTIAL_EXISTANCE_MISMATCH);
263+
expect(app1).to.not.be.undefined;
264+
expect(app2).to.be.undefined;
265+
});
266+
267+
it('should throw due to the httpAgent option being not supported by idemopotency', () => {
268+
let app1: App | undefined;
269+
let app2: App | undefined;
270+
const httpAgent = new http.Agent();
271+
const appOptions = {
272+
...mocks.appOptionsWithoutCredential,
273+
httpAgent
274+
}
275+
expect(() => {
276+
app1 = firebaseNamespace.initializeApp(appOptions, mocks.appName);
277+
app2 = firebaseNamespace.initializeApp(appOptions, mocks.appName);
278+
}).to.throw(INITIALIZE_APP_NOT_IDEMPOTENT_HTTP_AGENT);
279+
expect(app1).to.not.be.undefined;
280+
expect(app2).to.be.undefined;
281+
});
282+
283+
it('should throw due to idempotency check on httpAgent option on second app.', () => {
284+
let app1: App | undefined;
285+
let app2: App | undefined;
286+
const httpAgent = new http.Agent();
287+
const appOptions = {
288+
...mocks.appOptionsWithoutCredential,
289+
httpAgent
290+
}
291+
expect(() => {
292+
app1 = firebaseNamespace.initializeApp(mocks.appOptionsWithoutCredential, mocks.appName);
293+
app2 = firebaseNamespace.initializeApp(appOptions, mocks.appName);
294+
}).to.throw(INITIALIZE_APP_NOT_IDEMPOTENT_HTTP_AGENT);
295+
expect(app1).to.not.be.undefined;
296+
expect(app2).to.be.undefined;
297+
});
298+
299+
it('should throw due to idempotency check on httpAgent option on first app but not second app.', () => {
300+
let app1: App | undefined;
301+
let app2: App | undefined;
302+
const httpAgent = new http.Agent();
303+
const appOptions = {
304+
...mocks.appOptionsWithoutCredential,
305+
httpAgent
306+
}
307+
expect(() => {
308+
app1 = firebaseNamespace.initializeApp(appOptions, mocks.appName);
309+
app2 = firebaseNamespace.initializeApp(mocks.appOptionsWithoutCredential, mocks.appName);
310+
}).to.throw(INITIALIZE_APP_HTTP_AGENT_EXISTANCE_MISMATCH);
311+
expect(app1).to.not.be.undefined;
312+
expect(app2).to.be.undefined;
233313
});
234314

235315
it('should return a new app with the provided options and app name', () => {

0 commit comments

Comments
 (0)