discourse/test/javascripts/lib/formatter-test.js.es6

214 lines
7.1 KiB
Plaintext
Raw Normal View History

var clock;
import { relativeAge, autoUpdatingRelativeAge, updateRelativeAge, breakUp, number, longDate } from 'discourse/lib/formatter';
2015-08-07 15:08:27 -04:00
2017-06-14 13:57:58 -04:00
QUnit.module("lib:formatter", {
beforeEach() {
clock = sinon.useFakeTimers(new Date(2012,11,31,12,0).getTime());
},
2017-06-14 13:57:58 -04:00
afterEach() {
clock.restore();
}
});
var format = "tiny";
var leaveAgo = false;
var mins_ago = function(mins){
return new Date((new Date()) - mins * 60 * 1000);
};
var formatMins = function(mins) {
2015-08-07 15:08:27 -04:00
return relativeAge(mins_ago(mins), {format: format, leaveAgo: leaveAgo});
};
var formatHours = function(hours) {
return formatMins(hours * 60);
};
var formatDays = function(days) {
return formatHours(days * 24);
};
var shortDate = function(days){
return moment().subtract(days, 'days').format('MMM D');
};
2017-06-14 13:57:58 -04:00
QUnit.test("formating medium length dates", assert => {
format = "medium";
var strip = function(html){
return $(html).text();
};
var shortDateYear = function(days){
return moment().subtract(days, 'days').format("MMM D, 'YY");
};
leaveAgo = true;
2017-06-14 13:57:58 -04:00
assert.equal(strip(formatMins(1.4)), "1 min ago");
assert.equal(strip(formatMins(2)), "2 mins ago");
assert.equal(strip(formatMins(55)), "55 mins ago");
assert.equal(strip(formatMins(56)), "1 hour ago");
assert.equal(strip(formatHours(4)), "4 hours ago");
assert.equal(strip(formatHours(22)), "22 hours ago");
assert.equal(strip(formatHours(23)), "23 hours ago");
assert.equal(strip(formatHours(23.5)), "1 day ago");
assert.equal(strip(formatDays(4.85)), "4 days ago");
leaveAgo = false;
2017-06-14 13:57:58 -04:00
assert.equal(strip(formatMins(0)), "just now");
assert.equal(strip(formatMins(1.4)), "1 min");
assert.equal(strip(formatMins(2)), "2 mins");
assert.equal(strip(formatMins(55)), "55 mins");
assert.equal(strip(formatMins(56)), "1 hour");
assert.equal(strip(formatHours(4)), "4 hours");
assert.equal(strip(formatHours(22)), "22 hours");
assert.equal(strip(formatHours(23)), "23 hours");
assert.equal(strip(formatHours(23.5)), "1 day");
assert.equal(strip(formatDays(4.85)), "4 days");
assert.equal(strip(formatDays(6)), shortDate(6));
assert.equal(strip(formatDays(100)), shortDate(100)); // eg: Jan 23
assert.equal(strip(formatDays(500)), shortDateYear(500));
assert.equal($(formatDays(0)).attr("title"), longDate(new Date()));
assert.equal($(formatDays(0)).attr("class"), "date");
clock.restore();
clock = sinon.useFakeTimers(new Date(2012,0,9,12,0).getTime()); // Jan 9, 2012
2017-06-14 13:57:58 -04:00
assert.equal(strip(formatDays(8)), shortDate(8));
assert.equal(strip(formatDays(10)), shortDateYear(10));
});
2017-06-14 13:57:58 -04:00
QUnit.test("formating tiny dates", assert => {
var shortDateYear = function(days){
return moment().subtract(days, 'days').format("MMM 'YY");
};
format = "tiny";
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
var originalValue = Discourse.SiteSettings.relative_date_duration;
Discourse.SiteSettings.relative_date_duration = 7;
2017-06-14 13:57:58 -04:00
assert.equal(formatDays(7), "7d");
assert.equal(formatDays(8), shortDate(8));
Discourse.SiteSettings.relative_date_duration = 1;
2017-06-14 13:57:58 -04:00
assert.equal(formatDays(1), "1d");
assert.equal(formatDays(2), shortDate(2));
Discourse.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));
Discourse.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));
Discourse.SiteSettings.relative_date_duration = 14;
clock.restore();
clock = sinon.useFakeTimers(new Date(2012,0,12,12,0).getTime()); // Jan 12, 2012
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));
clock.restore();
clock = sinon.useFakeTimers(new Date(2012,0,20,12,0).getTime()); // Jan 20, 2012
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));
Discourse.SiteSettings.relative_date_duration = originalValue;
});
2017-06-14 13:57:58 -04:00
QUnit.test("autoUpdatingRelativeAge", assert => {
var d = moment().subtract(1, 'day').toDate();
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);
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));
2013-06-26 16:27:08 -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');
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');
});
2017-06-14 13:57:58 -04:00
QUnit.test("updateRelativeAge", assert =>{
var d = new Date();
2015-08-07 15:08:27 -04:00
var $elem = $(autoUpdatingRelativeAge(d));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
2015-08-07 15:08:27 -04:00
updateRelativeAge($elem);
2017-06-14 13:57:58 -04:00
assert.equal($elem.html(), "2m");
d = new Date();
2015-08-07 15:08:27 -04:00
$elem = $(autoUpdatingRelativeAge(d, {format: 'medium', leaveAgo: true}));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
2015-08-07 15:08:27 -04:00
updateRelativeAge($elem);
2017-06-14 13:57:58 -04:00
assert.equal($elem.html(), "2 mins ago");
});
2017-06-14 13:57:58 -04:00
QUnit.test("breakUp", assert =>{
2015-08-07 15:08:27 -04:00
var b = function(s,hint){ return breakUp(s,hint); };
2017-06-14 13:57:58 -04:00
assert.equal(b("hello"), "hello");
assert.equal(b("helloworld"), "helloworld");
assert.equal(b("HeMans11"), "He<wbr>&#8203;Mans<wbr>&#8203;11");
assert.equal(b("he_man"), "he_<wbr>&#8203;man");
assert.equal(b("he11111"), "he<wbr>&#8203;11111");
assert.equal(b("HRCBob"), "HRC<wbr>&#8203;Bob");
assert.equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>&#8203;marley<wbr>&#8203;too");
});
2017-06-14 13:57:58 -04:00
QUnit.test("number", assert => {
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");
});