node tests: Introduce message_store.create_mock_message() helper.

Previously, it was tedious to create actual message
objects in message_store for use in node tests.
This was mainly because, `add_message_metadata`
in message_store has many dependencies and
validation checks. Since it was difficult to create
actual message objects, many tests just mocked
the `message_store.get()` method to return the desired
message.

This commit adds a new helper method (`create_mock_message`)
to message_store, for use in node tests. This just stores
the object passed to it in the `stores_messages` map,
without any validation. We do not add any
default fields to the message object before saving
it from this helper, because doing so would decrease
the utility of this helper, and, if a test
depends on some field having a particular value,
then it would be better to just pass the field: value
pair from the test itself, for readability, rather
than relying on the helper to add the field for us.

This helper allows us to write deeper tests.

This commit also replaces some instances of mocking
`message_store.get()` to use this new helper method.
This commit is contained in:
Abhijeet Prasad Bodas 2021-03-05 11:04:11 +05:30 committed by Tim Abbott
parent aa0e5dd35b
commit 206fe1ef3b
3 changed files with 15 additions and 20 deletions

View File

@ -6,13 +6,13 @@ const {mock_esm, set_global, 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 message_store = mock_esm("../../static/js/message_store");
mock_esm("../../static/js/muting", { mock_esm("../../static/js/muting", {
is_topic_muted: () => false, is_topic_muted: () => false,
}); });
set_global("page_params", {}); set_global("page_params", {});
const {Filter} = zrequire("../js/filter"); const {Filter} = zrequire("../js/filter");
const message_store = zrequire("message_store");
const people = zrequire("people"); const people = zrequire("people");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
const unread = zrequire("unread"); const unread = zrequire("unread");
@ -71,14 +71,8 @@ run_test("get_unread_ids", () => {
display_recipient: [{id: alice.user_id}], display_recipient: [{id: alice.user_id}],
}; };
message_store.get = (msg_id) => { message_store.create_mock_message(stream_msg);
if (msg_id === stream_msg.id) { message_store.create_mock_message(private_msg);
return stream_msg;
} else if (msg_id === private_msg.id) {
return private_msg;
}
throw new Error("unexpected id");
};
stream_data.add_sub(sub); stream_data.add_sub(sub);

View File

@ -4,22 +4,20 @@ const {strict: assert} = require("assert");
const _ = require("lodash"); const _ = require("lodash");
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace"); const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
let page_params = set_global("page_params", { let page_params = set_global("page_params", {
realm_push_notifications_enabled: false, realm_push_notifications_enabled: false,
}); });
const message_store = mock_esm("../../static/js/message_store", {
get() {},
});
const muting = zrequire("muting"); const muting = zrequire("muting");
set_global("current_msg_list", {}); set_global("current_msg_list", {});
set_global("home_msg_list", {}); set_global("home_msg_list", {});
const {FoldDict} = zrequire("fold_dict"); const {FoldDict} = zrequire("fold_dict");
const message_store = zrequire("message_store");
const people = zrequire("people"); const people = zrequire("people");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
const unread = zrequire("unread"); const unread = zrequire("unread");
@ -86,7 +84,7 @@ test("empty_counts_while_home", () => {
test_notifiable_count(counts.home_unread_messages, 0); test_notifiable_count(counts.home_unread_messages, 0);
}); });
test("changing_topics", (override) => { test("changing_topics", () => {
// Summary: change the topic of a message from 'lunch' // Summary: change the topic of a message from 'lunch'
// to 'dinner' using update_unread_topics(). // to 'dinner' using update_unread_topics().
let count = unread.num_unread_for_topic(social.stream_id, "lunch"); let count = unread.num_unread_for_topic(social.stream_id, "lunch");
@ -185,12 +183,9 @@ test("changing_topics", (override) => {
unread: true, unread: true,
}; };
const message_dict = new Map(); message_store.create_mock_message(message);
message_dict.set(message.id, message); message_store.create_mock_message(other_message);
message_dict.set(other_message.id, other_message); message_store.create_mock_message(sticky_message);
message_dict.set(sticky_message.id, sticky_message);
override(message_store, "get", (msg_id) => message_dict.get(msg_id));
unread.process_loaded_messages([sticky_message]); unread.process_loaded_messages([sticky_message]);
count = unread.num_unread_for_topic(stream_id, "sticky"); count = unread.num_unread_for_topic(stream_id, "sticky");

View File

@ -33,6 +33,12 @@ export function user_ids() {
return Array.from(message_user_ids); return Array.from(message_user_ids);
} }
export function create_mock_message(message) {
// For use in tests only. `id` is a required field,
// everything else is optional, as required in the test.
stored_messages.set(message.id, message);
}
export function get(message_id) { export function get(message_id) {
if (message_id === undefined || message_id === null) { if (message_id === undefined || message_id === null) {
blueslip.error("message_store.get got bad value: " + message_id); blueslip.error("message_store.get got bad value: " + message_id);