namespace: Clean up template_path manipulation.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-08-04 14:01:11 -07:00 committed by Anders Kaseorg
parent 73fbca7ae9
commit 529d35c530
3 changed files with 7 additions and 21 deletions

View File

@ -23,17 +23,11 @@ const real_jquery_path = require.resolve("./real_jquery.js");
let in_mid_render = false; let in_mid_render = false;
let jquery_function; let jquery_function;
const template_path = "/web/templates/"; const template_path = path.resolve(__dirname, "../../templates");
/* istanbul ignore next */ /* istanbul ignore next */
function need_to_mock_template_error(filename) { function need_to_mock_template_error(filename) {
const i = filename.indexOf(template_path); const fn = path.relative(template_path, filename);
if (i < 0) {
throw new Error("programming error");
}
const fn = filename.slice(i + template_path.length);
return ` return `
Please use mock_template if your test needs to render ${fn} Please use mock_template if your test needs to render ${fn}
@ -79,7 +73,7 @@ function load(request, parent, isMain) {
return obj; return obj;
} }
if (filename.endsWith(".hbs") && filename.includes(template_path)) { if (filename.endsWith(".hbs") && filename.startsWith(template_path + path.sep)) {
const actual_render = actual_load(request, parent, isMain); const actual_render = actual_load(request, parent, isMain);
return template_stub({filename, actual_render}); return template_stub({filename, actual_render});
@ -208,16 +202,8 @@ exports._finish_template_mocking = () => {
used_templates.clear(); used_templates.clear();
}; };
exports._mock_template = (fn, exercise_template, f, {callsite = callsites()[1]} = {}) => { exports._mock_template = (fn, exercise_template, f) => {
const path = "../.." + template_path + fn; template_mocks.set(path.join(template_path, fn), {exercise_template, f});
const resolved_path = Module._resolveFilename(
path,
require.cache[callsite.getFileName()],
false,
);
template_mocks.set(resolved_path, {exercise_template, f});
}; };
exports.mock_esm = (module_path, obj = {}, {callsite = callsites()[1]} = {}) => { exports.mock_esm = (module_path, obj = {}, {callsite = callsites()[1]} = {}) => {

View File

@ -517,7 +517,7 @@ test("test_filter_pm", ({mock_template}) => {
assert.deepEqual(data, expected); assert.deepEqual(data, expected);
}); });
mock_template("user_with_status_icon", false, (data) => { mock_template("user_with_status_icon.hbs", false, (data) => {
assert.deepEqual(data, expected_user_with_icon[i]); assert.deepEqual(data, expected_user_with_icon[i]);
i += 1; i += 1;
}); });

View File

@ -379,7 +379,7 @@ test_ui("zoom_in_and_zoom_out", ({mock_template}) => {
}; };
stream_list.initialize_stream_cursor(); stream_list.initialize_stream_cursor();
mock_template("filter_topics", false, () => "filter-topics-stub"); mock_template("filter_topics.hbs", false, () => "filter-topics-stub");
let filter_topics_appended = false; let filter_topics_appended = false;
$stream_li1.children = () => ({ $stream_li1.children = () => ({
append(html) { append(html) {