diff --git a/jest.setup.ts b/jest.setup.ts index ebf3db47..c9ca6363 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -16,6 +16,13 @@ jest.doMock('react-native', () => { }, options: {}, }, + + { + appConfig: { + name: 'secondaryFromNative', + }, + options: {}, + }, ], }, RNFBPerfModule: {}, diff --git a/packages/analytics/__tests__/analytics.test.ts b/packages/analytics/__tests__/analytics.test.ts new file mode 100644 index 00000000..9af69c44 --- /dev/null +++ b/packages/analytics/__tests__/analytics.test.ts @@ -0,0 +1,93 @@ +import { firebase } from '../lib'; + +describe('Analytics', () => { + describe('namespace', () => { + it('accessible from firebase.app()', () => { + const app = firebase.app(); + expect(app.analytics).toBeDefined(); + expect(app.analytics().app).toEqual(app); + }); + + it('throws if non default app arg provided to firebase.analytics(APP)', () => { + const app = firebase.app('secondaryFromNative'); + + const expectedError = [ + 'You attempted to call "firebase.analytics(app)" but; analytics does not support multiple Firebase Apps.', + '', + 'Ensure the app provided is the default Firebase app only and not the "secondaryFromNative" app.', + ].join('\r\n'); + + // @ts-ignore + expect(() => firebase.analytics(app)).toThrowError(expectedError); + }); + + it('throws if analytics access from a non default app', () => { + const app = firebase.app('secondaryFromNative'); + + const expectedError = [ + 'You attempted to call "firebase.app(\'secondaryFromNative\').analytics" but; analytics does not support multiple Firebase Apps.', + '', + 'Ensure you access analytics from the default application only.', + ].join('\r\n'); + + expect(() => app.analytics()).toThrowError(expectedError); + }); + + // TODO in app/registry/namespace.js - if (!hasCustomUrlOrRegionSupport) + xit('throws if args provided to firebase.app().analytics(ARGS)', () => { + try { + // @ts-ignore + firebase.app().analytics('foo', 'arg2'); + return Promise.reject(new Error('Did not throw')); + } catch (e) { + e.message.should.containEql('does not support multiple Firebase Apps'); + return Promise.resolve(); + } + }); + }); + + describe('logEvent()', () => { + it('errors if name is not a string', () => { + // @ts-ignore + expect(() => firebase.analytics().logEvent(123)).toThrowError( + "firebase.analytics().logEvent(*) 'name' expected a string value.", + ); + }); + + it('errors if params is not an object', () => { + // @ts-ignore + expect(() => firebase.analytics().logEvent('invertase_event', 'foobar')).toThrowError( + "firebase.analytics().logEvent(_, *) 'params' expected an object value.", + ); + }); + + it('errors on using a reserved name', () => { + expect(() => firebase.analytics().logEvent('session_start')).toThrowError( + "firebase.analytics().logEvent(*) 'name' the event name 'session_start' is reserved and can not be used.", + ); + }); + + it('errors if name not alphanumeric', () => { + expect(() => firebase.analytics().logEvent('!@£$%^&*')).toThrowError( + "firebase.analytics().logEvent(*) 'name' invalid event name '!@£$%^&*'. Names should contain 1 to 32 alphanumeric characters or underscores.", + ); + }); + + it('errors if more than 25 params provided', () => { + expect(() => + firebase.analytics().logEvent('invertase', Object.assign({}, new Array(26).fill(1))), + ).toThrowError( + "firebase.analytics().logEvent(_, *) 'params' maximum number of parameters exceeded (25).", + ); + }); + + describe('setAnalyticsCollectionEnabled()', () => { + it('throws if not a boolean', () => { + // @ts-ignore + expect(() => firebase.analytics().setAnalyticsCollectionEnabled('foo')).toThrowError( + "firebase.analytics().setAnalyticsCollectionEnabled(*) 'enabled' expected a boolean value.", + ); + }); + }); + }); +}); diff --git a/packages/analytics/e2e/analytics.e2e.js b/packages/analytics/e2e/analytics.e2e.js index aaa86c0c..0aef598b 100644 --- a/packages/analytics/e2e/analytics.e2e.js +++ b/packages/analytics/e2e/analytics.e2e.js @@ -16,101 +16,9 @@ */ describe('analytics()', () => { - describe('namespace', () => { - it('accessible from firebase.app()', () => { - const app = firebase.app(); - should.exist(app.analytics); - app.analytics().logEvent.should.be.a.Function(); - app.analytics().emitter.should.be.a.Object(); - }); - - it('throws if non default app arg provided to firebase.analytics(APP)', () => { - const app = firebase.app('secondaryFromNative'); - try { - firebase.analytics(app); - return Promise.reject(new Error('Did not throw')); - } catch (e) { - e.message.should.containEql('does not support multiple Firebase Apps'); - return Promise.resolve(); - } - }); - - it('throws if analytics access from a non default app', () => { - const app = firebase.app('secondaryFromNative'); - try { - app.analytics(); - return Promise.reject(new Error('Did not throw')); - } catch (e) { - e.message.should.containEql('does not support multiple Firebase Apps'); - return Promise.resolve(); - } - }); - - // TODO in app/registry/namespace.js - if (!hasCustomUrlOrRegionSupport) - xit('throws if args provided to firebase.app().analytics(ARGS)', () => { - try { - firebase.app().analytics('foo', 'arg2'); - return Promise.reject(new Error('Did not throw')); - } catch (e) { - e.message.should.containEql('does not support multiple Firebase Apps'); - return Promise.resolve(); - } - }); - }); + describe('namespace', () => {}); describe('logEvent()', () => { - it('errors if name is not a string', () => { - try { - firebase.analytics().logEvent(123); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql("'name' expected a string value"); - return Promise.resolve(); - } - }); - - it('errors if params is not an object', () => { - try { - firebase.analytics().logEvent('invertase_event', 'foobar'); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql("'params' expected an object value"); - return Promise.resolve(); - } - }); - - it('errors on using a reserved name', () => { - try { - firebase.analytics().logEvent('session_start'); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql( - "'name' the event name 'session_start' is reserved and can not be used", - ); - return Promise.resolve(); - } - }); - - it('errors if name not alphanumeric', () => { - try { - firebase.analytics().logEvent('!@£$%^&*'); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql("'name' invalid event name '!@£$%^&*'"); - return Promise.resolve(); - } - }); - - it('errors if more than 25 params provided', () => { - try { - firebase.analytics().logEvent('invertase', Object.assign({}, new Array(26).fill(1))); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql("'params' maximum number of parameters exceeded (25)"); - return Promise.resolve(); - } - }); - it('log an event without parameters', async () => { await firebase.analytics().logEvent('invertase_event'); }); @@ -122,19 +30,17 @@ describe('analytics()', () => { string: 'string', }); }); + + it('log an event with parameters', async () => { + await firebase.analytics().logEvent('invertase_event', { + boolean: true, + number: 1, + string: 'string', + }); + }); }); describe('setAnalyticsCollectionEnabled()', () => { - it('throws if not a boolean', () => { - try { - firebase.analytics().setAnalyticsCollectionEnabled('foo'); - return Promise.reject(new Error('Did not throw.')); - } catch (e) { - e.message.should.containEql("'enabled' expected a boolean value"); - return Promise.resolve(); - } - }); - it('true', async () => { await firebase.analytics().setAnalyticsCollectionEnabled(true); });