Skip to content

Commit

Permalink
Merge pull request #27 from keenahn/ktj/travel-after-freeze
Browse files Browse the repository at this point in the history
feat: makes timekeeper.travel work if currently frozen
  • Loading branch information
freewil committed Sep 5, 2017
2 parents a54ffe2 + 1420680 commit 7d762be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Expand Up @@ -61,6 +61,8 @@ setTimeout(function() {
}, 500);
```

Note: If traveling when time is frozen, the time will be frozen to the new traveled time.

### Reflection:

```js
Expand Down
4 changes: 4 additions & 0 deletions lib/timekeeper.js
Expand Up @@ -154,6 +154,10 @@
date = new NativeDate(date);
}

if (freeze) {
timekeeper.freeze(date)
}

travel = date.getTime();
started = NativeDate.now();
};
Expand Down
30 changes: 30 additions & 0 deletions test/timekeeper.test.js
Expand Up @@ -75,6 +75,36 @@ describe('TimeKeeper', function() {
});
});
});

describe('when frozen', function() {
beforeEach(function() {
this.time = new Date(1330688329321);
tk.freeze(this.time);
});

afterEach(function() {
tk.reset();
});

it('freezes the time create with `new Date` to the supplied one', function(done) {
var newTime = new Date().getTime() + 100
tk.travel(newTime)
setTimeout(function() {
var date = new Date();
date.getTime().should.eql(newTime);
done();
}, 10, this.time);
});

it('freezes the time create with `Date#now` to the supplied one', function(done) {
var newTime = new Date().getTime() + 100
tk.travel(newTime)
setTimeout(function() {
Date.now().should.eql(newTime);
done();
}, 10, this.time);
});
});
});

describe('inheritance', function() {
Expand Down

0 comments on commit 7d762be

Please sign in to comment.