2021-02-28 05:27:15 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {with_overrides, zrequire} = require("../zjsunit/namespace");
|
2021-02-28 05:27:15 +01:00
|
|
|
const {make_stub} = require("../zjsunit/stub");
|
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2021-02-28 05:27:15 +01:00
|
|
|
|
2021-03-08 09:25:25 +01:00
|
|
|
const message_store = zrequire("message_store");
|
2021-02-28 05:27:15 +01:00
|
|
|
const starred_messages = zrequire("starred_messages");
|
2021-03-08 09:25:25 +01:00
|
|
|
const stream_popover = zrequire("stream_popover");
|
2021-02-28 05:27:15 +01:00
|
|
|
const top_left_corner = zrequire("top_left_corner");
|
|
|
|
|
|
|
|
run_test("add starred", (override) => {
|
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), []);
|
|
|
|
assert.equal(starred_messages.get_count(), 0);
|
|
|
|
|
|
|
|
override(starred_messages, "rerender_ui", () => {});
|
|
|
|
starred_messages.add([1, 2]);
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1, 2]);
|
|
|
|
assert.equal(starred_messages.get_count(), 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("remove starred", (override) => {
|
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), []);
|
|
|
|
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1, 2, 3]);
|
|
|
|
|
|
|
|
override(starred_messages, "rerender_ui", () => {});
|
|
|
|
starred_messages.remove([2, 3]);
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [1]);
|
|
|
|
assert.equal(starred_messages.get_count(), 1);
|
|
|
|
});
|
|
|
|
|
2021-03-08 09:25:25 +01:00
|
|
|
run_test("get starred ids in topic", () => {
|
|
|
|
for (const id of [1, 2, 3, 4, 5]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
starred_messages.get_starred_message_ids_in_topic(undefined, "topic name"),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
assert.deepEqual(starred_messages.get_starred_message_ids_in_topic(3, undefined), []);
|
|
|
|
|
|
|
|
// id: 1 isn't inserted, to test handling the case
|
|
|
|
// when message_store.get() returns undefined
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
id: 2,
|
|
|
|
type: "private",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Different stream
|
|
|
|
id: 3,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 19,
|
|
|
|
topic: "topic",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Different topic
|
|
|
|
id: 4,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 20,
|
|
|
|
topic: "some other topic",
|
|
|
|
});
|
2021-03-28 17:57:53 +02:00
|
|
|
message_store.update_message_cache({
|
2021-03-08 09:25:25 +01:00
|
|
|
// Correct match
|
|
|
|
id: 5,
|
|
|
|
type: "stream",
|
|
|
|
stream_id: 20,
|
|
|
|
topic: "topic",
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(starred_messages.get_starred_message_ids_in_topic(20, "topic"), [5]);
|
|
|
|
});
|
|
|
|
|
2021-02-28 05:27:15 +01:00
|
|
|
run_test("initialize", (override) => {
|
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_params.starred_messages = [4, 5, 6];
|
|
|
|
override(starred_messages, "rerender_ui", () => {});
|
|
|
|
starred_messages.initialize();
|
|
|
|
assert.deepEqual(starred_messages.get_starred_msg_ids(), [4, 5, 6]);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("rerender_ui", () => {
|
|
|
|
starred_messages.starred_ids.clear();
|
|
|
|
for (const id of [1, 2, 3]) {
|
|
|
|
starred_messages.starred_ids.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_params.starred_message_counts = true;
|
|
|
|
with_overrides((override) => {
|
|
|
|
const stub = make_stub();
|
2021-03-08 09:25:25 +01:00
|
|
|
override(stream_popover, "hide_topic_popover", () => {});
|
2021-02-28 05:27:15 +01:00
|
|
|
override(top_left_corner, "update_starred_count", stub.f);
|
|
|
|
starred_messages.rerender_ui();
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
const args = stub.get_args("count");
|
|
|
|
assert.equal(args.count, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
page_params.starred_message_counts = false;
|
|
|
|
with_overrides((override) => {
|
|
|
|
const stub = make_stub();
|
2021-03-08 09:25:25 +01:00
|
|
|
override(stream_popover, "hide_topic_popover", () => {});
|
2021-02-28 05:27:15 +01:00
|
|
|
override(top_left_corner, "update_starred_count", stub.f);
|
|
|
|
starred_messages.rerender_ui();
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
const args = stub.get_args("count");
|
|
|
|
assert.equal(args.count, 0);
|
|
|
|
});
|
|
|
|
});
|