2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-02-21 00:20:39 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
2020-12-01 00:02:16 +01:00
|
|
|
const {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
|
|
|
|
2018-12-26 19:56:19 +01:00
|
|
|
/*
|
|
|
|
This test suite is designed to find errors
|
|
|
|
in our initialization sequence. It doesn't
|
|
|
|
really validate any behavior, other than just
|
|
|
|
making sure things don't fail. For more
|
|
|
|
directed testing of individual modules, you
|
|
|
|
should create dedicated test suites.
|
|
|
|
|
|
|
|
Also, we stub a lot of initialization here that
|
|
|
|
is tricky to test due to dependencies on things
|
|
|
|
like jQuery. A good project is to work through
|
|
|
|
ignore_modules and try to make this test more
|
|
|
|
complete.
|
|
|
|
|
|
|
|
Also, it's good to be alert here for things
|
|
|
|
that can be cleaned up in the code--for example,
|
|
|
|
not everything needs to happen in `initialization`--
|
|
|
|
some things can happen later in a `launch` method.
|
|
|
|
|
|
|
|
*/
|
2020-07-15 01:29:15 +02:00
|
|
|
const util = zrequire("util");
|
2020-07-25 00:12:17 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {
|
2018-12-26 19:56:19 +01:00
|
|
|
location: {
|
2020-07-15 01:29:15 +02:00
|
|
|
protocol: "http",
|
2018-12-26 19:56:19 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("csrf_token", "whatever");
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", () => {});
|
|
|
|
set_global("resize", {});
|
|
|
|
set_global("page_params", {});
|
2018-12-26 19:56:19 +01:00
|
|
|
|
|
|
|
const ignore_modules = [
|
2020-07-15 01:29:15 +02:00
|
|
|
"activity",
|
|
|
|
"click_handlers",
|
|
|
|
"compose_pm_pill",
|
|
|
|
"copy_and_paste",
|
|
|
|
"drafts",
|
|
|
|
"emoji_picker",
|
|
|
|
"gear_menu",
|
|
|
|
"hashchange",
|
|
|
|
"hotspots",
|
2020-02-19 21:59:26 +01:00
|
|
|
// Accesses home_msg_list, which is a lot of complexity to setup
|
2020-07-15 01:29:15 +02:00
|
|
|
"message_fetch",
|
|
|
|
"message_scroll",
|
|
|
|
"message_viewport",
|
|
|
|
"panels",
|
|
|
|
"popovers",
|
|
|
|
"reload",
|
|
|
|
"scroll_bar",
|
|
|
|
"server_events",
|
|
|
|
"settings_sections",
|
|
|
|
"settings_panel_menu",
|
|
|
|
"settings_toggle",
|
|
|
|
"subs",
|
|
|
|
"timerender",
|
|
|
|
"ui",
|
|
|
|
"unread_ui",
|
2018-12-26 19:56:19 +01: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
|
|
|
for (const mod of ignore_modules) {
|
2018-12-26 19:56:19 +01:00
|
|
|
set_global(mod, {
|
|
|
|
initialize: () => {},
|
|
|
|
});
|
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-12-26 19:56:19 +01:00
|
|
|
|
|
|
|
util.is_mobile = () => false;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => "some-html");
|
2020-07-02 01:39:34 +02:00
|
|
|
ui.get_scroll_element = (element) => element;
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("alert_words");
|
|
|
|
zrequire("hash_util");
|
|
|
|
zrequire("echo");
|
|
|
|
zrequire("colorspace");
|
|
|
|
zrequire("stream_color");
|
|
|
|
zrequire("stream_edit");
|
|
|
|
zrequire("color_data");
|
|
|
|
zrequire("stream_data");
|
|
|
|
zrequire("muting");
|
|
|
|
zrequire("condense");
|
|
|
|
zrequire("spoilers");
|
|
|
|
zrequire("lightbox");
|
|
|
|
zrequire("overlays");
|
|
|
|
zrequire("invite");
|
2020-07-08 23:44:01 +02:00
|
|
|
zrequire("message_view_header");
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("narrow_state");
|
|
|
|
zrequire("people");
|
|
|
|
zrequire("presence");
|
|
|
|
zrequire("search_pill_widget");
|
|
|
|
zrequire("user_groups");
|
|
|
|
zrequire("unread");
|
|
|
|
zrequire("bot_data");
|
|
|
|
zrequire("markdown");
|
|
|
|
zrequire("upload");
|
|
|
|
zrequire("compose");
|
|
|
|
zrequire("composebox_typeahead");
|
|
|
|
zrequire("narrow");
|
|
|
|
zrequire("search_suggestion");
|
|
|
|
zrequire("search");
|
|
|
|
zrequire("tutorial");
|
2020-10-21 04:07:35 +02:00
|
|
|
rewiremock.proxy(() => zrequire("notifications"), {
|
|
|
|
"../../static/js/favicon": {},
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("pm_conversations");
|
|
|
|
zrequire("pm_list");
|
|
|
|
zrequire("list_cursor");
|
|
|
|
zrequire("keydown_util");
|
|
|
|
zrequire("stream_sort");
|
|
|
|
zrequire("stream_list");
|
|
|
|
zrequire("topic_list");
|
|
|
|
zrequire("topic_zoom");
|
|
|
|
zrequire("sent_messages");
|
|
|
|
zrequire("typing");
|
|
|
|
zrequire("top_left_corner");
|
|
|
|
zrequire("starred_messages");
|
|
|
|
zrequire("user_status");
|
|
|
|
zrequire("user_status_ui");
|
2020-02-21 00:20:39 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const ui_init = rewiremock.proxy(() => zrequire("ui_init"), {
|
|
|
|
"../../static/js/emojisets": {
|
|
|
|
initialize: () => {},
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2019-07-26 03:47:36 +02:00
|
|
|
|
2020-12-01 00:12:33 +01:00
|
|
|
set_global("$", make_zjquery());
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const document_stub = $.create("document-stub");
|
2018-12-26 19:56:19 +01:00
|
|
|
document.to_$ = () => document_stub;
|
|
|
|
document_stub.idle = () => {};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const window_stub = $.create("window-stub");
|
|
|
|
set_global("to_$", () => window_stub);
|
2018-12-26 19:56:19 +01:00
|
|
|
window_stub.idle = () => {};
|
|
|
|
|
|
|
|
ui_init.initialize_kitchen_sink_stuff = () => {};
|
|
|
|
|
|
|
|
page_params.realm_default_streams = [];
|
|
|
|
page_params.subscriptions = [];
|
|
|
|
page_params.unsubscribed = [];
|
|
|
|
page_params.never_subscribed = [];
|
|
|
|
page_params.realm_notifications_stream_id = -1;
|
2020-02-05 23:38:27 +01:00
|
|
|
page_params.unread_msgs = {
|
|
|
|
huddles: [],
|
|
|
|
pms: [],
|
|
|
|
streams: [],
|
|
|
|
mentions: [],
|
|
|
|
};
|
2019-11-21 21:01:15 +01:00
|
|
|
page_params.recent_private_conversations = [];
|
2020-02-06 04:31:32 +01:00
|
|
|
page_params.user_status = {};
|
2020-07-26 00:47:37 +02:00
|
|
|
page_params.realm_emoji = {};
|
2020-02-05 23:38:27 +01:00
|
|
|
page_params.realm_users = [];
|
|
|
|
page_params.realm_non_active_users = [];
|
|
|
|
page_params.cross_realm_bots = [];
|
|
|
|
page_params.muted_topics = [];
|
|
|
|
page_params.realm_user_groups = [];
|
|
|
|
page_params.realm_bots = [];
|
|
|
|
page_params.realm_filters = [];
|
|
|
|
page_params.starred_messages = [];
|
2020-02-07 16:33:03 +01:00
|
|
|
page_params.presences = [];
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
const $message_view_header = $.create("#message_view_header");
|
|
|
|
$message_view_header.append = () => {};
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload = () => {};
|
2018-12-26 19:56:19 +01:00
|
|
|
|
|
|
|
server_events.home_view_loaded = () => true;
|
|
|
|
|
|
|
|
resize.watch_manual_resize = () => {};
|
|
|
|
|
|
|
|
$("#stream_message_recipient_stream").typeahead = () => {};
|
|
|
|
$("#stream_message_recipient_topic").typeahead = () => {};
|
|
|
|
$("#private_message_recipient").typeahead = () => {};
|
|
|
|
$("#compose-textarea").typeahead = () => {};
|
|
|
|
$("#search_query").typeahead = () => {};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const value_stub = $.create("value");
|
|
|
|
const count_stub = $.create("count");
|
|
|
|
count_stub.set_find_results(".value", value_stub);
|
|
|
|
$(".top_left_starred_messages").set_find_results(".count", count_stub);
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-07-08 23:44:01 +02:00
|
|
|
$("#message_view_header .stream").length = 0;
|
2018-12-26 19:56:19 +01:00
|
|
|
|
2020-05-21 00:53:14 +02:00
|
|
|
// set find results doesn't work here since we call .empty() in the code.
|
2020-07-08 23:44:01 +02:00
|
|
|
$message_view_header.find = () => false;
|
2020-05-21 00:53:14 +02:00
|
|
|
|
2020-04-08 00:23:15 +02:00
|
|
|
compose.compute_show_video_chat_button = () => {};
|
|
|
|
$("#below-compose-content .video_link").toggle = () => {};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("initialize_everything", () => {
|
2018-12-26 19:56:19 +01:00
|
|
|
ui_init.initialize_everything();
|
|
|
|
});
|