From 844c8fb45ac6b5bd3f792a65e22b36e4844d1b26 Mon Sep 17 00:00:00 2001 From: Beatriz Mendes Date: Tue, 28 Mar 2023 13:23:27 +0200 Subject: [PATCH] fix(client): `ping` event configured after enable --- .../UserJourneyStatistics.js | 9 ++- .../__tests__/UserJourneyStatisticsSpec.js | 9 ++- .../event-handlers/PingEventHandler.js | 11 ++- .../__tests__/PingEventHandlerSpec.js | 70 +++++++++++++++---- 4 files changed, 80 insertions(+), 19 deletions(-) diff --git a/client/src/plugins/user-journey-statistics/UserJourneyStatistics.js b/client/src/plugins/user-journey-statistics/UserJourneyStatistics.js index 8cf3da43f9..5580d0dc25 100644 --- a/client/src/plugins/user-journey-statistics/UserJourneyStatistics.js +++ b/client/src/plugins/user-journey-statistics/UserJourneyStatistics.js @@ -75,13 +75,20 @@ export default class UserJourneyStatistics extends PureComponent { log('Enabling'); this.mixpanel.enable(this.MIXPANEL_TOKEN, this._editorID, this.MIXPANEL_STAGE); + + this.emit('telemetry.enabled'); }; + emit(event, payload) { + this.props.triggerAction('emit-event', { type: event, payload }); + } + disable = () => { log('Disabling.'); this.mixpanel.disable(); - this.emit('telemetry:disabled'); + + this.emit('telemetry.disabled'); }; async setEditorId() { diff --git a/client/src/plugins/user-journey-statistics/__tests__/UserJourneyStatisticsSpec.js b/client/src/plugins/user-journey-statistics/__tests__/UserJourneyStatisticsSpec.js index 900195ed96..c8f2c0ff77 100644 --- a/client/src/plugins/user-journey-statistics/__tests__/UserJourneyStatisticsSpec.js +++ b/client/src/plugins/user-journey-statistics/__tests__/UserJourneyStatisticsSpec.js @@ -176,6 +176,8 @@ describe('', () => { } }); + const emitSpy = sinon.stub(instance, 'emit'); + const mixpanel = instance.mixpanel; expect(mixpanel.isEnabled()).to.be.false; @@ -185,6 +187,7 @@ describe('', () => { // then expect(mixpanel.isEnabled()).to.be.true; + expect(emitSpy).to.have.been.calledWith('telemetry.enabled'); }); @@ -202,6 +205,8 @@ describe('', () => { } }); + const emitSpy = sinon.stub(instance, 'emit'); + const mixpanel = instance.mixpanel; // when @@ -211,6 +216,7 @@ describe('', () => { // then expect(mixpanel.isEnabled()).to.eql(false); + expect(emitSpy).to.have.been.calledWith('telemetry.disabled'); }); @@ -363,7 +369,8 @@ function createJourneyStatistics(props = {}) { }); } }, - _getGlobal: () => ({}) + _getGlobal: () => ({}), + triggerAction: () => {} }); } diff --git a/client/src/plugins/user-journey-statistics/event-handlers/PingEventHandler.js b/client/src/plugins/user-journey-statistics/event-handlers/PingEventHandler.js index 6a18e42e18..e5b594bb96 100644 --- a/client/src/plugins/user-journey-statistics/event-handlers/PingEventHandler.js +++ b/client/src/plugins/user-journey-statistics/event-handlers/PingEventHandler.js @@ -16,7 +16,8 @@ export default class PingEventHandler { constructor(props) { const { - track + track, + subscribe } = props; this.track = track; @@ -25,7 +26,13 @@ export default class PingEventHandler { this.sentInitially = false; this._intervalID = null; - this.configurePing(); + subscribe('telemetry.enabled', () => { + this.configurePing(); + }); + + subscribe('telemetry.disabled', () => { + clearInterval(this._intervalID); + }); } getPlugins = () => { diff --git a/client/src/plugins/user-journey-statistics/event-handlers/__tests__/PingEventHandlerSpec.js b/client/src/plugins/user-journey-statistics/event-handlers/__tests__/PingEventHandlerSpec.js index ee51d08466..1d560abd69 100644 --- a/client/src/plugins/user-journey-statistics/event-handlers/__tests__/PingEventHandlerSpec.js +++ b/client/src/plugins/user-journey-statistics/event-handlers/__tests__/PingEventHandlerSpec.js @@ -19,13 +19,14 @@ import Flags from '../../../../util/Flags'; describe('', () => { let track = sinon.spy(); + const subscribe = sinon.spy(); beforeEach(() => { const getGlobal = () => ({ appPlugins: [ { name: 'pluginName' } ] }); - new PingEventHandler({ track, getGlobal }); + new PingEventHandler({ track, getGlobal, subscribe }); MixpanelHandler.getInstance().enable('token', 'id', 'stage'); }); @@ -33,9 +34,36 @@ describe('', () => { afterEach(sinon.restore); + describe('should subscribe', () => { + + it('should subscribe to telemetry.enabled', () => { + expect(subscribe.getCall(0).args[0]).to.eql('telemetry.enabled'); + }); + + + it('should subscribe to telemetry.disabled', () => { + expect(subscribe.getCall(1).args[0]).to.eql('telemetry.disabled'); + }); + + }); + + describe('should send', () => { - it('should send initially', () => { + it('should not send before enabled', async () => { + + // then + expect(track).to.not.have.been.called; + }); + + + it('should send initially', async () => { + + // given + const handleTelemetryActivation = subscribe.getCall(0).args[1]; + + // when + await handleTelemetryActivation(); // then expect(track).to.have.been.called; @@ -57,7 +85,13 @@ describe('', () => { describe('plugins', () => { - it('should send installed plugins', () => { + it('should send installed plugins', async () => { + + // given + const handleTelemetryActivation = subscribe.getCall(0).args[1]; + + // when + await handleTelemetryActivation(); // then expect(track).to.have.been.calledWith('ping', { @@ -70,11 +104,23 @@ describe('', () => { describe('flags', () => { + let track; + + const handlePing = async () => { + track = sinon.spy(); + const subscribe = sinon.spy(); + + new PingEventHandler({ track, getGlobal: () => ({}), subscribe }); + const handleTelemetryActivation = subscribe.getCall(0).args[1]; + + await handleTelemetryActivation(); + }; + afterEach(() => { Flags.reset(); }); - it('should send set flags', () => { + it('should send set flags', async () => { // given Flags.init({ @@ -82,10 +128,8 @@ describe('', () => { myFlagB: false }); - const track = sinon.spy(); - // when - new PingEventHandler({ track, getGlobal: () => ({}) }); + await handlePing(); // then expect(track).to.have.been.calledWith('ping', { @@ -98,17 +142,15 @@ describe('', () => { }); - it('should mask non-boolean flags', () => { + it('should mask non-boolean flags', async () => { // given Flags.init({ flagWithPrivateDataSet: 'my/custom/filepath' }); - const track = sinon.spy(); - // when - new PingEventHandler({ track, getGlobal: () => ({}) }); + await handlePing(); // then expect(track).to.have.been.calledWith('ping', { @@ -118,17 +160,15 @@ describe('', () => { }); - it('should not overwrite original Flags through masking', () => { + it('should not overwrite original Flags through masking', async () => { // given Flags.init({ flagWithPrivateDataSet: 'my/custom/filepath' }); - const track = sinon.spy(); - // when - new PingEventHandler({ track, getGlobal: () => ({}) }); + await handlePing(); // then expect(Flags.data.flagWithPrivateDataSet).to.eql('my/custom/filepath');