node tests: Simplify compiling of templates.

Now we just have two methods of importance:

    compile_template
    render_template

The compile_template() function will automatically
(and recursively) compile any partials it depends
on and mark those as compiled.
This commit is contained in:
Steve Howell
2016-11-04 08:34:27 -07:00
committed by Tim Abbott
parent 0085164ad9
commit a2874a3fe9
6 changed files with 24 additions and 56 deletions

View File

@@ -30,14 +30,6 @@ var window = jsdom.jsdom().defaultView;
global.$ = require('jquery')(window);
var _ = global._;
// When writing these tests, the following command might be helpful:
// ./tools/get-handlebar-vars static/templates/*.handlebars
function render(template_name, args) {
global.use_template(template_name);
return global.templates.render(template_name, args);
}
(function test_t_tag() {
var args = {
"message": {
@@ -53,7 +45,7 @@ function render(template_name, args) {
};
var html = '<div style="height: 250px">';
html += render('actions_popover_content', args);
html += global.render_template('actions_popover_content', args);
html += "</div>";
var link = $(html).find("a.respond_button");
assert.equal(link.text().trim(), 'French');
@@ -84,10 +76,10 @@ function render(template_name, args) {
};
fs.readdirSync(path.join(__dirname, "../../static/templates/", "settings")).forEach(function (o) {
render(o.replace(/\.handlebars/, ""));
global.render_template(o.replace(/\.handlebars/, ""));
});
var html = render('settings_tab', args);
var html = global.render_template('settings_tab', args);
var div = $(html).find("div.notification-reminder");
assert.equal(div.text().trim(), 'Some French text with Zulip');
global.write_test_output("test_tr_tag settings", html);

View File

@@ -32,8 +32,8 @@ $.fn.expectOne = function () {
return this;
};
global.use_template('user_presence_row');
global.use_template('user_presence_rows');
global.compile_template('user_presence_row');
global.compile_template('user_presence_rows');
var people = require("js/people.js");
var activity = require('js/activity.js');

View File

@@ -29,10 +29,10 @@ $.fn.expectOne = function () {
return this;
};
global.use_template('sidebar_private_message_list');
global.use_template('stream_sidebar_row');
global.use_template('stream_privacy');
global.use_template('topic_list_item');
global.compile_template('sidebar_private_message_list');
global.compile_template('stream_sidebar_row');
global.compile_template('stream_privacy');
global.compile_template('topic_list_item');
(function test_topic_list_build_widget() {
var stream = "devel";

View File

@@ -32,10 +32,7 @@ var _ = global._;
// ./tools/get-handlebar-vars static/templates/*.handlebars
function render(template_name, args) {
global.partial_finder(template_name, function (name) {
global.use_template(name);
});
return global.templates.render(template_name, args);
return global.render_template(template_name, args);
}
fs.readdirSync(path.join(__dirname, "../../static/templates/", "settings")).forEach(function (o) {

View File

@@ -21,10 +21,10 @@ global.stub_out_jquery = namespace.stub_out_jquery;
// Set up helpers to render templates.
var render = require('./render.js');
global.use_template = render.use_template;
global.make_sure_all_templates_have_been_compiled = render.make_sure_all_templates_have_been_compiled;
global.partial_finder = render.partial_finder;
global.find_included_partials = render.find_included_partials;
global.compile_template = render.compile_template;
global.render_template = render.render_template;
global.walk = render.walk;
// Set up helpers to output HTML

View File

@@ -35,7 +35,18 @@ exports.make_sure_all_templates_have_been_compiled = function () {
});
};
exports.use_template = function (name) {
exports.render_template = function (name, args) {
exports.compile_template(name);
return global.templates.render(name, args);
};
exports.compile_template = function (name) {
var included_fns = exports.find_included_partials(name);
_.each(included_fns, function (fn) {
exports.compile_template(fn);
});
if (Handlebars.templates === undefined) {
Handlebars.templates = {};
}
@@ -138,38 +149,6 @@ exports.find_included_partials = function (name) {
return lst;
};
exports.partial_finder = (function () {
var meta = {
read: []
};
// this is the external function that is called that will recursively search
// for partials in a file and partials inside partials until it finds them all.
// it then adds them to a maintenance list of already read partials so that
// they don't have to be read/searched again.
var __prototype__ = function (name, callback) {
if (meta.read.indexOf(name) === -1) {
if (callback) {
callback(name);
}
meta.read.push(name);
var included_fns = exports.find_included_partials(name);
_.each(included_fns, function (fn) {
__prototype__(fn, callback);
});
}
};
return __prototype__;
}());
fs.readdirSync(path.join(__dirname, "../../static/templates/", "settings")).forEach(function (o) {
exports.use_template(o.replace(/\.handlebars/, ""));
});
return exports;
}());
module.exports = render;