zulip/frontend_tests/node_tests/narrow_activate.js

251 lines
6.8 KiB
JavaScript
Raw Normal View History

"use strict";
const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
rewiremock("../../static/js/resize").with({
resize_stream_filters_container: () => {},
});
const channel = {__esModule: true};
rewiremock("../../static/js/channel").with(channel);
const compose = {__esModule: true};
rewiremock("../../static/js/compose").with(compose);
const compose_actions = set_global("compose_actions", {});
set_global("current_msg_list", {});
const hashchange = set_global("hashchange", {});
set_global("home_msg_list", {});
const message_fetch = set_global("message_fetch", {});
const message_list = set_global("message_list", {
set_narrowed(value) {
this.narrowed = value;
},
});
const message_scroll = {__esModule: true};
rewiremock("../../static/js/message_scroll").with(message_scroll);
set_global("message_util", {});
const notifications = {__esModule: true};
rewiremock("../../static/js/notifications").with(notifications);
set_global("page_params", {});
const search = {__esModule: true};
rewiremock("../../static/js/search").with(search);
const stream_list = set_global("stream_list", {});
const message_view_header = set_global("message_view_header", {});
const top_left_corner = {__esModule: true};
rewiremock("../../static/js/top_left_corner").with(top_left_corner);
const typing_events = set_global("typing_events", {});
const ui_util = {__esModule: true};
rewiremock("../../static/js/ui_util").with(ui_util);
const unread_ops = set_global("unread_ops", {});
set_global("search_pill_widget", {
widget: {
clear() {
return true;
},
appendValue() {
return true;
},
},
});
set_global("recent_topics", {
hide: () => {},
is_visible: () => {},
});
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();
});
rewiremock("../../static/js/muting").with({
is_topic_muted: () => false,
});
rewiremock.enable();
const util = zrequire("util");
const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data");
zrequire("unread");
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() {
let 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_actions, "on_narrow");
stub(hashchange, "save_narrow");
stub(message_scroll, "hide_indicators");
stub(message_scroll, "show_loading_older");
stub(message_scroll, "hide_top_of_narrow_notices");
stub(notifications, "clear_compose_notifications");
stub(notifications, "redraw_title");
stub(search, "update_button_visibility");
stub(stream_list, "handle_narrow_activated");
stub(message_view_header, "initialize");
stub(top_left_corner, "handle_narrow_activated");
stub(typing_events, "render_notifications_for_narrow");
stub(ui_util, "change_tab_to");
stub(unread_ops, "process_visible");
stub(compose, "update_closed_compose_buttons_for_stream");
stub(compose, "update_closed_compose_buttons_for_private");
2018-04-11 22:16:30 +02:00
return {
clear: () => {
events = [];
},
push_event: (event) => {
events.push(event);
},
assert_events: (expected_events) => {
assert.deepEqual(expected_events, events);
},
};
}
function stub_message_list() {
message_list.MessageList = function (opts) {
this.data = opts.data;
this.view = {
set_message_offset(offset) {
this.offset = offset;
},
};
2018-04-11 22:16:30 +02:00
return this;
};
message_list.MessageList.prototype = {
get(msg_id) {
return this.data.get(msg_id);
2018-04-11 22:16:30 +02:00
},
empty() {
return this.data.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;
},
};
}
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,
offset: () => ({top: 25}),
};
current_msg_list.selected_id = () => -1;
current_msg_list.get_row = () => row;
2018-04-11 22:16:30 +02:00
message_list.all = {
all_messages: () => messages,
2018-04-11 22:16:30 +02:00
get: (msg_id) => {
assert.equal(msg_id, selected_id);
2018-04-11 22:16:30 +02:00
return selected_message;
},
data: {
fetch_status: {
has_found_newest: () => true,
},
},
empty: () => false,
first: () => ({id: 900}),
last: () => ({id: 1100}),
2018-04-11 22:16:30 +02:00
};
let cont;
2018-04-11 22:16:30 +02:00
message_fetch.load_messages_for_narrow = (opts) => {
cont = opts.cont;
assert.deepEqual(opts, {
cont: opts.cont,
anchor: 1000,
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_list.narrowed.selected_id, selected_id);
assert.equal(message_list.narrowed.view.offset, 25);
assert.equal(narrow_state.narrowed_to_pms(), false);
2018-04-11 22:16:30 +02:00
helper.assert_events([
[notifications, "clear_compose_notifications"],
[notifications, "redraw_title"],
[message_scroll, "hide_top_of_narrow_notices"],
[message_scroll, "hide_indicators"],
[ui_util, "change_tab_to"],
[unread_ops, "process_visible"],
[hashchange, "save_narrow"],
[compose, "update_closed_compose_buttons_for_stream"],
[search, "update_button_visibility"],
[compose_actions, "on_narrow"],
[top_left_corner, "handle_narrow_activated"],
[stream_list, "handle_narrow_activated"],
[typing_events, "render_notifications_for_narrow"],
[message_view_header, "initialize"],
2018-04-11 22:16:30 +02:00
]);
current_msg_list.selected_id = () => -1;
current_msg_list.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);
2018-04-11 22:16:30 +02:00
channel.post = (opts) => {
assert.equal(opts.url, "/json/report/narrow_times");
helper.push_event("report narrow times");
2018-04-11 22:16:30 +02:00
};
helper.clear();
cont();
helper.assert_events(["report narrow times"]);
});
rewiremock.disable();