2017-11-14 16:39:07 -05:00
|
|
|
import {
|
|
|
|
relativeAge,
|
|
|
|
autoUpdatingRelativeAge,
|
|
|
|
updateRelativeAge,
|
|
|
|
number,
|
|
|
|
longDate,
|
|
|
|
durationTiny
|
|
|
|
} from "discourse/lib/formatter";
|
2020-07-09 15:54:53 -04:00
|
|
|
import { discourseModule } from "helpers/qunit-helpers";
|
2015-08-07 15:08:27 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
discourseModule("lib:formatter", {
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2020-07-09 15:54:53 -04:00
|
|
|
this.clock = sinon.useFakeTimers(new Date(2012, 11, 31, 12, 0).getTime());
|
2013-08-06 13:47:24 -04:00
|
|
|
},
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
afterEach() {
|
2020-07-09 15:54:53 -04:00
|
|
|
this.clock.restore();
|
2013-08-06 13:47:24 -04:00
|
|
|
}
|
|
|
|
});
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
function formatMins(mins, opts = {}) {
|
|
|
|
let dt = new Date(new Date() - mins * 60 * 1000);
|
|
|
|
return relativeAge(dt, {
|
|
|
|
format: opts.format || "tiny",
|
|
|
|
leaveAgo: opts.leaveAgo
|
|
|
|
});
|
|
|
|
}
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
function formatHours(hours, opts) {
|
|
|
|
return formatMins(hours * 60, opts);
|
|
|
|
}
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
function formatDays(days, opts) {
|
|
|
|
return formatHours(days * 24, opts);
|
|
|
|
}
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
function shortDate(days) {
|
2014-08-30 02:24:47 -04:00
|
|
|
return moment()
|
|
|
|
.subtract(days, "days")
|
|
|
|
.format("MMM D");
|
2020-07-09 15:54:53 -04:00
|
|
|
}
|
2013-06-28 13:09:53 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
function shortDateTester(format) {
|
|
|
|
return function(days) {
|
2014-08-30 02:24:47 -04:00
|
|
|
return moment()
|
|
|
|
.subtract(days, "days")
|
2020-07-09 15:54:53 -04:00
|
|
|
.format(format);
|
2013-06-21 14:06:20 -04:00
|
|
|
};
|
2020-07-09 15:54:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function strip(html) {
|
|
|
|
return $(html).text();
|
|
|
|
}
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("formating medium length dates", function(assert) {
|
|
|
|
let shortDateYear = shortDateTester("MMM D, 'YY");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
strip(formatMins(1.4, { format: "medium", leaveAgo: true })),
|
|
|
|
"1 min ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatMins(2, { format: "medium", leaveAgo: true })),
|
|
|
|
"2 mins ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatMins(55, { format: "medium", leaveAgo: true })),
|
|
|
|
"55 mins ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatMins(56, { format: "medium", leaveAgo: true })),
|
|
|
|
"1 hour ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatHours(4, { format: "medium", leaveAgo: true })),
|
|
|
|
"4 hours ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatHours(22, { format: "medium", leaveAgo: true })),
|
|
|
|
"22 hours ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatHours(23, { format: "medium", leaveAgo: true })),
|
|
|
|
"23 hours ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatHours(23.5, { format: "medium", leaveAgo: true })),
|
|
|
|
"1 day ago"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
strip(formatDays(4.85, { format: "medium", leaveAgo: true })),
|
|
|
|
"4 days ago"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(strip(formatMins(0, { format: "medium" })), "just now");
|
|
|
|
assert.equal(strip(formatMins(1.4, { format: "medium" })), "1 min");
|
|
|
|
assert.equal(strip(formatMins(2, { format: "medium" })), "2 mins");
|
|
|
|
assert.equal(strip(formatMins(55, { format: "medium" })), "55 mins");
|
|
|
|
assert.equal(strip(formatMins(56, { format: "medium" })), "1 hour");
|
|
|
|
assert.equal(strip(formatHours(4, { format: "medium" })), "4 hours");
|
|
|
|
assert.equal(strip(formatHours(22, { format: "medium" })), "22 hours");
|
|
|
|
assert.equal(strip(formatHours(23, { format: "medium" })), "23 hours");
|
|
|
|
assert.equal(strip(formatHours(23.5, { format: "medium" })), "1 day");
|
|
|
|
assert.equal(strip(formatDays(4.85, { format: "medium" })), "4 days");
|
|
|
|
|
|
|
|
assert.equal(strip(formatDays(6, { format: "medium" })), shortDate(6));
|
|
|
|
assert.equal(strip(formatDays(100, { format: "medium" })), shortDate(100)); // eg: Jan 23
|
|
|
|
assert.equal(
|
|
|
|
strip(formatDays(500, { format: "medium" })),
|
|
|
|
shortDateYear(500)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
$(formatDays(0, { format: "medium" })).attr("title"),
|
|
|
|
longDate(new Date())
|
|
|
|
);
|
|
|
|
assert.equal($(formatDays(0, { format: "medium" })).attr("class"), "date");
|
|
|
|
|
|
|
|
this.clock.restore();
|
|
|
|
this.clock = sinon.useFakeTimers(new Date(2012, 0, 9, 12, 0).getTime()); // Jan 9, 2012
|
|
|
|
|
|
|
|
assert.equal(strip(formatDays(8, { format: "medium" })), shortDate(8));
|
|
|
|
assert.equal(strip(formatDays(10, { format: "medium" })), shortDateYear(10));
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("formating tiny dates", function(assert) {
|
|
|
|
let shortDateYear = shortDateTester("MMM 'YY");
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatMins(0), "1m");
|
|
|
|
assert.equal(formatMins(1), "1m");
|
|
|
|
assert.equal(formatMins(2), "2m");
|
|
|
|
assert.equal(formatMins(60), "1h");
|
|
|
|
assert.equal(formatHours(4), "4h");
|
|
|
|
assert.equal(formatHours(23), "23h");
|
|
|
|
assert.equal(formatHours(23.5), "1d");
|
|
|
|
assert.equal(formatDays(1), "1d");
|
|
|
|
assert.equal(formatDays(14), "14d");
|
|
|
|
assert.equal(formatDays(15), shortDate(15));
|
|
|
|
assert.equal(formatDays(92), shortDate(92));
|
|
|
|
assert.equal(formatDays(364), shortDate(364));
|
|
|
|
assert.equal(formatDays(365), shortDate(365));
|
|
|
|
assert.equal(formatDays(366), shortDateYear(366)); // leap year
|
|
|
|
assert.equal(formatDays(500), shortDateYear(500));
|
|
|
|
assert.equal(formatDays(365 * 2 + 1), shortDateYear(365 * 2 + 1)); // one leap year
|
2013-07-10 12:38:43 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
var originalValue = this.siteSettings.relative_date_duration;
|
|
|
|
this.siteSettings.relative_date_duration = 7;
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatDays(7), "7d");
|
|
|
|
assert.equal(formatDays(8), shortDate(8));
|
2013-07-10 12:38:43 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.siteSettings.relative_date_duration = 1;
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatDays(1), "1d");
|
|
|
|
assert.equal(formatDays(2), shortDate(2));
|
2013-07-10 12:38:43 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.siteSettings.relative_date_duration = 0;
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatMins(0), "1m");
|
|
|
|
assert.equal(formatMins(1), "1m");
|
|
|
|
assert.equal(formatMins(2), "2m");
|
|
|
|
assert.equal(formatMins(60), "1h");
|
|
|
|
assert.equal(formatDays(1), shortDate(1));
|
|
|
|
assert.equal(formatDays(2), shortDate(2));
|
|
|
|
assert.equal(formatDays(366), shortDateYear(366));
|
2013-07-10 12:38:43 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.siteSettings.relative_date_duration = null;
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatDays(1), "1d");
|
|
|
|
assert.equal(formatDays(14), "14d");
|
|
|
|
assert.equal(formatDays(15), shortDate(15));
|
2013-07-10 12:38:43 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.siteSettings.relative_date_duration = 14;
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.clock.restore();
|
|
|
|
this.clock = sinon.useFakeTimers(new Date(2012, 0, 12, 12, 0).getTime()); // Jan 12, 2012
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatDays(11), "11d");
|
|
|
|
assert.equal(formatDays(14), "14d");
|
|
|
|
assert.equal(formatDays(15), shortDateYear(15));
|
|
|
|
assert.equal(formatDays(366), shortDateYear(366));
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.clock.restore();
|
|
|
|
this.clock = sinon.useFakeTimers(new Date(2012, 0, 20, 12, 0).getTime()); // Jan 20, 2012
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(formatDays(14), "14d");
|
|
|
|
assert.equal(formatDays(15), shortDate(15));
|
|
|
|
assert.equal(formatDays(20), shortDateYear(20));
|
2013-08-06 13:47:24 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
this.siteSettings.relative_date_duration = originalValue;
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("autoUpdatingRelativeAge", function(assert) {
|
2014-08-20 16:22:58 -04:00
|
|
|
var d = moment()
|
|
|
|
.subtract(1, "day")
|
|
|
|
.toDate();
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
var $elem = $(autoUpdatingRelativeAge(d));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.data("format"), "tiny");
|
|
|
|
assert.equal($elem.data("time"), d.getTime());
|
|
|
|
assert.equal($elem.attr("title"), undefined);
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
$elem = $(autoUpdatingRelativeAge(d, { title: true }));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.attr("title"), longDate(d));
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
$elem = $(
|
|
|
|
autoUpdatingRelativeAge(d, {
|
|
|
|
format: "medium",
|
|
|
|
title: true,
|
|
|
|
leaveAgo: true
|
|
|
|
})
|
|
|
|
);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.data("format"), "medium-with-ago");
|
|
|
|
assert.equal($elem.data("time"), d.getTime());
|
|
|
|
assert.equal($elem.attr("title"), longDate(d));
|
|
|
|
assert.equal($elem.html(), "1 day ago");
|
2018-06-15 11:03:24 -04:00
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
$elem = $(autoUpdatingRelativeAge(d, { format: "medium" }));
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.data("format"), "medium");
|
|
|
|
assert.equal($elem.data("time"), d.getTime());
|
|
|
|
assert.equal($elem.attr("title"), undefined);
|
|
|
|
assert.equal($elem.html(), "1 day");
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("updateRelativeAge", function(assert) {
|
2013-06-18 16:27:40 -04:00
|
|
|
var d = new Date();
|
2015-08-07 15:08:27 -04:00
|
|
|
var $elem = $(autoUpdatingRelativeAge(d));
|
2013-06-18 16:27:40 -04:00
|
|
|
$elem.data("time", d.getTime() - 2 * 60 * 1000);
|
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
updateRelativeAge($elem);
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.html(), "2m");
|
2013-06-18 16:27:40 -04:00
|
|
|
|
|
|
|
d = new Date();
|
2015-08-07 15:08:27 -04:00
|
|
|
$elem = $(autoUpdatingRelativeAge(d, { format: "medium", leaveAgo: true }));
|
2013-06-18 16:27:40 -04:00
|
|
|
$elem.data("time", d.getTime() - 2 * 60 * 1000);
|
|
|
|
|
2015-08-07 15:08:27 -04:00
|
|
|
updateRelativeAge($elem);
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal($elem.html(), "2 mins ago");
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
2013-06-27 23:56:38 -04:00
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("number", function(assert) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(number(123), "123", "it returns a string version of the number");
|
|
|
|
assert.equal(number("123"), "123", "it works with a string command");
|
|
|
|
assert.equal(number(NaN), "0", "it returns 0 for NaN");
|
|
|
|
assert.equal(number(3333), "3.3k", "it abbreviates thousands");
|
|
|
|
assert.equal(number(2499999), "2.5M", "it abbreviates millions");
|
2018-06-07 04:59:17 -04:00
|
|
|
assert.equal(number("2499999.5"), "2.5M", "it abbreviates millions");
|
2018-03-02 05:00:48 -05:00
|
|
|
assert.equal(number(1000000), "1.0M", "it abbreviates a million");
|
|
|
|
assert.equal(number(999999), "999k", "it abbreviates hundreds of thousands");
|
2018-06-07 04:44:21 -04:00
|
|
|
assert.equal(
|
|
|
|
number(18.2),
|
|
|
|
"18",
|
|
|
|
"it returns a float number rounded to an integer as a string"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
number(18.6),
|
|
|
|
"19",
|
|
|
|
"it returns a float number rounded to an integer as a string"
|
|
|
|
);
|
2018-06-07 04:59:17 -04:00
|
|
|
assert.equal(
|
|
|
|
number("12.3"),
|
|
|
|
"12",
|
|
|
|
"it returns a string float rounded to an integer as a string"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
number("12.6"),
|
|
|
|
"13",
|
|
|
|
"it returns a string float rounded to an integer as a string"
|
|
|
|
);
|
2017-11-14 16:39:07 -05:00
|
|
|
});
|
|
|
|
|
2020-07-09 15:54:53 -04:00
|
|
|
QUnit.test("durationTiny", function(assert) {
|
2017-11-21 14:33:39 -05:00
|
|
|
assert.equal(durationTiny(), "—", "undefined is a dash");
|
|
|
|
assert.equal(durationTiny(null), "—", "null is a dash");
|
2017-11-14 16:39:07 -05:00
|
|
|
assert.equal(durationTiny(0), "< 1m", "0 seconds shows as < 1m");
|
|
|
|
assert.equal(durationTiny(59), "< 1m", "59 seconds shows as < 1m");
|
|
|
|
assert.equal(durationTiny(60), "1m", "60 seconds shows as 1m");
|
|
|
|
assert.equal(durationTiny(90), "2m", "90 seconds shows as 2m");
|
|
|
|
assert.equal(durationTiny(120), "2m", "120 seconds shows as 2m");
|
|
|
|
assert.equal(durationTiny(60 * 45), "1h", "45 minutes shows as 1h");
|
|
|
|
assert.equal(durationTiny(60 * 60), "1h", "60 minutes shows as 1h");
|
|
|
|
assert.equal(durationTiny(60 * 90), "2h", "90 minutes shows as 2h");
|
|
|
|
assert.equal(durationTiny(3600 * 23), "23h", "23 hours shows as 23h");
|
|
|
|
assert.equal(
|
|
|
|
durationTiny(3600 * 24 - 29),
|
|
|
|
"1d",
|
|
|
|
"23 hours 31 mins shows as 1d"
|
|
|
|
);
|
|
|
|
assert.equal(durationTiny(3600 * 24 * 89), "89d", "89 days shows as 89d");
|
|
|
|
assert.equal(
|
|
|
|
durationTiny(60 * (525600 - 1)),
|
|
|
|
"12mon",
|
|
|
|
"364 days shows as 12mon"
|
|
|
|
);
|
|
|
|
assert.equal(durationTiny(60 * 525600), "1y", "365 days shows as 1y");
|
|
|
|
assert.equal(durationTiny(86400 * 456), "1y", "456 days shows as 1y");
|
|
|
|
assert.equal(durationTiny(86400 * 457), "> 1y", "457 days shows as > 1y");
|
|
|
|
assert.equal(durationTiny(86400 * 638), "> 1y", "638 days shows as > 1y");
|
|
|
|
assert.equal(durationTiny(86400 * 639), "2y", "639 days shows as 2y");
|
|
|
|
assert.equal(durationTiny(86400 * 821), "2y", "821 days shows as 2y");
|
|
|
|
assert.equal(durationTiny(86400 * 822), "> 2y", "822 days shows as > 2y");
|
|
|
|
});
|