node_tests: Move more set_global mocks before zrequire calls.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-28 09:48:44 -08:00 committed by Tim Abbott
parent b38712d8d2
commit 1ec97070e8
10 changed files with 83 additions and 123 deletions

View File

@ -16,8 +16,17 @@ const events = require("./lib/events");
const noop = () => {};
set_global("DOMParser", new JSDOM().window.DOMParser);
let compose_actions_start_checked;
let compose_actions_expected_opts;
set_global("compose_actions", {
update_placeholder_text: noop,
start(msg_type, opts) {
assert.equal(msg_type, "stream");
assert.deepEqual(opts, compose_actions_expected_opts);
compose_actions_start_checked = true;
},
});
const _navigator = {
@ -1092,18 +1101,9 @@ test_ui("initialize", () => {
assert(!$("#compose #attach_files").hasClass("notdisplayed"));
assert(setup_upload_called);
let compose_actions_start_checked;
function set_up_compose_start_mock(expected_opts) {
compose_actions_start_checked = false;
set_global("compose_actions", {
start(msg_type, opts) {
assert.equal(msg_type, "stream");
assert.deepEqual(opts, expected_opts);
compose_actions_start_checked = true;
},
});
compose_actions_expected_opts = expected_opts;
}
(function test_page_params_narrow_path() {

View File

@ -13,7 +13,7 @@ const messages = [];
rewiremock("../../static/js/message_store").with({
get: (msg_id) => messages[msg_id - 1],
});
set_global("message_list", {
const message_list = set_global("message_list", {
all: {
all_messages() {
return messages;
@ -26,7 +26,7 @@ rewiremock.enable();
const rs = zrequire("recent_senders");
zrequire("message_util.js");
run_test("process_message_for_senders", () => {
run_test("process_message_for_senders", (override) => {
const stream1 = 1;
const stream2 = 2;
const stream3 = 3;
@ -211,17 +211,9 @@ run_test("process_message_for_senders", () => {
assert.equal(rs.get_topic_recent_senders(stream4, topic3).toString(), "");
assert.equal(rs.get_topic_recent_senders(stream5, topic4).toString(), "2,3");
set_global("message_list", {
all: {
all_messages() {
// messages[0] (message1) and messages[4] (message5) were removed.
const reduced_msgs = [...messages];
reduced_msgs.splice(4, 1);
reduced_msgs.splice(0, 1);
return reduced_msgs;
},
},
});
// messages[0] (message1) and messages[4] (message5) were removed.
const reduced_msgs = [...messages.slice(1, 4), ...messages.slice(5)];
override(message_list.all, "all_messages", () => reduced_msgs);
assert.equal(rs.get_topic_recent_senders(stream1, topic1).toString(), "2,1");
// delete message1 and message5 sent by sender1
rs.update_topics_of_deleted_message_ids([message1.id, message5.id]);

View File

@ -139,7 +139,7 @@ const sender2 = 2;
const messages = [];
set_global("message_list", {
const message_list = set_global("message_list", {
all: {
all_messages() {
return messages;
@ -688,22 +688,15 @@ run_test("test_reify_local_echo_message", () => {
);
});
run_test("test_delete_messages", () => {
run_test("test_delete_messages", (override) => {
rt = reset_module("recent_topics");
stub_out_filter_buttons();
rt.set_filter("all");
rt.process_messages(messages);
set_global("message_list", {
all: {
all_messages() {
// messages[0] was removed.
const reduced_msgs = [...messages];
reduced_msgs.splice(0, 1);
return reduced_msgs;
},
},
});
// messages[0] was removed.
let reduced_msgs = messages.slice(1);
override(message_list.all, "all_messages", () => reduced_msgs);
let all_topics = rt.get();
assert.equal(
@ -718,16 +711,8 @@ run_test("test_delete_messages", () => {
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2",
);
set_global("message_list", {
all: {
all_messages() {
// messages[0], messages[1] and message[2] were removed.
const reduced_msgs = [...messages];
reduced_msgs.splice(0, 3);
return reduced_msgs;
},
},
});
// messages[0], messages[1] and message[2] were removed.
reduced_msgs = messages.slice(3);
rt.update_topics_of_deleted_message_ids([messages[1].id, messages[2].id]);
@ -742,13 +727,6 @@ run_test("test_delete_messages", () => {
});
run_test("test_topic_edit", () => {
set_global("message_list", {
all: {
all_messages() {
return messages;
},
},
});
// NOTE: This test should always run in the end as it modified the messages data.
rt = reset_module("recent_topics");
stub_out_filter_buttons();

View File

@ -46,6 +46,8 @@ rewiremock("../../static/js/ui_report").with({
},
});
const message_events = set_global("message_events", {});
// Turn off $.now so we can import server_events.
set_global("$", {
now() {},
@ -59,7 +61,7 @@ set_global("$", $);
server_events.home_view_loaded();
run_test("message_event", () => {
run_test("message_event", (override) => {
const event = {
type: "message",
message: {
@ -69,11 +71,9 @@ run_test("message_event", () => {
};
let inserted;
set_global("message_events", {
insert_new_messages(messages) {
assert.equal(messages[0].content, event.message.content);
inserted = true;
},
override(message_events, "insert_new_messages", (messages) => {
assert.equal(messages[0].content, event.message.content);
inserted = true;
});
server_events._get_events_success([event]);
@ -84,14 +84,12 @@ run_test("message_event", () => {
const setup = () => {
server_events.home_view_loaded();
set_global("message_events", {
insert_new_messages() {
throw new Error("insert error");
},
update_messages() {
throw new Error("update error");
},
});
message_events.insert_new_messages = () => {
throw new Error("insert error");
};
message_events.update_messages = () => {
throw new Error("update error");
};
rewiremock("../../static/js/stream_events").with({
update_property() {
throw new Error("subs update error");

View File

@ -38,6 +38,7 @@ const ui_report = {__esModule: true};
rewiremock("../../static/js/ui_report").with(ui_report);
const page_params = set_global("page_params", {});
const input_pill = set_global("input_pill", {});
rewiremock.enable();
@ -53,9 +54,7 @@ function reset_test_setup(pill_container_stub) {
assert(create_item_handler);
return pills;
}
set_global("input_pill", {
create: input_pill_stub,
});
input_pill.create = input_pill_stub;
}
function test_ui(label, f) {

View File

@ -26,7 +26,6 @@ rewiremock("../../static/js/stream_color").with(stream_color);
const stream_list = set_global("stream_list", {});
const stream_muting = {__esModule: true};
rewiremock("../../static/js/stream_muting").with(stream_muting);
let subs = set_global("subs", {});
rewiremock("../../static/js/recent_topics").with({
complete_rerender: () => {},
});
@ -39,7 +38,7 @@ set_global("message_list", {
},
});
subs = set_global("subs", {update_settings_for_subscribed: noop});
const subs = set_global("subs", {update_settings_for_subscribed: noop});
rewiremock("../../static/js/overlays").with({streams_open: () => true});
rewiremock.enable();

View File

@ -34,7 +34,7 @@ const unread = zrequire("unread");
const stream_data = zrequire("stream_data");
const scroll_util = zrequire("scroll_util");
const stream_list = zrequire("stream_list");
zrequire("ui");
set_global("ui", {get_scroll_element: (element) => element});
stream_color.initialize();
@ -393,10 +393,6 @@ test_ui("narrowing", () => {
topic_list.get_stream_li = noop;
scroll_util.__Rewire__("scroll_element_into_container", noop);
set_global("ui", {
get_scroll_element: (element) => element,
});
assert(!$("<devel sidebar row html>").hasClass("active-filter"));
stream_list.set_event_handlers();

View File

@ -13,7 +13,6 @@ const narrow_state = {
topic() {},
};
rewiremock("../../static/js/narrow_state").with(narrow_state);
set_global("unread", {});
const muting = {
__esModule: true,

View File

@ -454,7 +454,7 @@ run_test("insert_message", (override) => {
override(pm_list, "update_private_messages", noop);
const helper = test_helper();
set_global((recent_topics.is_visible = () => false));
recent_topics.is_visible = () => false;
const new_message = {
sender_id: isaac.user_id,

View File

@ -8,8 +8,50 @@ const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const poll_widget = {__esModule: true};
rewiremock("../../static/js/poll_widget").with(poll_widget);
const events = [
{
data: {
option: "First option",
idx: 1,
type: "new_option",
},
sender_id: 101,
},
{
data: {
option: "Second option",
idx: 1,
type: "new_option",
},
sender_id: 102,
},
{
data: {
option: "Third option",
idx: 1,
type: "new_option",
},
sender_id: 102,
},
];
let widget_elem;
let is_event_handled;
let is_widget_activated;
rewiremock("../../static/js/poll_widget").with({
activate(data) {
is_widget_activated = true;
widget_elem = data.elem;
assert(widget_elem.hasClass("widget-content"));
widget_elem.handle_events = (e) => {
is_event_handled = true;
assert.notDeepStrictEqual(e, events);
events.shift();
assert.deepStrictEqual(e, events);
};
data.callback("test_data");
},
});
set_global("document", "document-stub");
const narrow_state = {__esModule: true};
@ -31,33 +73,6 @@ run_test("activate", (override) => {
let narrow_active;
override(narrow_state, "active", () => narrow_active);
const events = [
{
data: {
option: "First option",
idx: 1,
type: "new_option",
},
sender_id: 101,
},
{
data: {
option: "Second option",
idx: 1,
type: "new_option",
},
sender_id: 102,
},
{
data: {
option: "Third option",
idx: 1,
type: "new_option",
},
sender_id: 102,
},
];
const opts = {
events: events.slice(),
extra_data: "",
@ -72,24 +87,8 @@ run_test("activate", (override) => {
widget_type: "poll",
};
let widget_elem;
let is_event_handled;
let is_widget_activated;
let is_widget_elem_inserted;
override(poll_widget, "activate", (data) => {
is_widget_activated = true;
widget_elem = data.elem;
assert(widget_elem.hasClass("widget-content"));
widget_elem.handle_events = (e) => {
is_event_handled = true;
assert.notDeepStrictEqual(e, events);
events.shift();
assert.deepStrictEqual(e, events);
};
data.callback("test_data");
});
message_content.append = (elem) => {
is_widget_elem_inserted = true;
assert.equal(elem, widget_elem);