-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
4b4689e
commit 158d0cd
Showing
4 changed files
with
45 additions
and
47 deletions.
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
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,24 +1,19 @@ | ||
let time = new Date('2016-02-02T11:00:00.000Z').getTime(); | ||
|
||
module.exports = { | ||
real() { | ||
return { | ||
now() { | ||
return Date.now(); | ||
}, | ||
}; | ||
real: { | ||
now() { | ||
return Date.now(); | ||
}, | ||
}, | ||
fake() { | ||
|
||
let time = new Date('2016-02-02T11:00:00.000Z').getTime(); | ||
|
||
return { | ||
now() { | ||
return time; | ||
}, | ||
fix(value) { | ||
time = new Date(value).getTime(); | ||
if (Number.isNaN(time)) throw new Error(`Invalid Date: ${value}`); | ||
return this; | ||
}, | ||
}; | ||
fake: { | ||
now() { | ||
return time; | ||
}, | ||
fix(value) { | ||
time = new Date(value).getTime(); | ||
if (Number.isNaN(time)) throw new Error(`Invalid Date: ${value}`); | ||
return this; | ||
}, | ||
}, | ||
}; |
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,26 +1,26 @@ | ||
const assert = require('assert'); | ||
const { describe, it } = require('zunit'); | ||
const clock = require('..'); | ||
const { fake, real } = require('..'); | ||
|
||
describe('clocks', () => { | ||
|
||
it('fake clock should default to groundhog day', () => { | ||
assert.equal(clock.fake().now(), 1454410800000); | ||
assert.equal(fake.now(), 1454410800000); | ||
}); | ||
|
||
it('fake clock should fixed time', () => { | ||
assert.equal(clock.fake().fix(1469563181761).now(), 1469563181761); | ||
assert.equal(clock.fake().fix(new Date(1469563181761)).now(), 1469563181761); | ||
assert.equal(clock.fake().fix(new Date(1469563181761).toISOString()).now(), 1469563181761); | ||
assert.equal(fake.fix(1469563181761).now(), 1469563181761); | ||
assert.equal(fake.fix(new Date(1469563181761)).now(), 1469563181761); | ||
assert.equal(fake.fix(new Date(1469563181761).toISOString()).now(), 1469563181761); | ||
}); | ||
|
||
it('fake clock should reject attempts to fix time to an invalid value', () => { | ||
assert.throws(() => { | ||
clock.fake().fix('foo'); | ||
fake.fix('foo'); | ||
}, /Invalid Date: foo/); | ||
}); | ||
|
||
it('real clock should use correct time', () => { | ||
assert.ok(Math.abs(clock.real().now() - Date.now()) < 100); | ||
assert.ok(Math.abs(real.now() - Date.now()) < 100); | ||
}); | ||
}); |