2017-06-02 16:38:34 +02:00
|
|
|
|
set_global('$', global.make_zjquery());
|
2017-06-29 20:58:08 +02:00
|
|
|
|
set_global('i18n', global.stub_i18n);
|
2017-12-25 21:43:06 +01:00
|
|
|
|
set_global('page_params' , {
|
|
|
|
|
twenty_four_hour_time: true,
|
|
|
|
|
});
|
2017-11-08 19:52:44 +01:00
|
|
|
|
zrequire('XDate', 'xdate');
|
|
|
|
|
zrequire('timerender');
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_now_returns_today', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var expected = {
|
|
|
|
|
time_str: i18n.t('Today'),
|
|
|
|
|
formal_time_str: 'Friday, April 12, 2019',
|
|
|
|
|
needs_update: true,
|
|
|
|
|
};
|
|
|
|
|
var actual = timerender.render_now(today, today);
|
|
|
|
|
assert.equal(expected.time_str, actual.time_str);
|
|
|
|
|
assert.equal(expected.formal_time_str, actual.formal_time_str);
|
|
|
|
|
assert.equal(expected.needs_update, actual.needs_update);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_now_returns_yesterday', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var yesterday = today.clone().addDays(-1);
|
|
|
|
|
var expected = {
|
|
|
|
|
time_str: i18n.t('Yesterday'),
|
|
|
|
|
formal_time_str: 'Thursday, April 11, 2019',
|
|
|
|
|
needs_update: true,
|
|
|
|
|
};
|
|
|
|
|
var actual = timerender.render_now(yesterday, today);
|
|
|
|
|
assert.equal(expected.time_str, actual.time_str);
|
|
|
|
|
assert.equal(expected.formal_time_str, actual.formal_time_str);
|
|
|
|
|
assert.equal(expected.needs_update, actual.needs_update);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_now_returns_year', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var year_ago = today.clone().addYears(-1);
|
|
|
|
|
var expected = {
|
|
|
|
|
time_str: 'Apr 12, 2018',
|
|
|
|
|
formal_time_str: 'Thursday, April 12, 2018',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
|
|
|
|
var actual = timerender.render_now(year_ago, today);
|
|
|
|
|
assert.equal(expected.time_str, actual.time_str);
|
|
|
|
|
assert.equal(expected.formal_time_str, actual.formal_time_str);
|
|
|
|
|
assert.equal(expected.needs_update, actual.needs_update);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_now_returns_month_and_day', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var three_months_ago = today.clone().addMonths(-3, true);
|
|
|
|
|
var expected = {
|
|
|
|
|
time_str: 'Jan 12',
|
|
|
|
|
formal_time_str: 'Saturday, January 12, 2019',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
|
|
|
|
var actual = timerender.render_now(three_months_ago, today);
|
|
|
|
|
assert.equal(expected.time_str, actual.time_str);
|
|
|
|
|
assert.equal(expected.formal_time_str, actual.formal_time_str);
|
|
|
|
|
assert.equal(expected.needs_update, actual.needs_update);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_now_returns_year_with_year_boundary', () => {
|
2017-05-24 02:20:06 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var three_months_ago = today.clone().addMonths(-6, true);
|
|
|
|
|
var expected = {
|
|
|
|
|
time_str: 'Oct 12, 2018',
|
|
|
|
|
formal_time_str: 'Friday, October 12, 2018',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
|
|
|
|
var actual = timerender.render_now(three_months_ago, today);
|
|
|
|
|
assert.equal(expected.time_str, actual.time_str);
|
|
|
|
|
assert.equal(expected.formal_time_str, actual.formal_time_str);
|
|
|
|
|
assert.equal(expected.needs_update, actual.needs_update);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-24 02:20:06 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_date_renders_time_html', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var message_time = today.clone();
|
2017-06-02 16:38:34 +02:00
|
|
|
|
var expected_html = i18n.t('Today');
|
|
|
|
|
|
|
|
|
|
var attrs = new Dict();
|
|
|
|
|
var span_stub = $('<span />');
|
|
|
|
|
|
|
|
|
|
span_stub.attr = function (name, val) {
|
|
|
|
|
attrs.set(name, val);
|
|
|
|
|
return span_stub;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
span_stub.append = function (str) {
|
|
|
|
|
span_stub.html(str);
|
|
|
|
|
return span_stub;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var actual = timerender.render_date(message_time, undefined, today);
|
|
|
|
|
assert.equal(expected_html, actual.html());
|
2017-06-02 16:38:34 +02:00
|
|
|
|
assert.equal(attrs.get('title'), 'Friday, April 12, 2019');
|
2017-06-03 20:10:09 +02:00
|
|
|
|
assert.equal(attrs.get('class'), 'timerender0');
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('render_date_renders_time_above_html', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var message_time = today.clone();
|
|
|
|
|
var message_time_above = today.clone().addDays(-1);
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
|
|
|
|
var span_stub = $('<span />');
|
|
|
|
|
|
|
|
|
|
var appended_val;
|
|
|
|
|
span_stub.append = function (val) {
|
|
|
|
|
appended_val = val;
|
|
|
|
|
return span_stub;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var expected = [
|
|
|
|
|
'<i class="date-direction icon-vector-caret-up"></i>',
|
|
|
|
|
i18n.t('Yesterday'),
|
|
|
|
|
'<hr class="date-line">',
|
|
|
|
|
'<i class="date-direction icon-vector-caret-down"></i>',
|
|
|
|
|
i18n.t('Today'),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
timerender.render_date(message_time, message_time_above, today);
|
|
|
|
|
assert.deepEqual(appended_val, expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('get_full_time', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var timestamp = 1495091573; // 5/18/2017 7:12:53 AM (UTC+0)
|
2017-06-21 21:37:53 +02:00
|
|
|
|
var expected = '2017-05-18T07:12:53Z'; // ISO 8601 date format
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var actual = timerender.get_full_time(timestamp);
|
|
|
|
|
assert.equal(expected, actual);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('absolute_time_12_hour', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
set_global('page_params', {
|
|
|
|
|
twenty_four_hour_time: false,
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-26 07:20:31 +02:00
|
|
|
|
// timestamp with hour > 12, same year
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var timestamp = 1555091573000; // 4/12/2019 5:52:53 PM (UTC+0)
|
2017-07-26 07:20:31 +02:00
|
|
|
|
var today = new Date(timestamp);
|
|
|
|
|
var expected = 'Apr 12 05:52 PM';
|
|
|
|
|
var actual = timerender.absolute_time(timestamp, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
2017-07-26 07:20:31 +02:00
|
|
|
|
// timestamp with hour > 12, different year
|
|
|
|
|
today.setFullYear(today.getFullYear() + 1);
|
|
|
|
|
expected = 'Apr 12, 2019 05:52 PM';
|
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
|
|
|
|
// timestamp with hour < 12, same year
|
2017-05-18 21:18:11 +02:00
|
|
|
|
timestamp = 1495091573000; // 5/18/2017 7:12:53 AM (UTC+0)
|
2017-07-26 07:20:31 +02:00
|
|
|
|
today = new Date(timestamp);
|
|
|
|
|
expected = 'May 18 07:12 AM';
|
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
|
|
|
|
// timestamp with hour < 12, different year
|
|
|
|
|
today.setFullYear(today.getFullYear() + 1);
|
2017-06-28 06:50:45 +02:00
|
|
|
|
expected = 'May 18, 2017 07:12 AM';
|
2017-07-26 07:20:31 +02:00
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
assert.equal(expected, actual);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('absolute_time_24_hour', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
set_global('page_params', {
|
|
|
|
|
twenty_four_hour_time: true,
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-26 07:20:31 +02:00
|
|
|
|
// timestamp with hour > 12, same year
|
|
|
|
|
var timestamp = 1555091573000; // 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
var today = new Date(timestamp);
|
|
|
|
|
var expected = 'Apr 12 17:52';
|
|
|
|
|
var actual = timerender.absolute_time(timestamp, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
2017-07-26 07:20:31 +02:00
|
|
|
|
// timestamp with hour > 12, different year
|
|
|
|
|
today.setFullYear(today.getFullYear() + 1);
|
|
|
|
|
expected = 'Apr 12, 2019 17:52';
|
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
|
|
|
|
// timestamp with hour < 12, same year
|
2017-05-18 21:18:11 +02:00
|
|
|
|
timestamp = 1495091573000; // 5/18/2017 7:12:53 AM (UTC+0)
|
2017-07-26 07:20:31 +02:00
|
|
|
|
today = new Date(timestamp);
|
|
|
|
|
expected = 'May 18 07:12';
|
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
|
|
|
|
assert.equal(expected, actual);
|
|
|
|
|
|
|
|
|
|
// timestamp with hour < 12, different year
|
|
|
|
|
today.setFullYear(today.getFullYear() + 1);
|
2017-06-28 06:50:45 +02:00
|
|
|
|
expected = 'May 18, 2017 07:12';
|
2017-07-26 07:20:31 +02:00
|
|
|
|
actual = timerender.absolute_time(timestamp, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
assert.equal(expected, actual);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-18 21:18:11 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('set_full_datetime', () => {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
var message = {
|
2018-04-04 20:06:41 +02:00
|
|
|
|
timestamp: 1495091573, // 2017-5-18 07:12:53 AM (UTC+0)
|
2017-05-18 21:18:11 +02:00
|
|
|
|
};
|
|
|
|
|
var time_element = $('<span/>');
|
2017-06-02 16:38:34 +02:00
|
|
|
|
var attrs = new Dict();
|
|
|
|
|
|
|
|
|
|
time_element.attr = function (name, val) {
|
|
|
|
|
attrs.set(name, val);
|
|
|
|
|
return time_element;
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-04 20:06:41 +02:00
|
|
|
|
// since node >= 8 date.toLocaleDateString and
|
|
|
|
|
// date.toLocaleTimeString have been changed, instead of
|
|
|
|
|
// returning 5/18/2017 7:12:53 AM (UTC+0) they now return
|
|
|
|
|
// 2017-5-18 07:12:53 (UTC+0) - This change does not affect browsers
|
|
|
|
|
// since browsers have their own way of returning string that is
|
|
|
|
|
// or maybe inconsistenc with node's way.
|
|
|
|
|
var time = new Date(message.timestamp * 1000);
|
|
|
|
|
var expected = `${time.toLocaleDateString()} 07:12:53 (UTC+0)`;
|
2017-05-18 21:18:11 +02:00
|
|
|
|
timerender.set_full_datetime(message, time_element);
|
2017-06-02 16:38:34 +02:00
|
|
|
|
var actual = attrs.get('title');
|
2017-05-18 21:18:11 +02:00
|
|
|
|
assert.equal(expected, actual);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('last_seen_status_from_date', () => {
|
2017-05-12 20:16:39 +02:00
|
|
|
|
// Set base_dateto to March 1 2016 12.30 AM (months are zero based)
|
|
|
|
|
var base_date = new XDate(2016, 2, 1, 0, 30);
|
|
|
|
|
|
|
|
|
|
function assert_same(modifier, expected_status) {
|
|
|
|
|
var past_date = base_date.clone();
|
|
|
|
|
past_date = modifier(past_date);
|
|
|
|
|
var actual_status = timerender.last_seen_status_from_date(past_date, base_date);
|
|
|
|
|
assert.equal(actual_status, expected_status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addSeconds(-20); },
|
|
|
|
|
i18n.t("Last seen just now"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-1); },
|
|
|
|
|
i18n.t("Last seen just now"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-2); },
|
|
|
|
|
i18n.t("Last seen just now"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-30); },
|
|
|
|
|
i18n.t("Last seen 30 minutes ago"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-1); },
|
|
|
|
|
i18n.t("Last seen an hour ago"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-2); },
|
|
|
|
|
i18n.t("Last seen 2 hours ago"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-20); },
|
|
|
|
|
i18n.t("Last seen 20 hours ago"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-1); },
|
|
|
|
|
i18n.t("Last seen yesterday"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-2); },
|
|
|
|
|
i18n.t("Last seen on Feb 28"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-61); },
|
|
|
|
|
i18n.t("Last seen on Dec 31"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-300); },
|
|
|
|
|
i18n.t("Last seen on May 06"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-366); },
|
|
|
|
|
i18n.t("Last seen on Mar 01, 2015"));
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addYears(-3); },
|
|
|
|
|
i18n.t("Last seen on Mar 01, 2013"));
|
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-12-25 21:43:06 +01:00
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
|
run_test('set_full_datetime', () => {
|
2017-12-25 21:43:06 +01:00
|
|
|
|
var time = new XDate(1549958107000); // Tuesday 2/12/2019 07:55:07 AM (UTC+0)
|
|
|
|
|
var time_str = timerender.stringify_time(time);
|
|
|
|
|
var expected = '07:55';
|
|
|
|
|
assert.equal(expected, time_str);
|
|
|
|
|
|
|
|
|
|
page_params.twenty_four_hour_time = false;
|
|
|
|
|
time_str = timerender.stringify_time(time);
|
|
|
|
|
expected = '7:55 AM';
|
|
|
|
|
assert.equal(expected, time_str);
|
|
|
|
|
|
|
|
|
|
time = new XDate(1549979707000); // Tuesday 2/12/2019 13:55:07 PM (UTC+0)
|
|
|
|
|
page_params.twenty_four_hour_time = false;
|
|
|
|
|
time_str = timerender.stringify_time(time);
|
|
|
|
|
expected = '1:55 PM';
|
|
|
|
|
assert.equal(expected, time_str);
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|