mirror of
https://github.com/bower/bower.git
synced 2026-04-24 03:00:19 -04:00
Add tests to analytics and fix them
This commit is contained in:
@@ -2,15 +2,13 @@ var Q = require('q');
|
||||
var mout = require('mout');
|
||||
|
||||
var analytics = module.exports;
|
||||
|
||||
var insight;
|
||||
|
||||
// Initializes the application-wide insight singleton and asks for the
|
||||
// permission on the CLI during the first run.
|
||||
analytics.setup = function setup(config) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
// if `analytics` hasn't been explicitly set
|
||||
if (config.analytics == null) {
|
||||
// Insight takes long to load, and often causes problems
|
||||
// in non-interactive environment, so we load it lazily
|
||||
function ensureInsight () {
|
||||
if (!insight) {
|
||||
var Insight = require('insight');
|
||||
var pkg = require('../../package.json');
|
||||
insight = new Insight({
|
||||
@@ -18,6 +16,17 @@ analytics.setup = function setup(config) {
|
||||
packageName: pkg.name,
|
||||
packageVersion: pkg.version
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Initializes the application-wide insight singleton and asks for the
|
||||
// permission on the CLI during the first run.
|
||||
analytics.setup = function setup (config) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
// if `analytics` hasn't been explicitly set
|
||||
if (config.analytics == null) {
|
||||
ensureInsight();
|
||||
|
||||
// if there is a stored value
|
||||
if (insight.optOut !== undefined) {
|
||||
@@ -26,11 +35,10 @@ analytics.setup = function setup(config) {
|
||||
deferred.resolve();
|
||||
} else {
|
||||
if (config.interactive) {
|
||||
// prompt the user if this is an interactive session
|
||||
insight.askPermission(null, function(err, optOut) {
|
||||
// value is the *opposite* of user response
|
||||
// https://github.com/yeoman/insight/issues/31
|
||||
config.analytics = !optOut;
|
||||
insight.askPermission(null, function(err, optIn) {
|
||||
// optIn callback param was exactly opposite before 0.4.3
|
||||
// so we force at least insight@0.4.3 in package.json
|
||||
config.analytics = optIn;
|
||||
deferred.resolve();
|
||||
});
|
||||
} else {
|
||||
@@ -55,9 +63,8 @@ var Tracker = analytics.Tracker = function Tracker(config) {
|
||||
};
|
||||
|
||||
Tracker.prototype.track = function track() {
|
||||
if (!insight) {
|
||||
throw new Error('You must call analytics.setup() prior to tracking.');
|
||||
}
|
||||
ensureInsight();
|
||||
|
||||
insight.track.apply(insight, arguments);
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
"graceful-fs": "~3.0.1",
|
||||
"handlebars": "~2.0.0",
|
||||
"inquirer": "~0.7.1",
|
||||
"insight": "~0.4.1",
|
||||
"insight": "~0.4.3",
|
||||
"is-root": "~1.0.0",
|
||||
"junk": "~1.0.0",
|
||||
"lockfile": "~1.0.0",
|
||||
|
||||
105
test/util/analytics.js
Normal file
105
test/util/analytics.js
Normal file
@@ -0,0 +1,105 @@
|
||||
var expect = require('expect.js');
|
||||
var proxyquire = require('proxyquire');
|
||||
var object = require('mout').object;
|
||||
|
||||
describe('analytics', function () {
|
||||
|
||||
var mockAnalytics = function(stubs, promptResponse) {
|
||||
return proxyquire('../../lib/util/analytics', {
|
||||
insight: function () {
|
||||
return object.merge(stubs || {}, {
|
||||
askPermission: function (message, callback) {
|
||||
callback(undefined, promptResponse);
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
describe('#setup', function () {
|
||||
it('leaves analytics enabled if provided', function () {
|
||||
var config = { analytics: true };
|
||||
|
||||
return mockAnalytics().setup(config).then(function () {
|
||||
expect(config.analytics).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('leaves analytics disabled if provided', function () {
|
||||
var config = { analytics: false };
|
||||
|
||||
return mockAnalytics().setup(config).then(function () {
|
||||
expect(config.analytics).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults to false if insight.optOut is true', function () {
|
||||
var config = { };
|
||||
|
||||
return mockAnalytics({ optOut: true }).setup(config).then(function () {
|
||||
expect(config.analytics).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults to true if insight.optOut is false', function () {
|
||||
var config = { };
|
||||
|
||||
return mockAnalytics({ optOut: false }).setup(config).then(function () {
|
||||
expect(config.analytics).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults to true if insight.optOut is undefined and noninteractive', function () {
|
||||
var config = { };
|
||||
|
||||
return mockAnalytics({ optOut: undefined }).setup(config).then(function () {
|
||||
expect(config.analytics).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('defautls to true if interactive insights return true from prompt', function () {
|
||||
var config = { interactive: true };
|
||||
|
||||
return mockAnalytics({ optOut: undefined }, true).setup(config).then(function () {
|
||||
expect(config.analytics).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('defautls to false if interactive insights return false from prompt', function () {
|
||||
var config = { interactive: true };
|
||||
|
||||
return mockAnalytics({ optOut: undefined }, false).setup(config).then(function () {
|
||||
expect(config.analytics).to.be(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Tracker', function (next) {
|
||||
it('tracks if analytics = true', function(next) {
|
||||
var analytics = mockAnalytics({
|
||||
track: function (arg) {
|
||||
expect(arg).to.be('foo');
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
new analytics.Tracker({
|
||||
analytics: true
|
||||
}).track('foo');
|
||||
});
|
||||
|
||||
it('does not track if analytics = false', function () {
|
||||
var analytics = mockAnalytics({
|
||||
track: function (arg) {
|
||||
throw new Error();
|
||||
}
|
||||
});
|
||||
|
||||
expect(function () {
|
||||
new analytics.Tracker({
|
||||
analytics: false
|
||||
}).track('foo');
|
||||
}).to.not.throwError();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
describe('util', function () {
|
||||
require('./removeIgnores');
|
||||
require('./analytics');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user