Merge "Implement self.get and self.post from Gerrit JS API"

This commit is contained in:
Kasper Nilsson
2017-05-25 22:38:21 +00:00
committed by Gerrit Code Review
3 changed files with 52 additions and 1 deletions

View File

@@ -15,10 +15,10 @@ limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../core/gr-reporting/gr-reporting.html">
<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
<link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html">
<dom-module id="gr-js-api-interface">
<template></template>
<script src="gr-change-actions-js-api.js"></script>
<script src="gr-change-reply-js-api.js"></script>
<script src="gr-js-api-interface.js"></script>

View File

@@ -36,6 +36,8 @@ limitations under the License.
let plugin;
let errorStub;
let sandbox;
let getResponseObjectStub;
let sendStub;
const throwErrFn = function() {
throw Error('Unfortunately, this handler has stopped');
@@ -43,10 +45,16 @@ limitations under the License.
setup(() => {
sandbox = sinon.sandbox.create();
getResponseObjectStub = sandbox.stub().returns(Promise.resolve());
sendStub = sandbox.stub().returns(Promise.resolve());
stub('gr-rest-api-interface', {
getAccount() {
return Promise.resolve({name: 'Judy Hopps'});
},
getResponseObject: getResponseObjectStub,
send(...args) {
return sendStub(...args);
},
});
element = fixture('basic');
errorStub = sandbox.stub(console, 'error');
@@ -67,6 +75,27 @@ limitations under the License.
'http://test.com/plugins/testplugin/static/test.js');
});
test('get', done => {
const response = {foo: 'foo'};
getResponseObjectStub.returns(Promise.resolve(response));
plugin.get('/url', r => {
assert.isTrue(sendStub.calledWith('GET', '/url'));
assert.strictEqual(r, response);
done();
});
});
test('post', done => {
const payload = {foo: 'foo'};
const response = {bar: 'bar'};
getResponseObjectStub.returns(Promise.resolve(response));
plugin.post('/url', payload, r => {
assert.isTrue(sendStub.calledWith('POST', '/url', payload));
assert.strictEqual(r, response);
done();
});
});
test('history event', done => {
plugin.on(element.EventType.HISTORY, throwErrFn);
plugin.on(element.EventType.HISTORY, path => {

View File

@@ -24,6 +24,14 @@
GWT_PLUGIN_STUB[name] = warnNotSupported.bind(null, name);
}
let _restAPI;
const getRestAPI = () => {
if (!_restAPI) {
_restAPI = document.createElement('gr-rest-api-interface');
}
return _restAPI;
};
const API_VERSION = '0.1';
// GWT JSNI uses $wnd to refer to window.
@@ -77,6 +85,20 @@
return this._url.origin + '/plugins/' + this._name + (opt_path || '/');
};
Plugin.prototype._send = function(method, url, callback, opt_payload) {
return getRestAPI().send(method, url, opt_payload)
.then(getRestAPI().getResponseObject)
.then(callback);
};
Plugin.prototype.get = function(url, callback) {
return this._send('GET', url, callback);
},
Plugin.prototype.post = function(url, payload, callback) {
return this._send('POST', url, callback, payload);
},
Plugin.prototype.changeActions = function() {
return new GrChangeActionsInterface(Plugin._sharedAPIElement.getElement(
Plugin._sharedAPIElement.Element.CHANGE_ACTIONS));