Skip to content

Commit 034d446

Browse files
committed
fix: flakey LocalizationUtils time tests indepedent of current time
1 parent baa556b commit 034d446

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/utils/LocalizationUtils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ define(function (require, exports, module) {
113113
*
114114
* @param {Date} date - The date to compare and format.
115115
* @param {string} [lang] - Optional language code to use for formatting (e.g., 'en', 'fr').
116+
* @param {Date} [fromDate] - Optional date to use instead of now to compute the relative dateTime from.
116117
* @returns {string} - An intelligently formatted date string.
117118
*/
118-
function dateTimeFromNowFriendly(date, lang) {
119-
const now = new Date();
120-
const diffInMilliseconds = date - now;
119+
function dateTimeFromNowFriendly(date, lang, fromDate) {
120+
fromDate = fromDate || new Date();
121+
const diffInMilliseconds = date - fromDate;
121122
const diffInDays = Math.trunc(diffInMilliseconds / (1000 * 60 * 60 * 24));
122123

123124
// If within the last 30 days or the future, use relative time
@@ -126,7 +127,7 @@ define(function (require, exports, module) {
126127
}
127128

128129
// If in the current year, format as "MMM DD"
129-
const currentYear = now.getFullYear();
130+
const currentYear = fromDate.getFullYear();
130131
const dateYear = date.getFullYear();
131132

132133
const languageOption = [lang || brackets.getLocale() || "en", "en"];

test/spec/LocalizationUtils-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ define(function (require, exports, module) {
262262
});
263263

264264
it("should use formatted date without year for dates earlier this year", function () {
265-
const now = new Date();
266-
const testDate = new Date(now.getFullYear(), 1, 15); // Feb 15 of current year
267-
const result = LocalizationUtils.dateTimeFromNowFriendly(testDate, "en");
265+
const testDate = new Date(2024, 1, 15);
266+
const fromDate = new Date(2024, 11, 15);
267+
const result = LocalizationUtils.dateTimeFromNowFriendly(testDate, "en", fromDate);
268268
expect(result).toBe("Feb 15");
269269
});
270270

@@ -301,9 +301,9 @@ define(function (require, exports, module) {
301301

302302
// Current year tests
303303
it("should use formatted date without year for dates earlier this year (de locale)", function () {
304-
const now = new Date();
305-
const testDate = new Date(now.getFullYear(), 1, 15); // Feb 15 of current year
306-
const result = LocalizationUtils.dateTimeFromNowFriendly(testDate, "de");
304+
const testDate = new Date(2024, 1, 15);
305+
const fromDate = new Date(2024, 10, 15);
306+
const result = LocalizationUtils.dateTimeFromNowFriendly(testDate, "de", fromDate);
307307
expect(result).toBe("15. Feb.");
308308
});
309309

@@ -322,4 +322,4 @@ define(function (require, exports, module) {
322322
});
323323

324324
});
325-
});
325+
});

0 commit comments

Comments
 (0)