2016-11-10 19:08:45 +00:00
|
|
|
import * as analytics from '../index';
|
2016-12-05 22:42:40 +00:00
|
|
|
import {GA_HOST, getAppVersion, getAppPlatform} from '../../common/constants';
|
2016-11-16 18:16:55 +00:00
|
|
|
import * as db from '../../common/database';
|
|
|
|
import * as models from '../../models';
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
global.document = {
|
|
|
|
getElementsByTagName () {
|
|
|
|
return {
|
|
|
|
parentNode: {
|
|
|
|
insertBefore() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('init()', () => {
|
2016-11-16 18:16:55 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
return db.init(models.types(), {inMemoryOnly: true}, true);
|
|
|
|
});
|
|
|
|
|
2016-11-10 19:08:45 +00:00
|
|
|
it('correctly initializes', async () => {
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.useFakeTimers();
|
2016-11-10 19:08:45 +00:00
|
|
|
window.localStorage = {};
|
|
|
|
|
|
|
|
analytics.trackEvent('premature', 'event');
|
|
|
|
analytics.setAccountId('acct_premature');
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.runAllTicks();
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
window.ga = jest.genMockFunction();
|
2016-11-16 18:16:55 +00:00
|
|
|
await analytics.init('acct_123');
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.runAllTicks();
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
// Verify that Google Analytics works
|
2016-12-05 22:42:40 +00:00
|
|
|
expect(window.ga.mock.calls.length).toBe(7);
|
2016-11-16 18:16:55 +00:00
|
|
|
expect(window.ga.mock.calls[0][0]).toBe('create');
|
|
|
|
expect(window.ga.mock.calls[0][1]).toBe('UA-86416787-1');
|
|
|
|
expect(window.ga.mock.calls[0][2].storage).toBe('none');
|
|
|
|
expect(window.ga.mock.calls[0][2].clientId.length).toBe(36);
|
|
|
|
expect(window.ga.mock.calls[0].length).toBe(3);
|
2016-11-10 19:08:45 +00:00
|
|
|
expect(window.ga.mock.calls[1].slice(0, 2)).toEqual(['set', 'checkProtocolTask']);
|
|
|
|
expect(window.ga.mock.calls[1][2]()).toBeNull();
|
|
|
|
expect(window.ga.mock.calls[2]).toEqual(['set', 'location', `https://${GA_HOST}/`]);
|
2016-12-05 22:42:40 +00:00
|
|
|
expect(window.ga.mock.calls[3]).toEqual(['set', 'userId', 'acct_123']);
|
|
|
|
expect(window.ga.mock.calls[4]).toEqual(['set', 'dimension1', getAppPlatform()]);
|
|
|
|
expect(window.ga.mock.calls[5]).toEqual(['set', 'dimension2', getAppVersion()]);
|
|
|
|
expect(window.ga.mock.calls[6]).toEqual(['send', 'pageview']);
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
analytics.trackEvent('foo', 'bar', 'baz');
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.runAllTicks();
|
2016-12-05 22:42:40 +00:00
|
|
|
expect(window.ga.mock.calls.length).toBe(8);
|
|
|
|
expect(window.ga.mock.calls[7]).toEqual(['send', 'event', 'foo', 'bar', 'baz']);
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
analytics.setAccountId('acct_456');
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.runAllTicks();
|
2016-12-05 22:42:40 +00:00
|
|
|
expect(window.ga.mock.calls.length).toBe(9);
|
|
|
|
expect(window.ga.mock.calls[8]).toEqual(['set', 'userId', 'acct_456']);
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
// Try reinitializing
|
2016-11-16 17:18:39 +00:00
|
|
|
analytics.init();
|
2016-12-08 23:29:45 +00:00
|
|
|
jest.runAllTicks();
|
2016-12-05 22:42:40 +00:00
|
|
|
expect(window.ga.mock.calls.length).toBe(9);
|
2016-11-10 19:08:45 +00:00
|
|
|
|
|
|
|
// TODO: Verify that Segment works (although it's not that important)
|
|
|
|
});
|
|
|
|
});
|