Skip to content

Commit

Permalink
Merge pull request #139 from github/fix-bad-mock
Browse files Browse the repository at this point in the history
Fix bad mock
  • Loading branch information
koddsson authored Jun 2, 2020
2 parents 4937280 + dde1d80 commit 487a0be
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions test/time-ago.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
suite('time-ago', function() {
let dateNow

function freezeTime(date) {
dateNow = Date.now
Date.now = function() {
return date
function freezeTime(expected) {
dateNow = Date

function MockDate(...args) {
if (args.length) {
return new dateNow(...args)
}
return new dateNow(expected)
}

MockDate.UTC = dateNow.UTC
MockDate.parse = dateNow.parse
MockDate.now = () => expected.getTime()
MockDate.prototype = dateNow.prototype

// eslint-disable-next-line no-global-assign
Date = MockDate
}

teardown(function() {
if (dateNow) {
Date.now = dateNow
// eslint-disable-next-line no-global-assign
Date = dateNow
dateNow = null
}
})

Expand Down Expand Up @@ -61,7 +75,7 @@ suite('time-ago', function() {

test('rewrites time-ago datetimes < 18months as "months ago"', function() {
freezeTime(new Date(2020, 0, 1))
const then = new Date(2018, 10, 1).toISOString()
const then = new Date(2018, 9, 1).toISOString()
const timeElement = document.createElement('time-ago')
timeElement.setAttribute('datetime', then)
assert.equal(timeElement.textContent, '15 months ago')
Expand Down

0 comments on commit 487a0be

Please sign in to comment.