From 21f3879f4e6151e936cec7108b0fe3f97653ca0a Mon Sep 17 00:00:00 2001 From: Stephen Mathieson Date: Mon, 13 Mar 2017 18:41:19 -0700 Subject: [PATCH] add `mock.js` This patch adds a `mock.js` file, which we'll be able to use in development/test/etc (anywhere you don't want to send real events). I'm envisioning usage looking something like: ```js const { NODE_ENV = 'development' } = process.env const Analytics = NODE_ENV === 'production' ? require('analytics-node') : require('analytics-node/mock') const analytics = new Analytics('abc write key', { some: 'options' }) analytics.track({ userId: 'aaa', event: 'Did Something' }) ``` Closes #75. --- mock.js | 25 +++++++++++++++++++++++++ test/mock.js | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 mock.js create mode 100644 test/mock.js diff --git a/mock.js b/mock.js new file mode 100644 index 00000000..6aa1e409 --- /dev/null +++ b/mock.js @@ -0,0 +1,25 @@ +var Analytics = require('./') + +var temp = new Analytics('fakekey') + +module.exports = AnalyticsMock + +function AnalyticsMock () { + if (!(this instanceof AnalyticsMock)) { + return new AnalyticsMock() + } +} + +for (var key in temp) { + var fn = temp[key] + if (typeof fn === 'function') { + AnalyticsMock.prototype[key] = mock + } +} + +function mock () { + var callback = arguments[arguments.length - 1] + if (typeof callback === 'function') { + process.nextTick(callback) + } +} diff --git a/test/mock.js b/test/mock.js new file mode 100644 index 00000000..ae3a14a5 --- /dev/null +++ b/test/mock.js @@ -0,0 +1,27 @@ +/* global describe, it */ + +var assert = require('assert') +var AnalyticsMock = require('../mock') + +describe('AnalyticsMock', function () { + it('should not require `new`', function () { + var mock = AnalyticsMock() + assert(mock instanceof AnalyticsMock) + }) + + it('should expose all methods', function () { + var mock = new AnalyticsMock() + assert.equal(typeof mock.identify, 'function') + assert.equal(typeof mock.group, 'function') + assert.equal(typeof mock.track, 'function') + assert.equal(typeof mock.page, 'function') + assert.equal(typeof mock.screen, 'function') + assert.equal(typeof mock.alias, 'function') + assert.equal(typeof mock.flush, 'function') + }) + + it('should callback', function (done) { + var mock = new AnalyticsMock() + mock.track({ foo: 'bar' }, done) + }) +})