ES6ify /gr-js-api-interface/*

Bug: Issue 6179
Change-Id: I9e0672c64a6793ba019b7b4af65f22eaacd7e2f5
This commit is contained in:
Kasper Nilsson
2017-05-15 16:39:47 -07:00
parent 7bb7ef60ae
commit 75214af17f
6 changed files with 185 additions and 184 deletions

View File

@@ -22,13 +22,13 @@
} }
GrChangeActionsInterface.prototype.addPrimaryActionKey = function(key) { GrChangeActionsInterface.prototype.addPrimaryActionKey = function(key) {
if (this._el.primaryActionKeys.indexOf(key) !== -1) { return; } if (this._el.primaryActionKeys.includes(key)) { return; }
this._el.push('primaryActionKeys', key); this._el.push('primaryActionKeys', key);
}; };
GrChangeActionsInterface.prototype.removePrimaryActionKey = function(key) { GrChangeActionsInterface.prototype.removePrimaryActionKey = function(key) {
this._el.primaryActionKeys = this._el.primaryActionKeys.filter(function(k) { this._el.primaryActionKeys = this._el.primaryActionKeys.filter(k => {
return k !== key; return k !== key;
}); });
}; };

View File

@@ -37,43 +37,44 @@ breaking changes to gr-change-actions wont be noticed.
</test-fixture> </test-fixture>
<script> <script>
suite('gr-js-api-interface tests', function() { suite('gr-js-api-interface tests', () => {
var element; let element;
var changeActions; let changeActions;
// Because deepEqual doesnt behave in Safari. // Because deepEqual doesnt behave in Safari.
function assertArraysEqual(actual, expected) { function assertArraysEqual(actual, expected) {
assert.equal(actual.length, expected.length); assert.equal(actual.length, expected.length);
for (var i = 0; i < actual.length; i++) { for (let i = 0; i < actual.length; i++) {
assert.equal(actual[i], expected[i]); assert.equal(actual[i], expected[i]);
} }
} }
setup(function() { setup(() => {
element = fixture('basic'); element = fixture('basic');
element.change = {}; element.change = {};
element._hasKnownChainState = false; element._hasKnownChainState = false;
var plugin; let plugin;
Gerrit.install(function(p) { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
changeActions = plugin.changeActions(); changeActions = plugin.changeActions();
}); });
teardown(function() { teardown(() => {
changeActions = null; changeActions = null;
}); });
test('property existence', function() { test('property existence', () => {
[ const properties = [
'ActionType', 'ActionType',
'ChangeActions', 'ChangeActions',
'RevisionActions', 'RevisionActions',
].forEach(function(p) { ];
for (const p of properties) {
assertArraysEqual(changeActions[p], element[p]); assertArraysEqual(changeActions[p], element[p]);
}); }
}); });
test('add/remove primary action keys', function() { test('add/remove primary action keys', () => {
element.primaryActionKeys = []; element.primaryActionKeys = [];
changeActions.addPrimaryActionKey('foo'); changeActions.addPrimaryActionKey('foo');
assertArraysEqual(element.primaryActionKeys, ['foo']); assertArraysEqual(element.primaryActionKeys, ['foo']);
@@ -89,34 +90,34 @@ breaking changes to gr-change-actions wont be noticed.
assertArraysEqual(element.primaryActionKeys, []); assertArraysEqual(element.primaryActionKeys, []);
}); });
test('action buttons', function(done) { test('action buttons', done => {
var key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!'); const key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!');
var handler = sinon.spy(); const handler = sinon.spy();
changeActions.addTapListener(key, handler); changeActions.addTapListener(key, handler);
flush(function() { flush(() => {
MockInteractions.tap(element.$$('[data-action-key="' + key + '"]')); MockInteractions.tap(element.$$('[data-action-key="' + key + '"]'));
assert(handler.calledOnce); assert(handler.calledOnce);
changeActions.removeTapListener(key, handler); changeActions.removeTapListener(key, handler);
MockInteractions.tap(element.$$('[data-action-key="' + key + '"]')); MockInteractions.tap(element.$$('[data-action-key="' + key + '"]'));
assert(handler.calledOnce); assert(handler.calledOnce);
changeActions.remove(key); changeActions.remove(key);
flush(function() { flush(() => {
assert.isNull(element.$$('[data-action-key="' + key + '"]')); assert.isNull(element.$$('[data-action-key="' + key + '"]'));
done(); done();
}); });
}); });
}); });
test('action button properties', function(done) { test('action button properties', done => {
var key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!'); const key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!');
flush(function() { flush(() => {
var button = element.$$('[data-action-key="' + key + '"]'); const button = element.$$('[data-action-key="' + key + '"]');
assert.isOk(button); assert.isOk(button);
assert.equal(button.getAttribute('data-label'), 'Bork!'); assert.equal(button.getAttribute('data-label'), 'Bork!');
assert.isNotOk(button.disabled); assert.isNotOk(button.disabled);
changeActions.setLabel(key, 'Yo'); changeActions.setLabel(key, 'Yo');
changeActions.setEnabled(key, false); changeActions.setEnabled(key, false);
flush(function() { flush(() => {
assert.equal(button.getAttribute('data-label'), 'Yo'); assert.equal(button.getAttribute('data-label'), 'Yo');
assert.isTrue(button.disabled); assert.isTrue(button.disabled);
done(); done();
@@ -124,30 +125,30 @@ breaking changes to gr-change-actions wont be noticed.
}); });
}); });
test('hide action buttons', function(done) { test('hide action buttons', done => {
var key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!'); const key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!');
flush(function() { flush(() => {
var button = element.$$('[data-action-key="' + key + '"]'); const button = element.$$('[data-action-key="' + key + '"]');
assert.isOk(button); assert.isOk(button);
assert.isFalse(button.hasAttribute('hidden')); assert.isFalse(button.hasAttribute('hidden'));
changeActions.setActionHidden( changeActions.setActionHidden(
changeActions.ActionType.REVISION, key, true); changeActions.ActionType.REVISION, key, true);
flush(function() { flush(() => {
var button = element.$$('[data-action-key="' + key + '"]'); const button = element.$$('[data-action-key="' + key + '"]');
assert.isNotOk(button); assert.isNotOk(button);
done(); done();
}); });
}); });
}); });
test('move action button to overflow', function(done) { test('move action button to overflow', done => {
var key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!'); const key = changeActions.add(changeActions.ActionType.REVISION, 'Bork!');
flush(function() { flush(() => {
assert.isTrue(element.$.moreActions.hidden); assert.isTrue(element.$.moreActions.hidden);
assert.isOk(element.$$('[data-action-key="' + key + '"]')); assert.isOk(element.$$('[data-action-key="' + key + '"]'));
changeActions.setActionOverflow( changeActions.setActionOverflow(
changeActions.ActionType.REVISION, key, true); changeActions.ActionType.REVISION, key, true);
flush(function() { flush(() => {
assert.isNotOk(element.$$('[data-action-key="' + key + '"]')); assert.isNotOk(element.$$('[data-action-key="' + key + '"]'));
assert.isFalse(element.$.moreActions.hidden); assert.isFalse(element.$.moreActions.hidden);
assert.strictEqual(element.$.moreActions.items[0].name, 'Bork!'); assert.strictEqual(element.$.moreActions.items[0].name, 'Bork!');
@@ -156,17 +157,19 @@ breaking changes to gr-change-actions wont be noticed.
}); });
}); });
test('change actions priority', function(done) { test('change actions priority', done => {
var key1 = changeActions.add(changeActions.ActionType.REVISION, 'Bork!'); const key1 =
var key2 = changeActions.add(changeActions.ActionType.CHANGE, 'Squanch?'); changeActions.add(changeActions.ActionType.REVISION, 'Bork!');
flush(function() { const key2 =
var buttons = changeActions.add(changeActions.ActionType.CHANGE, 'Squanch?');
flush(() => {
let buttons =
Polymer.dom(element.root).querySelectorAll('[data-action-key]'); Polymer.dom(element.root).querySelectorAll('[data-action-key]');
assert.equal(buttons[0].getAttribute('data-action-key'), key1); assert.equal(buttons[0].getAttribute('data-action-key'), key1);
assert.equal(buttons[1].getAttribute('data-action-key'), key2); assert.equal(buttons[1].getAttribute('data-action-key'), key2);
changeActions.setActionPriority( changeActions.setActionPriority(
changeActions.ActionType.REVISION, key1, 10); changeActions.ActionType.REVISION, key1, 10);
flush(function() { flush(() => {
buttons = buttons =
Polymer.dom(element.root).querySelectorAll('[data-action-key]'); Polymer.dom(element.root).querySelectorAll('[data-action-key]');
assert.equal(buttons[0].getAttribute('data-action-key'), key2); assert.equal(buttons[0].getAttribute('data-action-key'), key2);

View File

@@ -37,35 +37,35 @@ limitations under the License.
</test-fixture> </test-fixture>
<script> <script>
suite('gr-change-reply-js-api tests', function() { suite('gr-change-reply-js-api tests', () => {
var element; let element;
var sandbox; let sandbox;
var changeReply; let changeReply;
setup(function() { setup(() => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getConfig: function() { return Promise.resolve({}); }, getConfig() { return Promise.resolve({}); },
getAccount: function() { return Promise.resolve(null); }, getAccount() { return Promise.resolve(null); },
}); });
element = fixture('basic'); element = fixture('basic');
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
var plugin; let plugin;
Gerrit.install(function(p) { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
changeReply = plugin.changeReply(); changeReply = plugin.changeReply();
}); });
teardown(function() { teardown(() => {
changeReply = null; changeReply = null;
sandbox.restore(); sandbox.restore();
}); });
test('calls', function() { test('calls', () => {
var setLabelValueStub = sinon.stub(element, 'setLabelValue'); const setLabelValueStub = sinon.stub(element, 'setLabelValue');
changeReply.setLabelValue('My-Label', '+1337'); changeReply.setLabelValue('My-Label', '+1337');
assert(setLabelValueStub.calledWithExactly('My-Label', '+1337')); assert(setLabelValueStub.calledWithExactly('My-Label', '+1337'));
var sendStub = sinon.stub(element, 'send'); const sendStub = sinon.stub(element, 'send');
changeReply.send(false); changeReply.send(false);
assert(sendStub.calledWithExactly(false)); assert(sendStub.calledWithExactly(false));
}); });

View File

@@ -14,7 +14,7 @@
(function() { (function() {
'use strict'; 'use strict';
var EventType = { const EventType = {
HISTORY: 'history', HISTORY: 'history',
LABEL_CHANGE: 'labelchange', LABEL_CHANGE: 'labelchange',
SHOW_CHANGE: 'showchange', SHOW_CHANGE: 'showchange',
@@ -25,7 +25,7 @@
POST_REVERT: 'postrevert', POST_REVERT: 'postrevert',
}; };
var Element = { const Element = {
CHANGE_ACTIONS: 'changeactions', CHANGE_ACTIONS: 'changeactions',
REPLY_DIALOG: 'replydialog', REPLY_DIALOG: 'replydialog',
}; };
@@ -44,11 +44,11 @@
}, },
}, },
Element: Element, Element,
EventType: EventType, EventType,
handleEvent: function(type, detail) { handleEvent(type, detail) {
Gerrit.awaitPluginsLoaded().then(function() { Gerrit.awaitPluginsLoaded().then(() => {
switch (type) { switch (type) {
case EventType.HISTORY: case EventType.HISTORY:
this._handleHistory(detail); this._handleHistory(detail);
@@ -67,27 +67,27 @@
type); type);
break; break;
} }
}.bind(this)); });
}, },
addElement: function(key, el) { addElement(key, el) {
this._elements[key] = el; this._elements[key] = el;
}, },
getElement: function(key) { getElement(key) {
return this._elements[key]; return this._elements[key];
}, },
addEventCallback: function(eventName, callback) { addEventCallback(eventName, callback) {
if (!this._eventCallbacks[eventName]) { if (!this._eventCallbacks[eventName]) {
this._eventCallbacks[eventName] = []; this._eventCallbacks[eventName] = [];
} }
this._eventCallbacks[eventName].push(callback); this._eventCallbacks[eventName].push(callback);
}, },
canSubmitChange: function(change, revision) { canSubmitChange(change, revision) {
var submitCallbacks = this._getEventCallbacks(EventType.SUBMIT_CHANGE); const submitCallbacks = this._getEventCallbacks(EventType.SUBMIT_CHANGE);
var cancelSubmit = submitCallbacks.some(function(callback) { const cancelSubmit = submitCallbacks.some(callback => {
try { try {
return callback(change, revision) === false; return callback(change, revision) === false;
} catch (err) { } catch (err) {
@@ -99,28 +99,29 @@
return !cancelSubmit; return !cancelSubmit;
}, },
_removeEventCallbacks: function() { _removeEventCallbacks() {
for (var k in EventType) { for (const k in EventType) {
if (!EventType.hasOwnProperty(k)) { continue; }
this._eventCallbacks[EventType[k]] = []; this._eventCallbacks[EventType[k]] = [];
} }
}, },
_handleHistory: function(detail) { _handleHistory(detail) {
this._getEventCallbacks(EventType.HISTORY).forEach(function(cb) { for (const cb of this._getEventCallbacks(EventType.HISTORY)) {
try { try {
cb(detail.path); cb(detail.path);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}); }
}, },
_handleShowChange: function(detail) { _handleShowChange(detail) {
this._getEventCallbacks(EventType.SHOW_CHANGE).forEach(function(cb) { for (const cb of this._getEventCallbacks(EventType.SHOW_CHANGE)) {
var change = detail.change; const change = detail.change;
var patchNum = detail.patchNum; const patchNum = detail.patchNum;
var revision; let revision;
for (var rev in change.revisions) { for (const rev in change.revisions) {
if (change.revisions[rev]._number == patchNum) { if (change.revisions[rev]._number == patchNum) {
revision = change.revisions[rev]; revision = change.revisions[rev];
break; break;
@@ -131,67 +132,63 @@
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}); }
}, },
handleCommitMessage: function(change, msg) { handleCommitMessage(change, msg) {
this._getEventCallbacks(EventType.COMMIT_MSG_EDIT).forEach( for (const cb of this._getEventCallbacks(EventType.COMMIT_MSG_EDIT)) {
function(cb) { try {
try { cb(change, msg);
cb(change, msg); } catch (err) {
} catch (err) { console.error(err);
console.error(err); }
} }
}
);
}, },
_handleComment: function(detail) { _handleComment(detail) {
this._getEventCallbacks(EventType.COMMENT).forEach(function(cb) { for (const cb of this._getEventCallbacks(EventType.COMMENT)) {
try { try {
cb(detail.node); cb(detail.node);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}); }
}, },
_handleLabelChange: function(detail) { _handleLabelChange(detail) {
this._getEventCallbacks(EventType.LABEL_CHANGE).forEach(function(cb) { for (const cb of this._getEventCallbacks(EventType.LABEL_CHANGE)) {
try { try {
cb(detail.change); cb(detail.change);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}); }
}, },
modifyRevertMsg: function(change, revertMsg, origMsg) { modifyRevertMsg(change, revertMsg, origMsg) {
this._getEventCallbacks(EventType.REVERT).forEach(function(callback) { for (const cb of this._getEventCallbacks(EventType.REVERT)) {
try { try {
revertMsg = callback(change, revertMsg, origMsg); revertMsg = cb(change, revertMsg, origMsg);
} catch (err) { } catch (err) {
console.error(err); console.error(err);
} }
}); }
return revertMsg; return revertMsg;
}, },
getLabelValuesPostRevert: function(change) { getLabelValuesPostRevert(change) {
var labels = {}; let labels = {};
this._getEventCallbacks(EventType.POST_REVERT).forEach( for (const cb of this._getEventCallbacks(EventType.POST_REVERT)) {
function(callback) { try {
try { labels = cb(change);
labels = callback(change); } catch (err) {
} catch (err) { console.error(err);
console.error(err); }
} }
}
);
return labels; return labels;
}, },
_getEventCallbacks: function(type) { _getEventCallbacks(type) {
return this._eventCallbacks[type] || []; return this._eventCallbacks[type] || [];
}, },
}); });

View File

@@ -31,45 +31,45 @@ limitations under the License.
</test-fixture> </test-fixture>
<script> <script>
suite('gr-js-api-interface tests', function() { suite('gr-js-api-interface tests', () => {
var element; let element;
var plugin; let plugin;
var errorStub; let errorStub;
var sandbox; let sandbox;
var throwErrFn = function() { const throwErrFn = function() {
throw Error('Unfortunately, this handler has stopped'); throw Error('Unfortunately, this handler has stopped');
}; };
setup(function() { setup(() => {
sandbox = sinon.sandbox.create(); sandbox = sinon.sandbox.create();
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getAccount: function() { getAccount() {
return Promise.resolve({name: 'Judy Hopps'}); return Promise.resolve({name: 'Judy Hopps'});
}, },
}); });
element = fixture('basic'); element = fixture('basic');
errorStub = sandbox.stub(console, 'error'); errorStub = sandbox.stub(console, 'error');
Gerrit._setPluginsCount(1); Gerrit._setPluginsCount(1);
Gerrit.install(function(p) { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
}); });
teardown(function() { teardown(() => {
sandbox.restore(); sandbox.restore();
element._removeEventCallbacks(); element._removeEventCallbacks();
plugin = null; plugin = null;
}); });
test('url', function() { test('url', () => {
assert.equal(plugin.url(), 'http://test.com/plugins/testplugin/'); assert.equal(plugin.url(), 'http://test.com/plugins/testplugin/');
assert.equal(plugin.url('/static/test.js'), assert.equal(plugin.url('/static/test.js'),
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
}); });
test('history event', function(done) { test('history event', done => {
plugin.on(element.EventType.HISTORY, throwErrFn); plugin.on(element.EventType.HISTORY, throwErrFn);
plugin.on(element.EventType.HISTORY, function(path) { plugin.on(element.EventType.HISTORY, path => {
assert.equal(path, '/path/to/awesomesauce'); assert.equal(path, '/path/to/awesomesauce');
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
done(); done();
@@ -78,13 +78,13 @@ limitations under the License.
{path: '/path/to/awesomesauce'}); {path: '/path/to/awesomesauce'});
}); });
test('showchange event', function(done) { test('showchange event', done => {
var testChange = { const testChange = {
_number: 42, _number: 42,
revisions: {def: {_number: 2}, abc: {_number: 1}}, revisions: {def: {_number: 2}, abc: {_number: 1}},
}; };
plugin.on(element.EventType.SHOW_CHANGE, throwErrFn); plugin.on(element.EventType.SHOW_CHANGE, throwErrFn);
plugin.on(element.EventType.SHOW_CHANGE, function(change, revision) { plugin.on(element.EventType.SHOW_CHANGE, (change, revision) => {
assert.deepEqual(change, testChange); assert.deepEqual(change, testChange);
assert.deepEqual(revision, testChange.revisions.abc); assert.deepEqual(revision, testChange.revisions.abc);
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
@@ -94,28 +94,28 @@ limitations under the License.
{change: testChange, patchNum: 1}); {change: testChange, patchNum: 1});
}); });
test('handleEvent awaits plugins load', function(done) { test('handleEvent awaits plugins load', done => {
var testChange = { const testChange = {
_number: 42, _number: 42,
revisions: {def: {_number: 2}, abc: {_number: 1}}, revisions: {def: {_number: 2}, abc: {_number: 1}},
}; };
var spy = sandbox.spy(); const spy = sandbox.spy();
Gerrit._setPluginsCount(1); Gerrit._setPluginsCount(1);
plugin.on(element.EventType.SHOW_CHANGE, spy); plugin.on(element.EventType.SHOW_CHANGE, spy);
element.handleEvent(element.EventType.SHOW_CHANGE, element.handleEvent(element.EventType.SHOW_CHANGE,
{change: testChange, patchNum: 1}); {change: testChange, patchNum: 1});
assert.isFalse(spy.called); assert.isFalse(spy.called);
Gerrit._setPluginsCount(0); Gerrit._setPluginsCount(0);
flush(function() { flush(() => {
assert.isTrue(spy.called); assert.isTrue(spy.called);
done(); done();
}); });
}); });
test('comment event', function(done) { test('comment event', done => {
var testCommentNode = {foo: 'bar'}; const testCommentNode = {foo: 'bar'};
plugin.on(element.EventType.COMMENT, throwErrFn); plugin.on(element.EventType.COMMENT, throwErrFn);
plugin.on(element.EventType.COMMENT, function(commentNode) { plugin.on(element.EventType.COMMENT, commentNode => {
assert.deepEqual(commentNode, testCommentNode); assert.deepEqual(commentNode, testCommentNode);
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
done(); done();
@@ -123,7 +123,7 @@ limitations under the License.
element.handleEvent(element.EventType.COMMENT, {node: testCommentNode}); element.handleEvent(element.EventType.COMMENT, {node: testCommentNode});
}); });
test('revert event', function() { test('revert event', () => {
function appendToRevertMsg(c, revertMsg, originalMsg) { function appendToRevertMsg(c, revertMsg, originalMsg) {
return revertMsg + '\n' + originalMsg.replace(/^/gm, '> ') + '\ninfo'; return revertMsg + '\n' + originalMsg.replace(/^/gm, '> ') + '\ninfo';
} }
@@ -134,16 +134,16 @@ limitations under the License.
plugin.on(element.EventType.REVERT, throwErrFn); plugin.on(element.EventType.REVERT, throwErrFn);
plugin.on(element.EventType.REVERT, appendToRevertMsg); plugin.on(element.EventType.REVERT, appendToRevertMsg);
assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'),
'test\n> origTest\ninfo'); 'test\n> origTest\ninfo');
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
plugin.on(element.EventType.REVERT, appendToRevertMsg); plugin.on(element.EventType.REVERT, appendToRevertMsg);
assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'),
'test\n> origTest\ninfo\n> origTest\ninfo'); 'test\n> origTest\ninfo\n> origTest\ninfo');
assert.isTrue(errorStub.calledTwice); assert.isTrue(errorStub.calledTwice);
}); });
test('postrevert event', function() { test('postrevert event', () => {
function getLabels(c) { function getLabels(c) {
return {'Code-Review': 1}; return {'Code-Review': 1};
} }
@@ -158,10 +158,10 @@ limitations under the License.
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
}); });
test('commitmsgedit event', function(done) { test('commitmsgedit event', done => {
var testMsg = 'Test CL commit message'; const testMsg = 'Test CL commit message';
plugin.on(element.EventType.COMMIT_MSG_EDIT, throwErrFn); plugin.on(element.EventType.COMMIT_MSG_EDIT, throwErrFn);
plugin.on(element.EventType.COMMIT_MSG_EDIT, function(change, msg) { plugin.on(element.EventType.COMMIT_MSG_EDIT, (change, msg) => {
assert.deepEqual(msg, testMsg); assert.deepEqual(msg, testMsg);
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
done(); done();
@@ -169,10 +169,10 @@ limitations under the License.
element.handleCommitMessage(null, testMsg); element.handleCommitMessage(null, testMsg);
}); });
test('labelchange event', function(done) { test('labelchange event', done => {
var testChange = {_number: 42}; const testChange = {_number: 42};
plugin.on(element.EventType.LABEL_CHANGE, throwErrFn); plugin.on(element.EventType.LABEL_CHANGE, throwErrFn);
plugin.on(element.EventType.LABEL_CHANGE, function(change) { plugin.on(element.EventType.LABEL_CHANGE, change => {
assert.deepEqual(change, testChange); assert.deepEqual(change, testChange);
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
done(); done();
@@ -180,41 +180,41 @@ limitations under the License.
element.handleEvent(element.EventType.LABEL_CHANGE, {change: testChange}); element.handleEvent(element.EventType.LABEL_CHANGE, {change: testChange});
}); });
test('submitchange', function() { test('submitchange', () => {
plugin.on(element.EventType.SUBMIT_CHANGE, throwErrFn); plugin.on(element.EventType.SUBMIT_CHANGE, throwErrFn);
plugin.on(element.EventType.SUBMIT_CHANGE, function() { return true; }); plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; });
assert.isTrue(element.canSubmitChange()); assert.isTrue(element.canSubmitChange());
assert.isTrue(errorStub.calledOnce); assert.isTrue(errorStub.calledOnce);
plugin.on(element.EventType.SUBMIT_CHANGE, function() { return false; }); plugin.on(element.EventType.SUBMIT_CHANGE, () => { return false; });
plugin.on(element.EventType.SUBMIT_CHANGE, function() { return true; }); plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; });
assert.isFalse(element.canSubmitChange()); assert.isFalse(element.canSubmitChange());
assert.isTrue(errorStub.calledTwice); assert.isTrue(errorStub.calledTwice);
}); });
test('versioning', function() { test('versioning', () => {
var callback = sandbox.spy(); const callback = sandbox.spy();
Gerrit.install(callback, '0.0pre-alpha'); Gerrit.install(callback, '0.0pre-alpha');
assert(callback.notCalled); assert(callback.notCalled);
}); });
test('getAccount', function(done) { test('getAccount', done => {
Gerrit.getLoggedIn().then(function(loggedIn) { Gerrit.getLoggedIn().then(loggedIn => {
assert.isTrue(loggedIn); assert.isTrue(loggedIn);
done(); done();
}); });
}); });
test('_setPluginsCount', function(done) { test('_setPluginsCount', done => {
stub('gr-reporting', { stub('gr-reporting', {
pluginsLoaded: function() { pluginsLoaded() {
assert.equal(Gerrit._pluginsPending, 0); assert.equal(Gerrit._pluginsPending, 0);
done(); done();
} },
}); });
Gerrit._setPluginsCount(0); Gerrit._setPluginsCount(0);
}); });
test('_arePluginsLoaded', function() { test('_arePluginsLoaded', () => {
assert.isTrue(Gerrit._arePluginsLoaded()); assert.isTrue(Gerrit._arePluginsLoaded());
Gerrit._setPluginsCount(1); Gerrit._setPluginsCount(1);
assert.isFalse(Gerrit._arePluginsLoaded()); assert.isFalse(Gerrit._arePluginsLoaded());
@@ -222,12 +222,12 @@ limitations under the License.
assert.isTrue(Gerrit._arePluginsLoaded()); assert.isTrue(Gerrit._arePluginsLoaded());
}); });
test('_pluginInstalled', function(done) { test('_pluginInstalled', done => {
stub('gr-reporting', { stub('gr-reporting', {
pluginsLoaded: function() { pluginsLoaded() {
assert.equal(Gerrit._pluginsPending, 0); assert.equal(Gerrit._pluginsPending, 0);
done(); done();
} },
}); });
Gerrit._setPluginsCount(2); Gerrit._setPluginsCount(2);
Gerrit._pluginInstalled(); Gerrit._pluginInstalled();
@@ -235,34 +235,34 @@ limitations under the License.
Gerrit._pluginInstalled(); Gerrit._pluginInstalled();
}); });
test('install calls _pluginInstalled', function() { test('install calls _pluginInstalled', () => {
sandbox.stub(Gerrit, '_pluginInstalled'); sandbox.stub(Gerrit, '_pluginInstalled');
Gerrit.install(function(p) { plugin = p; }, '0.1', Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js'); 'http://test.com/plugins/testplugin/static/test.js');
assert.isTrue(Gerrit._pluginInstalled.calledOnce); assert.isTrue(Gerrit._pluginInstalled.calledOnce);
}); });
test('install calls _pluginInstalled on error', function() { test('install calls _pluginInstalled on error', () => {
sandbox.stub(Gerrit, '_pluginInstalled'); sandbox.stub(Gerrit, '_pluginInstalled');
Gerrit.install(function() {}, '0.0pre-alpha'); Gerrit.install(() => {}, '0.0pre-alpha');
assert.isTrue(Gerrit._pluginInstalled.calledOnce); assert.isTrue(Gerrit._pluginInstalled.calledOnce);
}); });
test('installGwt calls _pluginInstalled', function() { test('installGwt calls _pluginInstalled', () => {
sandbox.stub(Gerrit, '_pluginInstalled'); sandbox.stub(Gerrit, '_pluginInstalled');
Gerrit.installGwt(); Gerrit.installGwt();
assert.isTrue(Gerrit._pluginInstalled.calledOnce); assert.isTrue(Gerrit._pluginInstalled.calledOnce);
}); });
test('installGwt returns a stub object', function() { test('installGwt returns a stub object', () => {
var plugin = Gerrit.installGwt(); const plugin = Gerrit.installGwt();
sandbox.stub(console, 'warn'); sandbox.stub(console, 'warn');
assert.isAbove(Object.keys(plugin).length, 0); assert.isAbove(Object.keys(plugin).length, 0);
Object.keys(plugin).forEach(function(name) { for (const name of Object.keys(plugin)) {
console.warn.reset(); console.warn.reset();
plugin[name](); plugin[name]();
assert.isTrue(console.warn.calledOnce); assert.isTrue(console.warn.calledOnce);
}); }
}); });
}); });
</script> </script>

View File

@@ -14,17 +14,17 @@
(function(window) { (function(window) {
'use strict'; 'use strict';
var warnNotSupported = function(opt_name) { const warnNotSupported = function(opt_name) {
console.warn('Plugin API method ' + (opt_name || '') + ' is not supported'); console.warn('Plugin API method ' + (opt_name || '') + ' is not supported');
}; };
var stubbedMethods = ['_loadedGwt', 'screen', 'settingsScreen', 'panel']; const stubbedMethods = ['_loadedGwt', 'screen', 'settingsScreen', 'panel'];
var GWT_PLUGIN_STUB = {}; const GWT_PLUGIN_STUB = {};
stubbedMethods.forEach(function(name) { for (const name of stubbedMethods) {
GWT_PLUGIN_STUB[name] = warnNotSupported.bind(null, name); GWT_PLUGIN_STUB[name] = warnNotSupported.bind(null, name);
}); }
var API_VERSION = '0.1'; const API_VERSION = '0.1';
// GWT JSNI uses $wnd to refer to window. // GWT JSNI uses $wnd to refer to window.
// http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html // http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
@@ -38,7 +38,7 @@
} }
this._url = new URL(opt_url); this._url = new URL(opt_url);
if (this._url.pathname.indexOf('/plugins') !== 0) { if (!this._url.pathname.startsWith('/plugins')) {
console.warn('Plugin not being loaded from /plugins base path:', console.warn('Plugin not being loaded from /plugins base path:',
this._url.href, '— Unable to determine name.'); this._url.href, '— Unable to determine name.');
return; return;
@@ -54,14 +54,14 @@
return this._name; return this._name;
}; };
Plugin.prototype.registerStyleModule = Plugin.prototype.registerStyleModule = function(stylingEndpointName,
function(stylingEndpointName, moduleName) { moduleName) {
if (!Gerrit._styleModules[stylingEndpointName]) { if (!Gerrit._styleModules[stylingEndpointName]) {
Gerrit._styleModules[stylingEndpointName] = []; Gerrit._styleModules[stylingEndpointName] = [];
} }
Gerrit._styleModules[stylingEndpointName].push({ Gerrit._styleModules[stylingEndpointName].push({
pluginUrl: this._url, pluginUrl: this._url,
moduleName: moduleName, moduleName,
}); });
}; };
@@ -87,7 +87,7 @@
Plugin._sharedAPIElement.Element.REPLY_DIALOG)); Plugin._sharedAPIElement.Element.REPLY_DIALOG));
}; };
var Gerrit = window.Gerrit || {}; const Gerrit = window.Gerrit || {};
// Number of plugins to initialize, -1 means 'not yet known'. // Number of plugins to initialize, -1 means 'not yet known'.
Gerrit._pluginsPending = -1; Gerrit._pluginsPending = -1;
@@ -102,12 +102,13 @@
Gerrit.css = function(rulesStr) { Gerrit.css = function(rulesStr) {
if (!Gerrit._customStyleSheet) { if (!Gerrit._customStyleSheet) {
var styleEl = document.createElement('style'); const styleEl = document.createElement('style');
document.head.appendChild(styleEl); document.head.appendChild(styleEl);
Gerrit._customStyleSheet = styleEl.sheet; Gerrit._customStyleSheet = styleEl.sheet;
} }
var name = '__pg_js_api_class_' + Gerrit._customStyleSheet.cssRules.length; const name = '__pg_js_api_class_' +
Gerrit._customStyleSheet.cssRules.length;
Gerrit._customStyleSheet.insertRule('.' + name + '{' + rulesStr + '}', 0); Gerrit._customStyleSheet.insertRule('.' + name + '{' + rulesStr + '}', 0);
return name; return name;
}; };
@@ -121,9 +122,9 @@
} }
// TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it). // TODO(andybons): Polyfill currentScript for IE10/11 (edge supports it).
var src = opt_src || (document.currentScript && const src = opt_src || (document.currentScript &&
document.currentScript.src || document.currentScript.baseURI); document.currentScript.src || document.currentScript.baseURI);
var plugin = new Plugin(src); const plugin = new Plugin(src);
try { try {
callback(plugin); callback(plugin);
} catch (e) { } catch (e) {
@@ -155,7 +156,7 @@
if (Gerrit._arePluginsLoaded()) { if (Gerrit._arePluginsLoaded()) {
Gerrit._allPluginsPromise = Promise.resolve(); Gerrit._allPluginsPromise = Promise.resolve();
} else { } else {
Gerrit._allPluginsPromise = new Promise(function(resolve) { Gerrit._allPluginsPromise = new Promise(resolve => {
Gerrit._resolveAllPluginsLoaded = resolve; Gerrit._resolveAllPluginsLoaded = resolve;
}); });
} }