From 741c0ac8cade44454118352767ec66a6232d7fb9 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 10 Feb 2021 16:42:59 -0800 Subject: [PATCH] node_tests: Change stub/events pattern to work with non-global modules. Signed-off-by: Anders Kaseorg --- frontend_tests/node_tests/general.js | 52 +++++------ frontend_tests/node_tests/hashchange.js | 98 +++++++++++--------- frontend_tests/node_tests/message_events.js | 34 +++---- frontend_tests/node_tests/narrow_activate.js | 90 +++++++++--------- 4 files changed, 141 insertions(+), 133 deletions(-) diff --git a/frontend_tests/node_tests/general.js b/frontend_tests/node_tests/general.js index ecdef9c0b3..72aeaa983d 100644 --- a/frontend_tests/node_tests/general.js +++ b/frontend_tests/node_tests/general.js @@ -399,10 +399,9 @@ function test_helper() { const events = []; return { - redirect: (module_name, func_name) => { - const full_name = module_name + "." + func_name; - global[module_name][func_name] = () => { - events.push(full_name); + redirect: (module, func_name) => { + module[func_name] = () => { + events.push([module, func_name]); }; }, events, @@ -427,13 +426,14 @@ function test_helper() { const home_msg_list = set_global("home_msg_list", {}); const message_list = set_global("message_list", {}); -set_global("message_util", {}); +const message_util = set_global("message_util", {}); const notifications = set_global("notifications", {}); -set_global("resize", {}); -set_global("stream_list", {}); -set_global("unread_ops", {}); -set_global("unread_ui", {}); +const resize = set_global("resize", {}); +let stream_list = set_global("stream_list", {}); +let unread_ops = set_global("unread_ops", {}); +const unread_ui = set_global("unread_ui", {}); +const huddle_data = zrequire("huddle_data"); const message_events = zrequire("message_events"); run_test("insert_message", (override) => { @@ -449,13 +449,13 @@ run_test("insert_message", (override) => { assert.equal(message_store.get(new_message.id), undefined); - helper.redirect("huddle_data", "process_loaded_messages"); - helper.redirect("message_util", "add_new_messages"); - helper.redirect("notifications", "received_messages"); - helper.redirect("resize", "resize_page_components"); - helper.redirect("stream_list", "update_streams_sidebar"); - helper.redirect("unread_ops", "process_visible"); - helper.redirect("unread_ui", "update_unread_counts"); + helper.redirect(huddle_data, "process_loaded_messages"); + helper.redirect(message_util, "add_new_messages"); + helper.redirect(notifications, "received_messages"); + helper.redirect(resize, "resize_page_components"); + helper.redirect(stream_list, "update_streams_sidebar"); + helper.redirect(unread_ops, "process_visible"); + helper.redirect(unread_ui, "update_unread_counts"); narrow_state.reset_current_filter(); @@ -466,14 +466,14 @@ run_test("insert_message", (override) => { // the code invokes various objects when a new message // comes in: assert.deepEqual(helper.events, [ - "huddle_data.process_loaded_messages", - "message_util.add_new_messages", - "message_util.add_new_messages", - "unread_ui.update_unread_counts", - "resize.resize_page_components", - "unread_ops.process_visible", - "notifications.received_messages", - "stream_list.update_streams_sidebar", + [huddle_data, "process_loaded_messages"], + [message_util, "add_new_messages"], + [message_util, "add_new_messages"], + [unread_ui, "update_unread_counts"], + [resize, "resize_page_components"], + [unread_ops, "process_visible"], + [notifications, "received_messages"], + [stream_list, "update_streams_sidebar"], ]); // Despite all of our stubbing/mocking, the call to @@ -522,7 +522,7 @@ const channel = set_global("channel", {}); const message_viewport = set_global("message_viewport", {}); zrequire("message_flags"); -const unread_ops = zrequire("unread_ops"); +unread_ops = zrequire("unread_ops"); run_test("unread_ops", () => { (function set_up() { @@ -604,7 +604,7 @@ run_test("unread_ops", () => { const topic_list = set_global("topic_list", {}); -const stream_list = zrequire("stream_list"); +stream_list = zrequire("stream_list"); const social_stream = { color: "red", diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js index 55254ece8d..24fbe85112 100644 --- a/frontend_tests/node_tests/hashchange.js +++ b/frontend_tests/node_tests/hashchange.js @@ -26,16 +26,16 @@ set_global("search", { set_global("document", "document-stub"); const history = set_global("history", {}); -set_global("admin", {}); -set_global("drafts", {}); +const admin = set_global("admin", {}); +const drafts = set_global("drafts", {}); set_global("favicon", {}); -set_global("floating_recipient_bar", {}); +const floating_recipient_bar = set_global("floating_recipient_bar", {}); const info_overlay = set_global("info_overlay", {}); -set_global("message_viewport", {}); +const message_viewport = set_global("message_viewport", {}); const narrow = set_global("narrow", {}); -set_global("overlays", {}); -set_global("settings", {}); -set_global("subs", {}); +const overlays = set_global("overlays", {}); +const settings = set_global("settings", {}); +const subs = set_global("subs", {}); const ui_util = set_global("ui_util", {}); run_test("operators_round_trip", () => { @@ -117,21 +117,21 @@ function test_helper() { let events = []; let narrow_terms; - function stub(module_name, func_name) { - global[module_name][func_name] = () => { - events.push(module_name + "." + func_name); + function stub(module, func_name) { + module[func_name] = () => { + events.push([module, func_name]); }; } - stub("admin", "launch"); - stub("drafts", "launch"); - stub("floating_recipient_bar", "update"); - stub("message_viewport", "stop_auto_scrolling"); - stub("narrow", "deactivate"); - stub("overlays", "close_for_hash_change"); - stub("settings", "launch"); - stub("subs", "launch"); - stub("ui_util", "blur_active_element"); + stub(admin, "launch"); + stub(drafts, "launch"); + stub(floating_recipient_bar, "update"); + stub(message_viewport, "stop_auto_scrolling"); + stub(narrow, "deactivate"); + stub(overlays, "close_for_hash_change"); + stub(settings, "launch"); + stub(subs, "launch"); + stub(ui_util, "blur_active_element"); ui_util.change_tab_to = (hash) => { events.push("change_tab_to " + hash); @@ -165,21 +165,21 @@ run_test("hash_interactions", () => { helper.clear_events(); hashchange.initialize(); helper.assert_events([ - "overlays.close_for_hash_change", - "message_viewport.stop_auto_scrolling", + [overlays, "close_for_hash_change"], + [message_viewport, "stop_auto_scrolling"], "change_tab_to #message_feed_container", - "narrow.deactivate", - "floating_recipient_bar.update", + [narrow, "deactivate"], + [floating_recipient_bar, "update"], ]); helper.clear_events(); $(window).trigger("hashchange"); helper.assert_events([ - "overlays.close_for_hash_change", - "message_viewport.stop_auto_scrolling", + [overlays, "close_for_hash_change"], + [message_viewport, "stop_auto_scrolling"], "change_tab_to #message_feed_container", - "narrow.deactivate", - "floating_recipient_bar.update", + [narrow, "deactivate"], + [floating_recipient_bar, "update"], ]); window.location.hash = "#narrow/stream/Denmark"; @@ -187,11 +187,11 @@ run_test("hash_interactions", () => { helper.clear_events(); $(window).trigger("hashchange"); helper.assert_events([ - "overlays.close_for_hash_change", - "message_viewport.stop_auto_scrolling", + [overlays, "close_for_hash_change"], + [message_viewport, "stop_auto_scrolling"], "change_tab_to #message_feed_container", "narrow.activate", - "floating_recipient_bar.update", + [floating_recipient_bar, "update"], ]); let terms = helper.get_narrow_terms(); assert.equal(terms[0].operand, "Denmark"); @@ -201,11 +201,11 @@ run_test("hash_interactions", () => { helper.clear_events(); $(window).trigger("hashchange"); helper.assert_events([ - "overlays.close_for_hash_change", - "message_viewport.stop_auto_scrolling", + [overlays, "close_for_hash_change"], + [message_viewport, "stop_auto_scrolling"], "change_tab_to #message_feed_container", "narrow.activate", - "floating_recipient_bar.update", + [floating_recipient_bar, "update"], ]); terms = helper.get_narrow_terms(); assert.equal(terms.length, 0); @@ -214,43 +214,55 @@ run_test("hash_interactions", () => { helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "subs.launch"]); + helper.assert_events([ + [overlays, "close_for_hash_change"], + [subs, "launch"], + ]); window.location.hash = "#keyboard-shortcuts/whatever"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "info: keyboard-shortcuts"]); + helper.assert_events([[overlays, "close_for_hash_change"], "info: keyboard-shortcuts"]); window.location.hash = "#message-formatting/whatever"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "info: message-formatting"]); + helper.assert_events([[overlays, "close_for_hash_change"], "info: message-formatting"]); window.location.hash = "#search-operators/whatever"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "info: search-operators"]); + helper.assert_events([[overlays, "close_for_hash_change"], "info: search-operators"]); window.location.hash = "#drafts"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "drafts.launch"]); + helper.assert_events([ + [overlays, "close_for_hash_change"], + [drafts, "launch"], + ]); window.location.hash = "#settings/alert-words"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "settings.launch"]); + helper.assert_events([ + [overlays, "close_for_hash_change"], + [settings, "launch"], + ]); window.location.hash = "#organization/user-list-admin"; helper.clear_events(); $(window).trigger("hashchange"); - helper.assert_events(["overlays.close_for_hash_change", "admin.launch"]); + helper.assert_events([ + [overlays, "close_for_hash_change"], + [admin, "launch"], + ]); let called_back; @@ -259,7 +271,7 @@ run_test("hash_interactions", () => { called_back = true; }); - helper.assert_events(["ui_util.blur_active_element"]); + helper.assert_events([[ui_util, "blur_active_element"]]); assert(called_back); }); @@ -271,7 +283,7 @@ run_test("save_narrow", () => { blueslip.expect("warn", "browser does not support pushState"); hashchange.save_narrow(operators); - helper.assert_events(["message_viewport.stop_auto_scrolling"]); + helper.assert_events([[message_viewport, "stop_auto_scrolling"]]); assert.equal(window.location.hash, "#narrow/is/private"); let url_pushed; @@ -283,6 +295,6 @@ run_test("save_narrow", () => { helper.clear_events(); hashchange.save_narrow(operators); - helper.assert_events(["message_viewport.stop_auto_scrolling"]); + helper.assert_events([[message_viewport, "stop_auto_scrolling"]]); assert.equal(url_pushed, "http://example.com/#narrow/is/starred"); }); diff --git a/frontend_tests/node_tests/message_events.js b/frontend_tests/node_tests/message_events.js index 7ff74b088a..f5c3d36948 100644 --- a/frontend_tests/node_tests/message_events.js +++ b/frontend_tests/node_tests/message_events.js @@ -16,15 +16,15 @@ const stream_topic_history = zrequire("stream_topic_history"); const unread = zrequire("unread"); set_global("$", make_zjquery()); -set_global("condense", {}); +const condense = set_global("condense", {}); set_global("current_msg_list", {}); -set_global("message_edit", {}); +const message_edit = set_global("message_edit", {}); const message_list = set_global("message_list", {}); -set_global("notifications", {}); +const notifications = set_global("notifications", {}); set_global("page_params", {}); -set_global("pm_list", {}); -set_global("stream_list", {}); -set_global("unread_ui", {}); +const pm_list = set_global("pm_list", {}); +const stream_list = set_global("stream_list", {}); +const unread_ui = set_global("unread_ui", {}); const alice = { email: "alice@example.com", @@ -44,13 +44,9 @@ stream_data.add_sub(denmark); function test_helper(side_effects) { const events = []; - for (const side_effect of side_effects) { - const parts = side_effect.split("."); - const module = parts[0]; - const field = parts[1]; - - global[module][field] = () => { - events.push(side_effect); + for (const [module, field] of side_effects) { + module[field] = () => { + events.push([module, field]); }; } @@ -109,12 +105,12 @@ run_test("update_messages", () => { }; const side_effects = [ - "condense.un_cache_message_content_height", - "message_edit.end_message_row_edit", - "notifications.received_messages", - "unread_ui.update_unread_counts", - "stream_list.update_streams_sidebar", - "pm_list.update_private_messages", + [condense, "un_cache_message_content_height"], + [message_edit, "end_message_row_edit"], + [notifications, "received_messages"], + [unread_ui, "update_unread_counts"], + [stream_list, "update_streams_sidebar"], + [pm_list, "update_private_messages"], ]; const helper = test_helper(side_effects); diff --git a/frontend_tests/node_tests/narrow_activate.js b/frontend_tests/node_tests/narrow_activate.js index ed05f31aea..e7bf887649 100644 --- a/frontend_tests/node_tests/narrow_activate.js +++ b/frontend_tests/node_tests/narrow_activate.js @@ -21,10 +21,10 @@ zrequire("unread"); const narrow = zrequire("narrow"); const channel = set_global("channel", {}); -set_global("compose", {}); -set_global("compose_actions", {}); +const compose = set_global("compose", {}); +const compose_actions = set_global("compose_actions", {}); set_global("current_msg_list", {}); -set_global("hashchange", {}); +const hashchange = set_global("hashchange", {}); set_global("home_msg_list", {}); const message_fetch = set_global("message_fetch", {}); const message_list = set_global("message_list", { @@ -32,17 +32,17 @@ const message_list = set_global("message_list", { this.narrowed = value; }, }); -set_global("message_scroll", {}); +const message_scroll = set_global("message_scroll", {}); set_global("message_util", {}); -set_global("notifications", {}); +const notifications = set_global("notifications", {}); set_global("page_params", {}); -set_global("search", {}); -set_global("stream_list", {}); -set_global("message_view_header", {}); -set_global("top_left_corner", {}); -set_global("typing_events", {}); -set_global("ui_util", {}); -set_global("unread_ops", {}); +const search = set_global("search", {}); +const stream_list = set_global("stream_list", {}); +const message_view_header = set_global("message_view_header", {}); +const top_left_corner = set_global("top_left_corner", {}); +const typing_events = set_global("typing_events", {}); +const ui_util = set_global("ui_util", {}); +const unread_ops = set_global("unread_ops", {}); set_global("search_pill_widget", { widget: { clear() { @@ -78,28 +78,28 @@ stream_data.add_sub(denmark); function test_helper() { let events = []; - function stub(module_name, func_name) { - global[module_name][func_name] = () => { - events.push(module_name + "." + func_name); + function stub(module, func_name) { + module[func_name] = () => { + events.push([module, func_name]); }; } - 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"); + 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"); return { clear: () => { @@ -202,20 +202,20 @@ run_test("basics", () => { assert.equal(narrow_state.narrowed_to_pms(), false); 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", + [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"], ]); current_msg_list.selected_id = () => -1;