zulip/web/tests/topic_list_data.test.js

291 lines
8.1 KiB
JavaScript
Raw Normal View History

"use strict";
const {strict: assert} = require("assert");
const _ = require("lodash");
const {mock_esm, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
mock_esm("../src/message_store", {
get() {
return {
stream_id: 556,
topic: "general",
};
},
});
const user_topics = mock_esm("../src/user_topics", {
is_topic_muted() {
return false;
},
is_topic_unmuted() {
return false;
},
});
const narrow_state = mock_esm("../src/narrow_state", {
topic() {},
});
const stream_data = zrequire("stream_data");
const stream_topic_history = zrequire("stream_topic_history");
const topic_list_data = zrequire("topic_list_data");
const unread = zrequire("unread");
const general = {
stream_id: 556,
name: "general",
};
stream_data.add_sub(general);
function get_list_info(zoom, search) {
const stream_id = general.stream_id;
const zoomed = zoom === undefined ? false : zoom;
const search_term = search === undefined ? "" : search;
return topic_list_data.get_list_info(stream_id, zoomed, search_term);
}
function test(label, f) {
run_test(label, (helpers) => {
stream_topic_history.reset();
f(helpers);
});
}
test("get_list_info w/real stream_topic_history", ({override}) => {
let list_info;
const empty_list_info = get_list_info();
assert.deepEqual(empty_list_info, {
items: [],
more_topics_have_unread_mention_messages: false,
more_topics_unreads: 0,
num_possible_topics: 0,
});
function add_topic_message(topic_name, message_id) {
stream_topic_history.add_message({
stream_id: general.stream_id,
topic_name,
message_id,
});
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
for (const i of _.range(10)) {
let topic_name;
// All odd topics are resolved.
if (i % 2) {
topic_name = "✔ topic ";
} else {
topic_name = "topic ";
}
add_topic_message(topic_name + i, 1000 + i);
}
override(narrow_state, "topic", () => "topic 6");
list_info = get_list_info();
assert.equal(list_info.items.length, 8);
assert.equal(list_info.more_topics_unreads, 0);
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 10);
assert.deepEqual(list_info.items[0], {
contains_unread_mention: false,
is_active_topic: false,
is_muted: false,
is_unmuted: false,
is_zero: true,
topic_display_name: "topic 9",
topic_name: "✔ topic 9",
topic_resolved_prefix: "✔ ",
unread: 0,
url: "#narrow/stream/556-general/topic/.E2.9C.94.20topic.209",
});
assert.deepEqual(list_info.items[1], {
contains_unread_mention: false,
is_active_topic: false,
is_muted: false,
is_unmuted: false,
is_zero: true,
topic_display_name: "topic 8",
topic_name: "topic 8",
topic_resolved_prefix: "",
unread: 0,
url: "#narrow/stream/556-general/topic/topic.208",
});
// If we zoom in, our results are based on topic filter.
// If topic search input is empty, we show all 10 topics.
const zoomed = true;
list_info = get_list_info(zoomed);
assert.equal(list_info.items.length, 10);
assert.equal(list_info.more_topics_unreads, 0);
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 10);
add_topic_message("After Brooklyn", 1008);
add_topic_message("Catering", 1009);
// When topic search input is not empty, we show topics
// based on the search term.
const search_term = "b,c";
list_info = get_list_info(zoomed, search_term);
assert.equal(list_info.items.length, 2);
assert.equal(list_info.more_topics_unreads, 0);
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 2);
});
test("get_list_info unreads", ({override}) => {
let list_info;
let message_id = 0;
for (let i = 15; i >= 0; i -= 1) {
stream_topic_history.add_message({
stream_id: general.stream_id,
message_id: (message_id += 1),
topic_name: `topic ${i}`,
});
}
function add_unreads(topic, count) {
unread.process_loaded_messages(
Array.from({length: count}, () => ({
id: (message_id += 1),
stream_id: general.stream_id,
topic,
type: "stream",
unread: true,
})),
);
}
function add_unreads_with_mention(topic, count) {
unread.process_loaded_messages(
Array.from({length: count}, () => ({
id: (message_id += 1),
stream_id: general.stream_id,
topic,
type: "stream",
unread: true,
mentioned: true,
mentioned_me_directly: true,
})),
);
}
/*
We have 16 topics, but we only show up
to 12 topics, depending on how many have
unread counts. We only show a max of 8
fully-read topics.
So first we'll get 10 topics, where 2 are
unread.
*/
add_unreads("topic 14", 1);
add_unreads("topic 13", 1);
/*
We added 1 unread message in 'topic 14',
but now we would add a unread message
with `mention` for user, to test
`more_topics_have_unread_mention_messages`.
*/
add_unreads_with_mention("topic 14", 1);
list_info = get_list_info();
assert.equal(list_info.items.length, 10);
assert.equal(list_info.more_topics_unreads, 0);
assert.equal(list_info.more_topics_have_unread_mention_messages, false);
assert.equal(list_info.num_possible_topics, 16);
assert.deepEqual(
list_info.items.map((li) => li.topic_name),
[
"topic 0",
"topic 1",
"topic 2",
"topic 3",
"topic 4",
"topic 5",
"topic 6",
"topic 7",
"topic 13",
"topic 14",
],
);
add_unreads("topic 12", 1);
add_unreads("topic 11", 1);
add_unreads("topic 10", 1);
list_info = get_list_info();
assert.equal(list_info.items.length, 12);
assert.equal(list_info.more_topics_unreads, 2);
assert.equal(list_info.more_topics_have_unread_mention_messages, true);
assert.equal(list_info.num_possible_topics, 16);
assert.deepEqual(
list_info.items.map((li) => li.topic_name),
[
"topic 0",
"topic 1",
"topic 2",
"topic 3",
"topic 4",
"topic 5",
"topic 6",
"topic 7",
"topic 10",
"topic 11",
"topic 12",
"topic 13",
],
);
add_unreads("topic 9", 1);
add_unreads("topic 8", 1);
add_unreads("topic 4", 1);
override(user_topics, "is_topic_muted", (stream_id, topic_name) => {
assert.equal(stream_id, general.stream_id);
return topic_name === "topic 4";
});
// muting the stream and unmuting the topic 5
// this should make topic 5 at top in items array
general.is_muted = true;
add_unreads("topic 5", 1);
override(user_topics, "is_topic_unmuted", (stream_id, topic_name) => {
assert.equal(stream_id, general.stream_id);
return topic_name === "topic 5";
});
list_info = get_list_info();
assert.equal(list_info.items.length, 12);
assert.equal(list_info.more_topics_unreads, 3);
assert.equal(list_info.more_topics_have_unread_mention_messages, true);
assert.equal(list_info.num_possible_topics, 16);
assert.deepEqual(
list_info.items.map((li) => li.topic_name),
[
"topic 5",
"topic 0",
"topic 1",
"topic 2",
"topic 3",
"topic 6",
"topic 7",
"topic 8",
"topic 9",
"topic 10",
"topic 11",
"topic 12",
],
);
});