PolyGerrit: Fix search bar not to show user email if it's undefined

GWTUI does not show the email if it's undefined.

Other wise it will fail with

"Server error: )]}' "User test \u003cundefined\u003e not found""

Change-Id: I00bc79f5463aa7b4ac6e16387a29ccc246c474ce
This commit is contained in:
Paladox none
2017-08-05 13:05:45 +00:00
parent 27208b1367
commit 4459c1d65d
3 changed files with 108 additions and 36 deletions

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../behaviors/gr-anonymous-name-behavior/gr-anonymous-name-behavior.html">
<link rel="import" href="../../../behaviors/gr-url-encoding-behavior.html">
<link rel="import" href="../../../behaviors/keyboard-shortcut-behavior/keyboard-shortcut-behavior.html">
<link rel="import" href="../../../bower_components/polymer/polymer.html">

View File

@@ -94,6 +94,7 @@
is: 'gr-search-bar',
behaviors: [
Gerrit.AnonymousNameBehavior,
Gerrit.KeyboardShortcutBehavior,
Gerrit.URLEncodingBehavior,
],
@@ -128,6 +129,13 @@
type: Number,
value: 1,
},
_config: Object,
},
attached() {
this.$.restAPI.getConfig().then(cfg => {
this._config = cfg;
});
},
_valueChanged(value) {
@@ -161,6 +169,10 @@
}
},
_accountOrAnon(name) {
return this.getUserName(this._config, name, false);
},
/**
* Fetch from the API the predicted accounts.
* @param {string} predicate - The first part of the search term, e.g.
@@ -177,8 +189,14 @@
MAX_AUTOCOMPLETE_RESULTS)
.then(accounts => {
if (!accounts) { return []; }
return accounts.map(acct =>
predicate + ':"' + acct.name + ' <' + acct.email + '>"');
return accounts.map(acct => {
if (acct.email) {
return predicate + ':"' + this._accountOrAnon(acct) +
' <' + acct.email + '>"';
} else {
return predicate + ':"' + this._accountOrAnon(acct) + '"';
}
});
}).then(accounts => {
// When the expression supplied is a beginning substring of 'self',
// add it as an autocomplete option.

View File

@@ -37,11 +37,17 @@ limitations under the License.
<script>
suite('gr-search-bar tests', () => {
let element;
let sandbox;
setup(() => {
sandbox = sinon.sandbox.create();
element = fixture('basic');
});
teardown(() => {
sandbox.restore();
});
test('value is propagated to _inputVal', () => {
element.value = 'foo';
assert.equal(element._inputVal, 'foo');
@@ -54,7 +60,7 @@ limitations under the License.
};
test('tap on search button triggers nav', done => {
sinon.stub(page, 'show', () => {
sandbox.stub(page, 'show', () => {
page.show.restore();
assert.notEqual(getActiveElement(), element.$.searchInput);
assert.notEqual(getActiveElement(), element.$.searchButton);
@@ -65,7 +71,7 @@ limitations under the License.
});
test('enter in search input triggers nav', done => {
sinon.stub(page, 'show', () => {
sandbox.stub(page, 'show', () => {
page.show.restore();
assert.notEqual(getActiveElement(), element.$.searchInput);
assert.notEqual(getActiveElement(), element.$.searchButton);
@@ -77,27 +83,24 @@ limitations under the License.
});
test('search query should be double-escaped', () => {
const showStub = sinon.stub(page, 'show');
const showStub = sandbox.stub(page, 'show');
element.$.searchInput.text = 'fate/stay';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.equal(showStub.lastCall.args[0], '/q/fate%252Fstay');
showStub.restore();
});
test('input blurred after commit', () => {
const showStub = sinon.stub(page, 'show');
const blurSpy = sinon.spy(element.$.searchInput.$.input, 'blur');
sandbox.stub(page, 'show');
const blurSpy = sandbox.spy(element.$.searchInput.$.input, 'blur');
element.$.searchInput.text = 'fate/stay';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
assert.isTrue(blurSpy.called);
showStub.restore();
blurSpy.restore();
});
test('empty search query does not trigger nav', () => {
const showSpy = sinon.spy(page, 'show');
const showSpy = sandbox.spy(page, 'show');
element.value = '';
MockInteractions.pressAndReleaseKeyOn(element.$.searchInput.$.input, 13,
null, 'enter');
@@ -105,16 +108,16 @@ limitations under the License.
});
test('keyboard shortcuts', () => {
const focusSpy = sinon.spy(element.$.searchInput, 'focus');
const selectAllSpy = sinon.spy(element.$.searchInput, 'selectAll');
const focusSpy = sandbox.spy(element.$.searchInput, 'focus');
const selectAllSpy = sandbox.spy(element.$.searchInput, 'selectAll');
MockInteractions.pressAndReleaseKeyOn(document.body, 191, null, '/');
assert.isTrue(focusSpy.called);
assert.isTrue(selectAllSpy.called);
});
suite('_getSearchSuggestions', () => {
setup(() => {
sinon.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
test('Autocompletes accounts', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
name: 'fred',
@@ -122,27 +125,6 @@ limitations under the License.
},
])
);
sinon.stub(element.$.restAPI, 'getSuggestedGroups', () =>
Promise.resolve({
Polygerrit: 0,
gerrit: 0,
gerrittest: 0,
})
);
sinon.stub(element.$.restAPI, 'getSuggestedProjects', () =>
Promise.resolve({
Polygerrit: 0,
})
);
});
teardown(() => {
element.$.restAPI.getSuggestedAccounts.restore();
element.$.restAPI.getSuggestedGroups.restore();
element.$.restAPI.getSuggestedProjects.restore();
});
test('Autocompletes accounts', done => {
element._getSearchSuggestions('owner:fr').then(s => {
assert.equal(s[0].value, 'owner:"fred <fred@goog.co>"');
done();
@@ -150,6 +132,14 @@ limitations under the License.
});
test('Inserts self as option when valid', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
name: 'fred',
email: 'fred@goog.co',
},
])
);
element._getSearchSuggestions('owner:s').then(s => {
assert.equal(s[0].value, 'owner:self');
}).then(() => {
@@ -161,6 +151,14 @@ limitations under the License.
});
test('Inserts me as option when valid', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
name: 'fred',
email: 'fred@goog.co',
},
])
);
element._getSearchSuggestions('owner:m').then(s => {
assert.equal(s[0].value, 'owner:me');
}).then(() => {
@@ -172,6 +170,13 @@ limitations under the License.
});
test('Autocompletes groups', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedGroups', () =>
Promise.resolve({
Polygerrit: 0,
gerrit: 0,
gerrittest: 0,
})
);
element._getSearchSuggestions('ownerin:pol').then(s => {
assert.equal(s[0].value, 'ownerin:Polygerrit');
done();
@@ -179,6 +184,11 @@ limitations under the License.
});
test('Autocompletes projects', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedProjects', () =>
Promise.resolve({
Polygerrit: 0,
})
);
element._getSearchSuggestions('project:pol').then(s => {
assert.equal(s[0].value, 'project:Polygerrit');
done();
@@ -203,6 +213,13 @@ limitations under the License.
});
test('Autocomplete doesnt override exact matches to input', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedGroups', () =>
Promise.resolve({
Polygerrit: 0,
gerrit: 0,
gerrittest: 0,
})
);
element._getSearchSuggestions('ownerin:gerrit').then(s => {
assert.equal(s[0].value, 'ownerin:gerrit');
done();
@@ -210,6 +227,14 @@ limitations under the License.
});
test('Autocomplete respects spaces', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
name: 'fred',
email: 'fred@goog.co',
},
])
);
element._getSearchSuggestions('is:ope').then(s => {
assert.equal(s[0].name, 'is:open');
assert.equal(s[0].value, 'is:open');
@@ -219,6 +244,34 @@ limitations under the License.
});
});
});
test('Autocompletes accounts with no email', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
name: 'fred',
},
])
);
element._getSearchSuggestions('owner:fr').then(s => {
assert.equal(s[0].value, 'owner:"fred"');
done();
});
});
test('Autocompletes accounts with email', done => {
sandbox.stub(element.$.restAPI, 'getSuggestedAccounts', () =>
Promise.resolve([
{
email: 'fred@goog.co',
},
])
);
element._getSearchSuggestions('owner:fr').then(s => {
assert.equal(s[0].value, 'owner:"Anonymous <fred@goog.co>"');
done();
});
});
});
});
</script>