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
This commit is contained in:
Becky Siegel
2017-11-20 13:52:02 -08:00
parent 0dfd7fe57d
commit 53df325cb8
6 changed files with 53 additions and 28 deletions

View File

@@ -42,15 +42,23 @@ limitations under the License.
.text:hover { .text:hover {
@apply --gr-account-label-text-hover-style; @apply --gr-account-label-text-hover-style;
} }
.email,
.showEmail .name {
display: none;
}
.showEmail .email {
display: inline-block;
}
</style> </style>
<span> <span>
<template is="dom-if" if="[[!hideAvatar]]"> <template is="dom-if" if="[[!hideAvatar]]">
<gr-avatar account="[[account]]" <gr-avatar account="[[account]]"
image-size="[[avatarImageSize]]"></gr-avatar> image-size="[[avatarImageSize]]"></gr-avatar>
</template> </template>
<span class="text"> <span class$="text [[_computeShowEmailClass(account)]]">
<span>[[_computeName(account, _serverConfig)]]</span> <span class="name">
<span hidden$="[[!_computeShowEmail(showEmail, account)]]"> [[_computeName(account, _serverConfig)]]</span>
<span class="email">
[[_computeEmailStr(account)]] [[_computeEmailStr(account)]]
</span> </span>
<template is="dom-if" if="[[account.status]]"> <template is="dom-if" if="[[account.status]]">

View File

@@ -26,10 +26,6 @@
type: Number, type: Number,
value: 32, value: 32,
}, },
showEmail: {
type: Boolean,
value: false,
},
title: { title: {
type: String, type: String,
reflectToAttribute: true, reflectToAttribute: true,
@@ -76,8 +72,9 @@
return result; return result;
}, },
_computeShowEmail(showEmail, account) { _computeShowEmailClass(account) {
return !!(showEmail && account && account.email); if (!account || account.name || !account.email) { return ''; }
return 'showEmail';
}, },
_computeEmailStr(account) { _computeEmailStr(account) {

View File

@@ -78,23 +78,21 @@ limitations under the License.
}), }),
'Anonymous <andybons+gerrit@gmail.com>'); 'Anonymous <andybons+gerrit@gmail.com>');
assert.equal(element._computeShowEmail(true, assert.equal(element._computeShowEmailClass(
{ {
name: 'Andrew Bonventre', name: 'Andrew Bonventre',
email: 'andybons+gerrit@gmail.com', email: 'andybons+gerrit@gmail.com',
}), true); }), '');
assert.equal(element._computeShowEmail(true, assert.equal(element._computeShowEmailClass(
{name: 'Andrew Bonventre'}), false); {
email: 'andybons+gerrit@gmail.com',
}), 'showEmail');
assert.equal(element._computeShowEmail(false, assert.equal(element._computeShowEmailClass({name: 'Andrew Bonventre'}),
{name: 'Andrew Bonventre'}), false); '');
assert.equal(element._computeShowEmail( assert.equal(element._computeShowEmailClass(undefined), '');
true, undefined), false);
assert.equal(element._computeShowEmail(
false, undefined), false);
assert.equal( assert.equal(
element._computeEmailStr({name: 'test', email: 'test'}), '(test)'); element._computeEmailStr({name: 'test', email: 'test'}), '(test)');

View File

@@ -39,8 +39,7 @@ limitations under the License.
<span> <span>
<a href$="[[_computeOwnerLink(account)]]" tabindex="-1"> <a href$="[[_computeOwnerLink(account)]]" tabindex="-1">
<gr-account-label account="[[account]]" <gr-account-label account="[[account]]"
avatar-image-size="[[avatarImageSize]]" avatar-image-size="[[avatarImageSize]]"></gr-account-label>
show-email="[[_computeShowEmail(account)]]"></gr-account-label>
</a> </a>
</span> </span>
</template> </template>

View File

@@ -35,9 +35,5 @@
account.email || account.username || account.name || account.email || account.username || account.name ||
account._account_id); account._account_id);
}, },
_computeShowEmail(account) {
return !!(account && !account.name);
},
}); });
})(); })();

View File

@@ -34,17 +34,44 @@ limitations under the License.
<script> <script>
suite('gr-account-link tests', () => { suite('gr-account-link tests', () => {
let element; let element;
let sandbox;
setup(() => { setup(() => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getConfig() { return Promise.resolve({}); }, getConfig() { return Promise.resolve({}); },
}); });
element = fixture('basic'); element = fixture('basic');
sandbox = sinon.sandbox.create();
});
teardown(() => {
sandbox.restore();
}); });
test('computed fields', () => { test('computed fields', () => {
assert.equal(element._computeShowEmail({name: 'asd'}), false); const url = 'test/url';
assert.equal(element._computeShowEmail({}), true); 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> </script>