fix(analytics): fix tests and add new ones for getServiceByEnvironment

This commit is contained in:
Matteo Pagliazzi
2020-11-30 11:39:39 +01:00
parent 7745a0e65a
commit 4e9a2de527
2 changed files with 23 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import nconf from 'nconf';
import Amplitude from 'amplitude'; import Amplitude from 'amplitude';
import { Visitor } from 'universal-analytics'; import { Visitor } from 'universal-analytics';
import * as analyticsService from '../../../../website/server/libs/analyticsService'; import * as analyticsService from '../../../../website/server/libs/analyticsService';
@@ -15,6 +16,22 @@ describe('analyticsService', () => {
sandbox.restore(); sandbox.restore();
}); });
describe('#getServiceByEnvironment', () => {
it('returns mock methods when not in production', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(false);
expect(analyticsService.getAnalyticsServiceByEnvironment())
.to.equal(analyticsService.mockAnalyticsService);
});
it('returns real methods when in production', () => {
sandbox.stub(nconf, 'get').withArgs('IS_PROD').returns(true);
expect(analyticsService.getAnalyticsServiceByEnvironment().track)
.to.equal(analyticsService.track);
expect(analyticsService.getAnalyticsServiceByEnvironment().trackPurchase)
.to.equal(analyticsService.trackPurchase);
});
});
describe('#track', () => { describe('#track', () => {
let eventType; let let eventType; let
data; data;

View File

@@ -32,8 +32,8 @@ describe('payments/index', () => {
sandbox.stub(sender, 'sendTxn'); sandbox.stub(sender, 'sendTxn');
sandbox.stub(user, 'sendMessage'); sandbox.stub(user, 'sendMessage');
sandbox.stub(analytics, 'trackPurchase'); sandbox.stub(analytics.mockAnalyticsService, 'trackPurchase');
sandbox.stub(analytics, 'track'); sandbox.stub(analytics.mockAnalyticsService, 'track');
sandbox.stub(notifications, 'sendNotification'); sandbox.stub(notifications, 'sendNotification');
data = { data = {
@@ -237,8 +237,8 @@ describe('payments/index', () => {
it('tracks subscription purchase as gift', async () => { it('tracks subscription purchase as gift', async () => {
await api.createSubscription(data); await api.createSubscription(data);
expect(analytics.trackPurchase).to.be.calledOnce; expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledOnce;
expect(analytics.trackPurchase).to.be.calledWith({ expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledWith({
uuid: user._id, uuid: user._id,
groupId: undefined, groupId: undefined,
itemPurchased: 'Subscription', itemPurchased: 'Subscription',
@@ -335,8 +335,8 @@ describe('payments/index', () => {
it('tracks subscription purchase', async () => { it('tracks subscription purchase', async () => {
await api.createSubscription(data); await api.createSubscription(data);
expect(analytics.trackPurchase).to.be.calledOnce; expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledOnce;
expect(analytics.trackPurchase).to.be.calledWith({ expect(analytics.mockAnalyticsService.trackPurchase).to.be.calledWith({
uuid: user._id, uuid: user._id,
groupId: undefined, groupId: undefined,
itemPurchased: 'Subscription', itemPurchased: 'Subscription',