Skip to content

Commit

Permalink
tests.. readme
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Aug 19, 2016
1 parent f3b0b2a commit 511dbaa
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 33 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ const usageStats = new UsageStats('UA-98765432-1', {
// start a new session
usageStats.start()

// user set an option..
// user sets an option..
usageStats.event('option', 'verbose-level', 'infinite')

// app is running in 'encoding' mode..
usageStats.screenView('encoding')

try {
// register a hit on 'encoding mode'
usageStats.screenView('encoding')
beginEncoding(options)
} catch (err) {
// exception tracking
Expand All @@ -39,7 +38,7 @@ try {
usageStats.end().send()
```

## List of stats sent
## List of metrics sent

Beside tracking events, exceptions and screenviews, the follow stats are collected each session.

Expand Down
4 changes: 2 additions & 2 deletions es5/lib/usage-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ var UsageStats = function () {
var reqOptions = url.parse(gaUrl.debug);
reqOptions.method = 'POST';
return this._request(reqOptions, createHitsPayload(toSend)).then(function (response) {
var output = {
_this._enqueue(toSend);
return {
hits: toSend,
result: JSON.parse(response.data.toString())
};
return JSON.stringify(output, null, ' ');
}).catch(function (err) {
if (err.code === 'ENOENT') {
return {
Expand Down
55 changes: 55 additions & 0 deletions es5/test/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

var test = require('test-runner');
var UsageStats = require('../lib/usage-stats');
var a = require('core-assert');
var fs = require('fs');
var path = require('path');
var os = require('os');
var rimraf = require('rimraf');

var tmpPath = path.resolve(__dirname, '../../tmp/test');
function getCacheDir(index) {
var dir = path.resolve(tmpPath, 'test' + index);
rimraf.sync(dir);
return dir;
}

try {
fs.mkdirSync(tmpPath);
} catch (err) {}

test('.send({ debug: true }) - screenview', function () {
var testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir(this.index)
});

testStats.screenView(this.name);
return testStats.send({ debug: true }).then(function (response) {
a.strictEqual(response.hits.length, 1);
a.ok(/t=screenview/.test(response.hits[0]));
a.strictEqual(response.result.hitParsingResult[0].valid, true);
var queued = testStats._dequeue();
a.strictEqual(queued.length, 1);
});
});

test('.send({ debug: true }) - screenview with a queue', function () {
var testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir(this.index)
});
testStats._enqueue(['v=1']);
testStats.screenView(this.name);
return testStats.send({ debug: true }).then(function (response) {
a.strictEqual(response.hits.length, 2);
a.ok(/t=screenview/.test(response.hits[1]));
a.strictEqual(response.result.hitParsingResult[1].valid, true);
var queued = testStats._dequeue();

a.strictEqual(queued.length, 2);
});
});
20 changes: 11 additions & 9 deletions es5/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ var a = require('core-assert');
var fs = require('fs');
var path = require('path');
var os = require('os');
var rimraf = require('rimraf');

var tmpPath = path.resolve(__dirname, '../../tmp/test');
var pathCount = 0;
function getCacheDir() {
return path.resolve(tmpPath, 'test' + pathCount++);
function getCacheDir(index) {
var dir = path.resolve(tmpPath, 'test' + index);
rimraf.sync(dir);
return dir;
}

try {
Expand Down Expand Up @@ -55,7 +57,7 @@ test('.event() validation', function () {
});

test('._enqueue(hits)', function () {
var testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir() });
var testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir(this.index) });
fs.writeFileSync(testStats._queuePath, '');
testStats._enqueue(['hit1', 'hit2']);
testStats._enqueue(['hit3']);
Expand All @@ -65,7 +67,7 @@ test('._enqueue(hits)', function () {
});

test('._dequeue(count)', function () {
var testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir() });
var testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir(this.index) });
fs.writeFileSync(testStats._queuePath, '');
testStats._enqueue(['hit1', 'hit2', 'hit3', 'hit4']);

Expand Down Expand Up @@ -104,7 +106,7 @@ test('successful send, nothing queued', function () {
return UsageTest;
}(UsageStats);

var testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir() });
var testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir(this.index) });
testStats.screenView('test');
return testStats.send().then(function (responses) {
var queued = testStats._dequeue();
Expand Down Expand Up @@ -135,7 +137,7 @@ test('failed send, something queued', function () {
return UsageTest;
}(UsageStats);

var testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir() });
var testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir(this.index) });
testStats.screenView('test');
return testStats.send().then(function (responses) {
var queued = testStats._dequeue();
Expand All @@ -147,7 +149,7 @@ test('.send() screenview (live)', function () {
var testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir()
dir: getCacheDir(this.index)
});

testStats.screenView(this.name);
Expand Down Expand Up @@ -187,7 +189,7 @@ test.skip('successful send with something queued', function () {
var testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir()
dir: getCacheDir(this.index)
});

testStats.screenView('test');
Expand Down
9 changes: 4 additions & 5 deletions jsdoc2md/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ const usageStats = new UsageStats('UA-98765432-1', {
// start a new session
usageStats.start()

// user set an option..
// user sets an option..
usageStats.event('option', 'verbose-level', 'infinite')

// app is running in 'encoding' mode..
usageStats.screenView('encoding')

try {
// register a hit on 'encoding mode'
usageStats.screenView('encoding')
beginEncoding(options)
} catch (err) {
// exception tracking
Expand All @@ -39,7 +38,7 @@ try {
usageStats.end().send()
```

## List of stats sent
## List of metrics sent

Beside tracking events, exceptions and screenviews, the follow stats are collected each session.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"core-assert": "~0.2.0",
"coveralls": "^2.11.12",
"jsdoc-to-markdown": "^2.0.0-alpha.5",
"rimraf": "^2.5.4",
"test-runner": "~0.1.12"
},
"standard": {
Expand Down
11 changes: 8 additions & 3 deletions src/lib/usage-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ class UsageStats {
const url = require('url')
const requests = []
if (options.debug) {
this._enqueue(toSend)
const reqOptions = url.parse(gaUrl.debug)
reqOptions.method = 'POST'
return this._request(reqOptions, createHitsPayload(toSend))
.then(response => {
const output = {
return {
hits: toSend,
result: JSON.parse(response.data.toString())
}
return JSON.stringify(output, null, ' ')
})
.catch(err => {
if (err.code === 'ENOENT') {
Expand Down Expand Up @@ -275,6 +275,7 @@ class UsageStats {
* @param [count] {number} - Number of hits to dequeue. Defaults to "all hits".
* @return {string[]}
* @private
* @sync
*/
_dequeue (count) {
try {
Expand All @@ -300,8 +301,10 @@ class UsageStats {
}

/**
* @param {string[]}
* Append an array of hits to the queue.
* @param {string[]} - Array of hits.
* @private
* @sync
*/
_enqueue (hits) {
fs.appendFileSync(this._queuePath, createHitsPayload(hits))
Expand Down Expand Up @@ -329,3 +332,5 @@ function createHitsPayload (array) {
}

module.exports = UsageStats

/* running jsdoc2m --debug offline, with a queue returns nothing and deletes the queue */
59 changes: 59 additions & 0 deletions src/test/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'
const test = require('test-runner')
const UsageStats = require('../lib/usage-stats')
const a = require('core-assert')
const fs = require('fs')
const path = require('path')
const os = require('os')
const rimraf = require('rimraf')

const tmpPath = path.resolve(__dirname, '../../tmp/test')
function getCacheDir (index) {
const dir = path.resolve(tmpPath, 'test' + index)
rimraf.sync(dir)
return dir
}

try {
fs.mkdirSync(tmpPath)
} catch (err) {
// exists
}

test('.send({ debug: true }) - screenview', function () {
const testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir(this.index)
})

testStats.screenView(this.name)
return testStats.send({ debug: true })
.then(response => {
a.strictEqual(response.hits.length, 1)
a.ok(/t=screenview/.test(response.hits[0]))
a.strictEqual(response.result.hitParsingResult[0].valid, true)
const queued = testStats._dequeue()
a.strictEqual(queued.length, 1)
})
})

test('.send({ debug: true }) - screenview with a queue', function () {
const testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir(this.index)
})
testStats._enqueue([ 'v=1' ])
testStats.screenView(this.name)
return testStats.send({ debug: true })
.then(response => {
// console.error(require('util').inspect(response, { depth: 3, colors: true }))
a.strictEqual(response.hits.length, 2)
a.ok(/t=screenview/.test(response.hits[1]))
a.strictEqual(response.result.hitParsingResult[1].valid, true)
const queued = testStats._dequeue()
// console.error(queued)
a.strictEqual(queued.length, 2)
})
})
20 changes: 11 additions & 9 deletions src/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const a = require('core-assert')
const fs = require('fs')
const path = require('path')
const os = require('os')
const rimraf = require('rimraf')

const tmpPath = path.resolve(__dirname, '../../tmp/test')
let pathCount = 0
function getCacheDir () {
return path.resolve(tmpPath, 'test' + pathCount++)
function getCacheDir (index) {
const dir = path.resolve(tmpPath, 'test' + index)
rimraf.sync(dir)
return dir
}

try {
Expand Down Expand Up @@ -48,7 +50,7 @@ test('.event() validation', function () {
})

test('._enqueue(hits)', function () {
const testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir() })
const testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir(this.index) })
fs.writeFileSync(testStats._queuePath, '')
testStats._enqueue([ 'hit1', 'hit2' ])
testStats._enqueue([ 'hit3' ])
Expand All @@ -58,7 +60,7 @@ test('._enqueue(hits)', function () {
})

test('._dequeue(count)', function () {
const testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir() })
const testStats = new UsageStats('UA-00000000-0', { dir: getCacheDir(this.index) })
fs.writeFileSync(testStats._queuePath, '')
testStats._enqueue([ 'hit1', 'hit2', 'hit3', 'hit4' ])

Expand All @@ -85,7 +87,7 @@ test('successful send, nothing queued', function () {
}
}

const testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir() })
const testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir(this.index) })
testStats.screenView('test')
return testStats.send()
.then(responses => {
Expand All @@ -105,7 +107,7 @@ test('failed send, something queued', function () {
}
}

const testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir() })
const testStats = new UsageTest('UA-00000000-0', { dir: getCacheDir(this.index) })
testStats.screenView('test')
return testStats.send()
.then(responses => {
Expand All @@ -118,7 +120,7 @@ test('.send() screenview (live)', function () {
const testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir()
dir: getCacheDir(this.index)
})

testStats.screenView(this.name)
Expand All @@ -145,7 +147,7 @@ test.skip('successful send with something queued', function () {
const testStats = new UsageStats('UA-70853320-3', {
name: 'usage-stats',
version: require('../../package').version,
dir: getCacheDir()
dir: getCacheDir(this.index)
})

testStats.screenView('test')
Expand Down

0 comments on commit 511dbaa

Please sign in to comment.