Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
feat(metrics): add tests and docs for front-end changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vladikoff committed May 29, 2018
1 parent a73b002 commit 17777c4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/scripts/lib/metrics.js
Expand Up @@ -208,7 +208,7 @@ define(function (require, exports, module) {
const flowModel = new Flow({
metrics: this,
sentryMetrics: this._sentryMetrics,
window: this._window,
window: this._window
});

if (flowModel.has('flowId')) {
Expand Down Expand Up @@ -459,6 +459,14 @@ define(function (require, exports, module) {
}
},

/**
* Marks some event already logged in metrics memory.
*
* Used in conjunction with `logEventOnce` when we know that some event was already logged elsewhere.
* Helps avoid event duplication.
*
* @param {String} eventName
*/
markEventLogged: function (eventName) {
this._eventMemory[eventName] = true;
},
Expand Down
12 changes: 12 additions & 0 deletions app/tests/spec/lib/metrics.js
Expand Up @@ -240,6 +240,18 @@ define(function (require, exports, module) {
});
});

describe('markEventLogged', function () {
it('does not log an event if marked logged', function () {
metrics.markEventLogged('event2');
metrics.logEventOnce('event1');
metrics.logEventOnce('event2');

const filteredData = metrics.getFilteredData();
assert.equal(filteredData.events.length, 1);
assert.equal(filteredData.events[0].type, 'event1');
});
});

describe('startTimer/stopTimer', function () {
it('adds a timer to output data', function () {
metrics.startTimer('timer1');
Expand Down
26 changes: 26 additions & 0 deletions app/tests/spec/models/flow.js
Expand Up @@ -21,18 +21,23 @@ define(function (require, exports, module) {
var flow;
var sentryMetricsMock;
var windowMock;
var metricsMock;

beforeEach(function () {
sentryMetricsMock = {
captureException: sinon.spy()
};
metricsMock = {
markEventLogged: sinon.spy()
};
windowMock = new WindowMock();
$(windowMock.document.body).removeData('flowId').removeAttr('data-flow-id');
$(windowMock.document.body).removeData('flowBegin').removeAttr('data-flow-begin');
});

function createFlow () {
flow = new Flow({
metrics: metricsMock,
sentryMetrics: sentryMetricsMock,
window: windowMock
});
Expand Down Expand Up @@ -76,6 +81,27 @@ define(function (require, exports, module) {
assert.equal(flow.get('flowBegin'), 42);
});

it('fetches from query parameters, if available', function () {
$(windowMock.document.body).attr('data-flow-id', BODY_FLOW_ID);
$(windowMock.document.body).attr('data-flow-begin', '42');

const QUERY_FLOW_BEGIN = '55';
const QUERY_FLOW_ID = 'A1031DF1031DF1031DF1031DF1031DF1031DF1031DF1031DF1031DF1031DF103';

windowMock.location.search = Url.objToSearchString({
/*eslint-disable camelcase*/
flow_begin_time: QUERY_FLOW_BEGIN,
flow_id: QUERY_FLOW_ID
/*eslint-enable camelcase*/
});

createFlow();

assert.equal(flow.get('flowId'), QUERY_FLOW_ID);
assert.equal(flow.get('flowBegin'), QUERY_FLOW_BEGIN);
assert.ok(metricsMock.markEventLogged.calledOnce);
});

it('logs an error when the resume token contains `flowId` but not `flowBegin`', function () {
windowMock.location.search = Url.objToSearchString({
resume: ResumeToken.stringify({ flowId: RESUME_FLOW_ID })
Expand Down

0 comments on commit 17777c4

Please sign in to comment.