2021-03-15 13:03:00 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-06-01 01:35:55 +02:00
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-06-01 01:35:55 +02:00
|
|
|
const $ = require("./lib/zjquery");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Let's continue to explore how we can test complex
|
|
|
|
interactions in the real code.
|
|
|
|
|
|
|
|
When a new message comes in, we update the three major
|
|
|
|
panes of the app:
|
|
|
|
|
|
|
|
* left sidebar - stream list
|
|
|
|
* middle pane - message view
|
|
|
|
* right sidebar - buddy list (aka "activity" list)
|
|
|
|
|
|
|
|
These are reflected by the following calls:
|
|
|
|
|
|
|
|
stream_list.update_streams_sidebar
|
|
|
|
message_util.add_new_messages
|
|
|
|
activity.process_loaded_messages
|
|
|
|
|
|
|
|
For now, though, let's focus on another side effect
|
|
|
|
of processing incoming messages:
|
|
|
|
|
|
|
|
unread_ops.process_visible
|
|
|
|
|
|
|
|
When new messages come in, they are often immediately
|
|
|
|
visible to users, so the app will communicate this
|
|
|
|
back to the server by calling unread_ops.process_visible.
|
|
|
|
|
|
|
|
In order to unit test this, we don't want to require
|
|
|
|
an actual server to be running. Instead, this example
|
|
|
|
will stub many of the "boundaries" to focus on the
|
|
|
|
core behavior.
|
|
|
|
|
|
|
|
The two key pieces here are as follows:
|
|
|
|
|
|
|
|
* Use mock_esm to avoid compiling the "real"
|
|
|
|
modules that are immaterial to our current
|
|
|
|
testing concerns.
|
|
|
|
|
|
|
|
* Use override(...) to simulate how we want
|
|
|
|
methods to behave. (Often we want them to
|
|
|
|
do nothing at all or return a simple
|
|
|
|
value.)
|
|
|
|
*/
|
|
|
|
|
2023-06-01 01:35:55 +02:00
|
|
|
set_global("document", {hasFocus: () => true});
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const channel = mock_esm("../src/channel");
|
2023-10-07 00:15:22 +02:00
|
|
|
const desktop_notifications = mock_esm("../src/desktop_notifications");
|
2023-02-22 23:04:10 +01:00
|
|
|
const message_lists = mock_esm("../src/message_lists");
|
|
|
|
const message_viewport = mock_esm("../src/message_viewport");
|
|
|
|
const unread_ui = mock_esm("../src/unread_ui");
|
2021-03-15 13:03:00 +01:00
|
|
|
|
2023-04-21 18:02:38 +02:00
|
|
|
message_lists.current = {view: {}};
|
|
|
|
message_lists.home = {view: {}};
|
2022-08-17 07:10:19 +02:00
|
|
|
message_lists.all_rendered_message_lists = () => [message_lists.home, message_lists.current];
|
2021-03-30 02:21:21 +02:00
|
|
|
|
2021-03-15 13:03:00 +01:00
|
|
|
const message_store = zrequire("message_store");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const unread = zrequire("unread");
|
|
|
|
const unread_ops = zrequire("unread_ops");
|
|
|
|
|
|
|
|
const denmark_stream = {
|
|
|
|
color: "blue",
|
|
|
|
name: "Denmark",
|
|
|
|
stream_id: 101,
|
|
|
|
subscribed: false,
|
|
|
|
};
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("unread_ops", ({override}) => {
|
2021-03-15 13:03:00 +01:00
|
|
|
stream_data.clear_subscriptions();
|
|
|
|
stream_data.add_sub(denmark_stream);
|
|
|
|
message_store.clear_for_testing();
|
|
|
|
unread.declare_bankruptcy();
|
|
|
|
|
|
|
|
const message_id = 50;
|
|
|
|
const test_messages = [
|
|
|
|
{
|
|
|
|
id: message_id,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: denmark_stream.stream_id,
|
|
|
|
topic: "copenhagen",
|
|
|
|
unread: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// Make our test message appear to be unread, so that
|
|
|
|
// we then need to subsequently process them as read.
|
2022-10-14 17:37:47 +02:00
|
|
|
message_store.update_message_cache(test_messages[0]);
|
2021-03-15 13:03:00 +01:00
|
|
|
unread.process_loaded_messages(test_messages);
|
|
|
|
|
2021-04-07 14:08:09 +02:00
|
|
|
// Make our message_viewport appear visible.
|
2023-06-01 01:35:55 +02:00
|
|
|
$("#message_feed_container").show();
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Make our "test" message appear visible.
|
|
|
|
override(message_viewport, "bottom_message_visible", () => true);
|
|
|
|
|
2021-03-30 02:21:21 +02:00
|
|
|
// Set message_lists.current containing messages that can be marked read
|
|
|
|
override(message_lists.current, "all_messages", () => test_messages);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Ignore these interactions for now:
|
2023-12-14 23:51:33 +01:00
|
|
|
override(message_lists.current.view, "show_message_as_read", noop);
|
|
|
|
override(message_lists.home.view, "show_message_as_read", noop);
|
|
|
|
override(desktop_notifications, "close_notification", noop);
|
|
|
|
override(unread_ui, "update_unread_counts", noop);
|
|
|
|
override(unread_ui, "notify_messages_remain_unread", noop);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// Set up a way to capture the options passed in to channel.post.
|
|
|
|
let channel_post_opts;
|
|
|
|
override(channel, "post", (opts) => {
|
|
|
|
channel_post_opts = opts;
|
|
|
|
});
|
|
|
|
|
|
|
|
let can_mark_messages_read;
|
|
|
|
|
|
|
|
// Set up an override to point to the above var, so we can
|
|
|
|
// toggle it easily from within the test (and avoid complicated
|
|
|
|
// data setup).
|
2021-03-30 02:21:21 +02:00
|
|
|
override(message_lists.current, "can_mark_messages_read", () => can_mark_messages_read);
|
2021-08-02 20:09:56 +02:00
|
|
|
override(message_lists.current, "has_unread_messages", () => true);
|
2021-03-15 13:03:00 +01:00
|
|
|
|
|
|
|
// First, test for a message list that cannot read messages.
|
|
|
|
can_mark_messages_read = false;
|
|
|
|
unread_ops.process_visible();
|
|
|
|
|
|
|
|
assert.deepEqual(channel_post_opts, undefined);
|
|
|
|
|
|
|
|
// Now flip the boolean, and get to the main thing we are testing.
|
|
|
|
can_mark_messages_read = true;
|
|
|
|
unread_ops.process_visible();
|
|
|
|
|
|
|
|
// The most important side effect of the above call is that
|
|
|
|
// we post info to the server. We can verify that the correct
|
|
|
|
// url and parameters are specified:
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
|
|
|
url: "/json/messages/flags",
|
|
|
|
data: {messages: "[50]", op: "add", flag: "read"},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Simulate a successful post (which also clears the queue
|
|
|
|
// in our message_flag code).
|
|
|
|
channel_post_opts.success({messages: [message_id]});
|
|
|
|
});
|