-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |