2021-03-15 13:03:00 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2024-08-02 15:34:06 +02:00
|
|
|
const {make_stream} = require("./lib/example_stream");
|
2024-08-02 19:50:39 +02:00
|
|
|
const {make_user} = require("./lib/example_user");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Hopefully the basic patterns for testing data-oriented modules
|
|
|
|
// are starting to become apparent. To reinforce that, we will present
|
|
|
|
// few more examples that also expose you to some of our core
|
|
|
|
// data objects. Also, we start testing some objects that have
|
|
|
|
// deeper dependencies.
|
|
|
|
|
2021-03-28 17:57:53 +02:00
|
|
|
const message_helper = zrequire("message_helper");
|
2021-03-15 13:03:00 +01:00
|
|
|
const message_store = zrequire("message_store");
|
|
|
|
const people = zrequire("people");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const stream_topic_history = zrequire("stream_topic_history");
|
|
|
|
const unread = zrequire("unread");
|
|
|
|
|
|
|
|
// It's typical to set up a little bit of data at the top of a
|
|
|
|
// test module, but you can also do this within tests. Here we
|
|
|
|
// will set up things at the top.
|
|
|
|
|
2024-08-02 19:50:39 +02:00
|
|
|
const isaac = make_user({
|
2021-03-15 13:03:00 +01:00
|
|
|
email: "isaac@example.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Isaac Newton",
|
2024-08-02 19:50:39 +02:00
|
|
|
});
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2024-07-11 13:54:09 +02:00
|
|
|
const denmark_stream = make_stream({
|
2021-03-15 13:03:00 +01:00
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 101,
|
|
|
|
subscribed: false,
|
2024-07-11 13:54:09 +02:00
|
|
|
});
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
const messages = {
|
|
|
|
isaac_to_denmark_stream: {
|
|
|
|
id: 400,
|
|
|
|
sender_id: isaac.user_id,
|
|
|
|
stream_id: denmark_stream.stream_id,
|
|
|
|
type: "stream",
|
|
|
|
flags: ["has_alert_word"],
|
2024-05-28 22:22:15 +02:00
|
|
|
subject: "copenhagen",
|
2021-03-15 13:03:00 +01:00
|
|
|
// note we don't have every field that a "real" message
|
|
|
|
// would have, and that can be fine
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// We aren't going to modify isaac in our tests, so we will
|
|
|
|
// create him at the top.
|
|
|
|
people.add_active_user(isaac);
|
|
|
|
|
|
|
|
// We are going to test a core module called messages_store.js next.
|
|
|
|
// This is an example of a deep unit test, where our dependencies
|
|
|
|
// are easy to test.
|
|
|
|
|
|
|
|
run_test("message_store", () => {
|
|
|
|
message_store.clear_for_testing();
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(denmark_stream);
|
|
|
|
|
|
|
|
const in_message = {...messages.isaac_to_denmark_stream};
|
|
|
|
|
|
|
|
assert.equal(in_message.alerted, undefined);
|
|
|
|
|
|
|
|
// Let's add a message into our message_store via
|
2021-03-28 17:57:53 +02:00
|
|
|
// message_helper.process_new_message.
|
2021-03-15 13:03:00 +01:00
|
|
|
assert.equal(message_store.get(in_message.id), undefined);
|
2021-03-28 17:57:53 +02:00
|
|
|
message_helper.process_new_message(in_message);
|
2021-03-15 13:03:00 +01:00
|
|
|
const message = message_store.get(in_message.id);
|
2024-05-28 22:07:25 +02:00
|
|
|
assert.equal(message.alerted, true);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// There are more side effects.
|
|
|
|
const topic_names = stream_topic_history.get_recent_topic_names(denmark_stream.stream_id);
|
|
|
|
assert.deepEqual(topic_names, ["copenhagen"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Tracking unread messages is a very fundamental part of the Zulip
|
|
|
|
// app, and we use the unread object to track unread messages.
|
|
|
|
|
|
|
|
run_test("unread", () => {
|
|
|
|
unread.declare_bankruptcy();
|
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(denmark_stream);
|
|
|
|
|
|
|
|
const stream_id = denmark_stream.stream_id;
|
|
|
|
const topic_name = "copenhagen";
|
|
|
|
|
|
|
|
assert.equal(unread.num_unread_for_topic(stream_id, topic_name), 0);
|
|
|
|
|
2024-05-28 22:07:25 +02:00
|
|
|
let in_message = {...messages.isaac_to_denmark_stream};
|
2024-05-28 22:22:15 +02:00
|
|
|
in_message = message_helper.process_new_message(in_message);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
unread.process_loaded_messages([in_message]);
|
|
|
|
assert.equal(unread.num_unread_for_topic(stream_id, topic_name), 1);
|
|
|
|
});
|