Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "ping" event #3521

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export default class UserJourneyStatistics extends PureComponent {
this.MIXPANEL_TOKEN = Flags.get(MIXPANEL_TOKEN) || DEFINED_MIXPANEL_TOKEN;
this.MIXPANEL_STAGE = Flags.get(MIXPANEL_STAGE) || DEFINED_MIXPANEL_STAGE;

this._isEnabled = false;

this._buttonRef = React.createRef(null);

this.mixpanel = MixpanelHandler.getInstance();
Expand All @@ -70,21 +68,27 @@ export default class UserJourneyStatistics extends PureComponent {
}

isEnabled = () => {
return this._isEnabled;
return MixpanelHandler.getInstance().isEnabled();
};

enable = () => {
log('Enabling');

this.mixpanel.enable(this.MIXPANEL_TOKEN, this._editorID, this.MIXPANEL_STAGE);
this._isEnabled = true;

this.emit('telemetry.enabled');
};

emit(event, payload) {
this.props.triggerAction('emit-event', { type: event, payload });
}

disable = () => {
log('Disabling.');

this.mixpanel.disable();
this._isEnabled = false;

this.emit('telemetry.disabled');
};

async setEditorId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ describe('<UserJourneyStatistics>', () => {
}
});

const emitSpy = sinon.stub(instance, 'emit');

const mixpanel = instance.mixpanel;

expect(mixpanel.isEnabled()).to.be.false;
Expand All @@ -185,6 +187,7 @@ describe('<UserJourneyStatistics>', () => {

// then
expect(mixpanel.isEnabled()).to.be.true;
expect(emitSpy).to.have.been.calledWith('telemetry.enabled');
});


Expand All @@ -202,6 +205,8 @@ describe('<UserJourneyStatistics>', () => {
}
});

const emitSpy = sinon.stub(instance, 'emit');

const mixpanel = instance.mixpanel;

// when
Expand All @@ -211,6 +216,7 @@ describe('<UserJourneyStatistics>', () => {

// then
expect(mixpanel.isEnabled()).to.eql(false);
expect(emitSpy).to.have.been.calledWith('telemetry.disabled');
});


Expand Down Expand Up @@ -363,7 +369,8 @@ function createJourneyStatistics(props = {}) {
});
}
},
_getGlobal: () => ({})
_getGlobal: () => ({}),
triggerAction: () => {}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default class PingEventHandler {
constructor(props) {

const {
track
track,
subscribe
} = props;

this.track = track;
Expand All @@ -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 = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,51 @@ import Flags from '../../../../util/Flags';
describe('<PingEventHandlerSpec>', () => {

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');
});

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;
Expand All @@ -57,7 +85,13 @@ describe('<PingEventHandlerSpec>', () => {

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', {
Expand All @@ -70,22 +104,32 @@ describe('<PingEventHandlerSpec>', () => {

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({
myFlagA: true,
myFlagB: false
});

const track = sinon.spy();

// when
new PingEventHandler({ track, getGlobal: () => ({}) });
await handlePing();

// then
expect(track).to.have.been.calledWith('ping', {
Expand All @@ -98,17 +142,15 @@ describe('<PingEventHandlerSpec>', () => {
});


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', {
Expand All @@ -118,17 +160,15 @@ describe('<PingEventHandlerSpec>', () => {
});


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');
Expand Down