Fix 'undefined' showing up in account labels

Previously, when there was an account label with an email address but
not a username, the username appeared as 'anonymous' along with the
email address. This updates the label to either show a name or an
email, but not both. In the case of cc's, which always have an email but
often not a username, the email will be shown.

Bug: Issue 7068
Change-Id: I0dad6bca3cec72d90e7807c5efd9f897e66e7189
diff --git a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.html b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.html
index 1d9a943..2a79945 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.html
+++ b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.html
@@ -42,15 +42,23 @@
       .text:hover {
         @apply --gr-account-label-text-hover-style;
       }
+      .email,
+      .showEmail .name {
+        display: none;
+      }
+      .showEmail .email {
+        display: inline-block;
+      }
     </style>
     <span>
       <template is="dom-if" if="[[!hideAvatar]]">
         <gr-avatar account="[[account]]"
             image-size="[[avatarImageSize]]"></gr-avatar>
       </template>
-      <span class="text">
-        <span>[[_computeName(account, _serverConfig)]]</span>
-        <span hidden$="[[!_computeShowEmail(showEmail, account)]]">
+      <span class$="text [[_computeShowEmailClass(account)]]">
+        <span class="name">
+          [[_computeName(account, _serverConfig)]]</span>
+        <span class="email">
           [[_computeEmailStr(account)]]
         </span>
         <template is="dom-if" if="[[account.status]]">
diff --git a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.js b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.js
index 2dee2f6..192eb83 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.js
+++ b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label.js
@@ -26,10 +26,6 @@
         type: Number,
         value: 32,
       },
-      showEmail: {
-        type: Boolean,
-        value: false,
-      },
       title: {
         type: String,
         reflectToAttribute: true,
@@ -76,8 +72,9 @@
       return result;
     },
 
-    _computeShowEmail(showEmail, account) {
-      return !!(showEmail && account && account.email);
+    _computeShowEmailClass(account) {
+      if (!account || account.name || !account.email) { return ''; }
+      return 'showEmail';
     },
 
     _computeEmailStr(account) {
diff --git a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label_test.html b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label_test.html
index 731c9b7..3087b0e 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-account-label/gr-account-label_test.html
@@ -78,23 +78,21 @@
           }),
           'Anonymous <andybons+gerrit@gmail.com>');
 
-      assert.equal(element._computeShowEmail(true,
+      assert.equal(element._computeShowEmailClass(
           {
             name: 'Andrew Bonventre',
             email: 'andybons+gerrit@gmail.com',
-          }), true);
+          }), '');
 
-      assert.equal(element._computeShowEmail(true,
-          {name: 'Andrew Bonventre'}), false);
+      assert.equal(element._computeShowEmailClass(
+          {
+            email: 'andybons+gerrit@gmail.com',
+          }), 'showEmail');
 
-      assert.equal(element._computeShowEmail(false,
-          {name: 'Andrew Bonventre'}), false);
+      assert.equal(element._computeShowEmailClass({name: 'Andrew Bonventre'}),
+          '');
 
-      assert.equal(element._computeShowEmail(
-          true, undefined), false);
-
-      assert.equal(element._computeShowEmail(
-          false, undefined), false);
+      assert.equal(element._computeShowEmailClass(undefined), '');
 
       assert.equal(
           element._computeEmailStr({name: 'test', email: 'test'}), '(test)');
diff --git a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.html b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.html
index 79747ba..7753c96 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.html
+++ b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.html
@@ -39,8 +39,7 @@
     <span>
       <a href$="[[_computeOwnerLink(account)]]" tabindex="-1">
         <gr-account-label account="[[account]]"
-            avatar-image-size="[[avatarImageSize]]"
-            show-email="[[_computeShowEmail(account)]]"></gr-account-label>
+            avatar-image-size="[[avatarImageSize]]"></gr-account-label>
       </a>
     </span>
   </template>
diff --git a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.js b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.js
index 7a120c0..9908714 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.js
+++ b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link.js
@@ -35,9 +35,5 @@
           account.email || account.username || account.name ||
           account._account_id);
     },
-
-    _computeShowEmail(account) {
-      return !!(account && !account.name);
-    },
   });
 })();
diff --git a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link_test.html b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link_test.html
index 11b099b..0c9661f 100644
--- a/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-account-link/gr-account-link_test.html
@@ -34,17 +34,44 @@
 <script>
   suite('gr-account-link tests', () => {
     let element;
+    let sandbox;
 
     setup(() => {
       stub('gr-rest-api-interface', {
         getConfig() { return Promise.resolve({}); },
       });
       element = fixture('basic');
+      sandbox = sinon.sandbox.create();
+    });
+
+    teardown(() => {
+      sandbox.restore();
     });
 
     test('computed fields', () => {
-      assert.equal(element._computeShowEmail({name: 'asd'}), false);
-      assert.equal(element._computeShowEmail({}), true);
+      const url = 'test/url';
+      const urlStub = sandbox.stub(Gerrit.Nav, 'getUrlForOwner').returns(url);
+      const account = {
+        email: 'email',
+        username: 'username',
+        name: 'name',
+        _account_id: '_account_id',
+      };
+      assert.isNotOk(element._computeOwnerLink());
+      assert.equal(element._computeOwnerLink(account), url);
+      assert.isTrue(urlStub.lastCall.calledWithExactly('email'));
+
+      delete account.email;
+      assert.equal(element._computeOwnerLink(account), url);
+      assert.isTrue(urlStub.lastCall.calledWithExactly('username'));
+
+      delete account.username;
+      assert.equal(element._computeOwnerLink(account), url);
+      assert.isTrue(urlStub.lastCall.calledWithExactly('name'));
+
+      delete account.name;
+      assert.equal(element._computeOwnerLink(account), url);
+      assert.isTrue(urlStub.lastCall.calledWithExactly('_account_id'));
     });
   });
 </script>