2013-08-06 21:30:31 +02:00
|
|
|
// These unit tests for static/js/message_list.js emphasize the model-ish
|
|
|
|
// aspects of the MessageList class. We have to stub out a few functions
|
|
|
|
// related to views and events to get the tests working.
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
|
|
|
util: 'js/util.js',
|
2013-09-19 00:43:59 +02:00
|
|
|
muting: 'js/muting.js',
|
2016-12-03 23:17:57 +01:00
|
|
|
MessageListView: 'js/message_list_view.js',
|
2013-08-21 20:27:14 +02:00
|
|
|
});
|
|
|
|
|
2013-08-06 21:30:31 +02:00
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
set_global('document', null);
|
2016-07-30 20:01:15 +02:00
|
|
|
|
|
|
|
global.stub_out_jquery();
|
2013-08-06 21:30:31 +02:00
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
set_global('feature_flags', {});
|
2016-04-21 22:49:23 +02:00
|
|
|
set_global('Filter', function () {});
|
2013-08-06 21:30:31 +02:00
|
|
|
|
2016-04-20 22:37:26 +02:00
|
|
|
var MessageList = require('js/message_list').MessageList;
|
2013-08-06 21:30:31 +02:00
|
|
|
|
|
|
|
(function test_basics() {
|
|
|
|
var table;
|
|
|
|
var filter = {};
|
|
|
|
|
|
|
|
var list = new MessageList(table, filter);
|
|
|
|
|
|
|
|
var messages = [
|
|
|
|
{
|
|
|
|
id: 50,
|
2016-12-03 23:17:57 +01:00
|
|
|
content: 'fifty',
|
2013-08-06 21:30:31 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 60,
|
2013-08-06 21:30:31 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 70,
|
2013-08-06 21:30:31 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 80,
|
|
|
|
},
|
2013-08-06 21:30:31 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
assert.equal(list.empty(), true);
|
|
|
|
|
|
|
|
list.append(messages, true);
|
|
|
|
|
2017-06-07 04:34:44 +02:00
|
|
|
assert.equal(list.num_items(), 4);
|
2013-08-06 21:30:31 +02:00
|
|
|
assert.equal(list.empty(), false);
|
|
|
|
assert.equal(list.first().id, 50);
|
|
|
|
assert.equal(list.last().id, 80);
|
|
|
|
|
|
|
|
assert.equal(list.get(50).content, 'fifty');
|
|
|
|
|
|
|
|
assert.equal(list.closest_id(49), 50);
|
|
|
|
assert.equal(list.closest_id(50), 50);
|
|
|
|
assert.equal(list.closest_id(51), 50);
|
|
|
|
assert.equal(list.closest_id(59), 60);
|
|
|
|
assert.equal(list.closest_id(60), 60);
|
|
|
|
assert.equal(list.closest_id(61), 60);
|
|
|
|
|
2016-04-23 00:56:44 +02:00
|
|
|
assert.deepEqual(list.all_messages(), messages);
|
2013-08-06 21:30:31 +02:00
|
|
|
|
|
|
|
global.$.Event = function (ev) {
|
|
|
|
assert.equal(ev, 'message_selected.zulip');
|
|
|
|
};
|
|
|
|
list.select_id(50);
|
|
|
|
|
|
|
|
assert.equal(list.selected_id(), 50);
|
2017-06-07 04:34:44 +02:00
|
|
|
assert.equal(list.selected_idx(), 0);
|
2013-08-06 21:30:31 +02:00
|
|
|
|
|
|
|
list.advance_past_messages([60, 80]);
|
|
|
|
assert.equal(list.selected_id(), 60);
|
2017-06-07 04:34:44 +02:00
|
|
|
assert.equal(list.selected_idx(), 1);
|
|
|
|
|
|
|
|
// Make sure not rerendered when reselected
|
|
|
|
var num_renders = 0;
|
|
|
|
list.rerender = function () {
|
|
|
|
num_renders += 1;
|
|
|
|
};
|
|
|
|
list.reselect_selected_id();
|
|
|
|
assert.equal(num_renders, 0);
|
|
|
|
assert.equal(list.selected_id(), 60);
|
2013-08-06 21:30:31 +02:00
|
|
|
|
|
|
|
var old_messages = [
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 30,
|
2013-08-06 21:30:31 +02:00
|
|
|
},
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 40,
|
|
|
|
},
|
2013-08-06 21:30:31 +02:00
|
|
|
];
|
|
|
|
list.prepend(old_messages, true);
|
|
|
|
assert.equal(list.first().id, 30);
|
|
|
|
assert.equal(list.last().id, 80);
|
|
|
|
|
|
|
|
var new_messages = [
|
|
|
|
{
|
2016-12-03 23:17:57 +01:00
|
|
|
id: 90,
|
|
|
|
},
|
2013-08-06 21:30:31 +02:00
|
|
|
];
|
|
|
|
list.append(new_messages, true);
|
|
|
|
assert.equal(list.last().id, 90);
|
|
|
|
|
2013-08-16 17:10:22 +02:00
|
|
|
list.view.clear_table = function () {};
|
2013-12-17 20:50:11 +01:00
|
|
|
|
|
|
|
list.remove_and_rerender([{id: 60}]);
|
2016-04-23 00:56:44 +02:00
|
|
|
var removed = list.all_messages().filter(function (msg) {
|
2013-12-17 20:50:11 +01:00
|
|
|
return msg.id !== 60;
|
|
|
|
});
|
2016-04-23 00:56:44 +02:00
|
|
|
assert.deepEqual(list.all_messages(), removed);
|
2013-12-17 20:50:11 +01:00
|
|
|
|
2013-08-06 21:30:31 +02:00
|
|
|
list.clear();
|
2016-04-23 00:56:44 +02:00
|
|
|
assert.deepEqual(list.all_messages(), []);
|
2013-08-06 21:30:31 +02:00
|
|
|
}());
|
2013-08-14 23:04:24 +02:00
|
|
|
|
|
|
|
(function test_nth_most_recent_id() {
|
|
|
|
var table;
|
|
|
|
var filter = {};
|
|
|
|
|
|
|
|
var list = new MessageList(table, filter);
|
|
|
|
list.append([{id:10}, {id:20}, {id:30}]);
|
|
|
|
assert.equal(list.nth_most_recent_id(1), 30);
|
|
|
|
assert.equal(list.nth_most_recent_id(2), 20);
|
|
|
|
assert.equal(list.nth_most_recent_id(3), 10);
|
|
|
|
assert.equal(list.nth_most_recent_id(4), -1);
|
|
|
|
}());
|
2014-03-11 20:17:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
(function test_local_echo() {
|
|
|
|
var table;
|
|
|
|
var filter = {};
|
|
|
|
|
|
|
|
var list = new MessageList(table, filter);
|
|
|
|
list.append([{id:10}, {id:20}, {id:30}, {id:20.02}, {id:20.03}, {id:40}, {id:50}, {id:60}]);
|
|
|
|
list._local_only= {20.02: {id:20.02}, 20.03: {id:20.03}};
|
|
|
|
|
|
|
|
assert.equal(list.closest_id(10), 10);
|
|
|
|
assert.equal(list.closest_id(20), 20);
|
|
|
|
assert.equal(list.closest_id(30), 30);
|
|
|
|
assert.equal(list.closest_id(20.02), 20.02);
|
|
|
|
assert.equal(list.closest_id(20.03), 20.03);
|
|
|
|
assert.equal(list.closest_id(29), 30);
|
|
|
|
assert.equal(list.closest_id(40), 40);
|
|
|
|
assert.equal(list.closest_id(50), 50);
|
|
|
|
assert.equal(list.closest_id(60), 60);
|
|
|
|
|
|
|
|
assert.equal(list.closest_id(60), 60);
|
|
|
|
assert.equal(list.closest_id(21), 20);
|
|
|
|
assert.equal(list.closest_id(29), 30);
|
|
|
|
assert.equal(list.closest_id(31), 30);
|
|
|
|
assert.equal(list.closest_id(54), 50);
|
|
|
|
assert.equal(list.closest_id(58), 60);
|
|
|
|
|
|
|
|
|
|
|
|
list = new MessageList(table, filter);
|
2016-12-02 15:16:33 +01:00
|
|
|
list.append([{id:10}, {id:20}, {id:30}, {id:20.02}, {id:20.03}, {id:40},
|
|
|
|
{id:50}, {id: 50.01}, {id: 50.02}, {id:60}]);
|
|
|
|
list._local_only= {20.02: {id:20.02}, 20.03: {id:20.03},
|
|
|
|
50.01: {id: 50.01}, 50.02: {id: 50.02}};
|
2014-03-11 20:17:14 +01:00
|
|
|
|
|
|
|
assert.equal(list.closest_id(10), 10);
|
|
|
|
assert.equal(list.closest_id(20), 20);
|
|
|
|
assert.equal(list.closest_id(30), 30);
|
|
|
|
assert.equal(list.closest_id(20.02), 20.02);
|
|
|
|
assert.equal(list.closest_id(20.03), 20.03);
|
|
|
|
assert.equal(list.closest_id(40), 40);
|
|
|
|
assert.equal(list.closest_id(50), 50);
|
|
|
|
assert.equal(list.closest_id(60), 60);
|
|
|
|
|
|
|
|
assert.equal(list.closest_id(60), 60);
|
|
|
|
assert.equal(list.closest_id(21), 20);
|
|
|
|
assert.equal(list.closest_id(29), 30);
|
|
|
|
assert.equal(list.closest_id(31), 30);
|
|
|
|
assert.equal(list.closest_id(47), 50);
|
|
|
|
assert.equal(list.closest_id(51), 50.02);
|
|
|
|
assert.equal(list.closest_id(59), 60);
|
|
|
|
assert.equal(list.closest_id(50.01), 50.01);
|
2016-03-16 19:39:12 +01:00
|
|
|
}());
|