zjsunit: Add stub_templates abstraction.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2019-07-10 20:06:20 -07:00 committed by Tim Abbott
parent 8761e09eed
commit a0122abf9a
16 changed files with 82 additions and 94 deletions

View File

@ -1,7 +1,6 @@
set_global('$', global.make_zjquery());
set_global('i18n', global.stub_i18n);
set_global('templates', {});
set_global('alert_words', {
words: ['foo', 'bar'],
});
@ -19,10 +18,10 @@ run_test('render_alert_words_ui', () => {
var alert_word_items = $.create('alert_word_items');
word_list.set_find_results('.alert-word-item', alert_word_items);
templates.render = (name, args) => {
global.stub_templates((name, args) => {
assert.equal(name, 'alert_word_settings_item');
return 'stub-' + args.word;
};
});
var new_alert_word = $('#create_alert_word_name');
assert(!new_alert_word.is_focused());

View File

@ -49,7 +49,6 @@ set_global('sent_messages', _sent_messages);
set_global('transmit', {});
set_global('channel', {});
set_global('templates', {});
set_global('echo', {});
set_global('stream_edit', {});
set_global('markdown', {});
@ -122,10 +121,10 @@ run_test('validate_stream_message_address_info', () => {
sub.subscribed = false;
stream_data.add_sub('social', sub);
templates.render = function (template_name) {
global.stub_templates(function (template_name) {
assert.equal(template_name, 'compose_not_subscribed');
return 'compose_not_subscribed_stub';
};
});
assert(!compose.validate_stream_message_address_info('social'));
assert.equal($('#compose-error-msg').html(), 'compose_not_subscribed_stub');
@ -185,10 +184,10 @@ run_test('validate', () => {
$("#zephyr-mirror-error").is = noop;
$("#private_message_recipient").select(noop);
templates.render = function (fn) {
global.stub_templates(function (fn) {
assert.equal(fn, 'input_pill');
return '<div>pill-html</div>';
};
});
}
function add_content_to_compose_box() {
@ -299,11 +298,11 @@ run_test('validate_stream_message', () => {
assert.equal(stream_name, 'social');
return 16;
};
templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'compose_all_everyone');
assert.equal(data.count, 16);
return 'compose_all_everyone_stub';
};
});
var compose_content;
$('#compose-all-everyone').append = function (data) {
compose_content = data;
@ -1084,13 +1083,13 @@ run_test('on_events', () => {
(function () {
var called;
templates.render = function (template_name, context) {
global.stub_templates(function (template_name, context) {
called = true;
assert.equal(template_name, 'compose_invite_users');
assert.equal(context.email, 'foo@bar.com');
assert.equal(context.name, 'Foo Barson');
return 'fake-compose-invite-user-template';
};
});
return function () { assert(called); };
}()),
@ -1134,7 +1133,7 @@ run_test('on_events', () => {
// Now try to mention the same person again. The template should
// not render.
templates.render = noop;
global.stub_templates(noop);
handler({}, data);
assert.equal($('#compose_invite_users').visible(), true);
assert(looked_for_existing);
@ -1362,12 +1361,12 @@ run_test('on_events', () => {
var checks = [
(function () {
var called;
templates.render = function (template_name, context) {
global.stub_templates(function (template_name, context) {
called = true;
assert.equal(template_name, 'compose_private_stream_alert');
assert.equal(context.stream_name, 'Denmark');
return 'fake-compose_private_stream_alert-template';
};
});
return function () { assert(called); };
}()),

View File

@ -44,7 +44,6 @@ set_global('people', {
};
},
});
set_global('templates', {});
set_global('markdown', {
apply_markdown: noop,
});
@ -318,12 +317,12 @@ run_test('format_drafts', () => {
return stub_render_now(time, new XDate(1549958107000));
};
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'draft_table_body');
// Tests formatting and sorting of drafts
assert.deepEqual(data.drafts, expected);
return '<draft table stub>';
};
});
drafts.open_modal = noop;
drafts.set_initial_element = noop;

View File

@ -22,7 +22,6 @@ zrequire('presence');
zrequire('buddy_data');
zrequire('hash_util');
zrequire('Handlebars', 'handlebars');
zrequire('templates');
zrequire('people');
zrequire('pm_conversations');
zrequire('pm_list');
@ -76,10 +75,10 @@ run_test('build_private_messages_list', () => {
var template_data;
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'sidebar_private_message_list');
template_data = data;
};
});
pm_list._build_private_messages_list(active_conversation_1, max_conversations);
@ -115,10 +114,10 @@ run_test('build_private_messages_list', () => {
});
run_test('expand_and_update_private_messages', () => {
global.templates.render = function (template_name) {
global.stub_templates(function (template_name) {
assert.equal(template_name, 'sidebar_private_message_list');
return 'fake-dom-for-pm-list';
};
});
var private_li = $(".top_left_private_messages");
var alice_li = $.create('alice-li-stub');

View File

@ -24,7 +24,6 @@ set_global('page_params', {
custom_profile_fields: [],
});
set_global('rows', {});
set_global('templates', {});
set_global('message_viewport', {
@ -142,7 +141,7 @@ run_test('sender_hover', () => {
return {};
};
templates.render = function (fn, opts) {
global.stub_templates(function (fn, opts) {
switch (fn) {
case 'no_arrow_popover':
assert.deepEqual(opts, {
@ -184,7 +183,7 @@ run_test('sender_hover', () => {
default:
throw Error('unrecognized template: ' + fn);
}
};
});
$('.user_popover_email').each = noop;
@ -233,7 +232,7 @@ run_test('actions_popover', () => {
};
};
templates.render = function (fn, opts) {
global.stub_templates(function (fn, opts) {
// TODO: Test all the properties of the popover
switch (fn) {
case 'actions_popover_content':
@ -244,7 +243,7 @@ run_test('actions_popover', () => {
default:
throw Error('unrecognized template: ' + fn);
}
};
});
handler.call(target, e);
});

View File

@ -50,7 +50,6 @@ set_global('blueslip', global.make_zblueslip());
set_global('page_params', {user_id: 5});
set_global('channel', {});
set_global('templates', {});
set_global('emoji_codes', {
name_to_codepoint: {
alien: '1f47d',
@ -325,7 +324,7 @@ run_test('add_and_remove_reaction', () => {
};
var template_called;
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
template_called = true;
assert.equal(template_name, 'message_reaction');
assert.equal(data.class, 'message_reaction reacted');
@ -333,7 +332,7 @@ run_test('add_and_remove_reaction', () => {
assert.equal(data.message_id, 1001);
assert.equal(data.title, 'You (click to remove) reacted with :8ball:');
return '<new reaction html>';
};
});
var insert_called;
$('<new reaction html>').insertBefore = function (element) {
@ -434,12 +433,12 @@ run_test('add_and_remove_reaction', () => {
};
template_called = false;
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(data.class, 'message_reaction');
assert(data.is_realm_emoji);
template_called = true;
return '<new reaction html>';
};
});
message_reactions.find = function (selector) {
assert.equal(selector, '.reaction_button');

View File

@ -34,14 +34,12 @@ const _page_params = {
const _realm_icon = {};
const _channel = {};
const _templates = {
render: function (name, data) {
if (name === 'settings/admin_realm_domains_list') {
assert(data.realm_domain.domain);
return 'stub-domains-list';
}
},
};
global.stub_templates(function (name, data) {
if (name === 'settings/admin_realm_domains_list') {
assert(data.realm_domain.domain);
return 'stub-domains-list';
}
});
const _overlays = {};
@ -68,7 +66,6 @@ set_global('overlays', _overlays);
set_global('page_params', _page_params);
set_global('realm_icon', _realm_icon);
set_global('realm_logo', _realm_logo);
set_global('templates', _templates);
set_global('ui_report', _ui_report);
zrequire('stream_data');

View File

@ -1,6 +1,5 @@
set_global('page_params', {});
set_global('$', global.make_zjquery());
set_global('templates', {});
set_global('loading', {});
set_global('Sortable', {create: () => {}});
@ -51,11 +50,11 @@ function test_populate(opts) {
loading.destroy_indicator = () => {};
const template_data = [];
templates.render = (fn, data) => {
global.stub_templates((fn, data) => {
assert.equal(fn, 'admin_profile_field_list');
template_data.push(data);
return 'whatever';
};
});
settings_profile_fields.do_populate_profile_fields(fields_data);

View File

@ -14,7 +14,6 @@ var pills = {
var create_item_handler;
set_global('channel', {});
set_global('templates', {});
set_global('blueslip', global.make_zblueslip());
set_global('typeahead_helper', {});
set_global('user_groups', {
@ -102,14 +101,14 @@ run_test('populate_user_groups', () => {
var templates_render_called = false;
var fake_rendered_temp = $.create('fake_admin_user_group_list_template_rendered');
templates.render = function (template, args) {
global.stub_templates(function (template, args) {
assert.equal(template, 'admin_user_group_list');
assert.equal(args.user_group.id, 1);
assert.equal(args.user_group.name, 'Mobile');
assert.equal(args.user_group.description, 'All mobile people');
templates_render_called = true;
return fake_rendered_temp;
};
});
var user_groups_list_append_called = false;
$('#user-groups').append = function (rendered_temp) {
@ -328,9 +327,9 @@ run_test('with_external_user', () => {
return noop;
};
templates.render = function () {
global.stub_templates(function () {
return noop;
};
});
people.get_person_from_user_id = function () {
return noop;

View File

@ -1,8 +1,6 @@
set_global('document', 'document-stub');
set_global('$', global.make_zjquery());
set_global('templates', {});
zrequire('unread_ui');
zrequire('Filter', 'js/filter');
zrequire('util');
@ -74,11 +72,11 @@ run_test('create_sidebar_row', () => {
devel_count.set_find_results('.value', devel_value);
devel_count.set_parent(sidebar_row);
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'stream_sidebar_row');
assert.equal(data.uri, '#narrow/stream/100-devel');
return '<devel sidebar row>';
};
});
stream_list.create_sidebar_row(devel);
assert.equal(devel_value.text(), '42');
@ -93,11 +91,11 @@ run_test('create_sidebar_row', () => {
social_count.set_find_results('.value', social_value);
social_count.set_parent(sidebar_row);
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'stream_sidebar_row');
assert.equal(data.uri, '#narrow/stream/200-social');
return '<social sidebar row>';
};
});
stream_list.create_sidebar_row(social);
assert.equal(social_value.text(), '42');
@ -132,12 +130,12 @@ run_test('create_sidebar_row', () => {
social.invite_only = true;
social.color = '#222222';
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'stream_privacy');
assert.equal(data.invite_only, true);
assert.equal(data.dark_background, 'dark_background');
return '<div>privacy-html';
};
});
stream_list.redraw_stream_privacy(social);
assert.equal(privacy_elem.html(), '<div>privacy-html');
@ -693,7 +691,7 @@ run_test('rename_stream', () => {
const li_stub = $.create('li stub');
li_stub.length = 0;
templates.render = (name, payload) => {
global.stub_templates((name, payload) => {
assert.equal(name, 'stream_sidebar_row');
assert.deepEqual(payload, {
name: 'Development',
@ -707,7 +705,7 @@ run_test('rename_stream', () => {
dark_background: payload.dark_background,
});
return {to_$: () => li_stub};
};
});
var count_updated;
stream_list.update_count_in_dom = (li) => {
@ -740,9 +738,9 @@ run_test('refresh_pin', () => {
const li_stub = $.create('li stub');
li_stub.length = 0;
templates.render = () => {
global.stub_templates(() => {
return {to_$: () => li_stub};
};
});
stream_list.update_count_in_dom = noop;
$('#stream_filters').append = noop;
@ -771,10 +769,10 @@ run_test('create_initial_sidebar_rows', () => {
stream_list.update_count_in_dom = noop;
global.templates.render = function (template_name, data) {
global.stub_templates(function (template_name, data) {
assert.equal(template_name, 'stream_sidebar_row');
return '<div>stub-html-' + data.name;
};
});
// Test this code with stubs above...
stream_list.create_initial_sidebar_rows();

View File

@ -1,6 +1,5 @@
global.stub_out_jquery();
set_global('templates', {});
set_global('ui', {
get_content_element: element => element,
get_scroll_element: element => element,
@ -71,10 +70,10 @@ run_test('filter_table', () => {
var populated_subs;
templates.render = (fn, data) => {
global.stub_templates((fn, data) => {
assert.equal(fn, 'subscriptions');
populated_subs = data.subscriptions;
};
});
subs.populate_stream_settings_left_panel();

View File

@ -5,7 +5,6 @@ set_global('narrow_state', {});
set_global('unread', {});
set_global('muting', {});
set_global('stream_popover', {});
set_global('templates', {});
set_global('message_list', {});
zrequire('hash_util');
@ -43,7 +42,7 @@ run_test('topic_list_build_widget', () => {
var checked_mutes;
var rendered;
templates.render = function (name, info) {
global.stub_templates(function (name, info) {
assert.equal(name, 'topic_list_item');
var expected = {
topic_name: 'coding',
@ -55,7 +54,7 @@ run_test('topic_list_build_widget', () => {
assert.deepEqual(info, expected);
rendered = true;
return '<topic list item>';
};
});
muting.is_topic_muted = function (stream_id, topic_name) {
assert.equal(stream_id, devel.stream_id);

View File

@ -1,6 +1,5 @@
set_global('i18n', global.stub_i18n);
set_global('page_params', {realm_is_zephyr_mirror_realm: false});
set_global('templates', {});
set_global('md5', function (s) {
return 'md5-' + s;
});
@ -413,13 +412,13 @@ run_test('render_person when emails hidden', () => {
// Test render_person with regular person, under hidden email visiblity case
settings_org.show_email = () => false;
var rendered = false;
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, matches[2].full_name);
assert.equal(args.secondary, undefined);
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_person(matches[2]), 'typeahead-item-stub');
assert(rendered);
});
@ -428,13 +427,13 @@ run_test('render_person', () => {
settings_org.show_email = () => true;
// Test render_person with regular person
var rendered = false;
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, matches[1].full_name);
assert.equal(args.secondary, matches[1].email);
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_person(matches[1]), 'typeahead-item-stub');
assert(rendered);
@ -448,25 +447,25 @@ run_test('render_person', () => {
special_item_text: "special_text",
};
rendered = false;
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, special_person.special_item_text);
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_person(special_person), 'typeahead-item-stub');
assert(rendered);
});
run_test('clear_rendered_person', () => {
var rendered = false;
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, matches[5].full_name);
assert.equal(args.secondary, matches[5].email);
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_person(matches[5]), 'typeahead-item-stub');
assert(rendered);
@ -492,13 +491,13 @@ run_test('render_stream', () => {
stream_id: 42,
name: 'Short Description',
};
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, stream.name);
assert.equal(args.secondary, stream.description);
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_stream(stream), 'typeahead-item-stub');
assert(rendered);
@ -509,14 +508,14 @@ run_test('render_stream', () => {
stream_id: 43,
name: 'Long Description',
};
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.equal(args.primary, stream.name);
var short_desc = stream.description.substring(0, 35);
assert.equal(args.secondary, short_desc + "...");
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_stream(stream), 'typeahead-item-stub');
assert(rendered);
});
@ -532,7 +531,7 @@ run_test('render_emoji', () => {
realm_emoji: 'TBD',
};
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.deepEqual(args, {
primary: 'thumbs up',
@ -543,7 +542,7 @@ run_test('render_emoji', () => {
});
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_emoji(test_emoji), 'typeahead-item-stub');
assert(rendered);
@ -554,7 +553,7 @@ run_test('render_emoji', () => {
emoji_url: 'TBD',
};
global.templates.render = function (template_name, args) {
global.stub_templates(function (template_name, args) {
assert.equal(template_name, 'typeahead_list_item');
assert.deepEqual(args, {
primary: 'realm emoji',
@ -565,7 +564,7 @@ run_test('render_emoji', () => {
});
rendered = true;
return 'typeahead-item-stub';
};
});
assert.equal(th.render_emoji(test_emoji), 'typeahead-item-stub');
assert(rendered);
});

View File

@ -33,7 +33,6 @@ set_global('$', () => {});
set_global('resize', {});
set_global('feature_flags', {});
set_global('page_params', {});
set_global('templates', {});
const ignore_modules = [
'activity',
@ -73,7 +72,7 @@ _.each(ignore_modules, (mod) => {
zrequire('util');
util.is_mobile = () => false;
templates.render = () => 'some-html';
global.stub_templates(() => 'some-html');
ui.get_scroll_element = element => element;
zrequire('echo');

View File

@ -5,7 +5,6 @@ set_global('i18n', global.stub_i18n);
set_global('people', {});
set_global('blueslip', global.make_zblueslip());
set_global('templates', {});
const noop = () => {};
const return_false = () => false;
@ -140,14 +139,14 @@ run_test('poll_data_holder my question', () => {
run_test('activate another person poll', () => {
people.is_my_user_id = return_false;
templates.render = (template_name) => {
global.stub_templates((template_name) => {
if (template_name === 'widgets/poll_widget') {
return 'widgets/poll_widget';
}
if (template_name === 'widgets/poll_widget_results') {
return 'widgets/poll_widget_results';
}
};
});
const widget_elem = $('<div>').addClass('widget-content');
@ -305,14 +304,14 @@ run_test('activate own poll', () => {
$.clear_all_elements();
people.is_my_user_id = return_true;
templates.render = (template_name) => {
global.stub_templates((template_name) => {
if (template_name === 'widgets/poll_widget') {
return 'widgets/poll_widget';
}
if (template_name === 'widgets/poll_widget_results') {
return 'widgets/poll_widget_results';
}
};
});
const widget_elem = $('<div>').addClass('widget-content');
let out_data;

View File

@ -67,6 +67,12 @@ global.make_zblueslip = require('./zblueslip.js').make_zblueslip;
// Set up fake translation
global.stub_i18n = require('./i18n.js');
// Set up Handlebars
global.stub_templates = stub => {
set_global('templates', {});
templates.render = stub;
};
var noop = function () {};
// Set up fake module.hot