diff --git a/plugins/discourse-local-dates/test/javascripts/helpers/freeze-time.js b/plugins/discourse-local-dates/test/javascripts/helpers/freeze-time.js new file mode 100644 index 00000000000..764bd7043ad --- /dev/null +++ b/plugins/discourse-local-dates/test/javascripts/helpers/freeze-time.js @@ -0,0 +1,21 @@ +import sinon from "sinon"; + +const PARIS = "Europe/Paris"; + +export default function freezeTime({ date, timezone }, cb) { + date = date || "2020-01-22 10:34"; + const newTimezone = timezone || PARIS; + const previousZone = moment.tz.guess(); + const now = moment.tz(date, newTimezone).valueOf(); + + sinon.useFakeTimers(now); + sinon.stub(moment.tz, "guess"); + moment.tz.guess.returns(newTimezone); + moment.tz.setDefault(newTimezone); + + cb(); + + moment.tz.guess.returns(previousZone); + moment.tz.setDefault(previousZone); + sinon.restore(); +} diff --git a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js index f4004d2cf20..ee4ee32ca27 100644 --- a/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js +++ b/plugins/discourse-local-dates/test/javascripts/unit/discourse-local-dates-test.js @@ -1,7 +1,7 @@ import { setupTest } from "ember-qunit"; import { module, test } from "qunit"; +import freezeTime from "../helpers/freeze-time"; import { applyLocalDates } from "../initializers/discourse-local-dates"; -import { freezeTime } from "./local-date-builder-test"; module("Unit | discourse-local-dates", function (hooks) { setupTest(hooks); diff --git a/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js b/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js index 815504289f3..9cde1170e50 100644 --- a/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js +++ b/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js @@ -1,6 +1,6 @@ import QUnit, { module, test } from "qunit"; -import sinon from "sinon"; import I18n from "discourse-i18n"; +import freezeTime from "../helpers/freeze-time"; import LocalDateBuilder from "../lib/local-date-builder"; const UTC = "Etc/UTC"; @@ -12,24 +12,6 @@ const LAGOS = "Africa/Lagos"; const LONDON = "Europe/London"; const SINGAPORE = "Asia/Singapore"; -export function freezeTime({ date, timezone }, cb) { - date = date || "2020-01-22 10:34"; - const newTimezone = timezone || PARIS; - const previousZone = moment.tz.guess(); - const now = moment.tz(date, newTimezone).valueOf(); - - sinon.useFakeTimers(now); - sinon.stub(moment.tz, "guess"); - moment.tz.guess.returns(newTimezone); - moment.tz.setDefault(newTimezone); - - cb(); - - moment.tz.guess.returns(previousZone); - moment.tz.setDefault(previousZone); - sinon.restore(); -} - QUnit.assert.buildsCorrectDate = function (options, expected, message) { const localTimezone = options.localTimezone || PARIS; delete options.localTimezone;