From 6657cddd7abadaabc82ed98731171597894d96df Mon Sep 17 00:00:00 2001 From: sahil839 Date: Mon, 6 Apr 2020 23:44:36 +0530 Subject: [PATCH] refractor: Move get_visible_email from popovers.js to people.js. This commit moves the get_visible_email function to people.js as this function will be used in other places and people.js seems relevant file for this. Tests are added to get full coverage. --- frontend_tests/node_tests/people.js | 24 ++++++++++++++++++++++++ static/js/people.js | 7 +++++++ static/js/popovers.js | 11 ++--------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/frontend_tests/node_tests/people.js b/frontend_tests/node_tests/people.js index c2fc176dd2..496aaedc0f 100644 --- a/frontend_tests/node_tests/people.js +++ b/frontend_tests/node_tests/people.js @@ -1056,3 +1056,27 @@ run_test('emails_strings_to_user_ids_array', function () { user_ids = people.emails_strings_to_user_ids_array('dummyuser@example.com'); assert.equal(user_ids, undefined); }); + +run_test('get_visible_email', function () { + const steven = { + email: 'steven@example.com', + delivery_email: 'steven-delivery@example.com', + user_id: 7, + full_name: 'Steven', + }; + + const maria = { + email: 'maria@example.com', + user_id: 728, + full_name: 'Maria', + }; + + people.add(steven); + people.add(maria); + + let email = people.get_visible_email(steven); + assert.equal(email, steven.delivery_email); + + email = people.get_visible_email(maria); + assert.equal(email, maria.email); +}); diff --git a/static/js/people.js b/static/js/people.js index 74d464e83d..ffa0198c32 100644 --- a/static/js/people.js +++ b/static/js/people.js @@ -102,6 +102,13 @@ exports.update_email = function (user_id, new_email) { // still work correctly. }; +exports.get_visible_email = function (user) { + if (user.delivery_email) { + return user.delivery_email; + } + return user.email; +}; + exports.get_user_id = function (email) { const person = exports.get_by_email(email); if (person === undefined) { diff --git a/static/js/popovers.js b/static/js/popovers.js index 73185e76dc..7b2565982b 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -149,13 +149,6 @@ function get_custom_profile_field_data(user, field, field_types, dateFormat) { return profile_field; } -function get_visible_email(user) { - if (user.delivery_email) { - return user.delivery_email; - } - return user.email; -} - function render_user_info_popover(user, popover_element, is_sender_popover, private_msg_class, template_class, popover_placement) { const is_me = people.is_my_user_id(user.user_id); @@ -184,7 +177,7 @@ function render_user_info_popover(user, popover_element, is_sender_popover, priv sent_by_uri: hash_util.by_sender_uri(user.email), show_email: settings_data.show_email(), show_user_profile: !(user.is_bot || page_params.custom_profile_fields.length === 0), - user_email: get_visible_email(user), + user_email: people.get_visible_email(user), user_full_name: user.full_name, user_id: user.user_id, user_last_seen_time_status: buddy_data.user_last_seen_time_status(user.user_id), @@ -307,7 +300,7 @@ exports.show_user_profile = function (user) { const args = { full_name: user.full_name, - email: get_visible_email(user), + email: people.get_visible_email(user), profile_data: profile_data, user_avatar: "avatar/" + user.email + "/medium", is_me: people.is_current_user(user.email),