Added test for disabling of analytics tracking

This commit is contained in:
Gregory Schier 2017-06-01 06:50:15 -07:00
parent 80a29a5ef1
commit ef64eea23b
2 changed files with 39 additions and 15 deletions

View File

@ -3,26 +3,30 @@ import {GA_HOST, getAppVersion, getAppPlatform} from '../../common/constants';
import * as db from '../../common/database';
import * as models from '../../models';
global.document = {
getElementsByTagName () {
return {
parentNode: {
insertBefore () {
}
}
};
}
};
describe('init()', () => {
beforeEach(() => {
window.localStorage = {};
global.document = {
getElementsByTagName () {
return {
parentNode: {
insertBefore () {
}
}
};
}
};
return db.init(models.types(), {inMemoryOnly: true}, true);
});
afterEach(() => {
// Remove any trace of GA
global.document = undefined;
global.window.ga = undefined;
});
it('correctly initializes', async () => {
jest.useFakeTimers();
window.localStorage = {};
analytics.trackEvent('premature', 'event');
analytics.setAccountId('acct_premature');
@ -32,6 +36,10 @@ describe('init()', () => {
await analytics.init('acct_123');
jest.runAllTicks();
// Make sure we have it enabled
const settings = await models.settings.getOrCreate();
expect(settings.disableAnalyticsTracking).toBe(false);
// Verify that Google Analytics works
expect(window.ga.mock.calls.length).toBe(7);
expect(window.ga.mock.calls[0][0]).toBe('create');
@ -62,4 +70,20 @@ describe('init()', () => {
jest.runAllTicks();
expect(window.ga.mock.calls.length).toBe(9);
});
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();
});
});

View File

@ -47,10 +47,10 @@ export async function update (settings, patch) {
return db.docUpdate(settings, patch);
}
export async function getOrCreate () {
export async function getOrCreate (patch = {}) {
const results = await db.all(type);
if (results.length === 0) {
return await create();
return await create(patch);
} else {
return results[0];
}