insomnia/app/analytics/__tests__/index.test.js

90 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-11-10 19:08:45 +00:00
import * as analytics from '../index';
import {GA_HOST, getAppPlatform, getAppVersion} from '../../common/constants';
2016-11-16 18:16:55 +00:00
import * as models from '../../models';
2017-07-20 03:36:44 +00:00
import {globalBeforeEach} from '../../__jest__/before-each';
2016-11-10 19:08:45 +00:00
describe('init()', () => {
beforeEach(async () => {
2017-07-20 03:36:44 +00:00
await globalBeforeEach();
window.localStorage = {};
global.document = {
getElementsByTagName () {
return {
parentNode: {
insertBefore () {
}
}
};
}
};
2016-11-16 18:16:55 +00:00
});
afterEach(() => {
// Remove any trace of GA
global.document = undefined;
global.window.ga = undefined;
});
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
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
// Make sure we have it enabled
const settings = await models.settings.getOrCreate();
expect(settings.disableAnalyticsTracking).toBe(false);
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
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
});
it('Does not work with click tracking disabled', async () => {
jest.useFakeTimers();
// Make sure we have it disabled
const settings = await models.settings.getOrCreate({disableAnalyticsTracking: true});
expect(settings.disableAnalyticsTracking).toBe(true);
await analytics.init('acct_123');
jest.runAllTicks();
expect(window.ga).toBeUndefined();
analytics.trackEvent('foo', 'bar', 'baz');
jest.runAllTicks();
expect(window.ga).toBeUndefined();
});
2016-11-10 19:08:45 +00:00
});