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 _ = 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 {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
const {page_params, user_settings} = require("../zjsunit/zpage_params");
|
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.
|
// Make our shrink limit higher, and go back to an empty search.
|
||||||
// We won't get all 1000 users, just the present ones.
|
// 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("");
|
user_ids = buddy_data.get_filtered_and_sorted_user_ids("");
|
||||||
});
|
});
|
||||||
assert.equal(user_ids.length, 700);
|
assert.equal(user_ids.length, 700);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const $ = require("../zjsunit/zjquery");
|
const $ = require("../zjsunit/zjquery");
|
||||||
const {page_params, user_settings} = require("../zjsunit/zpage_params");
|
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.query = "@back";
|
||||||
fake_this.token = "back";
|
fake_this.token = "back";
|
||||||
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
|
with_field_rewire(
|
||||||
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
|
compose_validate,
|
||||||
});
|
"warn_if_mentioning_unsubscribed_user",
|
||||||
|
unexpected_warn,
|
||||||
|
() => {
|
||||||
|
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
|
||||||
|
},
|
||||||
|
);
|
||||||
expected_value = "@*Backend* ";
|
expected_value = "@*Backend* ";
|
||||||
assert.equal(actual_value, expected_value);
|
assert.equal(actual_value, expected_value);
|
||||||
|
|
||||||
|
@ -453,9 +458,14 @@ test("content_typeahead_selected", ({override_rewire}) => {
|
||||||
|
|
||||||
fake_this.query = "@_kin";
|
fake_this.query = "@_kin";
|
||||||
fake_this.token = "kin";
|
fake_this.token = "kin";
|
||||||
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
|
with_field_rewire(
|
||||||
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
|
compose_validate,
|
||||||
});
|
"warn_if_mentioning_unsubscribed_user",
|
||||||
|
unexpected_warn,
|
||||||
|
() => {
|
||||||
|
actual_value = ct.content_typeahead_selected.call(fake_this, hamlet);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
expected_value = "@_**King Hamlet** ";
|
expected_value = "@_**King Hamlet** ";
|
||||||
assert.equal(actual_value, expected_value);
|
assert.equal(actual_value, expected_value);
|
||||||
|
@ -480,9 +490,14 @@ test("content_typeahead_selected", ({override_rewire}) => {
|
||||||
|
|
||||||
fake_this.query = "@_back";
|
fake_this.query = "@_back";
|
||||||
fake_this.token = "back";
|
fake_this.token = "back";
|
||||||
with_field(compose_validate, "warn_if_mentioning_unsubscribed_user", unexpected_warn, () => {
|
with_field_rewire(
|
||||||
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
|
compose_validate,
|
||||||
});
|
"warn_if_mentioning_unsubscribed_user",
|
||||||
|
unexpected_warn,
|
||||||
|
() => {
|
||||||
|
actual_value = ct.content_typeahead_selected.call(fake_this, backend);
|
||||||
|
},
|
||||||
|
);
|
||||||
expected_value = "@_*Backend* ";
|
expected_value = "@_*Backend* ";
|
||||||
assert.equal(actual_value, expected_value);
|
assert.equal(actual_value, expected_value);
|
||||||
|
|
||||||
|
@ -1578,7 +1593,7 @@ test("message people", ({override}) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
function get_results(search_key) {
|
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),
|
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_test_cases = require("../../zerver/tests/fixtures/markdown_test_cases");
|
||||||
const markdown_assert = require("../zjsunit/markdown_assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
const {page_params, user_settings} = require("../zjsunit/zpage_params");
|
const {page_params, user_settings} = require("../zjsunit/zpage_params");
|
||||||
|
@ -833,7 +833,7 @@ test("missing unicode emojis", ({override_rewire}) => {
|
||||||
test("katex_throws_unexpected_exceptions", () => {
|
test("katex_throws_unexpected_exceptions", () => {
|
||||||
blueslip.expect("error", "Error: some-exception");
|
blueslip.expect("error", "Error: some-exception");
|
||||||
const message = {raw_content: "$$a$$"};
|
const message = {raw_content: "$$a$$"};
|
||||||
with_field(
|
with_field_rewire(
|
||||||
markdown,
|
markdown,
|
||||||
"katex",
|
"katex",
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
|
|
||||||
const channel = mock_esm("../../static/js/channel");
|
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
|
// For testing purpose limit the batch size value to 5 instead of 1000
|
||||||
function send_read(messages) {
|
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);
|
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
|
// For testing purpose limit the batch size value to 5 instead of 1000
|
||||||
function send_read(messages) {
|
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);
|
message_flags.send_read(messages);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
|
|
||||||
|
@ -129,7 +129,7 @@ run_test("muting", () => {
|
||||||
|
|
||||||
// `messages_filtered_for_topic_mutes` should skip filtering
|
// `messages_filtered_for_topic_mutes` should skip filtering
|
||||||
// messages if `excludes_muted_topics` is false.
|
// messages if `excludes_muted_topics` is false.
|
||||||
with_field(
|
with_field_rewire(
|
||||||
muted_topics,
|
muted_topics,
|
||||||
"is_topic_muted",
|
"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
|
// If we are in a 1:1 PM narrow, `messages_filtered_for_user_mutes` should skip
|
||||||
// filtering messages.
|
// filtering messages.
|
||||||
with_field(
|
with_field_rewire(
|
||||||
muted_topics,
|
muted_topics,
|
||||||
"is_user_muted",
|
"is_user_muted",
|
||||||
() => {
|
() => {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
const {page_params} = require("../zjsunit/zpage_params");
|
const {page_params} = require("../zjsunit/zpage_params");
|
||||||
|
@ -227,7 +227,7 @@ test("errors", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// This should early return and not run pm_conversations.set_partner
|
// This should early return and not run pm_conversations.set_partner
|
||||||
with_field(
|
with_field_rewire(
|
||||||
pm_conversations,
|
pm_conversations,
|
||||||
"set_partner",
|
"set_partner",
|
||||||
() => assert.fail(),
|
() => assert.fail(),
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const $ = require("../zjsunit/zjquery");
|
const $ = require("../zjsunit/zjquery");
|
||||||
const {page_params} = require("../zjsunit/zpage_params");
|
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", () => {
|
run_test("narrow_to_compose_target errors", () => {
|
||||||
function test() {
|
function test() {
|
||||||
with_field(
|
with_field_rewire(
|
||||||
narrow,
|
narrow,
|
||||||
"activate",
|
"activate",
|
||||||
() => {
|
() => {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const $ = require("../zjsunit/zjquery");
|
const $ = require("../zjsunit/zjquery");
|
||||||
|
|
||||||
|
@ -268,7 +268,7 @@ test("ensure coverage", ({override}) => {
|
||||||
// where functions early exit.
|
// where functions early exit.
|
||||||
override(narrow_state, "active", () => false);
|
override(narrow_state, "active", () => false);
|
||||||
|
|
||||||
with_field(
|
with_field_rewire(
|
||||||
pm_list,
|
pm_list,
|
||||||
"rebuild_recent",
|
"rebuild_recent",
|
||||||
() => {
|
() => {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
const {user_settings} = require("../zjsunit/zpage_params");
|
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,
|
// If the server is suspected to be offline or reloading,
|
||||||
// then we suppress errors. The use case here is that we
|
// then we suppress errors. The use case here is that we
|
||||||
// haven't gotten info for a brand new user yet.
|
// haven't gotten info for a brand new user yet.
|
||||||
with_field(
|
with_field_rewire(
|
||||||
watchdog,
|
watchdog,
|
||||||
"suspects_user_is_offline",
|
"suspects_user_is_offline",
|
||||||
() => true,
|
() => true,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const {strict: assert} = require("assert");
|
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 {run_test} = require("../zjsunit/test");
|
||||||
|
|
||||||
const channel = mock_esm("../../static/js/channel");
|
const channel = mock_esm("../../static/js/channel");
|
||||||
|
@ -137,7 +137,7 @@ test("is_complete_for_stream_id", () => {
|
||||||
first: () => ({id: 5}),
|
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);
|
assert.equal(stream_topic_history.is_complete_for_stream_id(sub.stream_id), true);
|
||||||
|
|
||||||
// Now simulate a more recent message id.
|
// Now simulate a more recent message id.
|
||||||
|
|
|
@ -322,29 +322,40 @@ exports.finish = function () {
|
||||||
|
|
||||||
exports.with_field = function (obj, field, val, f) {
|
exports.with_field = function (obj, field, val, f) {
|
||||||
if ("__esModule" in obj && "__Rewire__" in obj) {
|
if ("__esModule" in obj && "__Rewire__" in obj) {
|
||||||
const old_val = field in obj ? obj[field] : obj.__GetDependency__(field);
|
throw new TypeError(
|
||||||
try {
|
"Cannot mutate an ES module from outside. Consider exporting a test helper function from it instead.",
|
||||||
obj.__Rewire__(field, val);
|
);
|
||||||
return f();
|
}
|
||||||
} finally {
|
|
||||||
obj.__Rewire__(field, old_val);
|
const had_val = Object.prototype.hasOwnProperty.call(obj, field);
|
||||||
}
|
const old_val = obj[field];
|
||||||
} else {
|
try {
|
||||||
const had_val = Object.prototype.hasOwnProperty.call(obj, field);
|
obj[field] = val;
|
||||||
const old_val = obj[field];
|
return f();
|
||||||
try {
|
} finally {
|
||||||
obj[field] = val;
|
if (had_val) {
|
||||||
return f();
|
obj[field] = old_val;
|
||||||
} finally {
|
} else {
|
||||||
if (had_val) {
|
delete obj[field];
|
||||||
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) {
|
exports.with_overrides = function (test_function) {
|
||||||
// This function calls test_function() and passes in
|
// This function calls test_function() and passes in
|
||||||
// a way to override the namespace temporarily.
|
// a way to override the namespace temporarily.
|
||||||
|
|
Loading…
Reference in New Issue