Skip to content

Commit

Permalink
#22. Mocked Date support.
Browse files Browse the repository at this point in the history
  • Loading branch information
purecatamphetamine committed Sep 23, 2019
1 parent c5e4ca3 commit 0279fcb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion source/JavascriptTimeAgo.js
Expand Up @@ -349,7 +349,7 @@ JavascriptTimeAgo.locale = JavascriptTimeAgo.addLocale
// Normalizes `.format()` `time` argument.
function getDateAndTimeBeingFormatted(input)
{
if (input.constructor === Date)
if (input.constructor === Date || isMockedDate(input))
{
return {
date : input,
Expand All @@ -372,6 +372,12 @@ function getDateAndTimeBeingFormatted(input)
throw new Error(`Unsupported relative time formatter input: ${typeof input}, ${input}`)
}

// During testing via some testing libraries `Date`s aren't actually `Date`s.
// https://github.com/catamphetamine/javascript-time-ago/issues/22
function isMockedDate(object) {
return typeof object === 'object' && typeof object.getTime === 'function'
}

// Get available time interval measurement units.
function getTimeIntervalMeasurementUnits(localeData, restrictedSetOfUnits)
{
Expand Down
7 changes: 7 additions & 0 deletions source/JavascriptTimeAgo.test.js
Expand Up @@ -64,6 +64,13 @@ describe(`time ago`, () =>
timeAgo.format(new Date()).should.equal('just now')
})

it(`should accept mocked Dates when testing`, () =>
{
const timeAgo = new JavascriptTimeAgo('en')
const mockedDate = { getTime: () => Date.now() }
timeAgo.format(mockedDate).should.equal('just now')
})

it(`should not accept anything but Dates and timestamps`, () =>
{
const timeAgo = new JavascriptTimeAgo('en')
Expand Down

0 comments on commit 0279fcb

Please sign in to comment.