Skip to content

Commit dfd2c7a

Browse files
committed
Add tests to analytics and fix them
1 parent 6637762 commit dfd2c7a

File tree

4 files changed

+129
-16
lines changed

4 files changed

+129
-16
lines changed

lib/util/analytics.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ var Q = require('q');
22
var mout = require('mout');
33

44
var analytics = module.exports;
5-
var insight;
65

7-
// Initializes the application-wide insight singleton and asks for the
8-
// permission on the CLI during the first run.
9-
analytics.setup = function setup(config) {
10-
var deferred = Q.defer();
6+
var insight;
117

12-
// if `analytics` hasn't been explicitly set
13-
if (config.analytics == null) {
8+
// Insight takes long to load, and often causes problems
9+
// in non-interactive environment, so we load it lazily
10+
function ensureInsight () {
11+
if (!insight) {
1412
var Insight = require('insight');
1513
var pkg = require('../../package.json');
1614
insight = new Insight({
1715
trackingCode: 'UA-43531210-1',
1816
packageName: pkg.name,
1917
packageVersion: pkg.version
2018
});
19+
}
20+
}
21+
22+
// Initializes the application-wide insight singleton and asks for the
23+
// permission on the CLI during the first run.
24+
analytics.setup = function setup (config) {
25+
var deferred = Q.defer();
26+
27+
// if `analytics` hasn't been explicitly set
28+
if (config.analytics == null) {
29+
ensureInsight();
2130

2231
// if there is a stored value
2332
if (insight.optOut !== undefined) {
@@ -26,11 +35,10 @@ analytics.setup = function setup(config) {
2635
deferred.resolve();
2736
} else {
2837
if (config.interactive) {
29-
// prompt the user if this is an interactive session
30-
insight.askPermission(null, function(err, optOut) {
31-
// value is the *opposite* of user response
32-
// https://github.com/yeoman/insight/issues/31
33-
config.analytics = !optOut;
38+
insight.askPermission(null, function(err, optIn) {
39+
// optIn callback param was exactly opposite before 0.4.3
40+
// so we force at least [email protected] in package.json
41+
config.analytics = optIn;
3442
deferred.resolve();
3543
});
3644
} else {
@@ -55,9 +63,8 @@ var Tracker = analytics.Tracker = function Tracker(config) {
5563
};
5664

5765
Tracker.prototype.track = function track() {
58-
if (!insight) {
59-
throw new Error('You must call analytics.setup() prior to tracking.');
60-
}
66+
ensureInsight();
67+
6168
insight.track.apply(insight, arguments);
6269
};
6370

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"graceful-fs": "~3.0.1",
3434
"handlebars": "~2.0.0",
3535
"inquirer": "~0.7.1",
36-
"insight": "~0.4.1",
36+
"insight": "~0.4.3",
3737
"is-root": "~1.0.0",
3838
"junk": "~1.0.0",
3939
"lockfile": "~1.0.0",

test/util/analytics.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
var expect = require('expect.js');
2+
var proxyquire = require('proxyquire');
3+
var object = require('mout').object;
4+
5+
describe('analytics', function () {
6+
7+
var mockAnalytics = function(stubs, promptResponse) {
8+
return proxyquire('../../lib/util/analytics', {
9+
insight: function () {
10+
return object.merge(stubs || {}, {
11+
askPermission: function (message, callback) {
12+
callback(undefined, promptResponse);
13+
}
14+
});
15+
},
16+
});
17+
};
18+
19+
describe('#setup', function () {
20+
it('leaves analytics enabled if provided', function () {
21+
var config = { analytics: true };
22+
23+
return mockAnalytics().setup(config).then(function () {
24+
expect(config.analytics).to.be(true);
25+
});
26+
});
27+
28+
it('leaves analytics disabled if provided', function () {
29+
var config = { analytics: false };
30+
31+
return mockAnalytics().setup(config).then(function () {
32+
expect(config.analytics).to.be(false);
33+
});
34+
});
35+
36+
it('defaults to false if insight.optOut is true', function () {
37+
var config = { };
38+
39+
return mockAnalytics({ optOut: true }).setup(config).then(function () {
40+
expect(config.analytics).to.be(false);
41+
});
42+
});
43+
44+
it('defaults to true if insight.optOut is false', function () {
45+
var config = { };
46+
47+
return mockAnalytics({ optOut: false }).setup(config).then(function () {
48+
expect(config.analytics).to.be(true);
49+
});
50+
});
51+
52+
it('defaults to true if insight.optOut is undefined and noninteractive', function () {
53+
var config = { };
54+
55+
return mockAnalytics({ optOut: undefined }).setup(config).then(function () {
56+
expect(config.analytics).to.be(true);
57+
});
58+
});
59+
60+
it('defautls to true if interactive insights return true from prompt', function () {
61+
var config = { interactive: true };
62+
63+
return mockAnalytics({ optOut: undefined }, true).setup(config).then(function () {
64+
expect(config.analytics).to.be(true);
65+
});
66+
});
67+
68+
it('defautls to false if interactive insights return false from prompt', function () {
69+
var config = { interactive: true };
70+
71+
return mockAnalytics({ optOut: undefined }, false).setup(config).then(function () {
72+
expect(config.analytics).to.be(false);
73+
});
74+
});
75+
});
76+
77+
describe('Tracker', function (next) {
78+
it('tracks if analytics = true', function(next) {
79+
var analytics = mockAnalytics({
80+
track: function (arg) {
81+
expect(arg).to.be('foo');
82+
next();
83+
}
84+
});
85+
86+
new analytics.Tracker({
87+
analytics: true
88+
}).track('foo');
89+
});
90+
91+
it('does not track if analytics = false', function () {
92+
var analytics = mockAnalytics({
93+
track: function (arg) {
94+
throw new Error();
95+
}
96+
});
97+
98+
expect(function () {
99+
new analytics.Tracker({
100+
analytics: false
101+
}).track('foo');
102+
}).to.not.throwError();
103+
});
104+
});
105+
});

test/util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
describe('util', function () {
22
require('./removeIgnores');
3+
require('./analytics');
34
});

0 commit comments

Comments
 (0)