Skip to content

Commit

Permalink
adding tests for lib/now.js
Browse files Browse the repository at this point in the history
  • Loading branch information
catdad committed Apr 13, 2016
1 parent 162553a commit 76c849c
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/now.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function now() {
}

function Timer(extras) {
if (!(this instanceof Timer)) {
return new Timer(extras);
}

this.times = {};

if (_.isPlainObject(extras)) {
Expand All @@ -37,7 +41,7 @@ Timer.prototype.end = function endTimer(id, status) {
// If this timer does not exist, or if it has already ended,
// return without updating the data.
if (!this.times[id] || this.times[id].end !== null) {
return;
return this;
}

this.times[id].end = now();
Expand Down
138 changes: 138 additions & 0 deletions test/now.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,141 @@
/* jshint node: true, mocha: true, expr: true */

var expect = require('chai').expect;
var now = require('../lib/now.js');

describe('[now]', function() {
it('is a function', function() {
expect(now).to.be.a('function');
});

it('returns a number in milliseconds', function() {
[now(), now(), now(), now(), now()].reduce(function(a, b) {
expect(a).to.be.a('number');
expect(b).to.be.a('number');
expect(a).to.be.below(b);
return b;
});
});
});

describe('[now:Timer]', function() {
it('can be called with the new keyword', function() {
var timer = new now.Timer();
expect(timer).to.be.instanceof(now.Timer);
});
it('can be called without the new keyword', function() {
var timer = now.Timer();
expect(timer).to.be.instanceof(now.Timer);
});

it('exposes its default members', function() {
var timer = now.Timer();

expect(timer).to.have.property('start').and.to.be.a('function');
expect(timer).to.have.property('end').and.to.be.a('function');
expect(timer).to.have.property('report').and.to.be.a('function');
});

it('exposes extra properties passed in as options', function() {
var opts = {
a: 1,
b: { has: 'props' },
c: function cFunc() {}
};

var timer = now.Timer(opts);

expect(timer).to.have.property('a').and.to.equal(opts.a);
expect(timer).to.have.property('b').and.to.equal(opts.b);
expect(timer).to.have.property('c').and.to.equal(opts.c);
});

it('cannot have default members overwritten by options', function() {
var opts = {
start: 'string value',
end: function customEnd() {}
};

var timer = now.Timer(opts);

expect(timer).to.have.property('start').and.to.equal(now.Timer.prototype.start);
expect(timer).to.have.property('end').and.to.equal(now.Timer.prototype.end);
});

it('reports on named timers', function() {
var timer = now.Timer();
var NAME = 'llama';

timer.start(NAME);
timer.end(NAME);

var report = timer.report();

expect(report).to.have.property(NAME)
.and.to.be.an('object');

var named = report[NAME];

expect(named).to.have.all.keys(['start', 'end', 'duration', 'status']);
expect(named).to.have.property('duration').and.to.be.above(0);
});

describe('#start', function() {
var NAME = 'pineapple';

it('returns this', function() {
var timer = now.Timer();
expect(timer.start(NAME)).to.equal(timer);
});
});

describe('#end', function() {
var NAME = 'pineapple';

it('returns this when it knows the name', function() {
var timer = now.Timer();
timer.start(NAME);
expect(timer.end(NAME)).to.equal(timer);
});

it('returns this when it does not know the name', function() {
var timer = now.Timer();
expect(timer.end(NAME)).to.equal(timer);
});

it('takes a status as the second parameter', function() {
var STATUS = 'elephant';
var timer = now.Timer();
timer.start(NAME);
timer.end(NAME, STATUS);

expect(timer.report())
.to.have.property(NAME)
.and.to.be.an('object')
.and.to.have.property('status')
.and.to.equal(STATUS);
});

it('calls toString of the status object', function() {
var STATUS = 'grasshopper';
var called = false;

var timer = now.Timer();
timer.start(NAME);
timer.end(NAME, {
toString: function() {
called = true;
return STATUS;
}
});

expect(timer.report())
.to.have.property(NAME)
.and.to.be.an('object')
.and.to.have.property('status')
.and.to.equal(STATUS);

expect(called).to.equal(true);
});
});
});

0 comments on commit 76c849c

Please sign in to comment.