Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjsunit: Optionally exercise template code.
Steve Howell3d1aae6253e3
diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.jsindex 627bfc2df6d..7fe93f11f8a 100644--- a/frontend_tests/node_tests/compose.js+++ b/frontend_tests/node_tests/compose.js@@ -2,7 +2,6 @@const {strict: assert} = require("assert");-const {JSDOM} = require("jsdom");const MockDate = require("mockdate");const {$t, $t_html} = require("../zjsunit/i18n");@@ -16,8 +15,6 @@ mock_cjs("jquery", $);const noop = () => {};-set_global("DOMParser", new JSDOM().window.DOMParser);-set_global("document", {});set_global("navigator", {});diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.jsindex eebb1557b02..bef7605c335 100644--- a/frontend_tests/node_tests/composebox_typeahead.js+++ b/frontend_tests/node_tests/composebox_typeahead.js@@ -38,7 +38,6 @@ set_global("document", "document-stub");const emoji = zrequire("../shared/js/emoji");const typeahead = zrequire("../shared/js/typeahead");const compose_state = zrequire("compose_state");-zrequire("templates");const typeahead_helper = zrequire("typeahead_helper");const muting = zrequire("muting");const people = zrequire("people");diff --git a/frontend_tests/node_tests/copy_and_paste.js b/frontend_tests/node_tests/copy_and_paste.jsindex 22af3ba3d09..381da848220 100644--- a/frontend_tests/node_tests/copy_and_paste.js+++ b/frontend_tests/node_tests/copy_and_paste.js@@ -11,11 +11,10 @@ const {page_params} = require("../zjsunit/zpage_params");const {window} = new JSDOM("<!DOCTYPE html><p>Hello world</p>");-const {DOMParser, document} = window;+const {document} = window;const $ = jquery(window);const compose_ui = mock_esm("../../static/js/compose_ui");-set_global("DOMParser", DOMParser);set_global("document", document);const copy_and_paste = zrequire("copy_and_paste");diff --git a/frontend_tests/node_tests/i18n.js b/frontend_tests/node_tests/i18n.jsindex d0b87868852..d1c4f3855de 100644--- a/frontend_tests/node_tests/i18n.js+++ b/frontend_tests/node_tests/i18n.js@@ -24,7 +24,6 @@ page_params.translation_data = {// `templates.js`.unmock_module("../../static/js/i18n");const {$t, $t_html} = zrequire("i18n");-zrequire("templates");run_test("$t", () => {// Normally the id would be provided by babel-plugin-formatjs, butdiff --git a/frontend_tests/node_tests/input_pill.js b/frontend_tests/node_tests/input_pill.jsindex f81647c2c07..1772b24ae54 100644--- a/frontend_tests/node_tests/input_pill.js+++ b/frontend_tests/node_tests/input_pill.js@@ -21,7 +21,6 @@ set_global("getSelection", () => ({anchorOffset: 0,}));-zrequire("templates");const input_pill = zrequire("input_pill");function pill_html(value, data_id, img_src) {diff --git a/frontend_tests/node_tests/recent_topics.js b/frontend_tests/node_tests/recent_topics.jsindex c372dee7905..4eedd84f65e 100644--- a/frontend_tests/node_tests/recent_topics.js+++ b/frontend_tests/node_tests/recent_topics.js@@ -152,7 +152,7 @@ mock_esm("../../static/js/unread", {const render_recent_topics_table = mock_template("recent_topics_table.hbs");const render_recent_topics_filters = mock_template("recent_topics_filters.hbs");-const render_recent_topic_row = mock_template("recent_topic_row.hbs");+const render_recent_topic_row = mock_template("recent_topic_row.hbs", true);const ls_container = new Map();set_global("localStorage", {@@ -654,7 +654,9 @@ test("basic assertions", (override) => {rt.clear_for_tests();override(render_recent_topics_table, "f", () => {});- override(render_recent_topic_row, "f", () => {});+ override(render_recent_topic_row, "f", (data, html) => {+ assert.ok(html.startsWith('<tr id="recent_topic'));+ });stub_out_filter_buttons();override(rt, "is_visible", () => true);diff --git a/frontend_tests/node_tests/templates.js b/frontend_tests/node_tests/templates.jsindex 860e74c2444..8014836514b 100644--- a/frontend_tests/node_tests/templates.js+++ b/frontend_tests/node_tests/templates.js@@ -2,13 +2,12 @@const {strict: assert} = require("assert");-const {JSDOM} = require("jsdom");--const {set_global, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");-set_global("DOMParser", new JSDOM().window.DOMParser);-zrequire("templates");+/*+ Note that the test runner automatically registers+ all of our handlers.+*/run_test("and", () => {const args = {diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.jsindex c91db777d9d..9603c47bcb7 100644--- a/frontend_tests/node_tests/util.js+++ b/frontend_tests/node_tests/util.js@@ -2,13 +2,11 @@const {strict: assert} = require("assert");-const {JSDOM} = require("jsdom");const _ = require("lodash");const {set_global, with_field, zrequire} = require("../zjsunit/namespace");const {run_test} = require("../zjsunit/test");-set_global("DOMParser", new JSDOM().window.DOMParser);set_global("document", {});const util = zrequire("util");diff --git a/frontend_tests/zjsunit/index.js b/frontend_tests/zjsunit/index.jsindex 73fd405da5c..c72dbd2bb77 100644--- a/frontend_tests/zjsunit/index.js+++ b/frontend_tests/zjsunit/index.js@@ -3,7 +3,8 @@const path = require("path");require("css.escape");-const Handlebars = require("handlebars/runtime");+require("handlebars/runtime");+const {JSDOM} = require("jsdom");const _ = require("lodash");const handlebars = require("./handlebars");@@ -14,6 +15,8 @@ const blueslip = require("./zblueslip");const zjquery = require("./zjquery");const zpage_params = require("./zpage_params");+global.DOMParser = new JSDOM().window.DOMParser;+require("@babel/register")({extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],only: [@@ -64,6 +67,8 @@ function short_tb(tb) {return lines.splice(0, i + 1).join("\n") + "\n(...)\n";}+require("../../static/js/templates"); // register Zulip extensions+function run_one_module(file) {zjquery.clear_initialize_function();zjquery.clear_all_elements();@@ -103,7 +108,6 @@ try {}namespace.finish();- Handlebars.HandlebarsEnvironment.call(Handlebars);}} catch (error) {if (error.stack) {diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.jsindex 90b1c2ec49b..86658586214 100644--- a/frontend_tests/zjsunit/namespace.js+++ b/frontend_tests/zjsunit/namespace.js@@ -15,6 +15,8 @@ const used_module_mocks = new Set();const jquery_path = require.resolve("jquery");const real_jquery_path = require.resolve("../zjsunit/real_jquery.js");+let in_mid_render = false;+function load(request, parent, isMain) {const filename = Module._resolveFilename(request, parent, isMain);if (module_mocks.has(filename)) {@@ -26,9 +28,15 @@ function load(request, parent, isMain) {return obj;}+ const actual_render = actual_load(request, parent, isMain);+// For template mocks, we have an underlying object that// we wrap with a render function.- const render = (data) => {+ const render = (...args) => {+ if (in_mid_render) {+ return actual_render(...args);+ }+if (obj.f.__default) {throw new Error(`You did render_foo = mock_template("${obj.__template_fn}")@@ -36,7 +44,17 @@ function load(request, parent, isMain) {`);}- // User will override "f" for now, which is a bit hacky.+ const data = args[0];++ if (obj.exercise_template) {+ in_mid_render = true;+ const html = actual_render(...args);+ in_mid_render = false;++ // User will override "f" for now, which is a bit hacky.+ return obj.f(data, html);+ }+return obj.f(data);};@@ -114,13 +132,15 @@ exports.mock_cjs = (fn, obj) => {return obj;};-exports.mock_template = (fn) => {+exports.mock_template = (fn, exercise_template) => {// We create an object with an f() function that the test author// can override.const obj = {f: () => {},__template_fn: fn,+ exercise_template: Boolean(exercise_template),};+obj.f.__default = true;const filename = get_validated_filename("../../static/templates/" + fn);@@ -132,11 +152,11 @@ exports.mock_template = (fn) => {return obj;};-exports.mock_esm = (request, obj = {}) => {+exports.mock_esm = (fn, obj = {}) => {if (typeof obj !== "object") {throw new TypeError("An ES module must be mocked with an object");}- return exports.mock_cjs(request, {...obj, __esModule: true});+ return exports.mock_cjs(fn, {...obj, __esModule: true});};exports.unmock_module = (request) => {@@ -176,6 +196,15 @@ exports.set_global = function (name, val) {};exports.zrequire = function (short_fn) {+ if (short_fn === "templates") {+ throw new Error(`+ There is no need to zrequire templates.js.++ The test runner automatically registers the+ Handlebar extensions.+ `);+ }+return require(`../../static/js/${short_fn}`);};