Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjsunit: Add with_function_call_disallowed_rewire.
Steve Howelle9640ceb16cd
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex b98e6a18f51..8df9df0dcab 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -2,7 +2,13 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");+const {+ mock_esm,+ set_global,+ with_field_rewire,+ with_function_call_disallowed_rewire,+ zrequire,+} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params, user_settings} = require("../zjsunit/zpage_params");@@ -433,10 +439,9 @@ test("content_typeahead_selected", ({override_rewire}) => {fake_this.query = "@back";fake_this.token = "back";- with_field_rewire(+ with_function_call_disallowed_rewire(compose_validate,"warn_if_mentioning_unsubscribed_user",- unexpected_warn,() => {actual_value = ct.content_typeahead_selected.call(fake_this, backend);},@@ -452,16 +457,11 @@ test("content_typeahead_selected", ({override_rewire}) => {// silent mentionfake_this.completing = "silent_mention";- function unexpected_warn() {- throw new Error("unexpected warning about unsubscribed user");- }-fake_this.query = "@_kin";fake_this.token = "kin";- with_field_rewire(+ with_function_call_disallowed_rewire(compose_validate,"warn_if_mentioning_unsubscribed_user",- unexpected_warn,() => {actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);},@@ -490,10 +490,9 @@ test("content_typeahead_selected", ({override_rewire}) => {fake_this.query = "@_back";fake_this.token = "back";- with_field_rewire(+ with_function_call_disallowed_rewire(compose_validate,"warn_if_mentioning_unsubscribed_user",- unexpected_warn,() => {actual_value = ct.content_typeahead_selected.call(fake_this, backend);},diff --git a/frontend_tests/node_tests/message_list_data.js b/frontend_tests/node_tests/message_list_data.jsindex a9d05981596..575d859b5eb 100644--- a/frontend_tests/node_tests/message_list_data.js+++ b/frontend_tests/node_tests/message_list_data.js@@ -2,7 +2,11 @@const {strict: assert} = require("assert");-const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");+const {+ set_global,+ with_function_call_disallowed_rewire,+ zrequire,+} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");@@ -129,33 +133,17 @@ run_test("muting", () => {// `messages_filtered_for_topic_mutes` should skip filtering// messages if `excludes_muted_topics` is false.- with_field_rewire(- muted_topics,- "is_topic_muted",- () => {- throw new Error(- "Messages should not be filtered for topic mutes if excludes_muted_topics is false.",- );- },- () => {- const res = mld.messages_filtered_for_topic_mutes(msgs);- assert.deepEqual(res, msgs);- },- );+ with_function_call_disallowed_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_field_rewire(- muted_topics,- "is_user_muted",- () => {- throw new Error("Messages should not be filtered for user mutes in 1:1 PM narrows.");- },- () => {- const res = mld.messages_filtered_for_user_mutes(msgs);- assert.deepEqual(res, msgs);- },- );+ with_function_call_disallowed_rewire(muted_users, "is_user_muted", () => {+ const res = mld.messages_filtered_for_user_mutes(msgs);+ assert.deepEqual(res, msgs);+ });// Test actual behaviour of `messages_filtered_for_*` methods.mld.excludes_muted_topics = true;diff --git a/frontend_tests/node_tests/message_store.js b/frontend_tests/node_tests/message_store.jsindex 98d7564bea0..5c925050b11 100644--- a/frontend_tests/node_tests/message_store.js+++ b/frontend_tests/node_tests/message_store.js@@ -2,7 +2,12 @@const {strict: assert} = require("assert");-const {mock_esm, set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");+const {+ mock_esm,+ set_global,+ with_function_call_disallowed_rewire,+ zrequire,+} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const blueslip = require("../zjsunit/zblueslip");const {page_params} = require("../zjsunit/zpage_params");@@ -227,14 +232,9 @@ test("errors", () => {};// This should early return and not run pm_conversations.set_partner- with_field_rewire(- pm_conversations,- "set_partner",- () => assert.fail(),- () => {- pm_conversations.process_message(message);- },- );+ with_function_call_disallowed_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.jsindex cea1a064f9e..0d5b4cd63f5 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_field_rewire, zrequire} = require("../zjsunit/namespace");+const {with_function_call_disallowed_rewire, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");const $ = require("../zjsunit/zjquery");const {page_params} = require("../zjsunit/zpage_params");@@ -550,16 +550,9 @@ run_test("show_invalid_narrow_message", ({mock_template}) => {run_test("narrow_to_compose_target errors", () => {function test() {- with_field_rewire(- narrow,- "activate",- () => {- throw new Error("should not activate!");- },- () => {- narrow.to_compose_target();- },- );+ with_function_call_disallowed_rewire(narrow, "activate", () => {+ narrow.to_compose_target();+ });}// No-op when not composing.diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex eb34b9e3763..933effe0d6a 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -356,6 +356,25 @@ exports.with_field_rewire = function (obj, field, val, f) {}};+exports.with_function_call_disallowed_rewire = function (obj, field, f) {+ // This is deprecated because it relies on the slow+ // babel-plugin-rewire-ts plugin.++ if (typeof obj[field] !== "function") {+ throw new TypeError(`Expected a function for ${field}`);+ }++ const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);+ try {+ obj.__Rewire__(field, () => {+ 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.