Skip to content

Commit

Permalink
#6, #7 "getDuration" will return remaining time in ms, will return ti…
Browse files Browse the repository at this point in the history
…me when timer is paused
  • Loading branch information
husa committed Jul 20, 2015
1 parent 17a84d4 commit 6e1c0f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.sublime*
test/manual/
6 changes: 4 additions & 2 deletions src/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
}

Timer.prototype.getDuration = function () {
if (this._.status !== 'started') return 0
return Math.round((this._.duration - (+new Date - this._.start)) / 1000)
if (this._.status === 'started')
return this._.duration - (+new Date - this._.start)
if (this._.status === 'paused') return this._.duration
return 0
}

Timer.prototype.getStatus = function () {
Expand Down
44 changes: 22 additions & 22 deletions test/specs/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ describe('Timer', function() {
stop = jasmine.createSpy();
tick = jasmine.createSpy();
end = jasmine.createSpy();

jasmine.clock().install();
jasmine.clock().mockDate();
});

afterEach(function(){
jasmine.clock().uninstall();
})

it('should be available as global', function() {
expect(Timer).toBeDefined();
});
Expand Down Expand Up @@ -60,29 +67,32 @@ describe('Timer', function() {

describe('#getDuration', function() {

it('should return 0 if timer isn\'t started', function(){
it('should return 0 if timer isn\'t started or paused', function() {
//initial
expect(timer.getDuration()).toEqual(0);
//after start
timer.start(10);
expect(timer.getDuration()).not.toEqual(0);
expect(timer.getDuration()).toEqual(10000);
//after pause
timer.pause();
expect(timer.getDuration()).toEqual(0);
//after start
timer.start();
expect(timer.getDuration()).not.toEqual(0);
expect(timer.getDuration()).toEqual(10000);
//after stop
timer.stop();
expect(timer.getDuration()).toEqual(0);
});

it('should return actual value', function(done) {
it('should return actual value', function() {
timer.start(10);
setTimeout(function() {
expect(timer.getDuration()).toEqual(9);
done();
}, 1000);
jasmine.clock().tick(100);
expect(timer.getDuration()).toEqual(9900);
jasmine.clock().tick(1100);
expect(timer.getDuration()).toEqual(8800);
timer.pause();
jasmine.clock().tick(100);
expect(timer.getDuration()).toEqual(8800);
timer.start();
jasmine.clock().tick(100);
expect(timer.getDuration()).toEqual(8700);
});
});

Expand All @@ -107,7 +117,7 @@ describe('Timer', function() {
timer.on('start', start);
timer.start(1);
expect(start).toHaveBeenCalled();
expect(start).toHaveBeenCalledWith(1);
expect(start).toHaveBeenCalledWith(1000);
});
});

Expand Down Expand Up @@ -174,14 +184,6 @@ describe('Timer', function() {

describe('#on', function() {

beforeEach(function(){
jasmine.clock().install();
});

afterEach(function() {
jasmine.clock().uninstall();
});

it('should attach start callback', function() {
timer.on('start', start);
timer.start(1);
Expand Down Expand Up @@ -232,7 +234,6 @@ describe('Timer', function() {
describe('#off', function() {

beforeEach(function() {
jasmine.clock().install();
timer.on('tick', tick);
timer.on('onstart', start);
timer.on('stop', stop);
Expand Down Expand Up @@ -264,7 +265,6 @@ describe('Timer', function() {
describe('#callbacks execution', function() {

beforeEach(function(){
jasmine.clock().install();
timer.options({
onstart : start,
ontick : tick,
Expand Down

0 comments on commit 6e1c0f0

Please sign in to comment.