insomnia/packages/insomnia-app/app/common/__tests__/analytics.test.js
Gregory Schier 549ce23ce8
Merge All Repositories into Monorepo for easier maintenance (#629)
* All projects into monorepo

* Update CI

* More CI updates

* Extracted a bunch of things into packages

* Publish

 - insomnia-plugin-base64@1.0.1
 - insomnia-plugin-default-headers@1.0.2
 - insomnia-plugin-file@1.0.1
 - insomnia-plugin-hash@1.0.1
 - insomnia-plugin-now@1.0.1
 - insomnia-plugin-request@1.0.1
 - insomnia-plugin-response@1.0.1
 - insomnia-plugin-uuid@1.0.1
 - insomnia-cookies@0.0.2
 - insomnia-importers@1.5.2
 - insomnia-prettify@0.0.3
 - insomnia-url@0.0.2
 - insomnia-xpath@0.0.2

* A bunch of small fixes

* Improved build script

* Fixed

* Merge dangling files

* Usability refactor

* Handle duplicate plugin names
2017-11-26 20:45:40 +00:00

118 lines
3.3 KiB
JavaScript

import * as electron from 'electron';
import {EventEmitter} from 'events';
import {globalBeforeEach} from '../../__jest__/before-each';
import * as models from '../../models/index';
import {_trackEvent} from '../analytics';
import {getAppPlatform, getAppVersion} from '../constants';
describe('init()', () => {
beforeEach(async () => {
await globalBeforeEach();
electron.net.request = jest.fn(url => {
const req = new EventEmitter();
req.end = function () {
};
return req;
});
jest.useFakeTimers();
});
it('does not work with tracking disabled', async () => {
const settings = await models.settings.getOrCreate({
disableAnalyticsTracking: true,
deviceId: 'device'
});
expect(settings.disableAnalyticsTracking).toBe(true);
expect(electron.net.request.mock.calls).toEqual([]);
await _trackEvent(true, 'Foo', 'Bar');
jest.runAllTimers();
expect(electron.net.request.mock.calls).toEqual([]);
});
it('works with tracking enabled', async () => {
const settings = await models.settings.getOrCreate({
disableAnalyticsTracking: false,
deviceId: 'device'
});
expect(settings.disableAnalyticsTracking).toBe(false);
expect(electron.net.request.mock.calls).toEqual([]);
await _trackEvent(true, 'Foo', 'Bar');
jest.runAllTimers();
expect(electron.net.request.mock.calls).toEqual([
[
'https://www.google-analytics.com/collect?' +
'v=1&' +
'tid=UA-86416787-1&' +
'cid=device&' +
'dl=https%3A%2F%2Fdesktop.insomnia.rest%2F&' +
'sr=1920x1080&' +
'ul=en-US&' +
`dt=Insomnia%20${getAppVersion()}&` +
`cd1=${getAppPlatform()}&` +
`cd2=${getAppVersion()}&` +
'vp=1900x1060&' +
'de=UTF-8&' +
't=event&' +
'ec=Foo&' +
'ea=Bar'
]
]);
});
it('tracks non-interactive event', async () => {
await models.settings.getOrCreate({deviceId: 'device'});
await _trackEvent(false, 'Foo', 'Bar');
jest.runAllTimers();
expect(electron.net.request.mock.calls).toEqual([
[
'https://www.google-analytics.com/collect?' +
'v=1&' +
'tid=UA-86416787-1&' +
'cid=device&' +
'dl=https%3A%2F%2Fdesktop.insomnia.rest%2F&' +
'sr=1920x1080&' +
'ul=en-US&' +
`dt=Insomnia%20${getAppVersion()}&` +
`cd1=${getAppPlatform()}&` +
`cd2=${getAppVersion()}&` +
'vp=1900x1060&' +
'de=UTF-8&' +
't=event&' +
'ec=Foo&' +
'ea=Bar&' +
'ni=1'
]
]);
});
it('tracks page view', async () => {
await models.settings.getOrCreate({deviceId: 'device'});
await _trackEvent(false, 'Foo', 'Bar');
jest.runAllTimers();
expect(electron.net.request.mock.calls).toEqual([
[
'https://www.google-analytics.com/collect?' +
'v=1&' +
'tid=UA-86416787-1&' +
'cid=device&' +
'dl=https%3A%2F%2Fdesktop.insomnia.rest%2F&' +
'sr=1920x1080&' +
'ul=en-US&' +
`dt=Insomnia%20${getAppVersion()}&` +
`cd1=${getAppPlatform()}&` +
`cd2=${getAppVersion()}&` +
'vp=1900x1060&' +
'de=UTF-8&' +
't=event&' +
'ec=Foo&' +
'ea=Bar&' +
'ni=1'
]
]);
});
});