2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {rewiremock, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-28 00:56:48 +01:00
|
|
|
const condense = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/condense").with(condense);
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("current_msg_list", {});
|
2021-02-28 00:56:21 +01:00
|
|
|
const message_edit = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/message_edit").with(message_edit);
|
2021-02-28 21:31:33 +01:00
|
|
|
const message_list = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/message_list").with(message_list);
|
2021-02-28 01:06:34 +01:00
|
|
|
const notifications = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/notifications").with(notifications);
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
2021-02-28 00:44:12 +01:00
|
|
|
const pm_list = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/pm_list").with(pm_list);
|
2021-02-28 21:31:02 +01:00
|
|
|
const stream_list = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/stream_list").with(stream_list);
|
2021-02-28 00:40:18 +01:00
|
|
|
const unread_ui = {__esModule: true};
|
|
|
|
|
|
|
|
rewiremock("../../static/js/unread_ui").with(unread_ui);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const message_events = zrequire("message_events");
|
|
|
|
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");
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-09-11 17:16:52 +02:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Patel",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:15:41 +02:00
|
|
|
const denmark = {
|
|
|
|
subscribed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: 101,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
function test_helper(side_effects) {
|
|
|
|
const events = [];
|
|
|
|
|
2021-02-11 01:42:59 +01:00
|
|
|
for (const [module, field] of side_effects) {
|
|
|
|
module[field] = () => {
|
|
|
|
events.push([module, field]);
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
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
|
|
|
}
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
const self = {};
|
|
|
|
|
|
|
|
self.verify = () => {
|
|
|
|
assert.deepEqual(side_effects, events);
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("update_messages", () => {
|
2018-09-11 17:16:52 +02:00
|
|
|
const original_message = {
|
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
2020-07-15 01:29:15 +02:00
|
|
|
flags: ["mentioned"],
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: alice.user_id,
|
2020-04-11 02:15:41 +02:00
|
|
|
stream_id: denmark.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
message_store.add_message_metadata(original_message);
|
2020-04-11 02:15:41 +02:00
|
|
|
message_store.set_message_booleans(original_message);
|
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
assert.equal(original_message.mentioned, true);
|
2020-04-11 02:15:41 +02:00
|
|
|
assert.equal(original_message.unread, true);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(stream_topic_history.get_recent_topic_names(denmark.stream_id), ["lunch"]);
|
2018-09-11 17:16:52 +02:00
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
unread.update_message_for_mention(original_message);
|
|
|
|
assert(unread.unread_mentions_counter.has(original_message.id));
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
const events = [
|
|
|
|
{
|
2020-04-11 02:15:41 +02:00
|
|
|
message_id: original_message.id,
|
2018-09-11 17:16:52 +02:00
|
|
|
flags: [],
|
2020-07-15 01:29:15 +02:00
|
|
|
orig_content: "old stuff",
|
|
|
|
content: "**new content**",
|
|
|
|
rendered_content: "<b>new content</b>",
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
current_msg_list.get_row = (message_id) => {
|
|
|
|
assert.equal(message_id, 111);
|
2020-07-15 01:29:15 +02:00
|
|
|
return ["row-stub"];
|
2018-09-11 17:16:52 +02:00
|
|
|
};
|
|
|
|
current_msg_list.view = {};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let rendered_mgs;
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
current_msg_list.view.rerender_messages = (msgs_to_rerender, message_content_edited) => {
|
|
|
|
rendered_mgs = msgs_to_rerender;
|
|
|
|
assert.equal(message_content_edited, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const side_effects = [
|
2021-02-11 01:42:59 +01:00
|
|
|
[condense, "un_cache_message_content_height"],
|
|
|
|
[message_edit, "end_message_row_edit"],
|
|
|
|
[notifications, "received_messages"],
|
|
|
|
[unread_ui, "update_unread_counts"],
|
|
|
|
[stream_list, "update_streams_sidebar"],
|
|
|
|
[pm_list, "update_private_messages"],
|
2018-09-11 17:16:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const helper = test_helper(side_effects);
|
|
|
|
|
|
|
|
page_params.realm_allow_edit_history = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
message_list.narrowed = "stub-to-ignore";
|
2018-09-11 17:16:52 +02:00
|
|
|
|
|
|
|
// TEST THIS:
|
|
|
|
message_events.update_messages(events);
|
|
|
|
|
2020-04-11 02:51:45 +02:00
|
|
|
assert(!unread.unread_mentions_counter.has(original_message.id));
|
|
|
|
|
2018-09-11 17:16:52 +02:00
|
|
|
helper.verify();
|
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.deepEqual(rendered_mgs, [
|
2018-09-11 17:16:52 +02:00
|
|
|
{
|
|
|
|
alerted: false,
|
2020-04-11 02:15:41 +02:00
|
|
|
collapsed: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "<b>new content</b>",
|
2020-04-11 02:15:41 +02:00
|
|
|
display_recipient: denmark.name,
|
|
|
|
historical: false,
|
2018-09-11 17:16:52 +02:00
|
|
|
id: 111,
|
2020-04-11 02:15:41 +02:00
|
|
|
is_stream: true,
|
2018-09-11 17:16:52 +02:00
|
|
|
last_edit_timestamp: undefined,
|
|
|
|
mentioned: false,
|
|
|
|
mentioned_me_directly: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
raw_content: "**new content**",
|
2018-09-11 17:16:52 +02:00
|
|
|
reactions: [],
|
2020-04-11 02:15:41 +02:00
|
|
|
reply_to: alice.email,
|
|
|
|
sender_email: alice.email,
|
|
|
|
sender_full_name: alice.full_name,
|
2018-09-11 17:16:52 +02:00
|
|
|
sender_id: 32,
|
|
|
|
sent_by_me: false,
|
2020-04-11 02:15:41 +02:00
|
|
|
starred: false,
|
|
|
|
stream: denmark.name,
|
|
|
|
stream_id: denmark.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "lunch",
|
|
|
|
type: "stream",
|
2020-04-11 02:15:41 +02:00
|
|
|
unread: true,
|
2018-09-11 17:16:52 +02:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|