From 63c9296d5339517450f79f176dc02d77b08020c8 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 4 Aug 2023 14:40:48 -0700 Subject: [PATCH] tests: Fix direct mutation of ES modules. Signed-off-by: Anders Kaseorg --- web/tests/compose.test.js | 6 +-- web/tests/compose_actions.test.js | 5 +-- web/tests/compose_fade.test.js | 6 +-- web/tests/compose_pm_pill.test.js | 10 ++--- web/tests/compose_state.test.js | 2 - web/tests/compose_validate.test.js | 6 +-- web/tests/compose_video.test.js | 4 +- web/tests/composebox_typeahead.test.js | 37 +++++++++------- web/tests/drafts.test.js | 24 +++++------ web/tests/narrow_activate.test.js | 15 +++---- web/tests/navbar_alerts.test.js | 39 ++++++++--------- web/tests/notifications.test.js | 6 +-- web/tests/presence.test.js | 10 ++--- web/tests/rendered_markdown.test.js | 8 ++-- web/tests/settings_user_groups_legacy.test.js | 42 ++++++++++--------- web/tests/settings_user_topics.test.js | 18 ++++---- web/tests/topic_generator.test.js | 18 ++++---- web/tests/typeahead_helper.test.js | 7 +--- web/tests/typing_status.test.js | 2 +- 19 files changed, 130 insertions(+), 135 deletions(-) diff --git a/web/tests/compose.test.js b/web/tests/compose.test.js index 0070c4ddb9..e8dbdb0680 100644 --- a/web/tests/compose.test.js +++ b/web/tests/compose.test.js @@ -374,7 +374,7 @@ test_ui("finish", ({override, override_rewire}) => { $(".compose-submit-button .loader").hide(); $("#compose-textarea").off("select"); $("#compose-textarea").val(""); - compose_ui.compose_spinner_visible = false; + override_rewire(compose_ui, "compose_spinner_visible", false); const res = compose.finish(); assert.equal(res, false); assert.ok(!$("#compose_banners .recipient_not_subscribed").visible()); @@ -388,7 +388,7 @@ test_ui("finish", ({override, override_rewire}) => { $("#compose .preview_message_area").show(); $("#compose .markdown_preview").hide(); $("#compose-textarea").val("foobarfoobar"); - compose_ui.compose_spinner_visible = false; + override_rewire(compose_ui, "compose_spinner_visible", false); compose_state.set_message_type("private"); override(compose_pm_pill, "get_emails", () => "bob@example.com"); override(compose_pm_pill, "get_user_ids", () => []); @@ -754,11 +754,9 @@ test_ui("create_message_object", ({override, override_rewire}) => { assert.equal(message.to_user_ids, "31,32"); assert.equal(message.content, "burrito"); - const {email_list_to_user_ids_string} = people; override_rewire(people, "email_list_to_user_ids_string", () => undefined); message = compose.create_message_object(); assert.deepEqual(message.to, [alice.email, bob.email]); - people.email_list_to_user_ids_string = email_list_to_user_ids_string; }); test_ui("DM policy disabled", ({override, override_rewire}) => { diff --git a/web/tests/compose_actions.test.js b/web/tests/compose_actions.test.js index 91626489bb..78bf3df313 100644 --- a/web/tests/compose_actions.test.js +++ b/web/tests/compose_actions.test.js @@ -75,9 +75,6 @@ const respond_to_message = compose_actions.respond_to_message; const reply_with_mention = compose_actions.reply_with_mention; const quote_and_reply = compose_actions.quote_and_reply; -compose_recipient.update_narrow_to_recipient_visibility = noop; -compose_recipient.on_compose_select_recipient_update = noop; - function assert_visible(sel) { assert.ok($(sel).visible()); } @@ -351,6 +348,8 @@ test("reply_with_mention", ({override, override_rewire, mock_template}) => { }); test("quote_and_reply", ({disallow, override, override_rewire}) => { + override_rewire(compose_recipient, "on_compose_select_recipient_update", noop); + mock_banners(); mock_stream_header_colorblock(); compose_state.set_message_type("stream"); diff --git a/web/tests/compose_fade.test.js b/web/tests/compose_fade.test.js index 3999795a30..638d50f9b1 100644 --- a/web/tests/compose_fade.test.js +++ b/web/tests/compose_fade.test.js @@ -27,9 +27,6 @@ const compose_recipient = zrequire("compose_recipient"); const compose_fade_helper = zrequire("compose_fade_helper"); const compose_state = zrequire("compose_state"); -compose_recipient.selected_stream_name = "social"; -compose_recipient.is_direct_message_selected = false; - const me = { email: "me@example.com", user_id: 30, @@ -55,6 +52,9 @@ people.add_active_user(alice); people.add_active_user(bob); run_test("set_focused_recipient", ({override_rewire}) => { + override_rewire(compose_recipient, "selected_stream_name", "social"); + override_rewire(compose_recipient, "is_direct_message_selected", false); + override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); const sub = { stream_id: 101, diff --git a/web/tests/compose_pm_pill.test.js b/web/tests/compose_pm_pill.test.js index 82065540b4..802af767c2 100644 --- a/web/tests/compose_pm_pill.test.js +++ b/web/tests/compose_pm_pill.test.js @@ -15,7 +15,7 @@ let pills = { pill: {}, }; -run_test("pills", ({override}) => { +run_test("pills", ({override, override_rewire}) => { const othello = { user_id: 1, email: "othello@example.com", @@ -74,7 +74,7 @@ run_test("pills", ({override}) => { }; let get_by_email_called = false; - people.get_by_email = (user_email) => { + override_rewire(people, "get_by_email", (user_email) => { get_by_email_called = true; switch (user_email) { case iago.email: @@ -85,10 +85,10 @@ run_test("pills", ({override}) => { default: throw new Error(`Unknown user email ${user_email}`); } - }; + }); let get_by_user_id_called = false; - people.get_by_user_id = (id) => { + override_rewire(people, "get_by_user_id", (id) => { get_by_user_id_called = true; switch (id) { case othello.user_id: @@ -99,7 +99,7 @@ run_test("pills", ({override}) => { default: throw new Error(`Unknown user ID ${id}`); } - }; + }); function test_create_item(handler) { (function test_rejection_path() { diff --git a/web/tests/compose_state.test.js b/web/tests/compose_state.test.js index b14a9167bf..b53507a118 100644 --- a/web/tests/compose_state.test.js +++ b/web/tests/compose_state.test.js @@ -10,7 +10,6 @@ const $ = require("./lib/zjquery"); const compose_pm_pill = mock_esm("../src/compose_pm_pill"); const compose_state = zrequire("compose_state"); -const compose_fade = zrequire("compose_fade"); const compose_recipient = zrequire("compose_recipient"); const stream_data = zrequire("stream_data"); @@ -31,7 +30,6 @@ run_test("private_message_recipient", ({override}) => { run_test("has_full_recipient", ({override, override_rewire}) => { mock_stream_header_colorblock(); $(`#compose_banners .topic_resolved`).remove = noop; - compose_fade.update_all = noop; $(".narrow_to_compose_recipients").toggleClass = noop; override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); diff --git a/web/tests/compose_validate.test.js b/web/tests/compose_validate.test.js index d52ae29028..7288064994 100644 --- a/web/tests/compose_validate.test.js +++ b/web/tests/compose_validate.test.js @@ -389,10 +389,10 @@ test_ui("validate_stream_message", ({override_rewire, mock_template}) => { assert.ok(compose_validate.validate()); assert.ok(!$("#compose-all-everyone").visible()); - peer_data.get_subscriber_count = (stream_id) => { + override_rewire(peer_data, "get_subscriber_count", (stream_id) => { assert.equal(stream_id, 101); return 16; - }; + }); let wildcard_warning_rendered = false; $("#compose_banner_area .wildcard_warning").length = 0; mock_template("compose_banner/wildcard_warning.hbs", false, (data) => { @@ -400,8 +400,6 @@ test_ui("validate_stream_message", ({override_rewire, mock_template}) => { assert.equal(data.subscriber_count, 16); }); - compose_banner.update_or_append_banner = () => {}; - override_rewire(compose_validate, "wildcard_mention_allowed", () => true); compose_state.message_content("Hey @**all**"); assert.ok(!compose_validate.validate()); diff --git a/web/tests/compose_video.test.js b/web/tests/compose_video.test.js index 711a0a4165..4705400bb9 100644 --- a/web/tests/compose_video.test.js +++ b/web/tests/compose_video.test.js @@ -9,6 +9,7 @@ const $ = require("./lib/zjquery"); const {page_params} = require("./lib/zpage_params"); const channel = mock_esm("../src/channel"); +const compose_closed_ui = mock_esm("../src/compose_closed_ui"); const compose_ui = mock_esm("../src/compose_ui"); const upload = mock_esm("../src/upload"); mock_esm("../src/resize", { @@ -26,7 +27,6 @@ set_global( ); const server_events_dispatch = zrequire("server_events_dispatch"); -const compose_closed = zrequire("compose_closed_ui"); const compose = zrequire("compose"); function stub_out_video_calls() { const $elem = $("#below-compose-content .video_link"); @@ -207,7 +207,7 @@ test("videos", ({override}) => { page_params.realm_video_chat_provider = realm_available_video_chat_providers.big_blue_button.id; - compose_closed.get_recipient_label = () => "a"; + override(compose_closed_ui, "get_recipient_label", () => "a"); channel.get = (options) => { assert.equal(options.url, "/json/calls/bigbluebutton/create"); diff --git a/web/tests/composebox_typeahead.test.js b/web/tests/composebox_typeahead.test.js index ce00a8d41f..5e5cd225fb 100644 --- a/web/tests/composebox_typeahead.test.js +++ b/web/tests/composebox_typeahead.test.js @@ -11,6 +11,13 @@ const {page_params, user_settings} = require("./lib/zpage_params"); const noop = () => {}; +let autosize_called; + +mock_esm("../src/compose_ui", { + autosize_textarea() { + autosize_called = true; + }, +}); const compose_validate = mock_esm("../src/compose_validate", { validate_message_length: () => true, warn_if_topic_resolved: noop, @@ -22,8 +29,6 @@ const message_user_ids = mock_esm("../src/message_user_ids", { }); const stream_topic_history_util = mock_esm("../src/stream_topic_history_util"); -let autosize_called; - let set_timeout_called; set_global("setTimeout", (f, time) => { f(); @@ -44,7 +49,6 @@ const stream_data = zrequire("stream_data"); const stream_list_sort = zrequire("stream_list_sort"); const compose = zrequire("compose"); const compose_pm_pill = zrequire("compose_pm_pill"); -const compose_ui = zrequire("compose_ui"); const compose_recipient = zrequire("compose_recipient"); const composebox_typeahead = zrequire("composebox_typeahead"); const settings_config = zrequire("settings_config"); @@ -252,8 +256,11 @@ emoji.initialize({ realm_emoji: {}, emoji_codes, }); -emoji.active_realm_emojis = new Map(); -emoji.emojis_by_name = emojis_by_name; +emoji.active_realm_emojis.clear(); +emoji.emojis_by_name.clear(); +for (const [key, val] of emojis_by_name.entries()) { + emoji.emojis_by_name.set(key, val); +} const ali = { email: "ali@zulip.com", @@ -411,10 +418,6 @@ test("topics_seen_for", ({override, override_rewire}) => { }); test("content_typeahead_selected", ({override}) => { - compose_ui.autosize_textarea = () => { - autosize_called = true; - }; - const fake_this = { query: "", $element: {}, @@ -695,6 +698,8 @@ function sorted_names_from(subs) { return subs.map((sub) => sub.name).sort(); } +const sweden_topics_to_show = ["<&>", "even more ice", "furniture", "ice", "kronor", "more ice"]; + test("initialize", ({override, override_rewire, mock_template}) => { mock_stream_header_colorblock(); mock_banners(); @@ -734,16 +739,15 @@ test("initialize", ({override, override_rewire, mock_template}) => { let topic_typeahead_called = false; $("#stream_message_recipient_topic").typeahead = (options) => { - const topics = ["<&>", "even more ice", "furniture", "ice", "kronor", "more ice"]; - stream_topic_history.get_recent_topic_names = (stream_id) => { + override_rewire(stream_topic_history, "get_recent_topic_names", (stream_id) => { assert.equal(stream_id, sweden_stream.stream_id); - return topics; - }; + return sweden_topics_to_show; + }); compose_state.set_stream_name("Sweden"); let actual_value = options.source(); // Topics should be sorted alphabetically, not by addition order. - let expected_value = topics; + let expected_value = sweden_topics_to_show; assert.deepEqual(actual_value, expected_value); // options.highlighter() @@ -1234,6 +1238,10 @@ test("initialize", ({override, override_rewire, mock_template}) => { }); test("begins_typeahead", ({override, override_rewire}) => { + override_rewire(stream_topic_history, "get_recent_topic_names", (stream_id) => { + assert.equal(stream_id, sweden_stream.stream_id); + return sweden_topics_to_show; + }); override(stream_topic_history_util, "get_server_history", () => {}); const begin_typehead_this = { @@ -1409,7 +1417,6 @@ test("begins_typeahead", ({override, override_rewire}) => { // topic_list // includes "more ice" - const sweden_topics_to_show = stream_topic_history.get_recent_topic_names(1); assert_typeahead_equals("#**Sweden>more ice", sweden_topics_to_show); sweden_topics_to_show.push("totally new topic"); assert_typeahead_equals("#**Sweden>totally new topic", sweden_topics_to_show); diff --git a/web/tests/drafts.test.js b/web/tests/drafts.test.js index 74122f3146..2ac696a039 100644 --- a/web/tests/drafts.test.js +++ b/web/tests/drafts.test.js @@ -13,7 +13,6 @@ const blueslip = zrequire("blueslip"); const compose_pm_pill = zrequire("compose_pm_pill"); const user_pill = zrequire("user_pill"); const people = zrequire("people"); -const compose_fade = zrequire("compose_fade"); const compose_state = zrequire("compose_state"); const compose_recipient = zrequire("compose_recipient"); const sub_store = zrequire("sub_store"); @@ -158,18 +157,16 @@ test("snapshot_message", ({override_rewire}) => { override_rewire(user_pill, "get_user_ids", () => [aaron.user_id]); override_rewire(compose_pm_pill, "set_from_emails", noop); override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); - compose_recipient.set_compose_recipient_id = () => {}; mock_banners(); - stream_data.get_sub = (stream_name) => { + override_rewire(stream_data, "get_sub", (stream_name) => { assert.equal(stream_name, "stream"); return {stream_id: 30}; - }; + }); $(".narrow_to_compose_recipients").toggleClass = noop; mock_stream_header_colorblock(); - compose_fade.update_all = noop; let curr_draft; @@ -177,7 +174,7 @@ test("snapshot_message", ({override_rewire}) => { compose_state.set_message_type(curr_draft.type); compose_state.message_content(curr_draft.content); if (curr_draft.type === "private") { - compose_recipient.set_compose_recipient_id(compose_recipient.DIRECT_MESSAGE_ID); + compose_state.set_compose_recipient_id(compose_recipient.DIRECT_MESSAGE_ID); } else { compose_state.set_stream_name(curr_draft.stream); } @@ -486,7 +483,7 @@ test("delete_all_drafts", () => { }); test("format_drafts", ({override_rewire, mock_template}) => { - stream_data.get_color = () => "#FFFFFF"; + override_rewire(stream_data, "get_color", () => "#FFFFFF"); function feb12() { return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0) } @@ -595,10 +592,10 @@ test("format_drafts", ({override_rewire, mock_template}) => { stub_render_now(time, new Date(1549958107000)), ); - sub_store.get = function (stream_id) { + override_rewire(sub_store, "get", (stream_id) => { assert.equal(stream_id, 30); return {name: "stream"}; - }; + }); override_rewire(user_pill, "get_user_ids", () => []); compose_state.set_message_type("private"); @@ -623,10 +620,10 @@ test("format_drafts", ({override_rewire, mock_template}) => { $.create("#drafts_table .draft-row", {children: []}); $("#draft_overlay").css = () => {}; - sub_store.get = function (stream_id) { + override_rewire(sub_store, "get", (stream_id) => { assert.equal(stream_id, 30); return {name: "stream-rename"}; - }; + }); expected[0].stream_name = "stream-rename"; @@ -636,6 +633,7 @@ test("format_drafts", ({override_rewire, mock_template}) => { }); test("filter_drafts", ({override_rewire, mock_template}) => { + override_rewire(stream_data, "get_color", () => "#FFFFFF"); function feb12() { return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0) } @@ -753,10 +751,10 @@ test("filter_drafts", ({override_rewire, mock_template}) => { stub_render_now(time, new Date(1549958107000)), ); - sub_store.get = function (stream_id) { + override_rewire(sub_store, "get", (stream_id) => { assert.equal(stream_id, 30); return {name: "stream", invite_only: false, is_web_public: false}; - }; + }); mock_template("draft_table_body.hbs", false, (data) => { // Tests splitting up drafts by current narrow. diff --git a/web/tests/narrow_activate.test.js b/web/tests/narrow_activate.test.js index d36f41402f..54eee862d3 100644 --- a/web/tests/narrow_activate.test.js +++ b/web/tests/narrow_activate.test.js @@ -14,6 +14,7 @@ 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 compose_recipient = mock_esm("../src/compose_recipient"); const hashchange = mock_esm("../src/hashchange"); const message_fetch = mock_esm("../src/message_fetch"); const message_list = mock_esm("../src/message_list"); @@ -55,8 +56,6 @@ 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"); @@ -70,13 +69,13 @@ const denmark = { }; stream_data.add_sub(denmark); -function test_helper() { +function test_helper({override}) { const events = []; function stub(module, func_name) { - module[func_name] = () => { + override(module, func_name, () => { events.push([module, func_name]); - }; + }); } stub(compose_banner, "clear_message_sent_banners"); @@ -84,7 +83,6 @@ function test_helper() { 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"); @@ -131,10 +129,10 @@ function stub_message_list() { }; } -run_test("basics", () => { +run_test("basics", ({override}) => { stub_message_list(); - const helper = test_helper(); + const helper = test_helper({override}); const terms = [{operator: "stream", operand: "Denmark"}]; const selected_id = 1000; @@ -201,7 +199,6 @@ run_test("basics", () => { 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, diff --git a/web/tests/navbar_alerts.test.js b/web/tests/navbar_alerts.test.js index 45515768d1..4aebaa7e8e 100644 --- a/web/tests/navbar_alerts.test.js +++ b/web/tests/navbar_alerts.test.js @@ -4,32 +4,33 @@ const {strict: assert} = require("assert"); const {addDays} = require("date-fns"); -const {zrequire} = require("./lib/namespace"); +const {mock_esm, zrequire} = require("./lib/namespace"); const {run_test} = require("./lib/test"); const {page_params} = require("./lib/zpage_params"); page_params.is_spectator = false; +const notifications = mock_esm("../src/notifications"); +const util = mock_esm("../src/util"); + const {localstorage} = zrequire("localstorage"); const navbar_alerts = zrequire("navbar_alerts"); -const notifications = zrequire("notifications"); -const util = zrequire("util"); function test(label, f) { - run_test(label, ({override}) => { + run_test(label, (helpers) => { window.localStorage.clear(); - f({override}); + f(helpers); }); } -test("allow_notification_alert", () => { +test("allow_notification_alert", ({disallow, override}) => { const ls = localstorage(); // Show alert. assert.equal(ls.get("dontAskForNotifications"), undefined); - util.is_mobile = () => false; - notifications.granted_desktop_notifications_permission = () => false; - notifications.permission_state = () => "granted"; + override(util, "is_mobile", () => false); + override(notifications, "granted_desktop_notifications_permission", () => false); + override(notifications, "permission_state", () => "granted"); assert.equal(navbar_alerts.should_show_notifications(ls), true); // Avoid showing if the user said to never show alert on this computer again. @@ -39,28 +40,24 @@ test("allow_notification_alert", () => { // Avoid showing if device is mobile. ls.set("dontAskForNotifications", undefined); assert.equal(navbar_alerts.should_show_notifications(ls), true); - util.is_mobile = () => true; + override(util, "is_mobile", () => true); assert.equal(navbar_alerts.should_show_notifications(ls), false); // Avoid showing if notification permission is denied. - util.is_mobile = () => false; + override(util, "is_mobile", () => false); assert.equal(navbar_alerts.should_show_notifications(ls), true); - notifications.permission_state = () => "denied"; + override(notifications, "permission_state", () => "denied"); assert.equal(navbar_alerts.should_show_notifications(ls), false); // Avoid showing if notification is already granted. - /* istanbul ignore next */ - notifications.permission_state = () => "granted"; - notifications.granted_desktop_notifications_permission = () => "granted"; + disallow(notifications, "permission_state"); + override(notifications, "granted_desktop_notifications_permission", () => "granted"); assert.equal(navbar_alerts.should_show_notifications(ls), false); // Don't ask for permission to spectator. - /* istanbul ignore next */ - util.is_mobile = () => false; - /* istanbul ignore next */ - notifications.granted_desktop_notifications_permission = () => false; - /* istanbul ignore next */ - notifications.permission_state = () => "granted"; + disallow(util, "is_mobile"); + disallow(notifications, "granted_desktop_notifications_permission"); + disallow(notifications, "permission_state"); page_params.is_spectator = true; assert.equal(navbar_alerts.should_show_notifications(ls), false); }); diff --git a/web/tests/notifications.test.js b/web/tests/notifications.test.js index 578cc9e1c9..2f42c972b6 100644 --- a/web/tests/notifications.test.js +++ b/web/tests/notifications.test.js @@ -2,15 +2,15 @@ const {strict: assert} = require("assert"); -const {zrequire} = require("./lib/namespace"); +const {mock_esm, zrequire} = require("./lib/namespace"); const {run_test} = require("./lib/test"); const $ = require("./lib/zjquery"); const {page_params, user_settings} = require("./lib/zpage_params"); +mock_esm("../src/spoilers", {hide_spoilers_in_notification() {}}); + const user_topics = zrequire("user_topics"); const stream_data = zrequire("stream_data"); -const spoilers = zrequire("spoilers"); -spoilers.hide_spoilers_in_notification = () => {}; const notifications = zrequire("notifications"); diff --git a/web/tests/presence.test.js b/web/tests/presence.test.js index 8fb2c5362f..65c454189c 100644 --- a/web/tests/presence.test.js +++ b/web/tests/presence.test.js @@ -77,11 +77,11 @@ people.add_active_user(jane); people.initialize_current_user(me.user_id); function test(label, f) { - run_test(label, ({override}) => { + run_test(label, (helpers) => { page_params.server_presence_offline_threshold_seconds = OFFLINE_THRESHOLD_SECS; user_settings.presence_enabled = true; presence.clear_internal_data(); - f({override}); + f(helpers); }); } @@ -263,7 +263,7 @@ test("falsy values", () => { } }); -test("big realms", () => { +test("big realms", ({override_rewire}) => { const presences = {}; const now = 5000; @@ -274,12 +274,10 @@ test("big realms", () => { // Make it seem like realm has a lot of people, in // which case we will not provide default values for // users that aren't in our presences payload. - const get_active_human_count = people.get_active_human_count; - people.get_active_human_count = () => 1000; + override_rewire(people, "get_active_human_count", () => 1000); presence.set_info(presences, now); assert.ok(presence.presence_info.has(sally.user_id)); assert.ok(!presence.presence_info.has(zoe.user_id)); - people.get_active_human_count = get_active_human_count; }); test("last_active_date", () => { diff --git a/web/tests/rendered_markdown.test.js b/web/tests/rendered_markdown.test.js index babfdef75d..a62535bc70 100644 --- a/web/tests/rendered_markdown.test.js +++ b/web/tests/rendered_markdown.test.js @@ -165,7 +165,7 @@ run_test("user-mention PM (wildcard)", () => { assert.ok($mention.hasClass("user-mention-me")); }); -run_test("user-mention Stream subbed (wildcard)", () => { +run_test("user-mention Stream subbed (wildcard)", ({override_rewire}) => { // Setup const $content = get_content_element(); const $mention = $.create("mention"); @@ -173,14 +173,14 @@ run_test("user-mention Stream subbed (wildcard)", () => { $content.set_find_results(".user-mention", $array([$mention])); const attr = () => stream.stream_id; set_closest_dot_find_result($content, {attr, length: 1}); - stream_data.is_user_subscribed = () => true; + override_rewire(stream_data, "is_user_subscribed", () => true); assert.ok(!$mention.hasClass("user-mention-me")); rm.update_elements($content); assert.ok($mention.hasClass("user-mention-me")); }); -run_test("user-mention Stream not subbed (wildcard)", () => { +run_test("user-mention Stream not subbed (wildcard)", ({override_rewire}) => { // Setup const $content = get_content_element(); const $mention = $.create("mention"); @@ -188,7 +188,7 @@ run_test("user-mention Stream not subbed (wildcard)", () => { $content.set_find_results(".user-mention", $array([$mention])); const attr = () => 1; set_closest_dot_find_result($content, {attr, length: 1}); - stream_data.is_user_subscribed = () => false; + override_rewire(stream_data, "is_user_subscribed", () => false); // Don't add user-mention-me class. assert.ok(!$mention.hasClass("user-mention-me")); diff --git a/web/tests/settings_user_groups_legacy.test.js b/web/tests/settings_user_groups_legacy.test.js index 1fed7a0bb2..ab1ccc57e7 100644 --- a/web/tests/settings_user_groups_legacy.test.js +++ b/web/tests/settings_user_groups_legacy.test.js @@ -21,6 +21,7 @@ const channel = mock_esm("../src/channel"); const confirm_dialog = mock_esm("../src/confirm_dialog"); const dialog_widget = mock_esm("../src/dialog_widget"); const input_pill = mock_esm("../src/input_pill"); +const settings_data = mock_esm("../src/settings_data"); const typeahead_helper = mock_esm("../src/typeahead_helper"); const user_groups = mock_esm("../src/user_groups", { get_user_group_from_id: noop, @@ -30,7 +31,6 @@ const user_groups = mock_esm("../src/user_groups", { const ui_report = mock_esm("../src/ui_report"); const people = zrequire("people"); -const settings_data = zrequire("settings_data"); const settings_user_groups_legacy = zrequire("settings_user_groups_legacy"); const user_pill = zrequire("user_pill"); @@ -49,11 +49,11 @@ function test_ui(label, f) { run_test(label, f, {sloppy_$: true}); } -test_ui("can_edit", () => { - settings_data.user_can_edit_user_groups = () => false; +test_ui("can_edit", ({override}) => { + override(settings_data, "user_can_edit_user_groups", () => false); assert.ok(!settings_user_groups_legacy.can_edit(1)); - settings_data.user_can_edit_user_groups = () => true; + override(settings_data, "user_can_edit_user_groups", () => true); user_groups.is_direct_member_of = (user_id, group_id) => { assert.equal(group_id, 1); assert.equal(user_id, undefined); @@ -85,7 +85,9 @@ const name_selector = `#user-groups #${CSS.escape(1)} .name`; const description_selector = `#user-groups #${CSS.escape(1)} .description`; const instructions_selector = `#user-groups #${CSS.escape(1)} .save-instructions`; -test_ui("populate_user_groups", ({mock_template}) => { +test_ui("populate_user_groups", ({mock_template, override, override_rewire}) => { + override(settings_data, "user_can_edit_user_groups", () => true); + const realm_user_group = { id: 1, name: "Mobile", @@ -112,7 +114,7 @@ test_ui("populate_user_groups", ({mock_template}) => { people.add_active_user(alice); people.add_active_user(bob); - people.get_realm_users = () => [iago, alice, bob]; + override_rewire(people, "get_realm_users", () => [iago, alice, bob]); user_groups.get_realm_user_groups = () => [realm_user_group]; @@ -133,7 +135,7 @@ test_ui("populate_user_groups", ({mock_template}) => { }; let get_by_user_id_called = false; - people.get_by_user_id = (user_id) => { + override_rewire(people, "get_by_user_id", (user_id) => { if (user_id === iago.user_id) { return iago; } @@ -141,10 +143,12 @@ test_ui("populate_user_groups", ({mock_template}) => { blueslip.expect("warn", "Undefined user in function append_user"); get_by_user_id_called = true; return undefined; - }; - people.is_known_user = function () { - return people.get_by_user_id !== undefined && people.get_by_user_id !== noop; - }; + }); + override_rewire( + people, + "is_known_user", + () => people.get_by_user_id !== undefined && people.get_by_user_id !== noop, + ); page_params.is_admin = true; @@ -266,7 +270,7 @@ test_ui("populate_user_groups", ({mock_template}) => { }; let get_by_email_called = false; - people.get_by_email = (user_email) => { + override_rewire(people, "get_by_email", (user_email) => { get_by_email_called = true; switch (user_email) { case iago.email: @@ -277,7 +281,7 @@ test_ui("populate_user_groups", ({mock_template}) => { default: throw new Error("Expected user email to be of Iago or Bob here."); } - }; + }); function test_create_item(handler) { (function test_rejection_path() { @@ -331,7 +335,7 @@ test_ui("populate_user_groups", ({mock_template}) => { "function", ); }); -test_ui("with_external_user", ({override_rewire, mock_template}) => { +test_ui("with_external_user", ({disallow_rewire, override_rewire, mock_template}) => { const realm_user_group = { id: 1, name: "Mobile", @@ -341,9 +345,8 @@ test_ui("with_external_user", ({override_rewire, mock_template}) => { user_groups.get_realm_user_groups = () => [realm_user_group]; - // We return [] because these are already tested, so we skip them - /* istanbul ignore next */ - people.get_realm_users = () => []; + // These are already tested, so we skip them + disallow_rewire(people, "get_realm_users"); mock_template( "settings/admin_user_group_list.hbs", @@ -351,7 +354,7 @@ test_ui("with_external_user", ({override_rewire, mock_template}) => { () => "settings/admin_user_group_list.hbs", ); - people.get_by_user_id = () => "user stub"; + override_rewire(people, "get_by_user_id", () => "user stub"); override_rewire(user_pill, "append_person", noop); @@ -489,7 +492,8 @@ test_ui("reset", () => { assert.equal(result, undefined); }); -test_ui("on_events", ({override_rewire, mock_template}) => { +test_ui("on_events", ({mock_template, override, override_rewire}) => { + override(settings_data, "user_can_edit_user_groups", () => true); mock_template("confirm_dialog/confirm_delete_user.hbs", false, (data) => { assert.deepEqual(data, { group_name: "Mobile", diff --git a/web/tests/settings_user_topics.test.js b/web/tests/settings_user_topics.test.js index 3d12ed1870..3070f696a9 100644 --- a/web/tests/settings_user_topics.test.js +++ b/web/tests/settings_user_topics.test.js @@ -22,7 +22,7 @@ const frontend = { }; stream_data.add_sub(frontend); -run_test("settings", ({override}) => { +run_test("settings", ({override, override_rewire}) => { user_topics.update_user_topics( frontend.stream_id, "js", @@ -83,12 +83,16 @@ run_test("settings", ({override}) => { }; let user_topic_visibility_policy_changed = false; - user_topics.set_user_topic_visibility_policy = (stream_id, topic, visibility_policy) => { - assert.equal(stream_id, frontend.stream_id); - assert.equal(topic, "js"); - assert.equal(visibility_policy, user_topics.all_visibility_policies.UNMUTED); - user_topic_visibility_policy_changed = true; - }; + override_rewire( + user_topics, + "set_user_topic_visibility_policy", + (stream_id, topic, visibility_policy) => { + assert.equal(stream_id, frontend.stream_id); + assert.equal(topic, "js"); + assert.equal(visibility_policy, user_topics.all_visibility_policies.UNMUTED); + user_topic_visibility_policy_changed = true; + }, + ); $topic_fake_this.value = user_topics.all_visibility_policies.UNMUTED; topic_change_handler.call($topic_fake_this, event); assert.ok(user_topic_visibility_policy_changed); diff --git a/web/tests/topic_generator.test.js b/web/tests/topic_generator.test.js index 7d0aa4fe2c..1bf05da0b7 100644 --- a/web/tests/topic_generator.test.js +++ b/web/tests/topic_generator.test.js @@ -5,24 +5,24 @@ const {strict: assert} = require("assert"); const {mock_esm, zrequire} = require("./lib/namespace"); const {run_test} = require("./lib/test"); -const user_topics = mock_esm("../src/user_topics"); +const pm_conversations = mock_esm("../src/pm_conversations", { + recent: {}, +}); const stream_data = mock_esm("../src/stream_data"); +const stream_list_sort = mock_esm("../src/stream_list_sort"); const stream_topic_history = mock_esm("../src/stream_topic_history"); const unread = mock_esm("../src/unread"); +const user_topics = mock_esm("../src/user_topics"); -const pm_conversations = zrequire("pm_conversations"); -pm_conversations.recent = {}; - -const stream_list_sort = zrequire("stream_list_sort"); const tg = zrequire("topic_generator"); -run_test("streams", () => { +run_test("streams", ({override}) => { function assert_next_stream(curr_stream, expected) { const actual = tg.get_next_stream(curr_stream); assert.equal(actual, expected); } - stream_list_sort.get_streams = () => ["announce", "muted", "devel", "test here"]; + override(stream_list_sort, "get_streams", () => ["announce", "muted", "devel", "test here"]); assert_next_stream(undefined, "announce"); assert_next_stream("NOT THERE", "announce"); @@ -76,7 +76,7 @@ run_test("topics", ({override}) => { // Now test the deeper function that is wired up to // real functions stream_data/stream_list_sort/unread. - stream_list_sort.get_streams = () => ["announce", "muted", "devel", "test here"]; + override(stream_list_sort, "get_streams", () => ["announce", "muted", "devel", "test here"]); const muted_stream_id = 400; const devel_stream_id = 401; @@ -121,7 +121,7 @@ run_test("topics", ({override}) => { }); run_test("get_next_unread_pm_string", ({override}) => { - pm_conversations.recent.get_strings = () => ["1", "read", "2,3", "4", "unk"]; + override(pm_conversations.recent, "get_strings", () => ["1", "read", "2,3", "4", "unk"]); override(unread, "num_unread_for_user_ids_string", (user_ids_string) => { if (user_ids_string === "unk") { diff --git a/web/tests/typeahead_helper.test.js b/web/tests/typeahead_helper.test.js index 6f7fb31122..f74665723b 100644 --- a/web/tests/typeahead_helper.test.js +++ b/web/tests/typeahead_helper.test.js @@ -768,11 +768,8 @@ test("render_emoji", ({mock_template}) => { emoji_name: "thumbs_up", emoji_code: "1f44d", }; - emoji.active_realm_emojis = new Map( - Object.entries({ - realm_emoji: "TBD", - }), - ); + emoji.active_realm_emojis.clear(); + emoji.active_realm_emojis.set("realm_emoji", "TBD"); mock_template("typeahead_list_item.hbs", false, (args) => { assert.deepEqual(args, expected_template_data); diff --git a/web/tests/typing_status.test.js b/web/tests/typing_status.test.js index dceb6d4874..6bf4f29879 100644 --- a/web/tests/typing_status.test.js +++ b/web/tests/typing_status.test.js @@ -22,7 +22,7 @@ function returns_time(secs) { } run_test("basics", ({override, override_rewire}) => { - typing_status.state = null; + assert.equal(typing_status.state, null); // invalid conversation basically does nothing let worker = {};