ES6ify /gr-change-metadata/*

Bug: Issue 6179
Change-Id: I3e66ec0bf5d78c0b696ebc465da6166dad1447b5
This commit is contained in:
Kasper Nilsson
2017-05-16 12:45:21 -07:00
parent 3172573751
commit 5514e0b5d7
4 changed files with 157 additions and 157 deletions

View File

@@ -40,76 +40,76 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-change-metadata integration tests', function() {
var sandbox;
var element;
suite('gr-change-metadata integration tests', () => {
let sandbox;
let element;
var sectionSelectors = [
const sectionSelectors = [
'section.assignee',
'section.labelStatus',
'section.strategy',
'section.topic',
];
var getStyle = function(selector, name) {
const getStyle = function(selector, name) {
return window.getComputedStyle(
Polymer.dom(element.root).querySelector(selector))[name];
};
setup(function() {
setup(() => {
sandbox = sinon.sandbox.create();
stub('gr-change-metadata', {
_computeShowLabelStatus: function() { return true; },
_computeShowReviewersByState: function() { return true; },
ready: function() {
_computeShowLabelStatus() { return true; },
_computeShowReviewersByState() { return true; },
ready() {
this.change = {labels: []};
this.serverConfig = {};
},
});
});
teardown(function() {
teardown(() => {
Gerrit._pluginsPending = -1;
Gerrit._allPluginsPromise = undefined;
sandbox.restore();
});
suite('by default', function() {
setup(function(done) {
suite('by default', () => {
setup(done => {
element = fixture('element');
flush(done);
});
sectionSelectors.forEach(function(sectionSelector) {
test(sectionSelector + ' does not have display: none', function() {
for (const sectionSelector of sectionSelectors) {
test(sectionSelector + ' does not have display: none', () => {
assert.notEqual(getStyle(sectionSelector, 'display'), 'none');
});
});
}
});
suite('with plugin style', function() {
setup(function(done) {
var pluginHost = fixture('plugin-host');
suite('with plugin style', () => {
setup(done => {
const pluginHost = fixture('plugin-host');
pluginHost.config = {
js_resource_paths: [],
html_resource_paths: [
new URL('test/plugin.html', window.location.href).toString(),
]
],
};
element = fixture('element');
var importSpy = sandbox.spy(element.$.externalStyle, '_import');
Gerrit.awaitPluginsLoaded().then(function() {
Promise.all(importSpy.returnValues).then(function() {
const importSpy = sandbox.spy(element.$.externalStyle, '_import');
Gerrit.awaitPluginsLoaded().then(() => {
Promise.all(importSpy.returnValues).then(() => {
flush(done);
});
});
});
sectionSelectors.forEach(function(sectionSelector) {
test(sectionSelector + ' may have display: none', function() {
for (const sectionSelector of sectionSelectors) {
test(sectionSelector + ' may have display: none', () => {
assert.equal(getStyle(sectionSelector, 'display'), 'none');
});
});
}
});
});
</script>

View File

@@ -14,7 +14,7 @@
(function() {
'use strict';
var SubmitTypeLabel = {
const SubmitTypeLabel = {
FAST_FORWARD_ONLY: 'Fast Forward Only',
MERGE_IF_NECESSARY: 'Merge if Necessary',
REBASE_IF_NECESSARY: 'Rebase if Necessary',
@@ -61,15 +61,15 @@
'_assigneeChanged(_assignee.*)',
],
_changeChanged: function(change) {
_changeChanged(change) {
this._assignee = change.assignee ? [change.assignee] : [];
},
_assigneeChanged: function(assigneeRecord) {
_assigneeChanged(assigneeRecord) {
if (!this.change) { return; }
var assignee = assigneeRecord.base;
const assignee = assigneeRecord.base;
if (assignee.length) {
var acct = assignee[0];
const acct = assignee[0];
if (this.change.assignee &&
acct._account_id === this.change.assignee._account_id) { return; }
this.set(['change', 'assignee'], acct);
@@ -81,7 +81,7 @@
}
},
_computeHideStrategy: function(change) {
_computeHideStrategy(change) {
return !this.changeIsOpen(change.status);
},
@@ -89,7 +89,7 @@
* This is a whitelist of web link types that provide direct links to
* the commit in the url property.
*/
_isCommitWebLink: function(link) {
_isCommitWebLink(link) {
return link.name === 'gitiles' || link.name === 'gitweb';
},
@@ -99,34 +99,34 @@
* an existential check can be used to hide or show the webLinks
* section.
*/
_computeWebLinks: function(commitInfo) {
if (!commitInfo || !commitInfo.web_links) { return null }
_computeWebLinks(commitInfo) {
if (!commitInfo || !commitInfo.web_links) { return null; }
// We are already displaying these types of links elsewhere,
// don't include in the metadata links section.
var webLinks = commitInfo.web_links.filter(
function(l) {return !this._isCommitWebLink(l); }.bind(this));
const webLinks = commitInfo.web_links.filter(
l => {return !this._isCommitWebLink(l); });
return webLinks.length ? webLinks : null;
},
_computeStrategy: function(change) {
_computeStrategy(change) {
return SubmitTypeLabel[change.submit_type];
},
_computeLabelNames: function(labels) {
_computeLabelNames(labels) {
return Object.keys(labels).sort();
},
_computeLabelValues: function(labelName, _labels) {
var result = [];
var labels = _labels.base;
var t = labels[labelName];
_computeLabelValues(labelName, _labels) {
const result = [];
const labels = _labels.base;
const t = labels[labelName];
if (!t) { return result; }
var approvals = t.all || [];
approvals.forEach(function(label) {
const approvals = t.all || [];
for (const label of approvals) {
if (label.value && label.value != labels[labelName].default_value) {
var labelClassName;
var labelValPrefix = '';
let labelClassName;
let labelValPrefix = '';
if (label.value > 0) {
labelValPrefix = '+';
labelClassName = 'approved';
@@ -139,35 +139,35 @@
account: label,
});
}
});
}
return result;
},
_computeValueTooltip: function(score, labelName) {
var values = this.change.labels[labelName].values;
_computeValueTooltip(score, labelName) {
const values = this.change.labels[labelName].values;
return values[score];
},
_handleTopicChanged: function(e, topic) {
_handleTopicChanged(e, topic) {
if (!topic.length) { topic = null; }
this.$.restAPI.setChangeTopic(this.change._number, topic);
},
_computeTopicReadOnly: function(mutable, change) {
_computeTopicReadOnly(mutable, change) {
return !mutable || !change.actions.topic || !change.actions.topic.enabled;
},
_computeAssigneeReadOnly: function(mutable, change) {
_computeAssigneeReadOnly(mutable, change) {
return !mutable ||
!change.actions.assignee ||
!change.actions.assignee.enabled;
},
_computeTopicPlaceholder: function(_topicReadOnly) {
_computeTopicPlaceholder(_topicReadOnly) {
return _topicReadOnly ? 'No Topic' : 'Click to add topic';
},
_computeShowReviewersByState: function(serverConfig) {
_computeShowReviewersByState(serverConfig) {
return !!serverConfig.note_db_enabled;
},
@@ -181,9 +181,9 @@
* @param {boolean} mutable this.mutable describes whether the
* change-metadata section is modifiable by the current user.
*/
_computeCanDeleteVote: function(reviewer, mutable) {
_computeCanDeleteVote(reviewer, mutable) {
if (!mutable) { return false; }
for (var i = 0; i < this.change.removable_reviewers.length; i++) {
for (let i = 0; i < this.change.removable_reviewers.length; i++) {
if (this.change.removable_reviewers[i]._account_id ===
reviewer._account_id) {
return true;
@@ -192,20 +192,20 @@
return false;
},
_onDeleteVote: function(e) {
_onDeleteVote(e) {
e.preventDefault();
var target = Polymer.dom(e).rootTarget;
var labelName = target.labelName;
var accountID = parseInt(target.getAttribute('data-account-id'), 10);
const target = Polymer.dom(e).rootTarget;
const labelName = target.labelName;
const accountID = parseInt(target.getAttribute('data-account-id'), 10);
this._xhrPromise =
this.$.restAPI.deleteVote(this.change.id, accountID, labelName)
.then(function(response) {
.then(response => {
if (!response.ok) { return response; }
var label = this.change.labels[labelName];
var labels = label.all || [];
for (var i = 0; i < labels.length; i++) {
const label = this.change.labels[labelName];
const labels = label.all || [];
for (let i = 0; i < labels.length; i++) {
if (labels[i]._account_id === accountID) {
for (var key in label) {
for (const key in label) {
if (label.hasOwnProperty(key) &&
label[key]._account_id === accountID) {
// Remove special label field, keeping change label values
@@ -217,19 +217,20 @@
break;
}
}
}.bind(this));
});
},
_computeShowLabelStatus: function(change) {
var isNewChange = change.status === this.ChangeStatus.NEW;
var hasLabels = Object.keys(change.labels).length > 0;
_computeShowLabelStatus(change) {
const isNewChange = change.status === this.ChangeStatus.NEW;
const hasLabels = Object.keys(change.labels).length > 0;
return isNewChange && hasLabels;
},
_computeMissingLabels: function(labels) {
var missingLabels = [];
for (var label in labels) {
var obj = labels[label];
_computeMissingLabels(labels) {
const missingLabels = [];
for (const label in labels) {
if (!labels.hasOwnProperty(label)) { continue; }
const obj = labels[label];
if (!obj.optional && !obj.approved) {
missingLabels.push(label);
}
@@ -237,26 +238,26 @@
return missingLabels;
},
_computeMissingLabelsHeader: function(labels) {
_computeMissingLabelsHeader(labels) {
return 'Needs label' +
(this._computeMissingLabels(labels).length > 1 ? 's' : '') + ':';
},
_showMissingLabels: function(labels) {
_showMissingLabels(labels) {
return !!this._computeMissingLabels(labels).length;
},
_showMissingRequirements: function(labels, workInProgress) {
_showMissingRequirements(labels, workInProgress) {
return workInProgress || this._showMissingLabels(labels);
},
_computeProjectURL: function(project) {
_computeProjectURL(project) {
return this.getBaseUrl() + '/q/project:' +
this.encodeURL(project, false);
},
_computeBranchURL: function(project, branch) {
var status;
_computeBranchURL(project, branch) {
let status;
if (this.change.status == this.ChangeStatus.NEW) {
status = 'open';
} else {
@@ -268,13 +269,13 @@
' status:' + this.encodeURL(status, false);
},
_computeTopicURL: function(topic) {
_computeTopicURL(topic) {
return this.getBaseUrl() + '/q/topic:' +
this.encodeURL('"' + topic + '"', false) +
'+(status:open OR status:merged)';
},
_handleTopicRemoved: function() {
_handleTopicRemoved() {
this.set(['change', 'topic'], '');
this.$.restAPI.setChangeTopic(this.change._number, null);
},

View File

@@ -33,25 +33,25 @@ limitations under the License.
</test-fixture>
<script>
suite('gr-change-metadata tests', function() {
var element;
var sandbox;
suite('gr-change-metadata tests', () => {
let element;
let sandbox;
setup(function() {
setup(() => {
sandbox = sinon.sandbox.create();
stub('gr-rest-api-interface', {
getConfig: function() { return Promise.resolve({}); },
getLoggedIn: function() { return Promise.resolve(false); },
getConfig() { return Promise.resolve({}); },
getLoggedIn() { return Promise.resolve(false); },
});
element = fixture('basic');
});
teardown(function() {
teardown(() => {
sandbox.restore();
});
test('computed fields', function() {
test('computed fields', () => {
assert.isFalse(element._computeHideStrategy({status: 'NEW'}));
assert.isFalse(element._computeHideStrategy({status: 'DRAFT'}));
assert.isTrue(element._computeHideStrategy({status: 'MERGED'}));
@@ -60,22 +60,22 @@ limitations under the License.
'Cherry Pick');
});
test('show strategy for open change', function() {
test('show strategy for open change', () => {
element.change = {status: 'NEW', submit_type: 'CHERRY_PICK', labels: {}};
flushAsynchronousOperations();
var strategy = element.$$('.strategy');
const strategy = element.$$('.strategy');
assert.ok(strategy);
assert.isFalse(strategy.hasAttribute('hidden'));
assert.equal(strategy.children[1].innerHTML, 'Cherry Pick');
});
test('hide strategy for closed change', function() {
test('hide strategy for closed change', () => {
element.change = {status: 'MERGED', labels: {}};
flushAsynchronousOperations();
assert.isTrue(element.$$('.strategy').hasAttribute('hidden'));
});
test('show CC section when NoteDb enabled', function() {
test('show CC section when NoteDb enabled', () => {
function hasCc() {
return element._showReviewersByState;
}
@@ -87,9 +87,9 @@ limitations under the License.
assert.isTrue(hasCc());
});
test('computes submit status', function() {
var showMissingLabels = false;
sandbox.stub(element, '_showMissingLabels', function() {
test('computes submit status', () => {
let showMissingLabels = false;
sandbox.stub(element, '_showMissingLabels', () => {
return showMissingLabels;
});
assert.isFalse(element._showMissingRequirements(null, false));
@@ -98,8 +98,8 @@ limitations under the License.
assert.isTrue(element._showMissingRequirements(null, false));
});
test('show missing labels', function() {
var labels = {};
test('show missing labels', () => {
let labels = {};
assert.isFalse(element._showMissingLabels(labels));
labels = {test: {}};
assert.isTrue(element._showMissingLabels(labels));
@@ -116,25 +116,25 @@ limitations under the License.
['test', 'test2']);
});
test('weblinks hidden when no weblinks', function() {
test('weblinks hidden when no weblinks', () => {
element.commitInfo = {};
flushAsynchronousOperations();
var webLinks = element.$.webLinks;
const webLinks = element.$.webLinks;
assert.isTrue(webLinks.hasAttribute('hidden'));
});
test('weblinks hidden when only gitiles weblink', function() {
test('weblinks hidden when only gitiles weblink', () => {
element.commitInfo = {web_links: [{name: 'gitiles', url: '#'}]};
flushAsynchronousOperations();
var webLinks = element.$.webLinks;
const webLinks = element.$.webLinks;
assert.isTrue(webLinks.hasAttribute('hidden'));
assert.equal(element._computeWebLinks(element.commitInfo), null);
});
test('weblinks are visible when other weblinks', function() {
test('weblinks are visible when other weblinks', () => {
element.commitInfo = {web_links: [{name: 'test', url: '#'}]};
flushAsynchronousOperations();
var webLinks = element.$.webLinks;
const webLinks = element.$.webLinks;
assert.isFalse(webLinks.hasAttribute('hidden'));
assert.equal(element._computeWebLinks(element.commitInfo).length, 1);
// With two non-gitiles weblinks, there are two returned.
@@ -143,18 +143,18 @@ limitations under the License.
assert.equal(element._computeWebLinks(element.commitInfo).length, 2);
});
test('weblinks are visible when gitiles and other weblinks', function() {
test('weblinks are visible when gitiles and other weblinks', () => {
element.commitInfo = {
web_links: [{name: 'test', url: '#'}, {name: 'gitiles', url: '#'}]};
flushAsynchronousOperations();
var webLinks = element.$.webLinks;
const webLinks = element.$.webLinks;
assert.isFalse(webLinks.hasAttribute('hidden'));
// Only the non-gitiles weblink is returned.
assert.equal(element._computeWebLinks(element.commitInfo).length, 1);
});
test('determines whether to show "Ready to Submit" label', function() {
var showMissingSpy = sandbox.spy(element, '_showMissingRequirements');
test('determines whether to show "Ready to Submit" label', () => {
const showMissingSpy = sandbox.spy(element, '_showMissingRequirements');
element.change = {status: 'NEW', submit_type: 'CHERRY_PICK', labels: {
test: {
all: [{_account_id: 1, name: 'bojack', value: 1}],
@@ -166,9 +166,9 @@ limitations under the License.
assert.isTrue(showMissingSpy.called);
});
suite('Topic removal', function() {
var change;
setup(function() {
suite('Topic removal', () => {
let change;
setup(() => {
change = {
_number: 'the number',
actions: {
@@ -189,8 +189,8 @@ limitations under the License.
};
});
test('_computeTopicReadOnly', function() {
var mutable = false;
test('_computeTopicReadOnly', () => {
let mutable = false;
assert.isTrue(element._computeTopicReadOnly(mutable, change));
mutable = true;
assert.isTrue(element._computeTopicReadOnly(mutable, change));
@@ -200,26 +200,26 @@ limitations under the License.
assert.isTrue(element._computeTopicReadOnly(mutable, change));
});
test('topic read only hides delete button', function() {
test('topic read only hides delete button', () => {
element.mutable = false;
element.change = change;
flushAsynchronousOperations();
var button = element.$$('gr-linked-chip').$$('gr-button');
const button = element.$$('gr-linked-chip').$$('gr-button');
assert.isTrue(button.hasAttribute('hidden'));
});
test('topic not read only does not hide delete button', function() {
test('topic not read only does not hide delete button', () => {
element.mutable = true;
change.actions.topic.enabled = true;
element.change = change;
flushAsynchronousOperations();
var button = element.$$('gr-linked-chip').$$('gr-button');
const button = element.$$('gr-linked-chip').$$('gr-button');
assert.isFalse(button.hasAttribute('hidden'));
});
});
suite('remove reviewer votes', function() {
setup(function() {
suite('remove reviewer votes', () => {
setup(() => {
sandbox.stub(element, '_computeValueTooltip').returns('');
sandbox.stub(element, '_computeTopicReadOnly').returns(true);
element.change = {
@@ -240,30 +240,39 @@ limitations under the License.
};
});
test('_computeCanDeleteVote hides delete button', function() {
test('_computeCanDeleteVote hides delete button', () => {
flushAsynchronousOperations();
var button = element.$$('gr-account-chip').$$('gr-button');
const button = element.$$('gr-account-chip').$$('gr-button');
assert.isTrue(button.hasAttribute('hidden'));
element.mutable = true;
assert.isTrue(button.hasAttribute('hidden'));
});
test('_computeCanDeleteVote shows delete button', function() {
test('_computeCanDeleteVote shows delete button', () => {
element.change.removable_reviewers = [
{
_account_id: 1,
name: 'bojack',
}
},
];
element.mutable = true;
flushAsynchronousOperations();
var button = element.$$('gr-account-chip').$$('gr-button');
const button = element.$$('gr-account-chip').$$('gr-button');
assert.isFalse(button.hasAttribute('hidden'));
});
test('deletes votes', function(done) {
test('deletes votes', done => {
sandbox.stub(element.$.restAPI, 'deleteVote')
.returns(Promise.resolve({'ok': true}));
.returns(Promise.resolve({ok: true}));
const spliceStub = sandbox.stub(element, 'splice', (path, index,
length) => {
assert.deepEqual(path, ['change.labels', 'test', 'all']);
assert.equal(index, 0);
assert.equal(length, 1);
assert.notOk(element.change.labels.test.recommended);
spliceStub.restore();
done();
});
element.change.removable_reviewers = [
{
_account_id: 1,
@@ -273,70 +282,60 @@ limitations under the License.
element.change.labels.test.recommended = {_account_id: 1};
element.mutable = true;
flushAsynchronousOperations();
var button = element.$$('gr-account-chip').$$('gr-button');
const button = element.$$('gr-account-chip').$$('gr-button');
MockInteractions.tap(button);
flushAsynchronousOperations();
var spliceStub = sinon.stub(element, 'splice',
function(path, index, length) {
assert.deepEqual(path, ['change.labels', 'test', 'all']);
assert.equal(index, 0);
assert.equal(length, 1);
assert.notOk(element.change.labels.test.recommended);
spliceStub.restore();
done();
});
});
test('changing topic calls setChangeTopic', function() {
var topicStub = sandbox.stub(element.$.restAPI, 'setChangeTopic',
function() {});
test('changing topic calls setChangeTopic', () => {
const topicStub = sandbox.stub(element.$.restAPI, 'setChangeTopic',
() => {});
element._handleTopicChanged({}, 'the new topic');
assert.isTrue(topicStub.calledWith('the number', 'the new topic'));
});
test('topic href has quotes', function() {
var hrefArr = element._computeTopicURL('test')
test('topic href has quotes', () => {
const hrefArr = element._computeTopicURL('test')
.split('%2522'); // Double-escaped quote.
assert.equal(hrefArr[1], 'test');
});
test('clicking x on topic chip removes topic', function() {
var topicStub = sandbox.stub(element.$.restAPI, 'setChangeTopic');
test('clicking x on topic chip removes topic', () => {
const topicStub = sandbox.stub(element.$.restAPI, 'setChangeTopic');
flushAsynchronousOperations();
var remove = element.$$('gr-linked-chip').$.remove;
const remove = element.$$('gr-linked-chip').$.remove;
MockInteractions.tap(remove);
assert.equal(element.change.topic, '');
assert.isTrue(topicStub.called);
});
suite('assignee field', function() {
var dummyAccount = {
suite('assignee field', () => {
const dummyAccount = {
_account_id: 1,
name: 'bojack',
};
var change = {
const change = {
actions: {
assignee: {enabled: false},
},
assignee: dummyAccount,
};
var deleteStub;
var setStub;
let deleteStub;
let setStub;
setup(function() {
setup(() => {
deleteStub = sandbox.stub(element.$.restAPI, 'deleteAssignee');
setStub = sandbox.stub(element.$.restAPI, 'setAssignee');
});
test('changing change recomputes _assignee', function() {
test('changing change recomputes _assignee', () => {
assert.isFalse(!!element._assignee.length);
var change = element.change;
const change = element.change;
change.assignee = dummyAccount;
element._changeChanged(change);
assert.deepEqual(element._assignee[0], dummyAccount);
});
test('modifying _assignee calls API', function() {
test('modifying _assignee calls API', () => {
assert.isFalse(!!element._assignee.length);
element.set('_assignee', [dummyAccount]);
assert.isTrue(setStub.calledOnce);
@@ -350,8 +349,8 @@ limitations under the License.
assert.isTrue(deleteStub.calledOnce);
});
test('_computeAssigneeReadOnly', function() {
var mutable = false;
test('_computeAssigneeReadOnly', () => {
let mutable = false;
assert.isTrue(element._computeAssigneeReadOnly(mutable, change));
mutable = true;
assert.isTrue(element._computeAssigneeReadOnly(mutable, change));

View File

@@ -1,6 +1,6 @@
<dom-module id="my-plugin">
<script>
Gerrit.install(function(plugin) {
Gerrit.install(plugin => {
plugin.registerStyleModule('change-metadata', 'my-plugin-style');
});
</script>