From 1631394826e03073f99caa990811f261590d3257 Mon Sep 17 00:00:00 2001 From: Natalie Tay Date: Wed, 12 Oct 2022 11:23:34 +0800 Subject: [PATCH] FEATURE: Show relative time when date is omitted (#18547) --- .../javascripts/lib/local-date-builder.js | 8 +++--- .../unit/discourse-local-dates-test.js | 4 +-- .../{lib => unit}/local-date-builder-test.js | 26 ++++++++++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) rename plugins/discourse-local-dates/test/javascripts/{lib => unit}/local-date-builder-test.js (96%) diff --git a/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js b/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js index 352ecd09b93..ee2cd449392 100644 --- a/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js +++ b/plugins/discourse-local-dates/assets/javascripts/lib/local-date-builder.js @@ -67,12 +67,12 @@ export default class LocalDateBuilder { } const previews = this._generatePreviews(localDate, displayedTimezone); - + const hasTime = hour !== undefined; return { pastEvent: !this.recurring && moment.tz(this.localTimezone).isAfter(localDate.datetime), - formatted: this._applyFormatting(localDate, displayedTimezone), + formatted: this._applyFormatting(localDate, displayedTimezone, hasTime), previews, textPreview: this._generateTextPreviews(previews), }; @@ -210,7 +210,7 @@ export default class LocalDateBuilder { return duration < 0 ? dates.reverse() : dates; } - _applyFormatting(localDate, displayedTimezone) { + _applyFormatting(localDate, displayedTimezone, hasTime) { if (this.countdown) { const diffTime = moment.tz(this.localTimezone).diff(localDate.datetime); @@ -241,7 +241,7 @@ export default class LocalDateBuilder { if (inCalendarRange && sameTimezone) { const date = localDate.datetimeWithZone(this.localTimezone); - if (date.hours() === 0 && date.minutes() === 0) { + if (hasTime && date.hours() === 0 && date.minutes() === 0) { return date.format("dddd"); } 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 d88a8bb7cfe..38db0c92d8c 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,6 +1,6 @@ import { module, test } from "qunit"; import { applyLocalDates } from "../initializers/discourse-local-dates"; -import { freezeTime } from "../lib/local-date-builder-test"; +import { freezeTime } from "./local-date-builder-test"; module("Unit | Discourse Local Dates | discourse-local-dates", function () { function createElementFromHTML(htmlString) { @@ -72,7 +72,7 @@ module("Unit | Discourse Local Dates | discourse-local-dates", function () { ); assert.equal( to.querySelector(".relative-time").textContent, - "Thursday" + "Yesterday" ); } ); diff --git a/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js b/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js similarity index 96% rename from plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js rename to plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js index 3cf8bb0f02c..f2a0f702a4d 100644 --- a/plugins/discourse-local-dates/test/javascripts/lib/local-date-builder-test.js +++ b/plugins/discourse-local-dates/test/javascripts/unit/local-date-builder-test.js @@ -1,5 +1,5 @@ import I18n from "I18n"; -import LocalDateBuilder from "./local-date-builder"; +import LocalDateBuilder from "../lib/local-date-builder"; import sinon from "sinon"; import QUnit, { module, test } from "qunit"; @@ -10,6 +10,7 @@ const NEW_YORK = "America/New_York"; const PARIS = "Europe/Paris"; 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"; @@ -61,7 +62,7 @@ QUnit.assert.buildsCorrectDate = function (options, expected, message) { } }; -module("lib:local-date-builder", function () { +module("Unit | Library | local-date-builder", function () { test("date", function (assert) { freezeTime({ date: "2020-03-11" }, () => { assert.buildsCorrectDate( @@ -70,6 +71,23 @@ module("lib:local-date-builder", function () { "it displays the date without time" ); }); + + freezeTime({ date: "2022-10-11", timezone: "Asia/Singapore" }, () => { + const localDateBuilder = new LocalDateBuilder( + { + date: "2022-10-12", + timezone: SINGAPORE, + localTimezone: SINGAPORE, + }, + SINGAPORE + ); + + assert.strictEqual( + localDateBuilder.build().formatted, + "Tomorrow", + "Displays relative day" + ); + }); }); test("date and time", function (assert) { @@ -91,8 +109,8 @@ module("lib:local-date-builder", function () { { time: "12:22:00", date: "2022-10-07", - timezone: "Asia/Singapore", - localTimezone: "Asia/Singapore", + timezone: SINGAPORE, + localTimezone: SINGAPORE, sameLocalDayAsFrom: true, }, { formatted: "12:22 PM (Singapore)" },