diff --git a/test/javascripts/src/relative_date_test.js b/test/javascripts/src/relative_date_test.js index a090636..1eb9e4a 100644 --- a/test/javascripts/src/relative_date_test.js +++ b/test/javascripts/src/relative_date_test.js @@ -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) => { @@ -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() + }) }) }) }) diff --git a/test/javascripts/src/test_helpers.js b/test/javascripts/src/test_helpers.js index 3bd3f5f..ddc1d29 100644 --- a/test/javascripts/src/test_helpers.js +++ b/test/javascripts/src/test_helpers.js @@ -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 + } } }