Files
zulip/frontend_tests/node_tests/topic_list.js
Steve Howell 5a8bccfe08 topic_data.js: Refactor topic history internals.
This commit introduces a per-stream topic_history class
inside of topic_data.js to better encapsulate how we store topic
history.

To the callers, nothing changes here.  (Some of our non-black-box
node tests change their way of setting up data, though, since the
internal data structures are different.)

The new class has the following improvements:

    * We use message_id instead of timestamp as our sorting key.
      (We could have done this in a prep commit, but it wouldn't
      have made the diff much cleaner here.)

    * We use a dictionary instead of a sorted list to store the
      data, so that writes are O(1) instead of O(NlogN).  Reads
      now do sorts, so they're O(NlogN) instead of O(N), but reads
      are fairly infrequent.  (The main goal here isn't actually
      performance, but instead it just simplifies the
      implementation.)

    * We isolate `topic_history` from the format of the messages.
      This prepares us for upcoming changes where updates to the
      data structure may come from topic history queries as well
      as messages.

    * We split out the message-add path from the message-remove
      path.  This prepares us to eventually get rid of the "count"
      mechanism that is kind of fragile and which has to be
      bypassed for historical topics.
2017-07-27 14:26:22 -07:00

55 lines
1.4 KiB
JavaScript

add_dependencies({
Handlebars: 'handlebars',
hash_util: 'js/hash_util',
hashchange: 'js/hashchange',
muting: 'js/muting',
narrow: 'js/narrow',
stream_data: 'js/stream_data',
topic_data: 'js/topic_data',
templates: 'js/templates',
});
set_global('unread', {});
var jsdom = require("jsdom");
var window = jsdom.jsdom().defaultView;
global.$ = require('jquery')(window);
var topic_list = require('js/topic_list.js');
global.compile_template('topic_list_item');
(function test_topic_list_build_widget() {
var stream_id = 555;
var active_topic = "testing";
var max_topics = 5;
topic_data.reset();
topic_data.add_message({
stream_id: stream_id,
topic_name: 'coding',
message_id: 400,
});
global.unread.num_unread_for_subject = function () {
return 1;
};
global.stream_data.get_sub_by_id = function (stream_id) {
assert.equal(stream_id, 555);
return 'devel';
};
var parent_elem = $('<div>');
var widget = topic_list.build_widget(parent_elem, stream_id, active_topic, max_topics);
var topic_html = widget.get_dom();
assert.equal(widget.get_parent(), parent_elem);
assert.equal(widget.get_stream_id(), stream_id);
var topic = $(topic_html).find('a').text().trim();
assert.equal(topic, 'coding');
global.write_test_output("test_topic_list_build_widget", parent_elem.html());
}());