zjsunit: Split deprecated __Rewire__ functionality out of with_field.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-01-08 02:12:52 -08:00 committed by Steve Howell
parent 64abdc199e
commit e17ba5260a
11 changed files with 76 additions and 50 deletions

View File

@ -4,7 +4,7 @@ const {strict: assert} = require("assert");
const _ = require("lodash");
const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
@ -456,7 +456,7 @@ 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(buddy_data, "max_size_before_shrinking", 50000, () => {
with_field_rewire(buddy_data, "max_size_before_shrinking", 50000, () => {
user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
});
assert.equal(user_ids.length, 700);

View File

@ -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_field_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,9 +433,14 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.query = "@back";
fake_this.token = "back";
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
});
with_field_rewire(
compose_validate,
"warn_if_mentioning_unsubscribed_user",
unexpected_warn,
() => {
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
},
);
expected_value = "@*Backend* ";
assert.equal(actual_value, expected_value);
@ -453,9 +458,14 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.query = "@_kin";
fake_this.token = "kin";
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
});
with_field_rewire(
compose_validate,
"warn_if_mentioning_unsubscribed_user",
unexpected_warn,
() => {
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
},
);
expected_value = "@_**King Hamlet** ";
assert.equal(actual_value, expected_value);
@ -480,9 +490,14 @@ test("content_typeahead_selected", ({override_rewire}) => {
fake_this.query = "@_back";
fake_this.token = "back";
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
});
with_field_rewire(
compose_validate,
"warn_if_mentioning_unsubscribed_user",
unexpected_warn,
() => {
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
},
);
expected_value = "@_*Backend* ";
assert.equal(actual_value, expected_value);
@ -1578,7 +1593,7 @@ test("message people", ({override}) => {
};
function get_results(search_key) {
return with_field(ct, "max_num_items", 2, () =>
return with_field_rewire(ct, "max_num_items", 2, () =>
ct.get_person_suggestions(search_key, opts),
);
}

View File

@ -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, zrequire} = require("../zjsunit/namespace");
const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
@ -833,7 +833,7 @@ test("missing unicode emojis", ({override_rewire}) => {
test("katex_throws_unexpected_exceptions", () => {
blueslip.expect("error", "Error: some-exception");
const message = {raw_content: "$$a$$"};
with_field(
with_field_rewire(
markdown,
"katex",
{

View File

@ -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_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const channel = mock_esm("../../static/js/channel");
@ -151,7 +151,7 @@ run_test("read", ({override}) => {
// For testing purpose limit the batch size value to 5 instead of 1000
function send_read(messages) {
with_field(message_flags, "_unread_batch_size", 5, () => {
with_field_rewire(message_flags, "_unread_batch_size", 5, () => {
message_flags.send_read(messages);
});
}
@ -270,7 +270,7 @@ 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(message_flags, "_unread_batch_size", 5, () => {
with_field_rewire(message_flags, "_unread_batch_size", 5, () => {
message_flags.send_read(messages);
});
}

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {set_global, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
@ -129,7 +129,7 @@ run_test("muting", () => {
// `messages_filtered_for_topic_mutes` should skip filtering
// messages if `excludes_muted_topics` is false.
with_field(
with_field_rewire(
muted_topics,
"is_topic_muted",
() => {
@ -145,7 +145,7 @@ run_test("muting", () => {
// If we are in a 1:1 PM narrow, `messages_filtered_for_user_mutes` should skip
// filtering messages.
with_field(
with_field_rewire(
muted_topics,
"is_user_muted",
() => {

View File

@ -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_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {page_params} = require("../zjsunit/zpage_params");
@ -227,7 +227,7 @@ test("errors", () => {
};
// This should early return and not run pm_conversations.set_partner
with_field(
with_field_rewire(
pm_conversations,
"set_partner",
() => assert.fail(),

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {with_field, zrequire} = require("../zjsunit/namespace");
const {with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
@ -550,7 +550,7 @@ run_test("show_invalid_narrow_message", ({mock_template}) => {
run_test("narrow_to_compose_target errors", () => {
function test() {
with_field(
with_field_rewire(
narrow,
"activate",
() => {

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@ -268,7 +268,7 @@ test("ensure coverage", ({override}) => {
// where functions early exit.
override(narrow_state, "active", () => false);
with_field(
with_field_rewire(
pm_list,
"rebuild_recent",
() => {

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {user_settings} = require("../zjsunit/zpage_params");
@ -100,7 +100,7 @@ test("unknown user", ({override}) => {
// If the server is suspected to be offline or reloading,
// then we suppress errors. The use case here is that we
// haven't gotten info for a brand new user yet.
with_field(
with_field_rewire(
watchdog,
"suspects_user_is_offline",
() => true,

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
const {mock_esm, with_field_rewire, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const channel = mock_esm("../../static/js/channel");
@ -137,7 +137,7 @@ test("is_complete_for_stream_id", () => {
first: () => ({id: 5}),
};
with_field(all_messages_data, "all_messages_data", all_messages_data_stub, () => {
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);
// Now simulate a more recent message id.

View File

@ -322,29 +322,40 @@ exports.finish = function () {
exports.with_field = function (obj, field, val, f) {
if ("__esModule" in obj && "__Rewire__" in obj) {
const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);
try {
obj.__Rewire__(field, val);
return f();
} finally {
obj.__Rewire__(field, old_val);
}
} else {
const had_val = Object.prototype.hasOwnProperty.call(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];
}
throw new TypeError(
"Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
);
}
const had_val = Object.prototype.hasOwnProperty.call(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.
const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);
try {
obj.__Rewire__(field, val);
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.