Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

zjsunit: Merge with_field, with_…_disallowed into with_overrides.

Anders Kaseorg4d0fca8285fe
diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js
index 4ed9037fb60..78c4baedcbd 100644
--- a/frontend_tests/node_tests/activity.js
+++ b/frontend_tests/node_tests/activity.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
@@ -680,16 +680,12 @@ test("electron_bridge", ({override_rewire}) => {
override_rewire(activity, "send_presence_to_server", () => {});
function with_bridge_idle(bridge_idle, f) {
- with_field(
- window,
- "electron_bridge",
- {
+ with_overrides(({override}) => {
+ override(window, "electron_bridge", {
get_idle_on_system: () => bridge_idle,
- },
- () => {
- f();
- },
- );
+ });
+ return f();
+ });
}
with_bridge_idle(true, () => {
@@ -699,7 +695,8 @@ test("electron_bridge", ({override_rewire}) => {
assert.equal(activity.compute_active_status(), "idle");
});
- with_field(window, "electron_bridge", undefined, () => {
+ with_overrides(({override}) => {
+ override(window, "electron_bridge", undefined);
activity.mark_client_idle();
assert.equal(activity.compute_active_status(), "idle");
activity.mark_client_active();
diff --git a/frontend_tests/node_tests/billing.js b/frontend_tests/node_tests/billing.js
index 927b34bae8d..198e9780d90 100644
--- a/frontend_tests/node_tests/billing.js
+++ b/frontend_tests/node_tests/billing.js
@@ -5,7 +5,7 @@ const fs = require("fs");
const {JSDOM} = require("jsdom");
-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@@ -48,10 +48,9 @@ run_test("card_update", ({override}) => {
$.get_initialize_function()();
const update_card_click_handler = $("#update-card-button").get_on_handler("click");
- with_field(helpers, "create_ajax_request", card_change_ajax, () => {
- update_card_click_handler({preventDefault: () => {}});
- assert.ok(create_ajax_request_called);
- });
+ override(helpers, "create_ajax_request", card_change_ajax);
+ update_card_click_handler({preventDefault: () => {}});
+ assert.ok(create_ajax_request_called);
});
run_test("planchange", ({override}) => {
@@ -72,10 +71,9 @@ run_test("planchange", ({override}) => {
$.get_initialize_function()();
const change_plan_status_click_handler = $("#change-plan-status").get_on_handler("click");
- with_field(helpers, "create_ajax_request", plan_change_ajax, () => {
- change_plan_status_click_handler({preventDefault: () => {}});
- assert.ok(create_ajax_request_called);
- });
+ override(helpers, "create_ajax_request", plan_change_ajax);
+ change_plan_status_click_handler({preventDefault: () => {}});
+ assert.ok(create_ajax_request_called);
});
run_test("licensechange", ({override, override_rewire}) => {
@@ -93,10 +91,9 @@ run_test("licensechange", ({override, override_rewire}) => {
success_callback();
create_ajax_request_called = true;
}
- with_field(helpers, "create_ajax_request", license_change_ajax, () => {
- billing.create_update_license_request();
- assert.ok(create_ajax_request_called);
- });
+ override(helpers, "create_ajax_request", license_change_ajax);
+ billing.create_update_license_request();
+ assert.ok(create_ajax_request_called);
let create_update_license_request_called = false;
override_rewire(billing, "create_update_license_request", () => {
@@ -164,10 +161,9 @@ run_test("licensechange", ({override, override_rewire}) => {
success_callback();
create_ajax_request_called = true;
}
- with_field(helpers, "create_ajax_request", licenses_at_next_renewal_change_ajax, () => {
- update_next_renewal_licenses_button_click_handler({preventDefault: () => {}});
- assert.ok(create_ajax_request_called);
- });
+ override(helpers, "create_ajax_request", licenses_at_next_renewal_change_ajax);
+ update_next_renewal_licenses_button_click_handler({preventDefault: () => {}});
+ assert.ok(create_ajax_request_called);
});
run_test("billing_template", () => {
diff --git a/frontend_tests/node_tests/buddy_data.js b/frontend_tests/node_tests/buddy_data.js
index 41de0286e3e..0e6cac36dcb 100644
--- a/frontend_tests/node_tests/buddy_data.js
+++ b/frontend_tests/node_tests/buddy_data.js
@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
const _ = require("lodash");
-const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
@@ -365,7 +365,7 @@ test("muted users excluded from search", () => {
assert.ok(buddy_data.matches_filter("sel", selma.user_id));
});
-test("bulk_data_hacks", () => {
+test("bulk_data_hacks", ({override_rewire}) => {
// sanity check
assert.equal(mark.user_id, 1005);
@@ -418,9 +418,8 @@ test("bulk_data_hacks", () => {
// Make our shrink limit higher, and go back to an empty search.
// We won't get all 1000 users, just the present ones.
- with_field_rewire(buddy_data, "max_size_before_shrinking", 50000, () => {
- user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
- });
+ override_rewire(buddy_data, "max_size_before_shrinking", 50000);
+ user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
assert.equal(user_ids.length, 700);
});
diff --git a/frontend_tests/node_tests/compose_actions.js b/frontend_tests/node_tests/compose_actions.js
index e4dfbe99cd8..2d29fd8571a 100644
--- a/frontend_tests/node_tests/compose_actions.js
+++ b/frontend_tests/node_tests/compose_actions.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@@ -92,14 +92,14 @@ function override_private_message_recipient({override_rewire}) {
}
function test(label, f) {
- run_test(label, ({override, override_rewire}) => {
+ run_test(label, (helpers) => {
// We don't test the css calls; we just skip over them.
$("#compose").css = () => {};
$(".new_message_textarea").css = () => {};
people.init();
compose_state.set_message_type(false);
- f({override, override_rewire});
+ f(helpers);
});
}
@@ -318,7 +318,7 @@ test("reply_with_mention", ({override, override_rewire}) => {
assert.equal(syntax_to_insert, "@**Bob Roberts|40**");
});
-test("quote_and_reply", ({override, override_rewire}) => {
+test("quote_and_reply", ({disallow, override, override_rewire}) => {
compose_state.set_message_type("stream");
const steve = {
user_id: 90,
@@ -390,15 +390,9 @@ test("quote_and_reply", ({override, override_rewire}) => {
raw_content: "Testing.",
};
- /* istanbul ignore next */
- function whiny_get() {
- assert.fail("channel.get should not be used if raw_content is present");
- }
-
replaced = false;
- with_field(channel, "get", whiny_get, () => {
- quote_and_reply(opts);
- });
+ disallow(channel, "get");
+ quote_and_reply(opts);
assert.ok(replaced);
selected_message = {
diff --git a/frontend_tests/node_tests/compose_video.js b/frontend_tests/node_tests/compose_video.js
index 2effbd19fdf..45b34342a3c 100644
--- a/frontend_tests/node_tests/compose_video.js
+++ b/frontend_tests/node_tests/compose_video.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, zrequire, set_global} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@@ -89,19 +89,13 @@ test("videos", ({override, override_rewire}) => {
},
};
- override_rewire(
- compose_ui,
- "insert_syntax_and_focus",
- /* istanbul ignore next */
- () => {
- throw new Error("unexpected insert_syntax_and_focus call");
- },
- );
-
const handler = $("body").get_on_handler("click", ".video_link");
$("#compose-textarea").val("");
- handler(ev);
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(compose_ui, "insert_syntax_and_focus");
+ handler(ev);
+ });
})();
(function test_jitsi_video_link_compose_clicked() {
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js
index e9db4700231..50409b5cad9 100644
--- a/frontend_tests/node_tests/composebox_typeahead.js
+++ b/frontend_tests/node_tests/composebox_typeahead.js
@@ -2,13 +2,7 @@
const {strict: assert} = require("assert");
-const {
- mock_esm,
- set_global,
- with_field_rewire,
- with_function_call_disallowed_rewire,
- zrequire,
-} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
@@ -473,13 +467,10 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.query = "@back";
fake_this.token = "back";
- with_function_call_disallowed_rewire(
- compose_validate,
- "warn_if_mentioning_unsubscribed_user",
- () => {
- actual_value = ct.content_typeahead_selected.call(fake_this, backend);
- },
- );
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(compose_validate, "warn_if_mentioning_unsubscribed_user");
+ actual_value = ct.content_typeahead_selected.call(fake_this, backend);
+ });
expected_value = "@*Backend* ";
assert.equal(actual_value, expected_value);
@@ -493,13 +484,10 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.completing = "silent_mention";
fake_this.query = "@_kin";
fake_this.token = "kin";
- with_function_call_disallowed_rewire(
- compose_validate,
- "warn_if_mentioning_unsubscribed_user",
- () => {
- actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
- },
- );
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(compose_validate, "warn_if_mentioning_unsubscribed_user");
+ actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
+ });
expected_value = "@_**King Hamlet** ";
assert.equal(actual_value, expected_value);
@@ -524,13 +512,10 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.query = "@_back";
fake_this.token = "back";
- with_function_call_disallowed_rewire(
- compose_validate,
- "warn_if_mentioning_unsubscribed_user",
- () => {
- actual_value = ct.content_typeahead_selected.call(fake_this, backend);
- },
- );
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(compose_validate, "warn_if_mentioning_unsubscribed_user");
+ actual_value = ct.content_typeahead_selected.call(fake_this, backend);
+ });
expected_value = "@_*Backend* ";
assert.equal(actual_value, expected_value);
@@ -1618,7 +1603,7 @@ test("typeahead_results", () => {
assert_stream_matches("city", []);
});
-test("message people", ({override}) => {
+test("message people", ({override, override_rewire}) => {
let results;
/*
@@ -1631,6 +1616,7 @@ test("message people", ({override}) => {
let user_ids = [hal.user_id, harry.user_id];
override(message_user_ids, "user_ids", () => user_ids);
+ override_rewire(ct, "max_num_items", 2);
const opts = {
want_broadcast: false,
@@ -1638,28 +1624,22 @@ test("message people", ({override}) => {
filter_pills: false,
};
- function get_results(search_key) {
- return with_field_rewire(ct, "max_num_items", 2, () =>
- ct.get_person_suggestions(search_key, opts),
- );
- }
-
- results = get_results("Ha");
+ results = ct.get_person_suggestions("Ha", opts);
assert.deepEqual(results, [harry, hamletcharacters]);
// Now let's exclude Hal.
user_ids = [hamlet.user_id, harry.user_id];
- results = get_results("Ha");
+ results = ct.get_person_suggestions("Ha", opts);
assert.deepEqual(results, [harry, hamletcharacters]);
user_ids = [hamlet.user_id, harry.user_id, hal.user_id];
- results = get_results("Ha");
+ results = ct.get_person_suggestions("Ha", opts);
assert.deepEqual(results, [harry, hamletcharacters]);
people.deactivate(harry);
- results = get_results("Ha");
+ results = ct.get_person_suggestions("Ha", opts);
// harry is excluded since it has been deactivated.
assert.deepEqual(results, [hamletcharacters, hal]);
});
diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js
index 934466a2691..331256a999c 100644
--- a/frontend_tests/node_tests/dispatch.js
+++ b/frontend_tests/node_tests/dispatch.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
@@ -328,14 +328,10 @@ run_test("realm settings", ({override}) => {
override(notifications, "redraw_title", noop);
function test_electron_dispatch(event, fake_send_event) {
- let called = false;
-
- with_field(electron_bridge, "send_event", fake_send_event, () => {
+ with_overrides(({override}) => {
+ override(electron_bridge, "send_event", fake_send_event);
dispatch(event);
- called = true;
});
-
- assert.ok(called);
}
// realm
diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.js
index fe819e50294..deb321e1399 100644
--- a/frontend_tests/node_tests/filter.js
+++ b/frontend_tests/node_tests/filter.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@@ -533,7 +533,7 @@ test("new_style_operators", () => {
assert.ok(filter.can_bucket_by("stream"));
});
-test("public_operators", () => {
+test("public_operators", ({override}) => {
stream_data.clear_subscriptions();
let operators = [
{operator: "stream", operand: "some_stream"},
@@ -542,16 +542,14 @@ test("public_operators", () => {
];
let filter = new Filter(operators);
- with_field(page_params, "narrow_stream", undefined, () => {
- assert_same_operators(filter.public_operators(), operators);
- });
+ override(page_params, "narrow_stream", undefined);
+ assert_same_operators(filter.public_operators(), operators);
assert.ok(filter.can_bucket_by("stream"));
operators = [{operator: "stream", operand: "default"}];
filter = new Filter(operators);
- with_field(page_params, "narrow_stream", "default", () => {
- assert_same_operators(filter.public_operators(), []);
- });
+ override(page_params, "narrow_stream", "default");
+ assert_same_operators(filter.public_operators(), []);
});
test("redundancies", () => {
@@ -695,7 +693,8 @@ test("predicate_basics", () => {
assert.ok(!predicate({stream_id: unknown_stream_id, stream: "unknown"}));
assert.ok(predicate({type: "private"}));
- with_field(page_params, "narrow_stream", "kiosk", () => {
+ with_overrides(({override}) => {
+ override(page_params, "narrow_stream", "kiosk");
assert.ok(predicate({stream: "kiosk"}));
});
@@ -906,10 +905,9 @@ function test_mit_exceptions() {
assert.ok(!predicate({type: "stream", stream: "foo", topic: "bar"}));
}
-test("mit_exceptions", () => {
- with_field(page_params, "realm_is_zephyr_mirror_realm", true, () => {
- test_mit_exceptions();
- });
+test("mit_exceptions", ({override}) => {
+ override(page_params, "realm_is_zephyr_mirror_realm", true);
+ test_mit_exceptions();
});
test("predicate_edge_cases", () => {
diff --git a/frontend_tests/node_tests/hotkey.js b/frontend_tests/node_tests/hotkey.js
index 6aa250ad919..3f7e8200196 100644
--- a/frontend_tests/node_tests/hotkey.js
+++ b/frontend_tests/node_tests/hotkey.js
@@ -2,13 +2,7 @@
const {strict: assert} = require("assert");
-const {
- mock_esm,
- set_global,
- with_field,
- with_overrides,
- zrequire,
-} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test");
@@ -264,7 +258,7 @@ function test_normal_typing() {
assert_unmapped('~!@#$%^*()_+{}:"<>');
}
-run_test("allow normal typing when processing text", ({override_rewire}) => {
+run_test("allow normal typing when processing text", ({override, override_rewire}) => {
// Unmapped keys should immediately return false, without
// calling any functions outside of hotkey.js.
assert_unmapped("bfmoyz");
@@ -273,16 +267,17 @@ run_test("allow normal typing when processing text", ({override_rewire}) => {
// All letters should return false if we are composing text.
override_rewire(hotkey, "processing_text", () => true);
- for (const settings_open of [() => true, () => false]) {
- for (const is_active of [() => true, () => false]) {
- for (const info_overlay_open of [() => true, () => false]) {
- with_field(overlays, "is_active", is_active, () => {
- with_field(overlays, "settings_open", settings_open, () => {
- with_field(overlays, "info_overlay_open", info_overlay_open, () => {
- test_normal_typing();
- });
- });
- });
+ let settings_open;
+ let is_active;
+ let info_overlay_open;
+ override(overlays, "is_active", () => is_active);
+ override(overlays, "settings_open", () => settings_open);
+ override(overlays, "info_overlay_open", () => info_overlay_open);
+
+ for (settings_open of [true, false]) {
+ for (is_active of [true, false]) {
+ for (info_overlay_open of [true, false]) {
+ test_normal_typing();
}
}
}
diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.js
index b40c07cafda..4625a4016f4 100644
--- a/frontend_tests/node_tests/markdown.js
+++ b/frontend_tests/node_tests/markdown.js
@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
const markdown_test_cases = require("../../zerver/tests/fixtures/markdown_test_cases");
const markdown_assert = require("../zjsunit/markdown_assert");
-const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");
+const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
@@ -844,22 +844,15 @@ test("missing unicode emojis", ({override_rewire}) => {
assert.equal(message.content, "<p>\u{1F6B2}</p>");
});
-test("katex_throws_unexpected_exceptions", () => {
+test("katex_throws_unexpected_exceptions", ({override_rewire}) => {
const message = {raw_content: "$$a$$"};
- with_field_rewire(
- markdown,
- "katex",
- {
- renderToString: () => {
- throw new Error("some-exception");
- },
- },
- () => {
- assert.throws(() => markdown.apply_markdown(message), {
- name: "Error",
- message:
- "some-exception\nPlease report this to https://zulip.com/development-community/",
- });
+ override_rewire(markdown, "katex", {
+ renderToString: () => {
+ throw new Error("some-exception");
},
- );
+ });
+ assert.throws(() => markdown.apply_markdown(message), {
+ name: "Error",
+ message: "some-exception\nPlease report this to https://zulip.com/development-community/",
+ });
});
diff --git a/frontend_tests/node_tests/message_flags.js b/frontend_tests/node_tests/message_flags.js
index 0620e4c03c4..5eb3362444a 100644
--- a/frontend_tests/node_tests/message_flags.js
+++ b/frontend_tests/node_tests/message_flags.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const channel = mock_esm("../../static/js/channel");
@@ -151,7 +151,8 @@ run_test("read", ({override}) => {
// For testing purpose limit the batch size value to 5 instead of 1000
function send_read(messages) {
- with_field_rewire(message_flags, "_unread_batch_size", 5, () => {
+ with_overrides(({override_rewire}) => {
+ override_rewire(message_flags, "_unread_batch_size", 5);
message_flags.send_read(messages);
});
}
@@ -270,7 +271,8 @@ run_test("read_empty_data", ({override}) => {
// For testing purpose limit the batch size value to 5 instead of 1000
function send_read(messages) {
- with_field_rewire(message_flags, "_unread_batch_size", 5, () => {
+ with_overrides(({override_rewire}) => {
+ override_rewire(message_flags, "_unread_batch_size", 5);
message_flags.send_read(messages);
});
}
diff --git a/frontend_tests/node_tests/message_list_data.js b/frontend_tests/node_tests/message_list_data.js
index 4723b36b688..557c7550f27 100644
--- a/frontend_tests/node_tests/message_list_data.js
+++ b/frontend_tests/node_tests/message_list_data.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {with_function_call_disallowed_rewire, zrequire} = require("../zjsunit/namespace");
+const {with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
@@ -145,14 +145,16 @@ run_test("muting", () => {
// `messages_filtered_for_topic_mutes` should skip filtering
// messages if `excludes_muted_topics` is false.
- with_function_call_disallowed_rewire(muted_topics, "is_topic_muted", () => {
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(muted_topics, "is_topic_muted");
const res = mld.messages_filtered_for_topic_mutes(msgs);
assert.deepEqual(res, msgs);
});
// If we are in a 1:1 PM narrow, `messages_filtered_for_user_mutes` should skip
// filtering messages.
- with_function_call_disallowed_rewire(muted_users, "is_user_muted", () => {
+ with_overrides(({disallow_rewire}) => {
+ disallow_rewire(muted_users, "is_user_muted");
const res = mld.messages_filtered_for_user_mutes(msgs);
assert.deepEqual(res, msgs);
});
diff --git a/frontend_tests/node_tests/message_store.js b/frontend_tests/node_tests/message_store.js
index ca775706e08..07dae7bb2c4 100644
--- a/frontend_tests/node_tests/message_store.js
+++ b/frontend_tests/node_tests/message_store.js
@@ -2,12 +2,7 @@
const {strict: assert} = require("assert");
-const {
- mock_esm,
- set_global,
- with_function_call_disallowed_rewire,
- zrequire,
-} = require("../zjsunit/namespace");
+const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params} = require("../zjsunit/zpage_params");
@@ -93,10 +88,10 @@ function convert_recipients(people) {
}
function test(label, f) {
- run_test(label, ({override}) => {
+ run_test(label, (helpers) => {
message_store.clear_for_testing();
message_user_ids.clear_for_testing();
- f({override});
+ f(helpers);
});
}
@@ -206,7 +201,7 @@ test("message_booleans_parity", () => {
});
});
-test("errors", () => {
+test("errors", ({disallow_rewire}) => {
// Test a user that doesn't exist
let message = {
type: "private",
@@ -230,9 +225,8 @@ test("errors", () => {
};
// This should early return and not run pm_conversations.set_partner
- with_function_call_disallowed_rewire(pm_conversations, "set_partner", () => {
- pm_conversations.process_message(message);
- });
+ disallow_rewire(pm_conversations, "set_partner");
+ pm_conversations.process_message(message);
});
test("reify_message_id", () => {
diff --git a/frontend_tests/node_tests/narrow.js b/frontend_tests/node_tests/narrow.js
index ab0884da62f..898c781e227 100644
--- a/frontend_tests/node_tests/narrow.js
+++ b/frontend_tests/node_tests/narrow.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {with_function_call_disallowed_rewire, zrequire, mock_esm} = require("../zjsunit/namespace");
+const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@@ -592,21 +592,17 @@ run_test("show_invalid_narrow_message", ({mock_template}) => {
);
});
-run_test("narrow_to_compose_target errors", () => {
- function test() {
- with_function_call_disallowed_rewire(narrow, "activate", () => {
- narrow.to_compose_target();
- });
- }
+run_test("narrow_to_compose_target errors", ({disallow_rewire}) => {
+ disallow_rewire(narrow, "activate");
// No-op when not composing.
compose_state.set_message_type(false);
- test();
+ narrow.to_compose_target();
// No-op when empty stream.
compose_state.set_message_type("stream");
compose_state.stream_name("");
- test();
+ narrow.to_compose_target();
});
run_test("narrow_to_compose_target streams", ({override_rewire}) => {
diff --git a/frontend_tests/node_tests/popovers.js b/frontend_tests/node_tests/popovers.js
index a0e9c3c64a1..3d10e269aad 100644
--- a/frontend_tests/node_tests/popovers.js
+++ b/frontend_tests/node_tests/popovers.js
@@ -3,7 +3,7 @@
const {strict: assert} = require("assert");
const {$t} = require("../zjsunit/i18n");
-const {mock_esm, set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@@ -39,9 +39,7 @@ mock_esm("../../static/js/stream_popover", {
const people = zrequire("people");
const user_status = zrequire("user_status");
const message_edit = zrequire("message_edit");
-
-// Bypass some scary code that runs when we import the module.
-const popovers = with_field($.fn, "popover", noop, () => zrequire("popovers"));
+const popovers = zrequire("popovers");
const alice = {
diff --git a/frontend_tests/node_tests/rendered_markdown.js b/frontend_tests/node_tests/rendered_markdown.js
index d54f33bf67e..dfdd72e1b22 100644
--- a/frontend_tests/node_tests/rendered_markdown.js
+++ b/frontend_tests/node_tests/rendered_markdown.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_cjs, mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_cjs, mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
@@ -275,7 +275,7 @@ run_test("timestamp", ({mock_template}) => {
assert.equal($timestamp_invalid.text(), "never-been-set");
});
-run_test("timestamp-twenty-four-hour-time", ({mock_template}) => {
+run_test("timestamp-twenty-four-hour-time", ({mock_template, override}) => {
mock_template("markdown_timestamp.hbs", true, (data, html) => {
// sanity check incoming data
assert.ok(data.text.startsWith("Wed, Jul 15 2020, "));
@@ -288,18 +288,13 @@ run_test("timestamp-twenty-four-hour-time", ({mock_template}) => {
$content.set_find_results("time", $array([$timestamp]));
// We will temporarily change the 24h setting for this test.
- with_field(user_settings, "twenty_four_hour_time", true, () => {
- rm.update_elements($content);
- assert.equal($timestamp.html(), '<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 20:40\n');
- });
+ override(user_settings, "twenty_four_hour_time", true);
+ rm.update_elements($content);
+ assert.equal($timestamp.html(), '<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 20:40\n');
- with_field(user_settings, "twenty_four_hour_time", false, () => {
- rm.update_elements($content);
- assert.equal(
- $timestamp.html(),
- '<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 8:40 PM\n',
- );
- });
+ override(user_settings, "twenty_four_hour_time", false);
+ rm.update_elements($content);
+ assert.equal($timestamp.html(), '<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 8:40 PM\n');
});
run_test("timestamp-error", () => {
diff --git a/frontend_tests/node_tests/search_suggestion_future.js b/frontend_tests/node_tests/search_suggestion_future.js
index d424a3e0da9..3117b331556 100644
--- a/frontend_tests/node_tests/search_suggestion_future.js
+++ b/frontend_tests/node_tests/search_suggestion_future.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const {page_params} = require("../zjsunit/zpage_params");
@@ -919,9 +919,10 @@ test("people_suggestion (Admin only email visibility)", ({override_rewire}) => {
people_suggestion_setup();
const query = "te";
- const suggestions = with_field(page_params, "is_admin", false, () =>
- get_suggestions("", query),
- );
+ const suggestions = with_overrides(({override}) => {
+ override(page_params, "is_admin", false);
+ return get_suggestions("", query);
+ });
const expected = [
"te",
diff --git a/frontend_tests/node_tests/search_suggestion_now.js b/frontend_tests/node_tests/search_suggestion_now.js
index ab87e73bbad..b2a2497c04d 100644
--- a/frontend_tests/node_tests/search_suggestion_now.js
+++ b/frontend_tests/node_tests/search_suggestion_now.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, with_overrides, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const {page_params} = require("../zjsunit/zpage_params");
@@ -962,9 +962,10 @@ test("people_suggestion (Admin only email visibility)", ({override_rewire}) => {
people_suggestion_setup();
const query = "te";
- const suggestions = with_field(page_params, "is_admin", false, () =>
- get_suggestions("", query),
- );
+ const suggestions = with_overrides(({override}) => {
+ override(page_params, "is_admin", false);
+ return get_suggestions("", query);
+ });
const expected = [
"te",
diff --git a/frontend_tests/node_tests/stream_topic_history.js b/frontend_tests/node_tests/stream_topic_history.js
index 559c22ac127..fd07b94d80b 100644
--- a/frontend_tests/node_tests/stream_topic_history.js
+++ b/frontend_tests/node_tests/stream_topic_history.js
@@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
-const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
+const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const channel = mock_esm("../../static/js/channel");
@@ -15,10 +15,10 @@ const stream_topic_history = zrequire("stream_topic_history");
const stream_topic_history_util = zrequire("stream_topic_history_util");
function test(label, f) {
- run_test(label, ({override}) => {
+ run_test(label, (helpers) => {
unread.declare_bankruptcy();
stream_topic_history.reset();
- f({override});
+ f(helpers);
});
}
@@ -121,7 +121,7 @@ test("basics", () => {
});
});
-test("is_complete_for_stream_id", () => {
+test("is_complete_for_stream_id", ({override_rewire}) => {
const sub = {
name: "devel",
stream_id: 444,
@@ -129,30 +129,28 @@ test("is_complete_for_stream_id", () => {
};
stream_data.add_sub(sub);
- const all_messages_data_stub = {
+ override_rewire(all_messages_data, "all_messages_data", {
empty: () => false,
fetch_status: {
has_found_newest: () => true,
},
first: () => ({id: 5}),
- };
+ });
- with_field_rewire(all_messages_data, "all_messages_data", all_messages_data_stub, () => {
- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);
+ assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);
- // Now simulate a more recent message id.
- all_messages_data.all_messages_data.first = () => ({id: sub.first_message_id + 1});
+ // Now simulate a more recent message id.
+ all_messages_data.all_messages_data.first = () => ({id: sub.first_message_id + 1});
- // Note that we'll return `true` here due to
- // fetched_stream_ids having the stream_id now.
- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);
+ // Note that we'll return `true` here due to
+ // fetched_stream_ids having the stream_id now.
+ assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);
- // But now clear the data to see what we'd have without
- // the previous call.
- stream_topic_history.reset();
+ // But now clear the data to see what we'd have without
+ // the previous call.
+ stream_topic_history.reset();
- assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), false);
- });
+ assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), false);
});
test("server_history", () => {
diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.js
index 4313a2f0096..54c954e6397 100644
--- a/frontend_tests/node_tests/util.js
+++ b/frontend_tests/node_tests/util.js
@@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
const _ = require("lodash");
-const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
+const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
set_global("document", {});
@@ -100,36 +100,27 @@ run_test("same_recipient", () => {
assert.ok(!util.same_recipient(undefined, undefined));
});
-run_test("robust_uri_decode", () => {
+run_test("robust_uri_decode", ({override}) => {
assert.equal(util.robust_uri_decode("xxx%3Ayyy"), "xxx:yyy");
assert.equal(util.robust_uri_decode("xxx%3"), "xxx");
- let error_message;
- with_field(
- global,
- "decodeURIComponent",
- () => {
- throw new Error("foo");
- },
+ override(global, "decodeURIComponent", () => {
+ throw new Error("foo");
+ });
+ assert.throws(
() => {
- try {
- util.robust_uri_decode("%E0%A4%A");
- } catch (error) {
- error_message = error.message;
- }
+ util.robust_uri_decode("%E0%A4%A");
},
+ {name: "Error", message: "foo"},
);
-
- assert.equal(error_message, "foo");
});
-run_test("dumb_strcmp", () => {
- with_field(Intl, "Collator", undefined, () => {
- const strcmp = util.make_strcmp();
- assert.equal(strcmp("a", "b"), -1);
- assert.equal(strcmp("c", "c"), 0);
- assert.equal(strcmp("z", "y"), 1);
- });
+run_test("dumb_strcmp", ({override}) => {
+ override(Intl, "Collator", undefined);
+ const strcmp = util.make_strcmp();
+ assert.equal(strcmp("a", "b"), -1);
+ assert.equal(strcmp("c", "c"), 0);
+ assert.equal(strcmp("z", "y"), 1);
});
run_test("get_edit_event_orig_topic", () => {
diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.js
index 6d89d4916b2..b7ad9ffd5a8 100644
--- a/frontend_tests/zjsunit/namespace.js
+++ b/frontend_tests/zjsunit/namespace.js
@@ -313,180 +313,142 @@ exports.finish = function () {
new_globals.clear();
};
-exports.with_field = function (obj, field, val, f) {
- assert.ok(
- !("__esModule" in obj && "__Rewire__" in obj),
- "Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
- );
-
- const had_val = Object.hasOwn(obj, field);
- const old_val = obj[field];
- try {
- obj[field] = val;
- return f();
- } finally {
- if (had_val) {
- obj[field] = old_val;
- } else {
- delete obj[field];
- }
- }
-};
-
-exports.with_field_rewire = function (obj, field, val, f) {
- // This is deprecated because it relies on the slow
- // babel-plugin-rewire-ts plugin. Consider alternatives such
- // as exporting a helper function for tests from the module
- // containing the function you need to mock.
-
- // https://github.com/rosswarren/babel-plugin-rewire-ts/issues/15
- const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);
-
- assert.notEqual(
- typeof old_val,
- "function",
- "Please try to avoid mocking here, or use override_rewire.",
- );
-
- try {
- obj.__Rewire__(field, val);
- return f();
- } finally {
- obj.__Rewire__(field, old_val);
- }
-};
-
-exports.with_function_call_disallowed_rewire = function (obj, field, f) {
- // This is deprecated because it relies on the slow
- // babel-plugin-rewire-ts plugin.
-
- // https://github.com/rosswarren/babel-plugin-rewire-ts/issues/15
- const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);
-
- assert.equal(typeof old_val, "function", `Expected a function for ${field}`);
-
- try {
- obj.__Rewire__(
- field,
- /* istanbul ignore next */
- () => {
- throw new Error(`unexpected call to ${field}`);
- },
- );
- return f();
- } finally {
- obj.__Rewire__(field, old_val);
- }
-};
-
exports.with_overrides = function (test_function) {
// This function calls test_function() and passes in
// a way to override the namespace temporarily.
const restore_callbacks = [];
- const unused_funcs = new Map();
- const override = function (obj, func_name, f) {
+ let ok = false;
+
+ const override = function (obj, prop, value, {unused = true} = {}) {
// Given an object `obj` (which is usually a module object),
- // we re-map `obj[func_name]` to the `f` passed in by the caller.
+ // we re-map `obj[prop]` to the `value` passed in by the caller.
// Then the outer function here (`with_overrides`) automatically
- // restores the original value of `obj[func_name]` as its last
+ // restores the original value of `obj[prop]` as its last
// step. Generally our code calls `run_test`, which wraps
// `with_overrides`.
- assert.ok(
- !("__esModule" in obj && "__Rewire__" in obj),
- "Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
- );
-
- assert.equal(
- typeof f,
- "function",
- "You can only override with a function. Use with_field for non-functions.",
- );
-
assert.ok(
typeof obj === "object" || typeof obj === "function",
`We cannot override a function for ${typeof obj} objects`,
);
assert.ok(
- obj[func_name] === undefined || typeof obj[func_name] === "function",
- `
- You are overriding a non-function with a function.
- This is almost certainly an error.
- `,
+ !("__esModule" in obj && "__Rewire__" in obj),
+ "Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
);
- if (!unused_funcs.has(obj)) {
- unused_funcs.set(obj, new Map());
+ const had_value = Object.hasOwn(obj, prop);
+ const old_value = obj[prop];
+ let new_value = value;
+
+ if (typeof value === "function") {
+ assert.ok(
+ old_value === undefined || typeof old_value === "function",
+ `
+ You are overriding a non-function with a function.
+ This is almost certainly an error.
+ `,
+ );
+
+ new_value = function (...args) {
+ unused = false;
+ return value.apply(this, args);
+ };
+
+ // Let zjquery know this function was patched with override,
+ // so it doesn't complain about us modifying it. (Other
+ // code can also use this, as needed.)
+ new_value._patched_with_override = true;
+ } else {
+ unused = false;
}
- unused_funcs.get(obj).set(func_name, true);
-
- const old_f = obj[func_name];
- const new_f = function (...args) {
- unused_funcs.get(obj).delete(func_name);
- return f.apply(this, args);
- };
-
- // Let zjquery know this function was patched with override,
- // so it doesn't complain about us modifying it. (Other
- // code can also use this, as needed.)
- new_f._patched_with_override = true;
-
- obj[func_name] = new_f;
+ obj[prop] = new_value;
restore_callbacks.push(() => {
- obj[func_name] = old_f;
+ if (ok) {
+ assert.ok(!unused, `${prop} never got invoked!`);
+ }
+ if (had_value) {
+ obj[prop] = old_value;
+ } else {
+ delete obj[prop];
+ }
});
};
- const override_rewire = function (obj, func_name, f) {
+ const disallow = function (obj, prop) {
+ override(
+ obj,
+ prop,
+ // istanbul ignore next
+ () => {
+ throw new Error(`unexpected call to ${prop}`);
+ },
+ {unused: false},
+ );
+ };
+
+ const override_rewire = function (obj, prop, value, {unused = true} = {}) {
// This is deprecated because it relies on the slow
// babel-plugin-rewire-ts plugin. Consider alternatives such
// as exporting a helper function for tests from the module
// containing the function you need to mock.
- assert.equal(
- typeof f,
- "function",
- "You can only override with a function. Use with_field for non-functions.",
- );
-
assert.ok(
typeof obj === "object" || typeof obj === "function",
`We cannot override a function for ${typeof obj} objects`,
);
- assert.ok(
- obj[func_name] === undefined || typeof obj[func_name] === "function",
- `
- You are overriding a non-function with a function.
- This is almost certainly an error.
- `,
- );
-
- if (!unused_funcs.has(obj)) {
- unused_funcs.set(obj, new Map());
- }
-
- unused_funcs.get(obj).set(func_name, true);
-
// https://github.com/rosswarren/babel-plugin-rewire-ts/issues/15
- const old_f = func_name in obj ? obj[func_name] : obj.__GetDependency__(func_name);
-
- const new_f = function (...args) {
- unused_funcs.get(obj).delete(func_name);
- return f.apply(this, args);
- };
+ const old_value = prop in obj ? obj[prop] : obj.__GetDependency__(prop);
+ let new_value = value;
+
+ if (typeof value === "function") {
+ assert.ok(
+ obj[prop] === undefined || typeof obj[prop] === "function",
+ `
+ You are overriding a non-function with a function.
+ This is almost certainly an error.
+ `,
+ );
+
+ new_value = function (...args) {
+ unused = false;
+ return value.apply(this, args);
+ };
+ } else {
+ unused = false;
+ }
- obj.__Rewire__(func_name, new_f);
+ obj.__Rewire__(prop, new_value);
restore_callbacks.push(() => {
- obj.__Rewire__(func_name, old_f);
+ if (ok) {
+ assert.ok(!unused, `${prop} never got invoked!`);
+ }
+ obj.__Rewire__(prop, old_value);
});
};
+ const disallow_rewire = function (obj, prop) {
+ // This is deprecated because it relies on the slow
+ // babel-plugin-rewire-ts plugin.
+
+ override_rewire(
+ obj,
+ prop,
+ // istanbul ignore next
+ () => {
+ throw new Error(`unexpected call to ${prop}`);
+ },
+ {unused: false},
+ );
+ };
+
+ let ret;
try {
- test_function({override, override_rewire});
+ ret = test_function({override, override_rewire, disallow, disallow_rewire});
+ ok = true;
} finally {
restore_callbacks.reverse();
for (const restore_callback of restore_callbacks) {
@@ -494,10 +456,5 @@ exports.with_overrides = function (test_function) {
}
}
- for (const module_unused_funcs of unused_funcs.values()) {
- for (const unused_name of module_unused_funcs.keys()) {
- /* istanbul ignore next */
- throw new Error(unused_name + " never got invoked!");
- }
- }
+ return ret;
};
diff --git a/frontend_tests/zjsunit/test.js b/frontend_tests/zjsunit/test.js
index 1b605eef8f5..8207a69fb32 100644
--- a/frontend_tests/zjsunit/test.js
+++ b/frontend_tests/zjsunit/test.js
@@ -31,8 +31,11 @@ exports.run_test = (label, f, opts) => {
try {
namespace._start_template_mocking();
- namespace.with_overrides(({override, override_rewire}) => {
- f({override, override_rewire, mock_template: namespace._mock_template});
+ namespace.with_overrides((helpers) => {
+ f({
+ ...helpers,
+ mock_template: namespace._mock_template,
+ });
});
namespace._finish_template_mocking();
} catch (error) /* istanbul ignore next */ {