tests(analytics): migration for jest tests (#3577)

[skip-ci]
This commit is contained in:
Darren Ackers
2020-04-30 14:42:03 +01:00
committed by GitHub
parent 2cc0f27232
commit 0cfb92cc96
3 changed files with 109 additions and 103 deletions

View File

@@ -16,6 +16,13 @@ jest.doMock('react-native', () => {
},
options: {},
},
{
appConfig: {
name: 'secondaryFromNative',
},
options: {},
},
],
},
RNFBPerfModule: {},

View File

@@ -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.",
);
});
});
});
});

View File

@@ -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);
});