2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
2020-12-01 00:02:16 +01:00
|
|
|
const {reset_module, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:12:33 +01:00
|
|
|
const {make_zjquery} = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("message_util");
|
2020-04-16 20:47:18 +02:00
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
const noop = () => {};
|
2020-07-15 00:34:28 +02:00
|
|
|
set_global(
|
|
|
|
"$",
|
2020-12-01 00:12:33 +01:00
|
|
|
make_zjquery({
|
2020-07-15 00:34:28 +02:00
|
|
|
silent: true,
|
|
|
|
}),
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("hashchange", {
|
2020-06-01 17:17:41 +02:00
|
|
|
exit_overlay: noop,
|
2020-04-08 13:59:56 +02:00
|
|
|
});
|
2021-02-10 04:53:22 +01:00
|
|
|
const overlays = set_global("overlays", {
|
2020-04-08 13:59:56 +02:00
|
|
|
open_overlay: (opts) => {
|
|
|
|
overlays.close_callback = opts.on_close;
|
|
|
|
},
|
2020-07-02 01:41:40 +02:00
|
|
|
recent_topics_open: () => true,
|
2020-04-08 13:59:56 +02:00
|
|
|
});
|
2020-08-20 21:24:06 +02:00
|
|
|
|
|
|
|
const people = zrequire("people");
|
|
|
|
people.is_my_user_id = (id) => id === 1;
|
|
|
|
people.sender_info_with_small_avatar_urls_for_sender_ids = (ids) => ids;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("timerender", {
|
2020-07-02 01:41:40 +02:00
|
|
|
last_seen_status_from_date: () => "Just now",
|
|
|
|
get_full_datetime: () => ({
|
|
|
|
date: "date",
|
|
|
|
time: "time",
|
|
|
|
}),
|
2020-05-22 08:16:08 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("unread", {
|
2020-05-22 08:16:08 +02:00
|
|
|
unread_topic_counter: {
|
2020-05-23 09:04:51 +02:00
|
|
|
get: (stream_id, topic) => {
|
|
|
|
if (stream_id === 1 && topic === "topic-1") {
|
|
|
|
// Only stream1, topic-1 is read.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
2020-05-22 08:16:08 +02:00
|
|
|
},
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("hash_util", {
|
2020-07-02 01:41:40 +02:00
|
|
|
by_stream_uri: () => "https://www.example.com",
|
|
|
|
by_stream_topic_uri: () => "https://www.example.com",
|
2020-05-22 08:16:08 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("recent_senders", {
|
2020-07-02 01:41:40 +02:00
|
|
|
get_topic_recent_senders: () => [1, 2],
|
2020-05-22 21:04:03 +02:00
|
|
|
});
|
2021-02-10 04:53:22 +01:00
|
|
|
const ListWidget = set_global("ListWidget", {
|
2020-06-01 17:17:41 +02:00
|
|
|
modifier: noop,
|
|
|
|
create: (container, mapped_topic_values, opts) => {
|
|
|
|
const formatted_topics = [];
|
2021-01-29 10:27:56 +01:00
|
|
|
ListWidget.modifier = opts.modifier;
|
2020-06-01 17:17:41 +02:00
|
|
|
for (const item of mapped_topic_values) {
|
|
|
|
formatted_topics.push(opts.modifier(item));
|
|
|
|
opts.filter.predicate(item);
|
|
|
|
}
|
2020-06-01 13:24:29 +02:00
|
|
|
// Just for coverage, the mechanisms
|
2021-01-29 10:27:56 +01:00
|
|
|
// are tested in list_widget.js
|
2020-06-01 13:24:29 +02:00
|
|
|
if (mapped_topic_values.length >= 2) {
|
|
|
|
opts.sort_fields.stream_sort(mapped_topic_values[0], mapped_topic_values[1]);
|
|
|
|
opts.sort_fields.stream_sort(mapped_topic_values[1], mapped_topic_values[0]);
|
|
|
|
opts.sort_fields.stream_sort(mapped_topic_values[0], mapped_topic_values[0]);
|
|
|
|
opts.sort_fields.topic_sort(mapped_topic_values[0], mapped_topic_values[1]);
|
|
|
|
opts.sort_fields.topic_sort(mapped_topic_values[1], mapped_topic_values[0]);
|
|
|
|
opts.sort_fields.topic_sort(mapped_topic_values[0], mapped_topic_values[0]);
|
|
|
|
}
|
2021-01-29 10:27:56 +01:00
|
|
|
return ListWidget;
|
2020-06-01 17:17:41 +02:00
|
|
|
},
|
|
|
|
hard_redraw: noop,
|
2021-01-29 10:27:56 +01:00
|
|
|
render_item: (item) => ListWidget.modifier(item),
|
2020-06-01 17:17:41 +02:00
|
|
|
});
|
2020-03-21 14:42:10 +01:00
|
|
|
|
|
|
|
// Custom Data
|
|
|
|
|
|
|
|
// New stream
|
|
|
|
const stream1 = 1;
|
2020-04-16 20:47:18 +02:00
|
|
|
const stream2 = 2;
|
|
|
|
const stream3 = 3;
|
2020-06-01 13:24:29 +02:00
|
|
|
const stream4 = 4;
|
2020-07-12 16:45:42 +02:00
|
|
|
const stream5 = 5; // Deleted stream
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-05-23 09:04:51 +02:00
|
|
|
// Topics in the stream, all unread except topic1 & stream1.
|
2020-07-16 23:29:01 +02:00
|
|
|
const topic1 = "topic-1"; // No Other sender & read.
|
|
|
|
const topic2 = "topic-2"; // Other sender
|
|
|
|
const topic3 = "topic-3"; // User not present
|
|
|
|
const topic4 = "topic-4"; // User not present
|
|
|
|
const topic5 = "topic-5"; // other sender
|
|
|
|
const topic6 = "topic-6"; // other sender
|
|
|
|
const topic7 = "topic-7"; // muted topic
|
2020-04-16 20:47:18 +02:00
|
|
|
const topic8 = "topic-8";
|
|
|
|
const topic9 = "topic-9";
|
2020-06-01 13:24:29 +02:00
|
|
|
const topic10 = "topic-10";
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("muting", {
|
2020-03-21 14:42:10 +01:00
|
|
|
is_topic_muted: (stream_id, topic) => {
|
|
|
|
if (stream_id === stream1 && topic === topic7) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// sender1 == current user
|
|
|
|
// sender2 == any other user
|
|
|
|
const sender1 = 1;
|
|
|
|
const sender2 = 2;
|
|
|
|
|
|
|
|
const messages = [];
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("message_list", {
|
2020-04-16 20:47:18 +02:00
|
|
|
all: {
|
2020-07-20 22:18:43 +02:00
|
|
|
all_messages() {
|
2020-04-16 20:47:18 +02:00
|
|
|
return messages;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("message_store", {
|
2020-07-02 01:41:40 +02:00
|
|
|
get: (msg_id) => messages[msg_id - 1],
|
2020-05-22 08:16:08 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("stream_data", {
|
2020-07-12 16:45:42 +02:00
|
|
|
get_sub_by_id: (stream) => {
|
|
|
|
if (stream === stream5) {
|
|
|
|
// No data is available for deactivated streams
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-07-12 16:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
color: "",
|
|
|
|
invite_only: false,
|
|
|
|
is_web_public: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
is_muted: () =>
|
|
|
|
// We only test via muted topics for now.
|
|
|
|
// TODO: Make muted streams and test them.
|
2020-07-15 00:34:28 +02:00
|
|
|
false,
|
2020-07-12 16:45:42 +02:00
|
|
|
});
|
2020-04-16 20:47:18 +02:00
|
|
|
|
2020-03-21 14:42:10 +01:00
|
|
|
let id = 0;
|
|
|
|
|
|
|
|
messages[0] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic1,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[1] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic2,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[2] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic2,
|
|
|
|
sender_id: sender2,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[3] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic3,
|
|
|
|
sender_id: sender2,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[4] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic4,
|
|
|
|
sender_id: sender2,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[5] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic5,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[6] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic5,
|
|
|
|
sender_id: sender2,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[7] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic6,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[8] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic6,
|
|
|
|
sender_id: sender2,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
messages[9] = {
|
|
|
|
stream_id: stream1,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream1",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic7,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
};
|
|
|
|
|
2020-06-01 13:24:29 +02:00
|
|
|
// a message of stream4
|
|
|
|
messages[10] = {
|
|
|
|
stream_id: stream4,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream4",
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-06-01 13:24:29 +02:00
|
|
|
topic: topic10,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-06-01 13:24:29 +02:00
|
|
|
};
|
|
|
|
|
2020-06-12 12:46:30 +02:00
|
|
|
function get_topic_key(stream_id, topic) {
|
|
|
|
return stream_id + ":" + topic.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:04:32 +02:00
|
|
|
function generate_topic_data(topic_info_array) {
|
|
|
|
// Since most of the fields are common, this function helps generate fixtures
|
|
|
|
// with non common fields.
|
2020-05-29 11:56:19 +02:00
|
|
|
$.clear_all_elements();
|
2020-05-28 17:04:32 +02:00
|
|
|
const data = [];
|
2020-05-29 12:03:22 +02:00
|
|
|
|
2020-05-29 11:56:19 +02:00
|
|
|
for (const [stream_id, topic, unread_count, muted, participated] of topic_info_array) {
|
2020-05-28 17:04:32 +02:00
|
|
|
data.push({
|
2020-06-12 12:07:48 +02:00
|
|
|
other_senders_count: 0,
|
2020-05-26 10:33:35 +02:00
|
|
|
invite_only: false,
|
|
|
|
is_web_public: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
last_msg_time: "Just now",
|
2020-06-26 12:25:35 +02:00
|
|
|
full_last_msg_date_time: "date time",
|
2020-07-15 00:34:28 +02:00
|
|
|
senders: [1, 2],
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "stream" + stream_id,
|
|
|
|
stream_color: "",
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream_url: "https://www.example.com",
|
2020-07-20 22:18:43 +02:00
|
|
|
topic,
|
2020-06-12 12:46:30 +02:00
|
|
|
topic_key: get_topic_key(stream_id, topic),
|
2020-07-15 01:29:15 +02:00
|
|
|
topic_url: "https://www.example.com",
|
2020-07-20 22:18:43 +02:00
|
|
|
unread_count,
|
|
|
|
muted,
|
2020-05-29 17:22:53 +02:00
|
|
|
topic_muted: muted,
|
2020-07-20 22:18:43 +02:00
|
|
|
participated,
|
2020-05-28 17:04:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
function verify_topic_data(all_topics, stream, topic, last_msg_id, participated) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const topic_data = all_topics.get(stream + ":" + topic);
|
2020-03-21 14:42:10 +01:00
|
|
|
assert.equal(topic_data.last_msg_id, last_msg_id);
|
|
|
|
assert.equal(topic_data.participated, participated);
|
|
|
|
}
|
|
|
|
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
let rt = zrequire("recent_topics");
|
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
run_test("test_recent_topics_launch", () => {
|
|
|
|
// Note: unread count and urls are fake,
|
|
|
|
// since they are generated in external libraries
|
|
|
|
// and are not to be tested here.
|
|
|
|
const expected = {
|
2020-06-13 14:03:34 +02:00
|
|
|
filter_participated: false,
|
|
|
|
filter_unread: false,
|
2020-05-29 17:22:53 +02:00
|
|
|
filter_muted: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
search_val: "",
|
2020-05-22 08:16:08 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "recent_topics_table") {
|
2020-05-29 12:03:22 +02:00
|
|
|
assert.deepEqual(data, expected);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (template_name === "recent_topics_filters") {
|
2020-05-29 12:03:22 +02:00
|
|
|
assert.equal(data.filter_unread, expected.filter_unread);
|
|
|
|
assert.equal(data.filter_participated, expected.filter_participated);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics table stub>";
|
2020-05-22 08:16:08 +02:00
|
|
|
});
|
2020-03-21 14:42:10 +01:00
|
|
|
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-05-22 08:16:08 +02:00
|
|
|
rt.process_messages(messages);
|
|
|
|
|
|
|
|
rt.launch();
|
2020-06-13 14:00:32 +02:00
|
|
|
|
|
|
|
// Test if search text is selected
|
2020-05-22 08:16:08 +02:00
|
|
|
overlays.close_callback();
|
|
|
|
|
|
|
|
// incorrect topic_key
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.inplace_rerender("stream_unknown:topic_unknown"), false);
|
2020-05-22 08:16:08 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_filter_all", () => {
|
2020-05-23 09:04:51 +02:00
|
|
|
// Just tests inplace rerender of a message
|
|
|
|
// in All topics filter.
|
2020-06-01 17:17:41 +02:00
|
|
|
const expected = {
|
|
|
|
filter_participated: false,
|
|
|
|
filter_unread: false,
|
|
|
|
filter_muted: false,
|
|
|
|
search_val: "",
|
|
|
|
};
|
|
|
|
let row_data;
|
|
|
|
let i;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "recent_topic_row") {
|
2020-06-01 17:17:41 +02:00
|
|
|
// All the row will be processed.
|
|
|
|
i -= 1;
|
|
|
|
assert.deepEqual(data, row_data[i]);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (template_name === "recent_topics_table") {
|
2020-06-01 17:17:41 +02:00
|
|
|
assert.deepEqual(data, expected);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics row stub>";
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// topic is not muted
|
2020-07-15 01:29:15 +02:00
|
|
|
row_data = generate_topic_data([[1, "topic-1", 0, false, true]]);
|
2020-06-01 17:17:41 +02:00
|
|
|
i = row_data.length;
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-05-23 09:04:51 +02:00
|
|
|
rt.process_messages([messages[0]]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
row_data = row_data.concat(generate_topic_data([[1, "topic-7", 1, true, true]]));
|
2020-06-01 17:17:41 +02:00
|
|
|
i = row_data.length;
|
2020-05-23 09:04:51 +02:00
|
|
|
// topic is muted (=== hidden)
|
|
|
|
rt.process_messages([messages[9]]);
|
2020-06-01 17:17:41 +02:00
|
|
|
|
|
|
|
// Test search
|
|
|
|
expected.search_val = "topic-1";
|
2020-07-15 01:29:15 +02:00
|
|
|
row_data = generate_topic_data([[1, "topic-1", 0, false, true]]);
|
2020-06-01 17:17:41 +02:00
|
|
|
i = row_data.length;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.inplace_rerender("1:topic-1"), true);
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_filter_unread", () => {
|
2020-05-23 09:04:51 +02:00
|
|
|
// Tests rerender of all topics when filter changes to "unread".
|
2020-06-01 17:17:41 +02:00
|
|
|
const expected = {
|
2020-05-23 09:39:47 +02:00
|
|
|
filter_participated: false,
|
|
|
|
filter_unread: true,
|
2020-05-29 17:22:53 +02:00
|
|
|
filter_muted: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
search_val: "",
|
2020-05-23 09:04:51 +02:00
|
|
|
};
|
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
const row_data = generate_topic_data([
|
|
|
|
// stream_id, topic, unread_count, muted, participated
|
2020-07-15 01:29:15 +02:00
|
|
|
[4, "topic-10", 1, false, true],
|
|
|
|
[1, "topic-7", 1, true, true],
|
|
|
|
[1, "topic-6", 1, false, true],
|
|
|
|
[1, "topic-5", 1, false, true],
|
|
|
|
[1, "topic-4", 1, false, false],
|
|
|
|
[1, "topic-3", 1, false, false],
|
|
|
|
[1, "topic-2", 1, false, true],
|
|
|
|
[1, "topic-1", 0, false, true],
|
2020-06-01 17:17:41 +02:00
|
|
|
]);
|
|
|
|
let i = 0;
|
|
|
|
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => "<recent_topics table stub>");
|
2020-05-23 09:04:51 +02:00
|
|
|
rt.process_messages(messages);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.inplace_rerender("1:topic-1"), true);
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons").removeClass("btn-recent-selected");
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "recent_topics_filters");
|
2020-06-01 17:17:41 +02:00
|
|
|
assert.equal(data.filter_unread, expected.filter_unread);
|
|
|
|
assert.equal(data.filter_participated, expected.filter_participated);
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics table stub>";
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
2020-05-29 12:03:22 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("unread");
|
2020-05-29 12:03:22 +02:00
|
|
|
rt.update_filters_view();
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "recent_topic_row") {
|
2020-06-01 17:17:41 +02:00
|
|
|
// All the row will be processed.
|
|
|
|
assert.deepEqual(data, row_data[i]);
|
|
|
|
i += 1;
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (template_name === "recent_topics_table") {
|
2020-06-01 17:17:41 +02:00
|
|
|
assert.deepEqual(data, expected);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics row stub>";
|
2020-05-29 12:03:22 +02:00
|
|
|
});
|
|
|
|
rt.process_messages([messages[0]]);
|
2020-05-23 09:04:51 +02:00
|
|
|
|
|
|
|
// Unselect "unread" filter by clicking twice.
|
2020-05-23 09:39:47 +02:00
|
|
|
expected.filter_unread = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons").addClass("btn-recent-selected");
|
|
|
|
rt.set_filter("unread");
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons").removeClass("btn-recent-selected");
|
2020-06-01 17:17:41 +02:00
|
|
|
// reselect "unread" filter
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("unread");
|
2020-06-01 17:17:41 +02:00
|
|
|
|
2020-05-23 09:04:51 +02:00
|
|
|
// Now clicking "all" filter should have no change to expected data.
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_filter_participated", () => {
|
2020-05-23 09:04:51 +02:00
|
|
|
// Tests rerender of all topics when filter changes to "unread".
|
2020-06-01 17:17:41 +02:00
|
|
|
const expected = {
|
2020-05-23 09:39:47 +02:00
|
|
|
filter_participated: true,
|
|
|
|
filter_unread: false,
|
2020-06-01 17:17:41 +02:00
|
|
|
filter_muted: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
search_val: "",
|
2020-05-23 09:04:51 +02:00
|
|
|
};
|
|
|
|
|
2020-06-01 17:17:41 +02:00
|
|
|
const row_data = generate_topic_data([
|
|
|
|
// stream_id, topic, unread_count, muted, participated
|
2020-07-15 01:29:15 +02:00
|
|
|
[4, "topic-10", 1, false, true],
|
|
|
|
[1, "topic-7", 1, true, true],
|
|
|
|
[1, "topic-6", 1, false, true],
|
|
|
|
[1, "topic-5", 1, false, true],
|
|
|
|
[1, "topic-4", 1, false, false],
|
|
|
|
[1, "topic-3", 1, false, false],
|
|
|
|
[1, "topic-2", 1, false, true],
|
|
|
|
[1, "topic-1", 0, false, true],
|
2020-06-01 17:17:41 +02:00
|
|
|
]);
|
|
|
|
let i = 0;
|
|
|
|
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => "<recent_topics table stub>");
|
2020-05-23 09:04:51 +02:00
|
|
|
rt.process_messages(messages);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.inplace_rerender("1:topic-4"), true);
|
2020-06-01 17:17:41 +02:00
|
|
|
|
|
|
|
// Set muted filter
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("muted");
|
|
|
|
assert.equal(rt.inplace_rerender("1:topic-7"), true);
|
2020-06-01 17:17:41 +02:00
|
|
|
|
|
|
|
// remove muted filter
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("muted");
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#recent_topics_filter_buttons").removeClass("btn-recent-selected");
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "recent_topics_filters");
|
2020-06-01 17:17:41 +02:00
|
|
|
assert.equal(data.filter_unread, expected.filter_unread);
|
|
|
|
assert.equal(data.filter_participated, expected.filter_participated);
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics table stub>";
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("participated");
|
2020-05-29 12:03:22 +02:00
|
|
|
rt.update_filters_view();
|
2020-06-01 17:17:41 +02:00
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (template_name === "recent_topic_row") {
|
2020-06-01 17:17:41 +02:00
|
|
|
// All the row will be processed.
|
|
|
|
assert.deepEqual(data, row_data[i]);
|
|
|
|
i += 1;
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (template_name === "recent_topics_table") {
|
2020-06-01 17:17:41 +02:00
|
|
|
assert.deepEqual(data, expected);
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<recent_topics row stub>";
|
2020-05-29 12:03:22 +02:00
|
|
|
});
|
|
|
|
rt.process_messages([messages[4]]);
|
2020-05-23 09:04:51 +02:00
|
|
|
|
2020-05-23 09:39:47 +02:00
|
|
|
expected.filter_participated = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-05-23 09:04:51 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_update_unread_count", () => {
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => "<recent_topics table stub>");
|
2020-05-29 12:03:22 +02:00
|
|
|
rt.process_messages(messages);
|
|
|
|
|
|
|
|
// update a message
|
2020-07-15 01:29:15 +02:00
|
|
|
generate_topic_data([[1, "topic-7", 1, false, true]]);
|
2020-06-12 12:46:30 +02:00
|
|
|
rt.update_topic_unread_count(messages[9]);
|
2020-05-29 12:03:22 +02:00
|
|
|
});
|
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
// template rendering is tested in test_recent_topics_launch.
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => "<recent_topics table stub>");
|
2020-05-22 08:16:08 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basic assertions", () => {
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-03-21 14:42:10 +01:00
|
|
|
rt.process_messages(messages);
|
|
|
|
let all_topics = rt.get();
|
|
|
|
|
2020-05-22 08:16:08 +02:00
|
|
|
// update a message
|
2020-07-15 01:29:15 +02:00
|
|
|
generate_topic_data([[1, "topic-7", 1, false, true]]);
|
2020-05-22 08:16:08 +02:00
|
|
|
rt.process_messages([messages[9]]);
|
2020-03-21 14:42:10 +01:00
|
|
|
// Check for expected lengths.
|
2020-06-01 13:24:29 +02:00
|
|
|
// total 8 topics, 1 muted
|
|
|
|
assert.equal(all_topics.size, 8);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
|
|
|
rt.process_message({
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-03-21 14:42:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Private msgs are not processed.
|
2020-06-01 13:24:29 +02:00
|
|
|
assert.equal(all_topics.size, 8);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-05-28 13:41:29 +02:00
|
|
|
// participated
|
|
|
|
verify_topic_data(all_topics, stream1, topic1, messages[0].id, true);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
|
|
|
// No message was sent by us.
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream1, topic3, messages[3].id, false);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
2020-05-28 13:41:29 +02:00
|
|
|
// Not participated
|
|
|
|
verify_topic_data(all_topics, stream1, topic4, messages[4].id, false);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
|
|
|
// topic3 now participated
|
|
|
|
rt.process_message({
|
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic3,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"1:topic-3,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream1, topic3, id, true);
|
2020-03-21 14:42:10 +01:00
|
|
|
|
|
|
|
// Send new message to topic7 (muted)
|
2020-05-22 08:16:08 +02:00
|
|
|
// The topic will be hidden when displayed
|
2020-03-21 14:42:10 +01:00
|
|
|
rt.process_message({
|
|
|
|
stream_id: stream1,
|
2020-07-15 00:34:28 +02:00
|
|
|
id: (id += 1),
|
2020-03-21 14:42:10 +01:00
|
|
|
topic: topic7,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-03-21 14:42:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"1:topic-7,1:topic-3,4:topic-10,1:topic-6,1:topic-5,1:topic-4,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-05-15 14:27:36 +02:00
|
|
|
|
2020-05-29 17:22:53 +02:00
|
|
|
// update_topic_is_muted now relies on external libraries completely
|
|
|
|
// so we don't need to check anythere here.
|
|
|
|
generate_topic_data([[1, topic1, 0, false, true]]);
|
|
|
|
assert.equal(rt.update_topic_is_muted(stream1, topic1), true);
|
2020-05-15 14:27:36 +02:00
|
|
|
// a topic gets muted which we are not tracking
|
2020-05-29 17:22:53 +02:00
|
|
|
assert.equal(rt.update_topic_is_muted(stream1, "topic-10"), false);
|
2020-03-21 14:42:10 +01:00
|
|
|
});
|
2020-04-16 20:47:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_reify_local_echo_message", () => {
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-05-28 09:48:58 +02:00
|
|
|
rt.process_messages(messages);
|
|
|
|
|
|
|
|
rt.process_message({
|
|
|
|
stream_id: stream1,
|
|
|
|
id: 1000.01,
|
|
|
|
topic: topic7,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-06-12 12:05:06 +02:00
|
|
|
});
|
2020-05-28 09:48:58 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
rt.reify_message_id_if_available({
|
|
|
|
old_id: 1000.01,
|
|
|
|
new_id: 1001,
|
|
|
|
}),
|
|
|
|
true,
|
|
|
|
);
|
2020-05-28 09:48:58 +02:00
|
|
|
|
|
|
|
rt.process_message({
|
|
|
|
stream_id: stream1,
|
|
|
|
id: 1001.01,
|
|
|
|
topic: topic7,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-06-12 12:05:06 +02:00
|
|
|
});
|
2020-05-28 09:48:58 +02:00
|
|
|
|
|
|
|
// A new message arrived in the same topic before we could reify the message_id
|
|
|
|
rt.process_message({
|
|
|
|
stream_id: stream1,
|
|
|
|
id: 1003,
|
|
|
|
topic: topic7,
|
|
|
|
sender_id: sender1,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2020-06-12 12:05:06 +02:00
|
|
|
});
|
2020-05-28 09:48:58 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
rt.reify_message_id_if_available({
|
|
|
|
old_id: 1000.01,
|
|
|
|
new_id: 1001,
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
);
|
2020-05-28 09:48:58 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_delete_messages", () => {
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-07-15 09:36:03 +02:00
|
|
|
rt.process_messages(messages);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("message_list", {
|
2020-07-15 09:36:03 +02:00
|
|
|
all: {
|
2020-07-20 22:18:43 +02:00
|
|
|
all_messages() {
|
2020-07-15 09:36:03 +02:00
|
|
|
// messages[0] was removed.
|
|
|
|
const reduced_msgs = [...messages];
|
|
|
|
reduced_msgs.splice(0, 1);
|
|
|
|
return reduced_msgs;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
let all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-08-06 21:00:28 +02:00
|
|
|
rt.update_topics_of_deleted_message_ids([messages[0].id]);
|
2020-07-15 09:36:03 +02:00
|
|
|
|
|
|
|
all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2",
|
|
|
|
);
|
2020-07-15 09:36:03 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("message_list", {
|
2020-07-15 09:36:03 +02:00
|
|
|
all: {
|
2020-07-20 22:18:43 +02:00
|
|
|
all_messages() {
|
2020-07-15 09:36:03 +02:00
|
|
|
// messages[0], messages[1] and message[2] were removed.
|
|
|
|
const reduced_msgs = [...messages];
|
|
|
|
reduced_msgs.splice(0, 3);
|
|
|
|
return reduced_msgs;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-08-06 21:00:28 +02:00
|
|
|
rt.update_topics_of_deleted_message_ids([messages[1].id, messages[2].id]);
|
2020-07-15 09:36:03 +02:00
|
|
|
|
|
|
|
all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3",
|
|
|
|
);
|
2020-08-07 09:16:25 +02:00
|
|
|
// test deleting a message which is not locally
|
|
|
|
// stored, doesn't raise any errors.
|
|
|
|
rt.update_topics_of_deleted_message_ids([-1]);
|
2020-07-15 09:36:03 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_topic_edit", () => {
|
|
|
|
set_global("message_list", {
|
2020-07-15 09:36:03 +02:00
|
|
|
all: {
|
2020-07-20 22:18:43 +02:00
|
|
|
all_messages() {
|
2020-07-15 09:36:03 +02:00
|
|
|
return messages;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-05-22 08:16:08 +02:00
|
|
|
// NOTE: This test should always run in the end as it modified the messages data.
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
rt.set_filter("all");
|
2020-04-16 20:47:18 +02:00
|
|
|
rt.process_messages(messages);
|
|
|
|
|
|
|
|
let all_topics = rt.get();
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
Array.from(all_topics.keys()).toString(),
|
|
|
|
"4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
|
|
|
|
);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
////////////////// test change topic //////////////////
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream1, topic6, messages[8].id, true);
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream1, topic8)), undefined);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
// change topic of topic6 to topic8
|
|
|
|
messages[7].topic = topic8;
|
|
|
|
messages[8].topic = topic8;
|
|
|
|
rt.process_topic_edit(stream1, topic6, topic8);
|
|
|
|
all_topics = rt.get();
|
|
|
|
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream1, topic8, messages[8].id, true);
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream1, topic6)), undefined);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
////////////////// test stream change //////////////////
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream1, topic1, messages[0].id, true);
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream2, topic1)), undefined);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
messages[0].stream_id = stream2;
|
|
|
|
rt.process_topic_edit(stream1, topic1, topic1, stream2);
|
|
|
|
all_topics = rt.get();
|
|
|
|
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream1, topic1)), undefined);
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream2, topic1, messages[0].id, true);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
////////////////// test stream & topic change //////////////////
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream2, topic1, messages[0].id, true);
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream3, topic9)), undefined);
|
2020-04-16 20:47:18 +02:00
|
|
|
|
|
|
|
messages[0].stream_id = stream3;
|
|
|
|
messages[0].topic = topic9;
|
|
|
|
rt.process_topic_edit(stream2, topic1, topic9, stream3);
|
|
|
|
all_topics = rt.get();
|
|
|
|
|
2020-06-12 12:46:30 +02:00
|
|
|
assert.equal(all_topics.get(get_topic_key(stream2, topic1)), undefined);
|
2020-05-28 13:41:29 +02:00
|
|
|
verify_topic_data(all_topics, stream3, topic9, messages[0].id, true);
|
2020-07-12 16:45:42 +02:00
|
|
|
|
|
|
|
// Message was moveed to a deleted stream, hence hidden regardless of filter.
|
|
|
|
messages[0].stream_id = stream5;
|
|
|
|
messages[0].topic = topic8;
|
|
|
|
rt.process_topic_edit(stream3, topic9, topic8, stream5);
|
|
|
|
all_topics = rt.get();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.filters_should_hide_topic(all_topics.get("5:topic-8")), true);
|
2020-04-16 20:47:18 +02:00
|
|
|
});
|
2020-06-26 08:42:35 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("test_search", () => {
|
node_tests: Don't remove require cache of module in zrequire.
There is good reason to do this (explanation is bit long!). With the
TypeScript migration, and the require and ES6 migrations that come
with it, we use require instead of set_global which loads the entire
module. Suppose we have a util module, which is used by some other
module, say message_store, and util is being required in message_store
since it is removed from window. Then, if a test zrequires
message_store first, and then zrequires the util module qand mocks one
of its methods, it will not be mocked for the message_store
module. The reason is:
1. zrequire('message_store') leads to require('util').
2. zrequire('util') removes the util module from cache and it is
reloaded. Now the util module in message_store and the one in
the test will be different and any updates to it in tests won't
be reflected in the actual code.
Which can lead to confusion for folks writing tests. I'll mention this
can be avoided doing zrequire('util') first but...that is not ideal.
And, since there was one outlier test that relied on this behavior,
we add the namespace.reset_module function.
2020-08-19 17:35:27 +02:00
|
|
|
rt = reset_module("recent_topics");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("t", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("T", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("to", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("top", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("ToP", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("Topi", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("tOpi", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("toPic", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("Topic", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("topic", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("recent", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("RECENT", "general", "Recent Topic"), true);
|
2020-06-29 11:15:48 +02:00
|
|
|
|
|
|
|
// match in any order of words
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("topic recent", "general", "Recent Topic"), true);
|
2020-06-29 11:15:48 +02:00
|
|
|
|
|
|
|
// Matches any sequence of words.
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("o", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("nt to", "general", "Recent Topic"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("z", "general", "Recent Topic"), false);
|
2020-06-29 11:15:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("?", "general", "Recent Topic"), false);
|
2020-06-26 08:42:35 +02:00
|
|
|
|
|
|
|
// Test special character match
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results(".*+?^${}()[]\\", "general", "Recent Topic"), false);
|
|
|
|
assert.equal(rt.topic_in_search_results("?", "general", "not-at-start?"), true);
|
2020-06-26 08:42:35 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("?", "general", "?"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("?", "general", "\\?"), true);
|
2020-06-26 08:42:35 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(rt.topic_in_search_results("\\", "general", "\\"), true);
|
|
|
|
assert.equal(rt.topic_in_search_results("\\", "general", "\\\\"), true);
|
2020-06-26 08:42:35 +02:00
|
|
|
});
|