Skip to content

Commit

Permalink
Fix flaky relative weekdate or date: date before this week test (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefarias authored Jan 8, 2025
1 parent 75883bd commit f958acf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 8 additions & 6 deletions test/javascripts/src/relative_date_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import LocalTime from "local_time"

const { addTimeEl, assert, defer, getText, setText, test, testAsync, testGroup, triggerEvent } = LocalTime.TestHelpers
const { addTimeEl, assert, defer, getText, testAsync, testGroup, stubNow } = LocalTime.TestHelpers

testGroup("relative date", () => {
testAsync("this year", (done) => {
Expand Down Expand Up @@ -109,11 +109,13 @@ testGroup("relative weekday or date", () => {
})

testAsync("before this week", (done) => {
const before = moment().subtract("days", 8)
const el = addTimeEl({ type: "weekday-or-date", datetime: before.toISOString() })
defer(() => {
assert.equal(getText(el), before.format("MMM D"))
done()
stubNow(`${moment().year()}-03-20`, () => {
const before = moment().subtract("days", 8)
const el = addTimeEl({ type: "weekday-or-date", datetime: before.toISOString() })
defer(() => {
assert.equal(getText(el), before.format("MMM D"))
done()
})
})
})
})
10 changes: 10 additions & 0 deletions test/javascripts/src/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ LocalTime.TestHelpers = {

defer(callback) {
setTimeout(callback, 1)
},

stubNow(dateString, callback) {
const originalNow = moment.now
try {
moment.now = () => new Date(dateString).getTime()
callback()
} finally {
moment.now = originalNow
}
}
}

0 comments on commit f958acf

Please sign in to comment.