zulip/frontend_tests/node_tests/activity.js
Steve Howell 06f4857221 buddy list: Make Group PMs appear more quickly.
It used to be the case that you would get new messages for a
huddle, but the huddle wouldn't show up on your buddy list until
the every-50-seconds mass update of the buddy list.

Now we make sure to work with non-stale jQuery objects, and,
more importantly, we resize ourselves if we add new huddles.

(The resize issue arises due to some complicated heuristics
where we don't want group PMs to take up too much of the buddy
list for users who don't have many in their history.)
2016-11-11 12:02:48 -08:00

227 lines
5.6 KiB
JavaScript

global.stub_out_jquery();
set_global('page_params', {
people_list: []
});
add_dependencies({
util: 'js/util.js',
people: 'js/people.js'
});
set_global('resize', {
resize_page_components: function () {}
});
set_global('document', {
hasFocus: function () {
return true;
}
});
global.people.add({
email: '[email protected]',
user_id: 1,
full_name: 'Alice Smith'
});
global.people.add({
email: '[email protected]',
user_id: 2,
full_name: "Fred Flintstone"
});
global.people.add({
email: '[email protected]',
user_id: 3,
full_name: 'Jill Hill'
});
global.people.add({
email: '[email protected]',
user_id: 4,
full_name: 'Marky Mark'
});
global.people.add({
email: '[email protected]',
user_id: 5,
full_name: 'Norbert Oswald'
});
var activity = require('js/activity.js');
activity.update_huddles = function () {};
(function test_sort_users() {
var users = ['[email protected]', '[email protected]', '[email protected]'];
var user_info = {
'[email protected]': {status: 'inactive'},
'[email protected]': {status: 'active'},
'[email protected]': {status: 'active'}
};
activity._sort_users(users, user_info);
assert.deepEqual(users, [
'[email protected]',
'[email protected]',
'[email protected]'
]);
}());
(function test_process_loaded_messages() {
var huddle1 = '[email protected],[email protected]';
var timestamp1 = 1382479029; // older
var huddle2 = '[email protected],[email protected]';
var timestamp2 = 1382479033; // newer
var old_timestamp = 1382479000;
var messages = [
{
type: 'private',
reply_to: huddle1,
timestamp: timestamp1
},
{
type: 'stream'
},
{
type: 'private',
reply_to: '[email protected]'
},
{
type: 'private',
reply_to: huddle2,
timestamp: timestamp2
},
{
type: 'private',
reply_to: huddle2,
timestamp: old_timestamp
}
];
activity.process_loaded_messages(messages);
assert.deepEqual(activity.get_huddles(), [huddle2, huddle1]);
}());
(function test_full_huddle_name() {
assert.equal(
activity.full_huddle_name('[email protected],[email protected]'),
'Alice Smith, Jill Hill'
);
assert.equal(
activity.full_huddle_name('[email protected],[email protected],[email protected]'),
'Alice Smith, Fred Flintstone, Jill Hill'
);
}());
(function test_short_huddle_name() {
assert.equal(
activity.short_huddle_name('[email protected]'),
'Alice Smith'
);
assert.equal(
activity.short_huddle_name('[email protected],[email protected]'),
'Alice Smith, Jill Hill'
);
assert.equal(
activity.short_huddle_name('[email protected],[email protected],[email protected]'),
'Alice Smith, Fred Flintstone, Jill Hill'
);
assert.equal(
activity.short_huddle_name('[email protected],[email protected],[email protected],[email protected]'),
'Alice Smith, Fred Flintstone, Jill Hill, + 1 other'
);
assert.equal(
activity.short_huddle_name('[email protected],[email protected],[email protected],[email protected],[email protected]'),
'Alice Smith, Fred Flintstone, Jill Hill, + 2 others'
);
}());
(function test_huddle_fraction_present() {
var huddle = '[email protected],[email protected],[email protected],[email protected]';
var presence_list = {
'[email protected]': {status: 'active'},
'[email protected]': {status: 'idle'}, // counts as present
// jill not in list
'[email protected]': {status: 'offline'} // does not count
};
assert.equal(
activity.huddle_fraction_present(huddle, presence_list),
'0.50'
);
}());
(function test_on_mobile_property() {
var base_time = 500;
var presence = {
website: {
status: "active",
timestamp: base_time
}
};
var status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS - 1, presence
);
assert.equal(status.mobile, false);
presence.Android = {
status: "active",
timestamp: base_time + activity._OFFLINE_THRESHOLD_SECS / 2,
pushable: false
};
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS, presence
);
assert.equal(status.mobile, true);
assert.equal(status.status, "active");
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS - 1, presence
);
assert.equal(status.mobile, false);
assert.equal(status.status, "active");
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS * 2, presence
);
assert.equal(status.mobile, false);
assert.equal(status.status, "offline");
presence.Android = {
status: "idle",
timestamp: base_time + activity._OFFLINE_THRESHOLD_SECS / 2,
pushable: true
};
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS, presence
);
assert.equal(status.mobile, true);
assert.equal(status.status, "idle");
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS - 1, presence
);
assert.equal(status.mobile, false);
assert.equal(status.status, "active");
status = activity._status_from_timestamp(
base_time + activity._OFFLINE_THRESHOLD_SECS * 2, presence
);
assert.equal(status.mobile, true);
assert.equal(status.status, "offline");
}());