Merge "Ensure Change and Reply plugin APIs have element set"

This commit is contained in:
Kasper Nilsson
2017-11-14 22:56:34 +00:00
committed by Gerrit Code Review
4 changed files with 234 additions and 124 deletions

View File

@@ -14,21 +14,49 @@
(function(window) { (function(window) {
'use strict'; 'use strict';
/**
* Ensure GrChangeActionsInterface instance has access to gr-change-actions
* element and retrieve if the interface was created before element.
* @param {!GrChangeActionsInterface} api
*/
function ensureEl(api) {
if (!api._el) {
const sharedApiElement = document.createElement('gr-js-api-interface');
setEl(api, sharedApiElement.getElement(
sharedApiElement.Element.CHANGE_ACTIONS));
}
}
/**
* Set gr-change-actions element to a GrChangeActionsInterface instance.
* @param {!GrChangeActionsInterface} api
* @param {!Element} el gr-change-actions
*/
function setEl(api, el) {
if (!el) {
console.warn('changeActions() is not ready');
return;
}
api._el = el;
api.RevisionActions = el.RevisionActions;
api.ChangeActions = el.ChangeActions;
api.ActionType = el.ActionType;
}
function GrChangeActionsInterface(plugin, el) { function GrChangeActionsInterface(plugin, el) {
this.plugin = plugin; this.plugin = plugin;
this._el = el; setEl(this, el);
this.RevisionActions = el.RevisionActions;
this.ChangeActions = el.ChangeActions;
this.ActionType = el.ActionType;
} }
GrChangeActionsInterface.prototype.addPrimaryActionKey = function(key) { GrChangeActionsInterface.prototype.addPrimaryActionKey = function(key) {
ensureEl(this);
if (this._el.primaryActionKeys.includes(key)) { 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) {
ensureEl(this);
this._el.primaryActionKeys = this._el.primaryActionKeys.filter(k => { this._el.primaryActionKeys = this._el.primaryActionKeys.filter(k => {
return k !== key; return k !== key;
}); });
@@ -36,45 +64,55 @@
GrChangeActionsInterface.prototype.setActionOverflow = function(type, key, GrChangeActionsInterface.prototype.setActionOverflow = function(type, key,
overflow) { overflow) {
ensureEl(this);
return this._el.setActionOverflow(type, key, overflow); return this._el.setActionOverflow(type, key, overflow);
}; };
GrChangeActionsInterface.prototype.setActionPriority = function(type, key, GrChangeActionsInterface.prototype.setActionPriority = function(type, key,
priority) { priority) {
ensureEl(this);
return this._el.setActionPriority(type, key, priority); return this._el.setActionPriority(type, key, priority);
}; };
GrChangeActionsInterface.prototype.setActionHidden = function(type, key, GrChangeActionsInterface.prototype.setActionHidden = function(type, key,
hidden) { hidden) {
ensureEl(this);
return this._el.setActionHidden(type, key, hidden); return this._el.setActionHidden(type, key, hidden);
}; };
GrChangeActionsInterface.prototype.add = function(type, label) { GrChangeActionsInterface.prototype.add = function(type, label) {
ensureEl(this);
return this._el.addActionButton(type, label); return this._el.addActionButton(type, label);
}; };
GrChangeActionsInterface.prototype.remove = function(key) { GrChangeActionsInterface.prototype.remove = function(key) {
ensureEl(this);
return this._el.removeActionButton(key); return this._el.removeActionButton(key);
}; };
GrChangeActionsInterface.prototype.addTapListener = function(key, handler) { GrChangeActionsInterface.prototype.addTapListener = function(key, handler) {
ensureEl(this);
this._el.addEventListener(key + '-tap', handler); this._el.addEventListener(key + '-tap', handler);
}; };
GrChangeActionsInterface.prototype.removeTapListener = function(key, GrChangeActionsInterface.prototype.removeTapListener = function(key,
handler) { handler) {
ensureEl(this);
this._el.removeEventListener(key + '-tap', handler); this._el.removeEventListener(key + '-tap', handler);
}; };
GrChangeActionsInterface.prototype.setLabel = function(key, text) { GrChangeActionsInterface.prototype.setLabel = function(key, text) {
ensureEl(this);
this._el.setActionButtonProp(key, 'label', text); this._el.setActionButtonProp(key, 'label', text);
}; };
GrChangeActionsInterface.prototype.setEnabled = function(key, enabled) { GrChangeActionsInterface.prototype.setEnabled = function(key, enabled) {
ensureEl(this);
this._el.setActionButtonProp(key, 'enabled', enabled); this._el.setActionButtonProp(key, 'enabled', enabled);
}; };
GrChangeActionsInterface.prototype.getActionDetails = function(action) { GrChangeActionsInterface.prototype.getActionDetails = function(action) {
ensureEl(this);
return this._el.getActionDetails(action) || return this._el.getActionDetails(action) ||
this._el.getActionDetails(this.plugin.getPluginName() + '~' + action); this._el.getActionDetails(this.plugin.getPluginName() + '~' + action);
}; };

View File

@@ -39,6 +39,7 @@ breaking changes to gr-change-actions wont be noticed.
suite('gr-js-api-interface tests', () => { suite('gr-js-api-interface tests', () => {
let element; let element;
let changeActions; let changeActions;
let plugin;
// Because deepEqual doesnt behave in Safari. // Because deepEqual doesnt behave in Safari.
function assertArraysEqual(actual, expected) { function assertArraysEqual(actual, expected) {
@@ -48,11 +49,30 @@ breaking changes to gr-change-actions wont be noticed.
} }
} }
suite('early init', () => {
setup(() => {
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
changeActions = plugin.changeActions();
element = fixture('basic');
});
teardown(() => {
changeActions = null;
});
test('does not throw', ()=> {
assert.doesNotThrow(() => {
changeActions.add('change', 'foo');
});
});
});
suite('normal init', () => {
setup(() => { setup(() => {
element = fixture('basic'); element = fixture('basic');
element.change = {}; element.change = {};
element._hasKnownChainState = false; element._hasKnownChainState = false;
let plugin;
Gerrit.install(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();
@@ -178,4 +198,5 @@ breaking changes to gr-change-actions wont be noticed.
}); });
}); });
}); });
});
</script> </script>

View File

@@ -14,6 +14,19 @@
(function(window) { (function(window) {
'use strict'; 'use strict';
/**
* Ensure GrChangeReplyInterface instance has access to gr-reply-dialog
* element and retrieve if the interface was created before element.
* @param {!GrChangeReplyInterfaceOld} api
*/
function ensureEl(api) {
if (!api._el) {
const sharedApiElement = document.createElement('gr-js-api-interface');
api._el = sharedApiElement.getElement(
sharedApiElement.Element.REPLY_DIALOG);
}
}
/** /**
* @deprecated * @deprecated
*/ */
@@ -22,14 +35,17 @@
} }
GrChangeReplyInterfaceOld.prototype.getLabelValue = function(label) { GrChangeReplyInterfaceOld.prototype.getLabelValue = function(label) {
ensureEl(this);
return this._el.getLabelValue(label); return this._el.getLabelValue(label);
}; };
GrChangeReplyInterfaceOld.prototype.setLabelValue = function(label, value) { GrChangeReplyInterfaceOld.prototype.setLabelValue = function(label, value) {
ensureEl(this);
this._el.setLabelValue(label, value); this._el.setLabelValue(label, value);
}; };
GrChangeReplyInterfaceOld.prototype.send = function(opt_includeComments) { GrChangeReplyInterfaceOld.prototype.send = function(opt_includeComments) {
ensureEl(this);
return this._el.send(opt_includeComments); return this._el.send(opt_includeComments);
}; };

View File

@@ -40,26 +40,33 @@ breaking changes to gr-reply-dialog wont be noticed.
let element; let element;
let sandbox; let sandbox;
let changeReply; let changeReply;
let plugin;
setup(() => { setup(() => {
sandbox = sinon.sandbox.create();
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getConfig() { return Promise.resolve({}); }, getConfig() { return Promise.resolve({}); },
getAccount() { return Promise.resolve(null); }, getAccount() { return Promise.resolve(null); },
}); });
element = fixture('basic'); });
sandbox = sinon.sandbox.create();
let plugin; teardown(() => {
sandbox.restore();
});
suite('early init', () => {
setup(() => {
Gerrit.install(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();
element = fixture('basic');
}); });
teardown(() => { teardown(() => {
changeReply = null; changeReply = null;
sandbox.restore();
}); });
test('calls', () => { test('works', () => {
sandbox.stub(element, 'getLabelValue').returns('+123'); sandbox.stub(element, 'getLabelValue').returns('+123');
assert.equal(changeReply.getLabelValue('My-Label'), '+123'); assert.equal(changeReply.getLabelValue('My-Label'), '+123');
@@ -73,4 +80,32 @@ breaking changes to gr-reply-dialog wont be noticed.
assert.isTrue(element.send.calledWithExactly(false)); assert.isTrue(element.send.calledWithExactly(false));
}); });
}); });
suite('normal init', () => {
setup(() => {
element = fixture('basic');
Gerrit.install(p => { plugin = p; }, '0.1',
'http://test.com/plugins/testplugin/static/test.js');
changeReply = plugin.changeReply();
});
teardown(() => {
changeReply = null;
});
test('works', () => {
sandbox.stub(element, 'getLabelValue').returns('+123');
assert.equal(changeReply.getLabelValue('My-Label'), '+123');
sandbox.stub(element, 'setLabelValue');
changeReply.setLabelValue('My-Label', '+1337');
assert.isTrue(
element.setLabelValue.calledWithExactly('My-Label', '+1337'));
sandbox.stub(element, 'send');
changeReply.send(false);
assert.isTrue(element.send.calledWithExactly(false));
});
});
});
</script> </script>