tests: Fix direct mutation of ES modules.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-08-04 14:40:48 -07:00 committed by Anders Kaseorg
parent 64c973d8ec
commit 63c9296d53
19 changed files with 130 additions and 135 deletions

View File

@ -374,7 +374,7 @@ test_ui("finish", ({override, override_rewire}) => {
$(".compose-submit-button .loader").hide(); $(".compose-submit-button .loader").hide();
$("#compose-textarea").off("select"); $("#compose-textarea").off("select");
$("#compose-textarea").val(""); $("#compose-textarea").val("");
compose_ui.compose_spinner_visible = false; override_rewire(compose_ui, "compose_spinner_visible", false);
const res = compose.finish(); const res = compose.finish();
assert.equal(res, false); assert.equal(res, false);
assert.ok(!$("#compose_banners .recipient_not_subscribed").visible()); assert.ok(!$("#compose_banners .recipient_not_subscribed").visible());
@ -388,7 +388,7 @@ test_ui("finish", ({override, override_rewire}) => {
$("#compose .preview_message_area").show(); $("#compose .preview_message_area").show();
$("#compose .markdown_preview").hide(); $("#compose .markdown_preview").hide();
$("#compose-textarea").val("foobarfoobar"); $("#compose-textarea").val("foobarfoobar");
compose_ui.compose_spinner_visible = false; override_rewire(compose_ui, "compose_spinner_visible", false);
compose_state.set_message_type("private"); compose_state.set_message_type("private");
override(compose_pm_pill, "get_emails", () => "bob@example.com"); override(compose_pm_pill, "get_emails", () => "bob@example.com");
override(compose_pm_pill, "get_user_ids", () => []); 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.to_user_ids, "31,32");
assert.equal(message.content, "burrito"); assert.equal(message.content, "burrito");
const {email_list_to_user_ids_string} = people;
override_rewire(people, "email_list_to_user_ids_string", () => undefined); override_rewire(people, "email_list_to_user_ids_string", () => undefined);
message = compose.create_message_object(); message = compose.create_message_object();
assert.deepEqual(message.to, [alice.email, bob.email]); 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}) => { test_ui("DM policy disabled", ({override, override_rewire}) => {

View File

@ -75,9 +75,6 @@ const respond_to_message = compose_actions.respond_to_message;
const reply_with_mention = compose_actions.reply_with_mention; const reply_with_mention = compose_actions.reply_with_mention;
const quote_and_reply = compose_actions.quote_and_reply; 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) { function assert_visible(sel) {
assert.ok($(sel).visible()); 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}) => { test("quote_and_reply", ({disallow, override, override_rewire}) => {
override_rewire(compose_recipient, "on_compose_select_recipient_update", noop);
mock_banners(); mock_banners();
mock_stream_header_colorblock(); mock_stream_header_colorblock();
compose_state.set_message_type("stream"); compose_state.set_message_type("stream");

View File

@ -27,9 +27,6 @@ const compose_recipient = zrequire("compose_recipient");
const compose_fade_helper = zrequire("compose_fade_helper"); const compose_fade_helper = zrequire("compose_fade_helper");
const compose_state = zrequire("compose_state"); const compose_state = zrequire("compose_state");
compose_recipient.selected_stream_name = "social";
compose_recipient.is_direct_message_selected = false;
const me = { const me = {
email: "me@example.com", email: "me@example.com",
user_id: 30, user_id: 30,
@ -55,6 +52,9 @@ people.add_active_user(alice);
people.add_active_user(bob); people.add_active_user(bob);
run_test("set_focused_recipient", ({override_rewire}) => { 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", () => {}); override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {});
const sub = { const sub = {
stream_id: 101, stream_id: 101,

View File

@ -15,7 +15,7 @@ let pills = {
pill: {}, pill: {},
}; };
run_test("pills", ({override}) => { run_test("pills", ({override, override_rewire}) => {
const othello = { const othello = {
user_id: 1, user_id: 1,
email: "othello@example.com", email: "othello@example.com",
@ -74,7 +74,7 @@ run_test("pills", ({override}) => {
}; };
let get_by_email_called = false; 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; get_by_email_called = true;
switch (user_email) { switch (user_email) {
case iago.email: case iago.email:
@ -85,10 +85,10 @@ run_test("pills", ({override}) => {
default: default:
throw new Error(`Unknown user email ${user_email}`); throw new Error(`Unknown user email ${user_email}`);
} }
}; });
let get_by_user_id_called = false; 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; get_by_user_id_called = true;
switch (id) { switch (id) {
case othello.user_id: case othello.user_id:
@ -99,7 +99,7 @@ run_test("pills", ({override}) => {
default: default:
throw new Error(`Unknown user ID ${id}`); throw new Error(`Unknown user ID ${id}`);
} }
}; });
function test_create_item(handler) { function test_create_item(handler) {
(function test_rejection_path() { (function test_rejection_path() {

View File

@ -10,7 +10,6 @@ const $ = require("./lib/zjquery");
const compose_pm_pill = mock_esm("../src/compose_pm_pill"); const compose_pm_pill = mock_esm("../src/compose_pm_pill");
const compose_state = zrequire("compose_state"); const compose_state = zrequire("compose_state");
const compose_fade = zrequire("compose_fade");
const compose_recipient = zrequire("compose_recipient"); const compose_recipient = zrequire("compose_recipient");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
@ -31,7 +30,6 @@ run_test("private_message_recipient", ({override}) => {
run_test("has_full_recipient", ({override, override_rewire}) => { run_test("has_full_recipient", ({override, override_rewire}) => {
mock_stream_header_colorblock(); mock_stream_header_colorblock();
$(`#compose_banners .topic_resolved`).remove = noop; $(`#compose_banners .topic_resolved`).remove = noop;
compose_fade.update_all = noop;
$(".narrow_to_compose_recipients").toggleClass = noop; $(".narrow_to_compose_recipients").toggleClass = noop;
override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {});

View File

@ -389,10 +389,10 @@ test_ui("validate_stream_message", ({override_rewire, mock_template}) => {
assert.ok(compose_validate.validate()); assert.ok(compose_validate.validate());
assert.ok(!$("#compose-all-everyone").visible()); 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); assert.equal(stream_id, 101);
return 16; return 16;
}; });
let wildcard_warning_rendered = false; let wildcard_warning_rendered = false;
$("#compose_banner_area .wildcard_warning").length = 0; $("#compose_banner_area .wildcard_warning").length = 0;
mock_template("compose_banner/wildcard_warning.hbs", false, (data) => { 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); assert.equal(data.subscriber_count, 16);
}); });
compose_banner.update_or_append_banner = () => {};
override_rewire(compose_validate, "wildcard_mention_allowed", () => true); override_rewire(compose_validate, "wildcard_mention_allowed", () => true);
compose_state.message_content("Hey @**all**"); compose_state.message_content("Hey @**all**");
assert.ok(!compose_validate.validate()); assert.ok(!compose_validate.validate());

View File

@ -9,6 +9,7 @@ const $ = require("./lib/zjquery");
const {page_params} = require("./lib/zpage_params"); const {page_params} = require("./lib/zpage_params");
const channel = mock_esm("../src/channel"); 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 compose_ui = mock_esm("../src/compose_ui");
const upload = mock_esm("../src/upload"); const upload = mock_esm("../src/upload");
mock_esm("../src/resize", { mock_esm("../src/resize", {
@ -26,7 +27,6 @@ set_global(
); );
const server_events_dispatch = zrequire("server_events_dispatch"); const server_events_dispatch = zrequire("server_events_dispatch");
const compose_closed = zrequire("compose_closed_ui");
const compose = zrequire("compose"); const compose = zrequire("compose");
function stub_out_video_calls() { function stub_out_video_calls() {
const $elem = $("#below-compose-content .video_link"); const $elem = $("#below-compose-content .video_link");
@ -207,7 +207,7 @@ test("videos", ({override}) => {
page_params.realm_video_chat_provider = page_params.realm_video_chat_provider =
realm_available_video_chat_providers.big_blue_button.id; 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) => { channel.get = (options) => {
assert.equal(options.url, "/json/calls/bigbluebutton/create"); assert.equal(options.url, "/json/calls/bigbluebutton/create");

View File

@ -11,6 +11,13 @@ const {page_params, user_settings} = require("./lib/zpage_params");
const noop = () => {}; const noop = () => {};
let autosize_called;
mock_esm("../src/compose_ui", {
autosize_textarea() {
autosize_called = true;
},
});
const compose_validate = mock_esm("../src/compose_validate", { const compose_validate = mock_esm("../src/compose_validate", {
validate_message_length: () => true, validate_message_length: () => true,
warn_if_topic_resolved: noop, 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"); const stream_topic_history_util = mock_esm("../src/stream_topic_history_util");
let autosize_called;
let set_timeout_called; let set_timeout_called;
set_global("setTimeout", (f, time) => { set_global("setTimeout", (f, time) => {
f(); f();
@ -44,7 +49,6 @@ const stream_data = zrequire("stream_data");
const stream_list_sort = zrequire("stream_list_sort"); const stream_list_sort = zrequire("stream_list_sort");
const compose = zrequire("compose"); const compose = zrequire("compose");
const compose_pm_pill = zrequire("compose_pm_pill"); const compose_pm_pill = zrequire("compose_pm_pill");
const compose_ui = zrequire("compose_ui");
const compose_recipient = zrequire("compose_recipient"); const compose_recipient = zrequire("compose_recipient");
const composebox_typeahead = zrequire("composebox_typeahead"); const composebox_typeahead = zrequire("composebox_typeahead");
const settings_config = zrequire("settings_config"); const settings_config = zrequire("settings_config");
@ -252,8 +256,11 @@ emoji.initialize({
realm_emoji: {}, realm_emoji: {},
emoji_codes, emoji_codes,
}); });
emoji.active_realm_emojis = new Map(); emoji.active_realm_emojis.clear();
emoji.emojis_by_name = emojis_by_name; emoji.emojis_by_name.clear();
for (const [key, val] of emojis_by_name.entries()) {
emoji.emojis_by_name.set(key, val);
}
const ali = { const ali = {
email: "ali@zulip.com", email: "ali@zulip.com",
@ -411,10 +418,6 @@ test("topics_seen_for", ({override, override_rewire}) => {
}); });
test("content_typeahead_selected", ({override}) => { test("content_typeahead_selected", ({override}) => {
compose_ui.autosize_textarea = () => {
autosize_called = true;
};
const fake_this = { const fake_this = {
query: "", query: "",
$element: {}, $element: {},
@ -695,6 +698,8 @@ function sorted_names_from(subs) {
return subs.map((sub) => sub.name).sort(); 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}) => { test("initialize", ({override, override_rewire, mock_template}) => {
mock_stream_header_colorblock(); mock_stream_header_colorblock();
mock_banners(); mock_banners();
@ -734,16 +739,15 @@ test("initialize", ({override, override_rewire, mock_template}) => {
let topic_typeahead_called = false; let topic_typeahead_called = false;
$("#stream_message_recipient_topic").typeahead = (options) => { $("#stream_message_recipient_topic").typeahead = (options) => {
const topics = ["<&>", "even more ice", "furniture", "ice", "kronor", "more ice"]; override_rewire(stream_topic_history, "get_recent_topic_names", (stream_id) => {
stream_topic_history.get_recent_topic_names = (stream_id) => {
assert.equal(stream_id, sweden_stream.stream_id); assert.equal(stream_id, sweden_stream.stream_id);
return topics; return sweden_topics_to_show;
}; });
compose_state.set_stream_name("Sweden"); compose_state.set_stream_name("Sweden");
let actual_value = options.source(); let actual_value = options.source();
// Topics should be sorted alphabetically, not by addition order. // 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); assert.deepEqual(actual_value, expected_value);
// options.highlighter() // options.highlighter()
@ -1234,6 +1238,10 @@ test("initialize", ({override, override_rewire, mock_template}) => {
}); });
test("begins_typeahead", ({override, override_rewire}) => { 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", () => {}); override(stream_topic_history_util, "get_server_history", () => {});
const begin_typehead_this = { const begin_typehead_this = {
@ -1409,7 +1417,6 @@ test("begins_typeahead", ({override, override_rewire}) => {
// topic_list // topic_list
// includes "more ice" // 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); assert_typeahead_equals("#**Sweden>more ice", sweden_topics_to_show);
sweden_topics_to_show.push("totally new topic"); sweden_topics_to_show.push("totally new topic");
assert_typeahead_equals("#**Sweden>totally new topic", sweden_topics_to_show); assert_typeahead_equals("#**Sweden>totally new topic", sweden_topics_to_show);

View File

@ -13,7 +13,6 @@ const blueslip = zrequire("blueslip");
const compose_pm_pill = zrequire("compose_pm_pill"); const compose_pm_pill = zrequire("compose_pm_pill");
const user_pill = zrequire("user_pill"); const user_pill = zrequire("user_pill");
const people = zrequire("people"); const people = zrequire("people");
const compose_fade = zrequire("compose_fade");
const compose_state = zrequire("compose_state"); const compose_state = zrequire("compose_state");
const compose_recipient = zrequire("compose_recipient"); const compose_recipient = zrequire("compose_recipient");
const sub_store = zrequire("sub_store"); 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(user_pill, "get_user_ids", () => [aaron.user_id]);
override_rewire(compose_pm_pill, "set_from_emails", noop); override_rewire(compose_pm_pill, "set_from_emails", noop);
override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {}); override_rewire(compose_recipient, "on_compose_select_recipient_update", () => {});
compose_recipient.set_compose_recipient_id = () => {};
mock_banners(); mock_banners();
stream_data.get_sub = (stream_name) => { override_rewire(stream_data, "get_sub", (stream_name) => {
assert.equal(stream_name, "stream"); assert.equal(stream_name, "stream");
return {stream_id: 30}; return {stream_id: 30};
}; });
$(".narrow_to_compose_recipients").toggleClass = noop; $(".narrow_to_compose_recipients").toggleClass = noop;
mock_stream_header_colorblock(); mock_stream_header_colorblock();
compose_fade.update_all = noop;
let curr_draft; let curr_draft;
@ -177,7 +174,7 @@ test("snapshot_message", ({override_rewire}) => {
compose_state.set_message_type(curr_draft.type); compose_state.set_message_type(curr_draft.type);
compose_state.message_content(curr_draft.content); compose_state.message_content(curr_draft.content);
if (curr_draft.type === "private") { 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 { } else {
compose_state.set_stream_name(curr_draft.stream); compose_state.set_stream_name(curr_draft.stream);
} }
@ -486,7 +483,7 @@ test("delete_all_drafts", () => {
}); });
test("format_drafts", ({override_rewire, mock_template}) => { test("format_drafts", ({override_rewire, mock_template}) => {
stream_data.get_color = () => "#FFFFFF"; override_rewire(stream_data, "get_color", () => "#FFFFFF");
function feb12() { function feb12() {
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0) 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)), 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); assert.equal(stream_id, 30);
return {name: "stream"}; return {name: "stream"};
}; });
override_rewire(user_pill, "get_user_ids", () => []); override_rewire(user_pill, "get_user_ids", () => []);
compose_state.set_message_type("private"); compose_state.set_message_type("private");
@ -623,10 +620,10 @@ test("format_drafts", ({override_rewire, mock_template}) => {
$.create("#drafts_table .draft-row", {children: []}); $.create("#drafts_table .draft-row", {children: []});
$("#draft_overlay").css = () => {}; $("#draft_overlay").css = () => {};
sub_store.get = function (stream_id) { override_rewire(sub_store, "get", (stream_id) => {
assert.equal(stream_id, 30); assert.equal(stream_id, 30);
return {name: "stream-rename"}; return {name: "stream-rename"};
}; });
expected[0].stream_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}) => { test("filter_drafts", ({override_rewire, mock_template}) => {
override_rewire(stream_data, "get_color", () => "#FFFFFF");
function feb12() { function feb12() {
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0) 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)), 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); assert.equal(stream_id, 30);
return {name: "stream", invite_only: false, is_web_public: false}; return {name: "stream", invite_only: false, is_web_public: false};
}; });
mock_template("draft_table_body.hbs", false, (data) => { mock_template("draft_table_body.hbs", false, (data) => {
// Tests splitting up drafts by current narrow. // Tests splitting up drafts by current narrow.

View File

@ -14,6 +14,7 @@ const all_messages_data = mock_esm("../src/all_messages_data");
const compose_actions = mock_esm("../src/compose_actions"); const compose_actions = mock_esm("../src/compose_actions");
const compose_banner = mock_esm("../src/compose_banner"); const compose_banner = mock_esm("../src/compose_banner");
const compose_closed_ui = mock_esm("../src/compose_closed_ui"); 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 hashchange = mock_esm("../src/hashchange");
const message_fetch = mock_esm("../src/message_fetch"); const message_fetch = mock_esm("../src/message_fetch");
const message_list = mock_esm("../src/message_list"); const message_list = mock_esm("../src/message_list");
@ -55,8 +56,6 @@ mock_esm("../src/user_topics", {
is_topic_muted: () => false, is_topic_muted: () => false,
}); });
const compose_recipient = zrequire("compose_recipient");
const util = zrequire("util");
const narrow_state = zrequire("narrow_state"); const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
const narrow = zrequire("narrow"); const narrow = zrequire("narrow");
@ -70,13 +69,13 @@ const denmark = {
}; };
stream_data.add_sub(denmark); stream_data.add_sub(denmark);
function test_helper() { function test_helper({override}) {
const events = []; const events = [];
function stub(module, func_name) { function stub(module, func_name) {
module[func_name] = () => { override(module, func_name, () => {
events.push([module, func_name]); events.push([module, func_name]);
}; });
} }
stub(compose_banner, "clear_message_sent_banners"); stub(compose_banner, "clear_message_sent_banners");
@ -84,7 +83,6 @@ function test_helper() {
stub(compose_closed_ui, "update_reply_recipient_label"); stub(compose_closed_ui, "update_reply_recipient_label");
stub(hashchange, "save_narrow"); stub(hashchange, "save_narrow");
stub(message_feed_loading, "hide_indicators"); stub(message_feed_loading, "hide_indicators");
stub(message_feed_loading, "show_loading_older");
stub(message_feed_top_notices, "hide_top_of_narrow_notices"); stub(message_feed_top_notices, "hide_top_of_narrow_notices");
stub(notifications, "redraw_title"); stub(notifications, "redraw_title");
stub(stream_list, "handle_narrow_activated"); stub(stream_list, "handle_narrow_activated");
@ -131,10 +129,10 @@ function stub_message_list() {
}; };
} }
run_test("basics", () => { run_test("basics", ({override}) => {
stub_message_list(); stub_message_list();
const helper = test_helper(); const helper = test_helper({override});
const terms = [{operator: "stream", operand: "Denmark"}]; const terms = [{operator: "stream", operand: "Denmark"}];
const selected_id = 1000; const selected_id = 1000;
@ -201,7 +199,6 @@ run_test("basics", () => {
message_lists.current.selected_id = () => -1; message_lists.current.selected_id = () => -1;
message_lists.current.get_row = () => row; message_lists.current.get_row = () => row;
util.sorted_ids = () => [];
narrow.activate([{operator: "is", operand: "private"}], { narrow.activate([{operator: "is", operand: "private"}], {
then_select_id: selected_id, then_select_id: selected_id,

View File

@ -4,32 +4,33 @@ const {strict: assert} = require("assert");
const {addDays} = require("date-fns"); const {addDays} = require("date-fns");
const {zrequire} = require("./lib/namespace"); const {mock_esm, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test"); const {run_test} = require("./lib/test");
const {page_params} = require("./lib/zpage_params"); const {page_params} = require("./lib/zpage_params");
page_params.is_spectator = false; page_params.is_spectator = false;
const notifications = mock_esm("../src/notifications");
const util = mock_esm("../src/util");
const {localstorage} = zrequire("localstorage"); const {localstorage} = zrequire("localstorage");
const navbar_alerts = zrequire("navbar_alerts"); const navbar_alerts = zrequire("navbar_alerts");
const notifications = zrequire("notifications");
const util = zrequire("util");
function test(label, f) { function test(label, f) {
run_test(label, ({override}) => { run_test(label, (helpers) => {
window.localStorage.clear(); window.localStorage.clear();
f({override}); f(helpers);
}); });
} }
test("allow_notification_alert", () => { test("allow_notification_alert", ({disallow, override}) => {
const ls = localstorage(); const ls = localstorage();
// Show alert. // Show alert.
assert.equal(ls.get("dontAskForNotifications"), undefined); assert.equal(ls.get("dontAskForNotifications"), undefined);
util.is_mobile = () => false; override(util, "is_mobile", () => false);
notifications.granted_desktop_notifications_permission = () => false; override(notifications, "granted_desktop_notifications_permission", () => false);
notifications.permission_state = () => "granted"; override(notifications, "permission_state", () => "granted");
assert.equal(navbar_alerts.should_show_notifications(ls), true); assert.equal(navbar_alerts.should_show_notifications(ls), true);
// Avoid showing if the user said to never show alert on this computer again. // 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. // Avoid showing if device is mobile.
ls.set("dontAskForNotifications", undefined); ls.set("dontAskForNotifications", undefined);
assert.equal(navbar_alerts.should_show_notifications(ls), true); 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); assert.equal(navbar_alerts.should_show_notifications(ls), false);
// Avoid showing if notification permission is denied. // 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); 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); assert.equal(navbar_alerts.should_show_notifications(ls), false);
// Avoid showing if notification is already granted. // Avoid showing if notification is already granted.
/* istanbul ignore next */ disallow(notifications, "permission_state");
notifications.permission_state = () => "granted"; override(notifications, "granted_desktop_notifications_permission", () => "granted");
notifications.granted_desktop_notifications_permission = () => "granted";
assert.equal(navbar_alerts.should_show_notifications(ls), false); assert.equal(navbar_alerts.should_show_notifications(ls), false);
// Don't ask for permission to spectator. // Don't ask for permission to spectator.
/* istanbul ignore next */ disallow(util, "is_mobile");
util.is_mobile = () => false; disallow(notifications, "granted_desktop_notifications_permission");
/* istanbul ignore next */ disallow(notifications, "permission_state");
notifications.granted_desktop_notifications_permission = () => false;
/* istanbul ignore next */
notifications.permission_state = () => "granted";
page_params.is_spectator = true; page_params.is_spectator = true;
assert.equal(navbar_alerts.should_show_notifications(ls), false); assert.equal(navbar_alerts.should_show_notifications(ls), false);
}); });

View File

@ -2,15 +2,15 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const {zrequire} = require("./lib/namespace"); const {mock_esm, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test"); const {run_test} = require("./lib/test");
const $ = require("./lib/zjquery"); const $ = require("./lib/zjquery");
const {page_params, user_settings} = require("./lib/zpage_params"); const {page_params, user_settings} = require("./lib/zpage_params");
mock_esm("../src/spoilers", {hide_spoilers_in_notification() {}});
const user_topics = zrequire("user_topics"); const user_topics = zrequire("user_topics");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
const spoilers = zrequire("spoilers");
spoilers.hide_spoilers_in_notification = () => {};
const notifications = zrequire("notifications"); const notifications = zrequire("notifications");

View File

@ -77,11 +77,11 @@ people.add_active_user(jane);
people.initialize_current_user(me.user_id); people.initialize_current_user(me.user_id);
function test(label, f) { function test(label, f) {
run_test(label, ({override}) => { run_test(label, (helpers) => {
page_params.server_presence_offline_threshold_seconds = OFFLINE_THRESHOLD_SECS; page_params.server_presence_offline_threshold_seconds = OFFLINE_THRESHOLD_SECS;
user_settings.presence_enabled = true; user_settings.presence_enabled = true;
presence.clear_internal_data(); 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 presences = {};
const now = 5000; const now = 5000;
@ -274,12 +274,10 @@ test("big realms", () => {
// Make it seem like realm has a lot of people, in // Make it seem like realm has a lot of people, in
// which case we will not provide default values for // which case we will not provide default values for
// users that aren't in our presences payload. // users that aren't in our presences payload.
const get_active_human_count = people.get_active_human_count; override_rewire(people, "get_active_human_count", () => 1000);
people.get_active_human_count = () => 1000;
presence.set_info(presences, now); presence.set_info(presences, now);
assert.ok(presence.presence_info.has(sally.user_id)); assert.ok(presence.presence_info.has(sally.user_id));
assert.ok(!presence.presence_info.has(zoe.user_id)); assert.ok(!presence.presence_info.has(zoe.user_id));
people.get_active_human_count = get_active_human_count;
}); });
test("last_active_date", () => { test("last_active_date", () => {

View File

@ -165,7 +165,7 @@ run_test("user-mention PM (wildcard)", () => {
assert.ok($mention.hasClass("user-mention-me")); assert.ok($mention.hasClass("user-mention-me"));
}); });
run_test("user-mention Stream subbed (wildcard)", () => { run_test("user-mention Stream subbed (wildcard)", ({override_rewire}) => {
// Setup // Setup
const $content = get_content_element(); const $content = get_content_element();
const $mention = $.create("mention"); const $mention = $.create("mention");
@ -173,14 +173,14 @@ run_test("user-mention Stream subbed (wildcard)", () => {
$content.set_find_results(".user-mention", $array([$mention])); $content.set_find_results(".user-mention", $array([$mention]));
const attr = () => stream.stream_id; const attr = () => stream.stream_id;
set_closest_dot_find_result($content, {attr, length: 1}); 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")); assert.ok(!$mention.hasClass("user-mention-me"));
rm.update_elements($content); rm.update_elements($content);
assert.ok($mention.hasClass("user-mention-me")); 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 // Setup
const $content = get_content_element(); const $content = get_content_element();
const $mention = $.create("mention"); const $mention = $.create("mention");
@ -188,7 +188,7 @@ run_test("user-mention Stream not subbed (wildcard)", () => {
$content.set_find_results(".user-mention", $array([$mention])); $content.set_find_results(".user-mention", $array([$mention]));
const attr = () => 1; const attr = () => 1;
set_closest_dot_find_result($content, {attr, length: 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. // Don't add user-mention-me class.
assert.ok(!$mention.hasClass("user-mention-me")); assert.ok(!$mention.hasClass("user-mention-me"));

View File

@ -21,6 +21,7 @@ const channel = mock_esm("../src/channel");
const confirm_dialog = mock_esm("../src/confirm_dialog"); const confirm_dialog = mock_esm("../src/confirm_dialog");
const dialog_widget = mock_esm("../src/dialog_widget"); const dialog_widget = mock_esm("../src/dialog_widget");
const input_pill = mock_esm("../src/input_pill"); 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 typeahead_helper = mock_esm("../src/typeahead_helper");
const user_groups = mock_esm("../src/user_groups", { const user_groups = mock_esm("../src/user_groups", {
get_user_group_from_id: noop, 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 ui_report = mock_esm("../src/ui_report");
const people = zrequire("people"); const people = zrequire("people");
const settings_data = zrequire("settings_data");
const settings_user_groups_legacy = zrequire("settings_user_groups_legacy"); const settings_user_groups_legacy = zrequire("settings_user_groups_legacy");
const user_pill = zrequire("user_pill"); const user_pill = zrequire("user_pill");
@ -49,11 +49,11 @@ function test_ui(label, f) {
run_test(label, f, {sloppy_$: true}); run_test(label, f, {sloppy_$: true});
} }
test_ui("can_edit", () => { test_ui("can_edit", ({override}) => {
settings_data.user_can_edit_user_groups = () => false; override(settings_data, "user_can_edit_user_groups", () => false);
assert.ok(!settings_user_groups_legacy.can_edit(1)); 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) => { user_groups.is_direct_member_of = (user_id, group_id) => {
assert.equal(group_id, 1); assert.equal(group_id, 1);
assert.equal(user_id, undefined); 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 description_selector = `#user-groups #${CSS.escape(1)} .description`;
const instructions_selector = `#user-groups #${CSS.escape(1)} .save-instructions`; 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 = { const realm_user_group = {
id: 1, id: 1,
name: "Mobile", name: "Mobile",
@ -112,7 +114,7 @@ test_ui("populate_user_groups", ({mock_template}) => {
people.add_active_user(alice); people.add_active_user(alice);
people.add_active_user(bob); 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]; 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; 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) { if (user_id === iago.user_id) {
return iago; return iago;
} }
@ -141,10 +143,12 @@ test_ui("populate_user_groups", ({mock_template}) => {
blueslip.expect("warn", "Undefined user in function append_user"); blueslip.expect("warn", "Undefined user in function append_user");
get_by_user_id_called = true; get_by_user_id_called = true;
return undefined; return undefined;
}; });
people.is_known_user = function () { override_rewire(
return people.get_by_user_id !== undefined && people.get_by_user_id !== noop; people,
}; "is_known_user",
() => people.get_by_user_id !== undefined && people.get_by_user_id !== noop,
);
page_params.is_admin = true; page_params.is_admin = true;
@ -266,7 +270,7 @@ test_ui("populate_user_groups", ({mock_template}) => {
}; };
let get_by_email_called = false; 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; get_by_email_called = true;
switch (user_email) { switch (user_email) {
case iago.email: case iago.email:
@ -277,7 +281,7 @@ test_ui("populate_user_groups", ({mock_template}) => {
default: default:
throw new Error("Expected user email to be of Iago or Bob here."); throw new Error("Expected user email to be of Iago or Bob here.");
} }
}; });
function test_create_item(handler) { function test_create_item(handler) {
(function test_rejection_path() { (function test_rejection_path() {
@ -331,7 +335,7 @@ test_ui("populate_user_groups", ({mock_template}) => {
"function", "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 = { const realm_user_group = {
id: 1, id: 1,
name: "Mobile", name: "Mobile",
@ -341,9 +345,8 @@ test_ui("with_external_user", ({override_rewire, mock_template}) => {
user_groups.get_realm_user_groups = () => [realm_user_group]; user_groups.get_realm_user_groups = () => [realm_user_group];
// We return [] because these are already tested, so we skip them // These are already tested, so we skip them
/* istanbul ignore next */ disallow_rewire(people, "get_realm_users");
people.get_realm_users = () => [];
mock_template( mock_template(
"settings/admin_user_group_list.hbs", "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", () => "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); override_rewire(user_pill, "append_person", noop);
@ -489,7 +492,8 @@ test_ui("reset", () => {
assert.equal(result, undefined); 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) => { mock_template("confirm_dialog/confirm_delete_user.hbs", false, (data) => {
assert.deepEqual(data, { assert.deepEqual(data, {
group_name: "Mobile", group_name: "Mobile",

View File

@ -22,7 +22,7 @@ const frontend = {
}; };
stream_data.add_sub(frontend); stream_data.add_sub(frontend);
run_test("settings", ({override}) => { run_test("settings", ({override, override_rewire}) => {
user_topics.update_user_topics( user_topics.update_user_topics(
frontend.stream_id, frontend.stream_id,
"js", "js",
@ -83,12 +83,16 @@ run_test("settings", ({override}) => {
}; };
let user_topic_visibility_policy_changed = false; let user_topic_visibility_policy_changed = false;
user_topics.set_user_topic_visibility_policy = (stream_id, topic, visibility_policy) => { override_rewire(
assert.equal(stream_id, frontend.stream_id); user_topics,
assert.equal(topic, "js"); "set_user_topic_visibility_policy",
assert.equal(visibility_policy, user_topics.all_visibility_policies.UNMUTED); (stream_id, topic, visibility_policy) => {
user_topic_visibility_policy_changed = true; 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_fake_this.value = user_topics.all_visibility_policies.UNMUTED;
topic_change_handler.call($topic_fake_this, event); topic_change_handler.call($topic_fake_this, event);
assert.ok(user_topic_visibility_policy_changed); assert.ok(user_topic_visibility_policy_changed);

View File

@ -5,24 +5,24 @@ const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("./lib/namespace"); const {mock_esm, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test"); 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_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 stream_topic_history = mock_esm("../src/stream_topic_history");
const unread = mock_esm("../src/unread"); 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"); const tg = zrequire("topic_generator");
run_test("streams", () => { run_test("streams", ({override}) => {
function assert_next_stream(curr_stream, expected) { function assert_next_stream(curr_stream, expected) {
const actual = tg.get_next_stream(curr_stream); const actual = tg.get_next_stream(curr_stream);
assert.equal(actual, expected); 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(undefined, "announce");
assert_next_stream("NOT THERE", "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 // Now test the deeper function that is wired up to
// real functions stream_data/stream_list_sort/unread. // 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 muted_stream_id = 400;
const devel_stream_id = 401; const devel_stream_id = 401;
@ -121,7 +121,7 @@ run_test("topics", ({override}) => {
}); });
run_test("get_next_unread_pm_string", ({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) => { override(unread, "num_unread_for_user_ids_string", (user_ids_string) => {
if (user_ids_string === "unk") { if (user_ids_string === "unk") {

View File

@ -768,11 +768,8 @@ test("render_emoji", ({mock_template}) => {
emoji_name: "thumbs_up", emoji_name: "thumbs_up",
emoji_code: "1f44d", emoji_code: "1f44d",
}; };
emoji.active_realm_emojis = new Map( emoji.active_realm_emojis.clear();
Object.entries({ emoji.active_realm_emojis.set("realm_emoji", "TBD");
realm_emoji: "TBD",
}),
);
mock_template("typeahead_list_item.hbs", false, (args) => { mock_template("typeahead_list_item.hbs", false, (args) => {
assert.deepEqual(args, expected_template_data); assert.deepEqual(args, expected_template_data);

View File

@ -22,7 +22,7 @@ function returns_time(secs) {
} }
run_test("basics", ({override, override_rewire}) => { run_test("basics", ({override, override_rewire}) => {
typing_status.state = null; assert.equal(typing_status.state, null);
// invalid conversation basically does nothing // invalid conversation basically does nothing
let worker = {}; let worker = {};