2013-08-06 13:47:24 -04:00
|
|
|
var clock;
|
|
|
|
|
|
|
|
module("Discourse.Formatter", {
|
|
|
|
setup: function() {
|
|
|
|
clock = sinon.useFakeTimers(new Date(2012,11,31,12,0).getTime());
|
|
|
|
},
|
|
|
|
|
|
|
|
teardown: function() {
|
|
|
|
clock.restore();
|
|
|
|
}
|
|
|
|
});
|
2013-06-18 16:27:40 -04:00
|
|
|
|
|
|
|
var format = "tiny";
|
|
|
|
var leaveAgo = false;
|
|
|
|
var mins_ago = function(mins){
|
|
|
|
return new Date((new Date()) - mins * 60 * 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
var formatMins = function(mins) {
|
|
|
|
return Discourse.Formatter.relativeAge(mins_ago(mins), {format: format, leaveAgo: leaveAgo});
|
|
|
|
};
|
|
|
|
|
|
|
|
var formatHours = function(hours) {
|
|
|
|
return formatMins(hours * 60);
|
|
|
|
};
|
|
|
|
|
|
|
|
var formatDays = function(days) {
|
|
|
|
return formatHours(days * 24);
|
|
|
|
};
|
|
|
|
|
2013-06-28 13:09:53 -04:00
|
|
|
var shortDate = function(days){
|
2014-08-30 02:24:47 -04:00
|
|
|
return moment().subtract(days, 'days').format('MMM D');
|
2013-06-28 13:09:53 -04:00
|
|
|
};
|
|
|
|
|
2013-06-18 16:27:40 -04:00
|
|
|
test("formating medium length dates", function() {
|
|
|
|
|
|
|
|
format = "medium";
|
|
|
|
var strip = function(html){
|
|
|
|
return $(html).text();
|
2013-06-21 14:06:20 -04:00
|
|
|
};
|
2013-06-18 16:27:40 -04:00
|
|
|
|
|
|
|
var shortDateYear = function(days){
|
2014-08-30 02:24:47 -04:00
|
|
|
return moment().subtract(days, 'days').format("MMM D, 'YY");
|
2013-06-21 14:06:20 -04:00
|
|
|
};
|
2013-06-18 16:27:40 -04:00
|
|
|
|
|
|
|
leaveAgo = true;
|
2013-06-20 04:00:31 -04:00
|
|
|
equal(strip(formatMins(1.4)), "1 min ago");
|
|
|
|
equal(strip(formatMins(2)), "2 mins ago");
|
2015-06-22 13:57:02 -04:00
|
|
|
equal(strip(formatMins(55)), "55 mins ago");
|
|
|
|
equal(strip(formatMins(56)), "1 hour ago");
|
2013-06-18 16:27:40 -04:00
|
|
|
equal(strip(formatHours(4)), "4 hours ago");
|
|
|
|
equal(strip(formatHours(22)), "22 hours ago");
|
|
|
|
equal(strip(formatHours(23)), "1 day ago");
|
|
|
|
equal(strip(formatDays(4.85)), "4 days ago");
|
|
|
|
|
|
|
|
leaveAgo = false;
|
|
|
|
equal(strip(formatMins(0)), "just now");
|
2013-06-20 04:00:31 -04:00
|
|
|
equal(strip(formatMins(1.4)), "1 min");
|
|
|
|
equal(strip(formatMins(2)), "2 mins");
|
2015-06-22 13:57:02 -04:00
|
|
|
equal(strip(formatMins(55)), "55 mins");
|
|
|
|
equal(strip(formatMins(56)), "1 hour");
|
2013-06-18 16:27:40 -04:00
|
|
|
equal(strip(formatHours(4)), "4 hours");
|
|
|
|
equal(strip(formatHours(22)), "22 hours");
|
|
|
|
equal(strip(formatHours(23)), "1 day");
|
|
|
|
equal(strip(formatDays(4.85)), "4 days");
|
|
|
|
|
|
|
|
equal(strip(formatDays(6)), shortDate(6));
|
2014-01-29 15:45:06 -05:00
|
|
|
equal(strip(formatDays(100)), shortDate(100)); // eg: Jan 23
|
2013-06-18 16:27:40 -04:00
|
|
|
equal(strip(formatDays(500)), shortDateYear(500));
|
|
|
|
|
|
|
|
equal($(formatDays(0)).attr("title"), moment().format('MMMM D, YYYY h:mma'));
|
|
|
|
equal($(formatDays(0)).attr("class"), "date");
|
|
|
|
|
2013-08-06 13:47:24 -04:00
|
|
|
clock.restore();
|
|
|
|
clock = sinon.useFakeTimers(new Date(2012,0,9,12,0).getTime()); // Jan 9, 2012
|
|
|
|
|
|
|
|
equal(strip(formatDays(8)), shortDate(8));
|
|
|
|
equal(strip(formatDays(10)), shortDateYear(10));
|
|
|
|
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test("formating tiny dates", function() {
|
2013-08-06 13:47:24 -04:00
|
|
|
var shortDateYear = function(days){
|
2014-08-20 16:22:58 -04:00
|
|
|
return moment().subtract(days, 'days').format("MMM 'YY");
|
2013-08-06 13:47:24 -04:00
|
|
|
};
|
|
|
|
|
2013-06-18 16:27:40 -04:00
|
|
|
format = "tiny";
|
|
|
|
equal(formatMins(0), "< 1m");
|
|
|
|
equal(formatMins(2), "2m");
|
|
|
|
equal(formatMins(60), "1h");
|
|
|
|
equal(formatHours(4), "4h");
|
|
|
|
equal(formatDays(1), "1d");
|
2013-06-28 13:09:53 -04:00
|
|
|
equal(formatDays(14), "14d");
|
|
|
|
equal(formatDays(15), shortDate(15));
|
|
|
|
equal(formatDays(92), shortDate(92));
|
|
|
|
equal(formatDays(364), shortDate(364));
|
2013-08-06 13:47:24 -04:00
|
|
|
equal(formatDays(365), shortDate(365));
|
|
|
|
equal(formatDays(366), shortDateYear(366)); // leap year
|
|
|
|
equal(formatDays(500), shortDateYear(500));
|
|
|
|
equal(formatDays(365*2 + 1), shortDateYear(365*2 + 1)); // one leap year
|
2013-07-10 12:38:43 -04:00
|
|
|
|
|
|
|
var originalValue = Discourse.SiteSettings.relative_date_duration;
|
|
|
|
Discourse.SiteSettings.relative_date_duration = 7;
|
|
|
|
equal(formatDays(7), "7d");
|
|
|
|
equal(formatDays(8), shortDate(8));
|
|
|
|
|
|
|
|
Discourse.SiteSettings.relative_date_duration = 1;
|
|
|
|
equal(formatDays(1), "1d");
|
|
|
|
equal(formatDays(2), shortDate(2));
|
|
|
|
|
|
|
|
Discourse.SiteSettings.relative_date_duration = 0;
|
|
|
|
equal(formatMins(0), "< 1m");
|
|
|
|
equal(formatMins(2), "2m");
|
|
|
|
equal(formatMins(60), "1h");
|
|
|
|
equal(formatDays(1), shortDate(1));
|
|
|
|
equal(formatDays(2), shortDate(2));
|
2013-08-06 13:47:24 -04:00
|
|
|
equal(formatDays(366), shortDateYear(366));
|
2013-07-10 12:38:43 -04:00
|
|
|
|
|
|
|
Discourse.SiteSettings.relative_date_duration = null;
|
|
|
|
equal(formatDays(1), '1d');
|
|
|
|
equal(formatDays(14), '14d');
|
|
|
|
equal(formatDays(15), shortDate(15));
|
|
|
|
|
2013-08-06 13:47:24 -04:00
|
|
|
Discourse.SiteSettings.relative_date_duration = 14;
|
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
clock = sinon.useFakeTimers(new Date(2012,0,12,12,0).getTime()); // Jan 12, 2012
|
|
|
|
|
|
|
|
equal(formatDays(11), "11d");
|
|
|
|
equal(formatDays(14), "14d");
|
|
|
|
equal(formatDays(15), shortDateYear(15));
|
|
|
|
equal(formatDays(366), shortDateYear(366));
|
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
clock = sinon.useFakeTimers(new Date(2012,0,20,12,0).getTime()); // Jan 20, 2012
|
|
|
|
|
|
|
|
equal(formatDays(14), "14d");
|
|
|
|
equal(formatDays(15), shortDate(15));
|
|
|
|
equal(formatDays(20), shortDateYear(20));
|
|
|
|
|
2013-07-10 12:38:43 -04:00
|
|
|
Discourse.SiteSettings.relative_date_duration = originalValue;
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
|
|
|
|
2013-08-06 13:47:24 -04:00
|
|
|
module("Discourse.Formatter");
|
|
|
|
|
2013-06-18 16:27:40 -04:00
|
|
|
test("autoUpdatingRelativeAge", function() {
|
2014-08-20 16:22:58 -04:00
|
|
|
var d = moment().subtract(1, 'day').toDate();
|
2013-06-18 16:27:40 -04:00
|
|
|
|
|
|
|
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
|
|
|
|
equal($elem.data('format'), "tiny");
|
|
|
|
equal($elem.data('time'), d.getTime());
|
2013-06-26 16:27:08 -04:00
|
|
|
equal($elem.attr('title'), undefined);
|
2013-06-18 16:27:40 -04:00
|
|
|
|
2013-06-26 16:27:08 -04:00
|
|
|
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d, {title: true}));
|
|
|
|
equal($elem.attr('title'), moment(d).longDate());
|
|
|
|
|
|
|
|
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium', title: true, leaveAgo: true}));
|
2013-06-18 16:27:40 -04:00
|
|
|
equal($elem.data('format'), "medium-with-ago");
|
|
|
|
equal($elem.data('time'), d.getTime());
|
|
|
|
equal($elem.attr('title'), moment(d).longDate());
|
|
|
|
equal($elem.html(), '1 day ago');
|
|
|
|
|
|
|
|
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium'}));
|
|
|
|
equal($elem.data('format'), "medium");
|
|
|
|
equal($elem.data('time'), d.getTime());
|
2013-06-26 16:27:08 -04:00
|
|
|
equal($elem.attr('title'), undefined);
|
2013-06-18 16:27:40 -04:00
|
|
|
equal($elem.html(), '1 day');
|
|
|
|
});
|
|
|
|
|
|
|
|
test("updateRelativeAge", function(){
|
|
|
|
|
|
|
|
var d = new Date();
|
|
|
|
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
|
|
|
|
$elem.data('time', d.getTime() - 2 * 60 * 1000);
|
|
|
|
|
|
|
|
Discourse.Formatter.updateRelativeAge($elem);
|
|
|
|
|
|
|
|
equal($elem.html(), "2m");
|
|
|
|
|
|
|
|
d = new Date();
|
|
|
|
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d, {format: 'medium', leaveAgo: true}));
|
|
|
|
$elem.data('time', d.getTime() - 2 * 60 * 1000);
|
|
|
|
|
|
|
|
Discourse.Formatter.updateRelativeAge($elem);
|
|
|
|
|
2013-06-20 04:00:31 -04:00
|
|
|
equal($elem.html(), "2 mins ago");
|
2013-06-18 16:27:40 -04:00
|
|
|
});
|
2013-06-27 23:56:38 -04:00
|
|
|
|
|
|
|
test("breakUp", function(){
|
|
|
|
|
2013-12-30 00:07:35 -05:00
|
|
|
var b = function(s,hint){ return Discourse.Formatter.breakUp(s,hint); };
|
2013-06-27 23:56:38 -04:00
|
|
|
|
|
|
|
equal(b("hello"), "hello");
|
2013-12-22 19:11:41 -05:00
|
|
|
equal(b("helloworld"), "helloworld");
|
2013-12-30 00:07:35 -05:00
|
|
|
equal(b("HeMans11"), "He<wbr>​Mans<wbr>​11");
|
|
|
|
equal(b("he_man"), "he_<wbr>​man");
|
|
|
|
equal(b("he11111"), "he<wbr>​11111");
|
|
|
|
equal(b("HRCBob"), "HRC<wbr>​Bob");
|
|
|
|
equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>​marley<wbr>​too");
|
2013-06-27 23:56:38 -04:00
|
|
|
|
|
|
|
});
|
2014-03-04 13:44:53 -05:00
|
|
|
|
|
|
|
test("number", function() {
|
|
|
|
equal(Discourse.Formatter.number(123), "123", "it returns a string version of the number");
|
|
|
|
equal(Discourse.Formatter.number("123"), "123", "it works with a string command");
|
|
|
|
equal(Discourse.Formatter.number(NaN), "0", "it reeturns 0 for NaN");
|
|
|
|
equal(Discourse.Formatter.number(3333), "3.3K", "it abbreviates thousands");
|
|
|
|
});
|