node tests: Add test_render_windows().

This commit is contained in:
Steve Howell 2018-03-21 18:56:30 -04:00 committed by Tim Abbott
parent 9a6e84a1c3
commit 3dd1d784cb
1 changed files with 137 additions and 1 deletions

View File

@ -1,6 +1,14 @@
set_global('$', global.make_zjquery());
set_global('document', 'document-stub');
zrequire('util');
zrequire('XDate', 'node_modules/xdate/src/xdate');
var MessageListView = zrequire('message_list_view');
zrequire('Filter', 'js/filter');
zrequire('FetchStatus', 'js/fetch_status');
zrequire('MessageListView', 'js/message_list_view');
zrequire('message_list');
var noop = function () {};
set_global('page_params', {
twenty_four_hour_time: false,
@ -25,6 +33,18 @@ set_global('timerender', {
},
});
set_global('rows', {
get_table: function () {
return {
children: function () {
return {
detach: noop,
};
},
};
},
});
(function test_merge_message_groups() {
// MessageListView has lots of DOM code, so we are going to test the message
// group mearging logic on its own.
@ -339,3 +359,119 @@ set_global('timerender', {
}());
}());
(function test_render_windows() {
// We only render up to 400 messages at a time in our message list,
// and we only change the window (which is a range, really, with
// start/end) when the pointer moves outside of the window or close
// to the edges.
var view = (function make_view() {
var table_name = 'zfilt';
var filter = new Filter();
var opts = {};
var list = new message_list.MessageList(table_name, filter, opts);
var view = list.view;
// Stub out functionality that is not core to the rendering window
// logic.
list.unmuted_messages = function (messages) {
return messages;
};
// We don't need to actually render the DOM. The windowing logic
// sits above that layer.
view.render = noop;
view.rerender_preserving_scrolltop = noop;
return view;
}());
var list = view.list;
(function test_with_empty_list() {
// The function should early exit here.
var rendered = view.maybe_rerender();
assert.equal(rendered, false);
}());
var messages;
function reset_list(opts) {
messages = _.map(_.range(opts.count), function (i) {
return {
id: i,
};
});
list.selected_idx = function () { return 0; };
list.clear();
list.add_messages(messages, {});
}
function verify_no_move_range(start, end) {
// In our render window, there are up to 300 positions in
// the list where we can move the pointer without forcing
// a re-render. The code avoids hasty re-renders for
// performance reasons.
_.each(_.range(start, end), function (idx) {
list.selected_idx = function () { return idx; };
var rendered = view.maybe_rerender();
assert.equal(rendered, false);
});
}
function verify_move(idx, range) {
var start = range[0];
var end = range[1];
list.selected_idx = function () { return idx; };
var rendered = view.maybe_rerender();
assert.equal(rendered, true);
assert.equal(view._render_win_start, start);
assert.equal(view._render_win_end, end);
}
reset_list({count: 51});
verify_no_move_range(0, 51);
reset_list({count: 450});
verify_no_move_range(0, 350);
verify_move(350, [150, 450]);
verify_no_move_range(200, 400);
verify_move(199, [0, 400]);
verify_no_move_range(50, 350);
verify_move(350, [150, 450]);
verify_no_move_range(200, 400);
verify_move(199, [0, 400]);
verify_no_move_range(0, 350);
verify_move(400, [200, 450]);
reset_list({count: 800});
verify_no_move_range(0, 350);
verify_move(350, [150, 550]);
verify_no_move_range(200, 500);
verify_move(500, [300, 700]);
verify_no_move_range(350, 650);
verify_move(650, [450, 800]);
verify_no_move_range(500, 750);
verify_move(499, [299, 699]);
verify_no_move_range(349, 649);
verify_move(348, [148, 548]);
verify_no_move_range(198, 398);
verify_move(197, [0, 400]);
verify_no_move_range(0, 350);
}());