mirror of https://github.com/zulip/zulip.git
zjsunit: Split deprecated __Rewire__ functionality out of with_field.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
64abdc199e
commit
e17ba5260a
|
@ -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);
|
||||
|
|
|
@ -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, () => {
|
||||
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, () => {
|
||||
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, () => {
|
||||
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),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
() => {
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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",
|
||||
() => {
|
||||
|
|
|
@ -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",
|
||||
() => {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -322,14 +322,11 @@ 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);
|
||||
throw new TypeError(
|
||||
"Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
||||
const had_val = Object.prototype.hasOwnProperty.call(obj, field);
|
||||
const old_val = obj[field];
|
||||
try {
|
||||
|
@ -342,6 +339,20 @@ exports.with_field = function (obj, field, val, f) {
|
|||
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue