zulip/web/tests/narrow_activate.test.js

212 lines
6.3 KiB
JavaScript
Raw Normal View History

"use strict";
const {strict: assert} = require("assert");
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const $ = require("./lib/zjquery");
mock_esm("../src/resize", {
resize_stream_filters_container() {},
});
const all_messages_data = mock_esm("../src/all_messages_data");
const compose_actions = mock_esm("../src/compose_actions");
const compose_banner = mock_esm("../src/compose_banner");
const compose_closed_ui = mock_esm("../src/compose_closed_ui");
const hashchange = mock_esm("../src/hashchange");
const message_fetch = mock_esm("../src/message_fetch");
const message_list = mock_esm("../src/message_list");
const message_lists = mock_esm("../src/message_lists", {
home: {},
current: {},
set_current(msg_list) {
message_lists.current = msg_list;
},
});
const message_feed_top_notices = mock_esm("../src/message_feed_top_notices");
const message_feed_loading = mock_esm("../src/message_feed_loading");
const message_view_header = mock_esm("../src/message_view_header");
const notifications = mock_esm("../src/notifications");
const stream_list = mock_esm("../src/stream_list");
const left_sidebar_navigation_area = mock_esm("../src/left_sidebar_navigation_area");
const typing_events = mock_esm("../src/typing_events");
const unread_ops = mock_esm("../src/unread_ops");
mock_esm("../src/recent_topics_util", {
is_visible() {},
});
mock_esm("../src/pm_list", {
handle_narrow_activated() {},
});
mock_esm("../src/unread_ui", {
reset_unread_banner() {},
update_unread_banner() {},
});
2018-04-11 22:16:30 +02:00
//
// We have strange hacks in narrow.activate to sleep 0
// seconds.
set_global("setTimeout", (f, t) => {
2018-04-11 22:16:30 +02:00
assert.equal(t, 0);
f();
});
mock_esm("../src/user_topics", {
is_topic_muted: () => false,
});
const compose_recipient = zrequire("compose_recipient");
const util = zrequire("util");
const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data");
const narrow = zrequire("narrow");
const denmark = {
2018-04-11 22:16:30 +02:00
subscribed: false,
color: "blue",
name: "Denmark",
2018-04-11 22:16:30 +02:00
stream_id: 1,
is_muted: true,
2018-04-11 22:16:30 +02:00
};
stream_data.add_sub(denmark);
2018-04-11 22:16:30 +02:00
function test_helper() {
const events = [];
2018-04-11 22:16:30 +02:00
function stub(module, func_name) {
module[func_name] = () => {
events.push([module, func_name]);
2018-04-11 22:16:30 +02:00
};
}
stub(compose_banner, "clear_message_sent_banners");
stub(compose_actions, "on_narrow");
stub(compose_closed_ui, "update_reply_recipient_label");
stub(hashchange, "save_narrow");
stub(message_feed_loading, "hide_indicators");
stub(message_feed_loading, "show_loading_older");
stub(message_feed_top_notices, "hide_top_of_narrow_notices");
stub(notifications, "redraw_title");
stub(stream_list, "handle_narrow_activated");
stub(message_view_header, "render_title_area");
stub(left_sidebar_navigation_area, "handle_narrow_activated");
stub(typing_events, "render_notifications_for_narrow");
stub(compose_recipient, "update_narrow_to_recipient_visibility");
stub(unread_ops, "process_visible");
stub(compose_closed_ui, "update_buttons_for_stream");
stub(compose_closed_ui, "update_buttons_for_private");
// We don't test the css calls; we just skip over them.
$("#mark_read_on_scroll_state_banner").toggleClass = () => {};
2018-04-11 22:16:30 +02:00
return {
assert_events(expected_events) {
assert.deepEqual(events, expected_events);
2018-04-11 22:16:30 +02:00
},
};
}
function stub_message_list() {
message_list.MessageList = class MessageList {
constructor(opts) {
this.data = opts.data;
}
view = {
set_message_offset(offset) {
this.offset = offset;
},
};
2018-04-11 22:16:30 +02:00
get(msg_id) {
return this.data.get(msg_id);
}
2018-04-11 22:16:30 +02:00
visibly_empty() {
return this.data.visibly_empty();
}
2018-04-11 22:16:30 +02:00
select_id(msg_id) {
2018-04-11 22:16:30 +02:00
this.selected_id = msg_id;
}
2018-04-11 22:16:30 +02:00
};
}
run_test("basics", () => {
2018-04-11 22:16:30 +02:00
stub_message_list();
const helper = test_helper();
const terms = [{operator: "stream", operand: "Denmark"}];
2018-04-11 22:16:30 +02:00
const selected_id = 1000;
2018-04-11 22:16:30 +02:00
const selected_message = {
2018-04-11 22:16:30 +02:00
id: selected_id,
type: "stream",
2018-04-11 22:16:30 +02:00
stream_id: denmark.stream_id,
topic: "whatever",
2018-04-11 22:16:30 +02:00
};
const messages = [selected_message];
2018-04-11 22:16:30 +02:00
const row = {
length: 1,
get_offset_to_window: () => ({top: 25}),
};
message_lists.current.selected_id = () => -1;
message_lists.current.get_row = () => row;
2018-04-11 22:16:30 +02:00
all_messages_data.all_messages_data = {
all_messages: () => messages,
visibly_empty: () => false,
first: () => ({id: 900}),
last: () => ({id: 1100}),
2018-04-11 22:16:30 +02:00
};
message_fetch.load_messages_for_narrow = (opts) => {
// Only validates the anchor and set of fields
2018-04-11 22:16:30 +02:00
assert.deepEqual(opts, {
cont: opts.cont,
msg_list: opts.msg_list,
anchor: 1000,
2018-04-11 22:16:30 +02:00
});
opts.cont();
2018-04-11 22:16:30 +02:00
};
narrow.activate(terms, {
then_select_id: selected_id,
});
2018-04-11 22:16:30 +02:00
assert.equal(message_lists.current.selected_id, selected_id);
assert.equal(message_lists.current.view.offset, 25);
assert.equal(narrow_state.narrowed_to_pms(), false);
2018-04-11 22:16:30 +02:00
helper.assert_events([
[message_feed_top_notices, "hide_top_of_narrow_notices"],
[message_feed_loading, "hide_indicators"],
[compose_banner, "clear_message_sent_banners"],
[notifications, "redraw_title"],
[unread_ops, "process_visible"],
[hashchange, "save_narrow"],
[compose_closed_ui, "update_buttons_for_stream"],
[compose_closed_ui, "update_reply_recipient_label"],
[compose_actions, "on_narrow"],
[left_sidebar_navigation_area, "handle_narrow_activated"],
[stream_list, "handle_narrow_activated"],
[typing_events, "render_notifications_for_narrow"],
[message_view_header, "render_title_area"],
[compose_recipient, "update_narrow_to_recipient_visibility"],
2018-04-11 22:16:30 +02:00
]);
message_lists.current.selected_id = () => -1;
message_lists.current.get_row = () => row;
util.sorted_ids = () => [];
narrow.activate([{operator: "is", operand: "private"}], {
then_select_id: selected_id,
});
assert.equal(narrow_state.narrowed_to_pms(), true);
});