From 2143180d27d9ba0939a365ebcfd56fa069f6e2c8 Mon Sep 17 00:00:00 2001 From: Roman Mazur Date: Wed, 2 Oct 2019 17:43:41 +0300 Subject: [PATCH] event-tracker: Use std mixpanel types Custom type definitions are removed for mixpanel module since they are embedded into their npm package. Change-type: patch Signed-off-by: Roman Mazur --- src/event-tracker.ts | 13 +++++++------ test/09-event-tracker.spec.ts | 17 +++++++++++------ typings/mixpanel.d.ts | 1 - 3 files changed, 18 insertions(+), 13 deletions(-) delete mode 100644 typings/mixpanel.d.ts diff --git a/src/event-tracker.ts b/src/event-tracker.ts index a7800c2f..a4e27d0b 100644 --- a/src/event-tracker.ts +++ b/src/event-tracker.ts @@ -2,8 +2,7 @@ import * as Bluebird from 'bluebird'; import mask = require('json-mask'); import * as _ from 'lodash'; import * as memoizee from 'memoizee'; - -import Mixpanel = require('mixpanel'); +import * as mixpanel from 'mixpanel'; import { ConfigType } from './config'; import log from './lib/supervisor-console'; @@ -35,7 +34,7 @@ const mixpanelMask = [ export class EventTracker { private defaultProperties: EventTrackProperties | null; - private client: any; + private client: mixpanel.Mixpanel | null; public constructor() { this.client = null; @@ -54,10 +53,10 @@ export class EventTracker { uuid, supervisorVersion, }; - if (unmanaged || mixpanelHost == null) { + if (unmanaged || mixpanelHost == null || mixpanelToken == null) { return; } - this.client = Mixpanel.init(mixpanelToken, { + this.client = mixpanel.init(mixpanelToken, { host: mixpanelHost.host, path: mixpanelHost.path, }); @@ -94,7 +93,9 @@ export class EventTracker { // Call this function at maximum once every minute return _.throttle( properties => { - this.client.track(event, properties); + if (this.client != null) { + this.client.track(event, properties); + } }, eventDebounceTime, { leading: true }, diff --git a/test/09-event-tracker.spec.ts b/test/09-event-tracker.spec.ts index 47030a72..1fb15e69 100644 --- a/test/09-event-tracker.spec.ts +++ b/test/09-event-tracker.spec.ts @@ -1,5 +1,6 @@ +import { Mixpanel } from 'mixpanel'; import * as mixpanel from 'mixpanel'; -import { stub } from 'sinon'; +import { SinonStub, stub } from 'sinon'; import { expect } from './lib/chai-config'; @@ -9,12 +10,16 @@ import supervisorVersion = require('../src/lib/supervisor-version'); describe('EventTracker', () => { let eventTrackerOffline: EventTracker; let eventTracker: EventTracker; + let initStub: SinonStub; before(() => { - stub(mixpanel, 'init').callsFake(token => ({ - token, - track: stub().returns(undefined), - })); + initStub = stub(mixpanel, 'init').callsFake( + token => + (({ + token, + track: stub().returns(undefined), + } as unknown) as Mixpanel), + ); eventTrackerOffline = new EventTracker(); eventTracker = new EventTracker(); @@ -23,7 +28,7 @@ describe('EventTracker', () => { after(() => { (EventTracker.prototype as any).logEvent.restore(); - return mixpanel.init.restore(); + return initStub.restore(); }); it('initializes in unmanaged mode', () => { diff --git a/typings/mixpanel.d.ts b/typings/mixpanel.d.ts deleted file mode 100644 index 271b8d30..00000000 --- a/typings/mixpanel.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'mixpanel';