DEV: Extract a test helper in local-dates (#29196)

This commit is contained in:
Jarek Radosz 2024-10-15 10:27:42 +02:00 committed by GitHub
parent c1325dbe13
commit 29ac39cf41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 20 deletions

View File

@ -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();
}

View File

@ -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);

View File

@ -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;