Skip to content

Instantly share code, notes, and snippets.

@SabreCat
Last active August 29, 2015 14:22
Show Gist options
  • Save SabreCat/a1e559cb636c05cb0e0c to your computer and use it in GitHub Desktop.
Save SabreCat/a1e559cb636c05cb0e0c to your computer and use it in GitHub Desktop.
Analytics service TDD WIP
/**
* Created by Sabe on 6/11/2015.
*/
'use strict';
describe('Analytics Service', function () {
var analytics;
var onScriptLoad;
beforeEach(function() {
inject(function(Analytics) {
analytics = Analytics;
});
});
describe('Amplitude', function() {
before(function() {
sinon.stub(amplitude, 'setUserId');
sinon.stub(amplitude, 'logEvent');
sinon.stub(amplitude, 'setUserProperties');
});
afterEach(function() {
amplitude.setUserId.reset();
amplitude.logEvent.reset();
amplitude.setUserProperties.reset();
});
after(function() {
amplitude.setUserId.restore();
amplitude.logEvent.restore();
amplitude.setUserProperties.restore();
});
sinon.stub($, 'getScript').returns(function(name,callback) {
onScriptLoad = callback;
});
it('does not call provider without script loaded', function() {
analytics.login();
expect(amplitude.setUserId).to.have.been.notCalled;
});
it('puts data on a queue while waiting for script', function() {
analytics.track('action');
expect(amplitudeCache).to.eql([['setUserId'],['logEvent','action']]);
});
it('sends queued data when script loads', function() {
onScriptLoad();
expect(amplitude.setUserId).to.have.been.calledOnce;
expect(amplitude.logEvent).to.have.been.calledOnce;
expect(amplitude.logEvent).to.have.been.calledWith('action');
});
it('flushes the cache after sending data', function() {
expect(amplitudeCache).to.not.exist;
});
it('sets up tracking when user registers', function() {
analytics.register();
expect(amplitude.setUserId).to.have.been.calledOnce;
});
it('sets up tracking when user logs in', function() {
analytics.login();
expect(amplitude.setUserId).to.have.been.calledOnce;
});
it('tracks a simple user action', function() {
analytics.track('action');
expect(amplitude.logEvent).to.have.been.calledOnce;
expect(amplitude.logEvent).to.have.been.calledWith('action');
});
it('tracks a user action with properties', function() {
analytics.track('action',{'booleanProperty': true, 'numericProperty': 17, 'stringProperty': 'bagel'});
expect(amplitude.logEvent).to.have.been.calledOnce;
expect(amplitude.logEvent).to.have.been.calledWith('action',{'booleanProperty': true, 'numericProperty': 17, 'stringProperty': 'bagel'});
});
it('updates user-level properties', function() {
analytics.updateUser({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
expect(amplitude.setUserProperties).to.have.been.calledOnce;
expect(amplitude.setUserProperties).to.have.been.calledWith({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment