compose: Avoid render for duplicate mentions.

There is no reason to render the template for compose mention
warnings if the user is already in the widget.

This commit also restructures the unit test significantly to more
carefully exercise each case, particularly in regard to when
templates get rendered.
This commit is contained in:
Steve Howell
2017-07-09 08:13:42 -04:00
parent 05a21b3729
commit 1cf18cfbeb
2 changed files with 83 additions and 28 deletions

View File

@@ -874,38 +874,93 @@ function test_with_mock_socket(test_params) {
}, },
}; };
function setup(msg_type, is_zephyr_mirror, mentioned_full_name) { $('#compose_invite_users .compose_invite_user').length = 0;
compose_fade.would_receive_message = function (email) {
assert.equal(email, 'foo@bar.com'); function test_noop_case(msg_type, is_zephyr_mirror, mentioned_full_name) {
return false;
};
templates.render = function (template_name, context) {
assert.equal(template_name, 'compose-invite-users');
assert.equal(context.email, 'foo@bar.com');
assert.equal(context.name, 'foobar');
return 'fake-compose-invite-user-template';
};
$("#compose_invite_users").append = function (html) {
assert.equal(html, 'fake-compose-invite-user-template');
};
$("#compose_invite_users").hide();
compose_state.set_message_type(msg_type); compose_state.set_message_type(msg_type);
page_params.realm_is_zephyr_mirror_realm = is_zephyr_mirror; page_params.realm_is_zephyr_mirror_realm = is_zephyr_mirror;
data.mentioned.full_name = mentioned_full_name; data.mentioned.full_name = mentioned_full_name;
}
function test(msg_type, is_zephyr_mirror, mentioned_full_name,
compose_invite_users_visible) {
setup(msg_type, is_zephyr_mirror, mentioned_full_name);
handler({}, data); handler({}, data);
assert.equal($('#compose_invite_users').visible(), assert.equal($('#compose_invite_users').visible(), false);
compose_invite_users_visible);
} }
test('private', true, 'everyone', false); test_noop_case('private', true, 'everyone');
test('stream', true, 'everyone', false); test_noop_case('stream', true, 'everyone');
test('stream', false, 'everyone', false); test_noop_case('stream', false, 'everyone');
test('stream', false, 'foobar', true);
// Test mentioning a user that should gets a warning.
$("#compose_invite_users").hide();
compose_state.set_message_type('stream');
page_params.realm_is_zephyr_mirror_realm = false;
var checks = [
(function () {
var called;
compose_fade.would_receive_message = function (email) {
called = true;
assert.equal(email, 'foo@bar.com');
return false;
};
return function () { assert(called); };
}()),
(function () {
var called;
templates.render = 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); };
}()),
(function () {
var called;
$("#compose_invite_users").append = function (html) {
called = true;
assert.equal(html, 'fake-compose-invite-user-template');
};
return function () { assert(called); };
}()),
];
data = {
mentioned: {
email: 'foo@bar.com',
full_name: 'Foo Barson',
},
};
handler({}, data);
assert.equal($('#compose_invite_users').visible(), true);
_.each(checks, function (f) { f(); });
// Simulate that the row was added to the DOM.
var warning_row = $('<warning row>');
var looked_for_existing;
warning_row.data = function (field) {
assert.equal(field, 'useremail');
looked_for_existing = true;
return 'foo@bar.com';
};
var previous_users = $('#compose_invite_users .compose_invite_user');
previous_users.length = 1;
previous_users[0] = warning_row;
// Now try to mention the same person again. The template should
// not render.
templates.render = noop;
handler({}, data);
assert.equal($('#compose_invite_users').visible(), true);
assert(looked_for_existing);
}()); }());
(function test_compose_all_everyone_confirm_clicked() { (function test_compose_all_everyone_confirm_clicked() {

View File

@@ -653,8 +653,6 @@ exports.initialize = function () {
} }
if (compose_fade.would_receive_message(email) === false) { if (compose_fade.would_receive_message(email) === false) {
var new_row = templates.render("compose-invite-users",
{email: email, name: data.mentioned.full_name});
var error_area = $("#compose_invite_users"); var error_area = $("#compose_invite_users");
var existing_invites_area = $('#compose_invite_users .compose_invite_user'); var existing_invites_area = $('#compose_invite_users .compose_invite_user');
@@ -663,6 +661,8 @@ exports.initialize = function () {
}); });
if (existing_invites.indexOf(email) === -1) { if (existing_invites.indexOf(email) === -1) {
var context = {email: email, name: data.mentioned.full_name};
var new_row = templates.render("compose-invite-users", context);
error_area.append(new_row); error_area.append(new_row);
} }