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);
|
2018-12-07 22:14:28 +01:00
|
|
|
|
set_global('page_params', {
|
2017-12-25 21:43:06 +01:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const expected = {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
time_str: i18n.t('Today'),
|
|
|
|
|
formal_time_str: 'Friday, April 12, 2019',
|
|
|
|
|
needs_update: true,
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_now(today, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const yesterday = today.clone().addDays(-1);
|
|
|
|
|
const expected = {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
time_str: i18n.t('Yesterday'),
|
|
|
|
|
formal_time_str: 'Thursday, April 11, 2019',
|
|
|
|
|
needs_update: true,
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_now(yesterday, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const year_ago = today.clone().addYears(-1);
|
|
|
|
|
const expected = {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
time_str: 'Apr 12, 2018',
|
|
|
|
|
formal_time_str: 'Thursday, April 12, 2018',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_now(year_ago, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const three_months_ago = today.clone().addMonths(-3, true);
|
|
|
|
|
const expected = {
|
2017-05-18 21:18:11 +02:00
|
|
|
|
time_str: 'Jan 12',
|
|
|
|
|
formal_time_str: 'Saturday, January 12, 2019',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_now(three_months_ago, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const three_months_ago = today.clone().addMonths(-6, true);
|
|
|
|
|
const expected = {
|
2017-05-24 02:20:06 +02:00
|
|
|
|
time_str: 'Oct 12, 2018',
|
|
|
|
|
formal_time_str: 'Friday, October 12, 2018',
|
|
|
|
|
needs_update: false,
|
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_now(three_months_ago, today);
|
2017-05-24 02:20:06 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const message_time = today.clone();
|
|
|
|
|
const expected_html = i18n.t('Today');
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const attrs = new Dict();
|
|
|
|
|
const span_stub = $('<span />');
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual = timerender.render_date(message_time, undefined, today);
|
2017-05-18 21:18:11 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const today = new XDate(1555091573000); // Friday 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
const message_time = today.clone();
|
|
|
|
|
const message_time_above = today.clone().addDays(-1);
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const span_stub = $('<span />');
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let appended_val;
|
2017-06-02 16:38:34 +02:00
|
|
|
|
span_stub.append = function (val) {
|
|
|
|
|
appended_val = val;
|
|
|
|
|
return span_stub;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const expected = [
|
2018-10-15 15:52:54 +02:00
|
|
|
|
'<i class="date-direction fa fa-caret-up"></i>',
|
2017-06-02 16:38:34 +02:00
|
|
|
|
i18n.t('Yesterday'),
|
|
|
|
|
'<hr class="date-line">',
|
2018-10-15 15:52:54 +02:00
|
|
|
|
'<i class="date-direction fa fa-caret-down"></i>',
|
2017-06-02 16:38:34 +02:00
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const timestamp = 1495091573; // 5/18/2017 7:12:53 AM (UTC+0)
|
|
|
|
|
const expected = '2017-05-18T07:12:53Z'; // ISO 8601 date format
|
|
|
|
|
const actual = timerender.get_full_time(timestamp);
|
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_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
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let timestamp = 1555091573000; // 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
let today = new Date(timestamp);
|
|
|
|
|
let expected = 'Apr 12 05:52 PM';
|
|
|
|
|
let 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
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let timestamp = 1555091573000; // 4/12/2019 5:52:53 PM (UTC+0)
|
|
|
|
|
let today = new Date(timestamp);
|
|
|
|
|
let expected = 'Apr 12 17:52';
|
|
|
|
|
let 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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const message = {
|
2019-06-04 04:28:47 +02:00
|
|
|
|
timestamp: 1495091573, // 2017/5/18 7:12:53 AM (UTC+0)
|
2017-05-18 21:18:11 +02:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const time_element = $('<span/>');
|
|
|
|
|
const attrs = new Dict();
|
2017-06-02 16:38:34 +02:00
|
|
|
|
|
|
|
|
|
time_element.attr = function (name, val) {
|
|
|
|
|
attrs.set(name, val);
|
|
|
|
|
return time_element;
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-04 04:28:47 +02:00
|
|
|
|
// The formatting of the string time.toLocale(Date|Time)String() on Node
|
|
|
|
|
// might differ from the browser.
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const time = new Date(message.timestamp * 1000);
|
|
|
|
|
const expected = `${time.toLocaleDateString()} 7:12:53 AM (UTC+0)`;
|
2017-05-18 21:18:11 +02:00
|
|
|
|
timerender.set_full_datetime(message, time_element);
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const 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)
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let base_date = new XDate(2016, 2, 1, 0, 30);
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
function assert_same(modifier, expected_status) {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let past_date = base_date.clone();
|
2017-05-12 20:16:39 +02:00
|
|
|
|
past_date = modifier(past_date);
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const actual_status = timerender.last_seen_status_from_date(past_date, base_date);
|
2017-05-12 20:16:39 +02:00
|
|
|
|
assert.equal(actual_status, expected_status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addSeconds(-20); },
|
2018-08-15 20:58:12 +02:00
|
|
|
|
i18n.t("Just now"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-1); },
|
2018-08-15 20:58:12 +02:00
|
|
|
|
i18n.t("Just now"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-2); },
|
2018-08-15 20:58:12 +02:00
|
|
|
|
i18n.t("Just now"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addMinutes(-30); },
|
2018-08-28 19:38:57 +02:00
|
|
|
|
i18n.t("30 minutes ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-1); },
|
2018-08-28 19:38:57 +02:00
|
|
|
|
i18n.t("An hour ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-2); },
|
2018-08-28 19:38:57 +02:00
|
|
|
|
i18n.t("2 hours ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addHours(-20); },
|
2018-08-28 19:38:57 +02:00
|
|
|
|
i18n.t("20 hours ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-1); },
|
2018-08-28 19:38:57 +02:00
|
|
|
|
i18n.t("Yesterday"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-2); },
|
2019-03-13 19:23:57 +01:00
|
|
|
|
i18n.t("2 days ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-61); },
|
2019-03-13 19:23:57 +01:00
|
|
|
|
i18n.t("61 days ago"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-300); },
|
2019-03-13 19:23:57 +01:00
|
|
|
|
i18n.t("May 06,\xa02015"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-366); },
|
2019-03-13 19:23:57 +01:00
|
|
|
|
i18n.t("Mar 01,\xa02015"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addYears(-3); },
|
2019-03-13 19:23:57 +01:00
|
|
|
|
i18n.t("Mar 01,\xa02013"));
|
|
|
|
|
|
|
|
|
|
// Set base_dateto to May 1 2016 12.30 AM (months are zero based)
|
|
|
|
|
base_date = new XDate(2016, 4, 1, 0, 30);
|
|
|
|
|
|
|
|
|
|
assert_same(function (d) { return d.addDays(-91); },
|
|
|
|
|
i18n.t("Jan\xa031"));
|
2017-05-12 20:16:39 +02:00
|
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let time = new XDate(1549958107000); // Tuesday 2/12/2019 07:55:07 AM (UTC+0)
|
|
|
|
|
let time_str = timerender.stringify_time(time);
|
|
|
|
|
let expected = '07:55';
|
2017-12-25 21:43:06 +01:00
|
|
|
|
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
|
|
|
|
});
|