blob: ca0f3722aa9622afa3a089f08e1c8df5b823844e [file] [log] [blame]
Andrew Bonventred2b90432016-03-29 14:12:33 -04001<!DOCTYPE html>
2<!--
3Copyright (C) 2016 The Android Open Source Project
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16-->
17
18<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
19<title>gr-api-interface</title>
20
Viktar Donich29e1ce52017-03-28 17:02:44 -070021<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
Andrew Bonventred2b90432016-03-29 14:12:33 -040022<script src="../../../bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040023<link rel="import" href="../../../test/common-test-setup.html"/>
Andrew Bonventred2b90432016-03-29 14:12:33 -040024<link rel="import" href="gr-js-api-interface.html">
25
Viktar Donich29e1ce52017-03-28 17:02:44 -070026<script>void(0);</script>
27
Andrew Bonventred2b90432016-03-29 14:12:33 -040028<test-fixture id="basic">
29 <template>
30 <gr-js-api-interface></gr-js-api-interface>
31 </template>
32</test-fixture>
33
34<script>
Kasper Nilsson75214af2017-05-15 16:39:47 -070035 suite('gr-js-api-interface tests', () => {
36 let element;
37 let plugin;
38 let errorStub;
39 let sandbox;
Viktar Donich23893272017-05-24 16:03:42 -070040 let getResponseObjectStub;
41 let sendStub;
Viktar Donich5ad95122016-12-15 14:06:34 -080042
Kasper Nilsson75214af2017-05-15 16:39:47 -070043 const throwErrFn = function() {
Andrew Bonventre033b9312016-06-15 14:09:32 -070044 throw Error('Unfortunately, this handler has stopped');
45 };
Andrew Bonventred2b90432016-03-29 14:12:33 -040046
Kasper Nilsson75214af2017-05-15 16:39:47 -070047 setup(() => {
Viktar Donich5ad95122016-12-15 14:06:34 -080048 sandbox = sinon.sandbox.create();
Viktar Donich23893272017-05-24 16:03:42 -070049 getResponseObjectStub = sandbox.stub().returns(Promise.resolve());
Viktar Donichd6be68a2017-06-28 15:07:51 -070050 sendStub = sandbox.stub().returns(Promise.resolve({status: 200}));
Andrew Bonventre5c38e9a2016-06-29 14:45:23 -040051 stub('gr-rest-api-interface', {
Kasper Nilsson75214af2017-05-15 16:39:47 -070052 getAccount() {
Andrew Bonventre5c38e9a2016-06-29 14:45:23 -040053 return Promise.resolve({name: 'Judy Hopps'});
54 },
Viktar Donich23893272017-05-24 16:03:42 -070055 getResponseObject: getResponseObjectStub,
56 send(...args) {
57 return sendStub(...args);
58 },
Viktar Donich26039652016-06-29 17:05:14 -070059 });
Andrew Bonventred2b90432016-03-29 14:12:33 -040060 element = fixture('basic');
Viktar Donich5ad95122016-12-15 14:06:34 -080061 errorStub = sandbox.stub(console, 'error');
Wyatt Allen64705582016-12-08 14:19:51 -080062 Gerrit._setPluginsCount(1);
Kasper Nilsson75214af2017-05-15 16:39:47 -070063 Gerrit.install(p => { plugin = p; }, '0.1',
Andrew Bonventre4adac422016-04-19 14:33:53 -040064 'http://test.com/plugins/testplugin/static/test.js');
Andrew Bonventred2b90432016-03-29 14:12:33 -040065 });
66
Kasper Nilsson75214af2017-05-15 16:39:47 -070067 teardown(() => {
Viktar Donich5ad95122016-12-15 14:06:34 -080068 sandbox.restore();
Andrew Bonventred2b90432016-03-29 14:12:33 -040069 element._removeEventCallbacks();
70 plugin = null;
71 });
72
Kasper Nilsson75214af2017-05-15 16:39:47 -070073 test('url', () => {
Andrew Bonventre4adac422016-04-19 14:33:53 -040074 assert.equal(plugin.url(), 'http://test.com/plugins/testplugin/');
75 assert.equal(plugin.url('/static/test.js'),
76 'http://test.com/plugins/testplugin/static/test.js');
77 });
78
Viktar Donichd6be68a2017-06-28 15:07:51 -070079 test('_send on failure rejects with response text', () => {
80 sendStub.returns(Promise.resolve(
Kasper Nilssonac8bea92017-07-20 13:07:48 -070081 {status: 400, text() { return Promise.resolve('text'); }}));
Viktar Donichd6be68a2017-06-28 15:07:51 -070082 return plugin._send().catch(r => {
83 assert.equal(r, 'text');
Viktar Donich23893272017-05-24 16:03:42 -070084 });
85 });
86
Viktar Donichd6be68a2017-06-28 15:07:51 -070087 test('_send on failure without text rejects with code', () => {
88 sendStub.returns(Promise.resolve(
Kasper Nilssonac8bea92017-07-20 13:07:48 -070089 {status: 400, text() { return Promise.resolve(null); }}));
Viktar Donichd6be68a2017-06-28 15:07:51 -070090 return plugin._send().catch(r => {
91 assert.equal(r, '400');
92 });
93 });
94
95 test('get', () => {
96 const response = {foo: 'foo'};
97 getResponseObjectStub.returns(Promise.resolve(response));
98 return plugin.get('/url', r => {
99 assert.isTrue(sendStub.calledWith('GET', '/url'));
100 assert.strictEqual(r, response);
101 });
102 });
103
104 test('get using Promise', () => {
105 const response = {foo: 'foo'};
106 getResponseObjectStub.returns(Promise.resolve(response));
107 return plugin.get('/url', r => 'rubbish').then(r => {
108 assert.isTrue(sendStub.calledWith('GET', '/url'));
109 assert.strictEqual(r, response);
110 });
111 });
112
113 test('post', () => {
Viktar Donich23893272017-05-24 16:03:42 -0700114 const payload = {foo: 'foo'};
115 const response = {bar: 'bar'};
116 getResponseObjectStub.returns(Promise.resolve(response));
Viktar Donichd6be68a2017-06-28 15:07:51 -0700117 return plugin.post('/url', payload, r => {
Viktar Donich23893272017-05-24 16:03:42 -0700118 assert.isTrue(sendStub.calledWith('POST', '/url', payload));
119 assert.strictEqual(r, response);
Viktar Donich23893272017-05-24 16:03:42 -0700120 });
121 });
122
Kasper Nilsson75214af2017-05-15 16:39:47 -0700123 test('history event', done => {
Andrew Bonventre033b9312016-06-15 14:09:32 -0700124 plugin.on(element.EventType.HISTORY, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700125 plugin.on(element.EventType.HISTORY, path => {
Andrew Bonventred2b90432016-03-29 14:12:33 -0400126 assert.equal(path, '/path/to/awesomesauce');
Andrew Bonventre033b9312016-06-15 14:09:32 -0700127 assert.isTrue(errorStub.calledOnce);
Andrew Bonventred2b90432016-03-29 14:12:33 -0400128 done();
129 });
130 element.handleEvent(element.EventType.HISTORY,
131 {path: '/path/to/awesomesauce'});
132 });
133
Kasper Nilsson75214af2017-05-15 16:39:47 -0700134 test('showchange event', done => {
135 const testChange = {
Andrew Bonventred2b90432016-03-29 14:12:33 -0400136 _number: 42,
Wyatt Allen64705582016-12-08 14:19:51 -0800137 revisions: {def: {_number: 2}, abc: {_number: 1}},
Andrew Bonventred2b90432016-03-29 14:12:33 -0400138 };
Andrew Bonventre033b9312016-06-15 14:09:32 -0700139 plugin.on(element.EventType.SHOW_CHANGE, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700140 plugin.on(element.EventType.SHOW_CHANGE, (change, revision) => {
Andrew Bonventred2b90432016-03-29 14:12:33 -0400141 assert.deepEqual(change, testChange);
142 assert.deepEqual(revision, testChange.revisions.abc);
Andrew Bonventre033b9312016-06-15 14:09:32 -0700143 assert.isTrue(errorStub.calledOnce);
Andrew Bonventred2b90432016-03-29 14:12:33 -0400144 done();
145 });
146 element.handleEvent(element.EventType.SHOW_CHANGE,
147 {change: testChange, patchNum: 1});
148 });
149
Kasper Nilsson75214af2017-05-15 16:39:47 -0700150 test('handleEvent awaits plugins load', done => {
151 const testChange = {
Wyatt Allen64705582016-12-08 14:19:51 -0800152 _number: 42,
153 revisions: {def: {_number: 2}, abc: {_number: 1}},
154 };
Kasper Nilsson75214af2017-05-15 16:39:47 -0700155 const spy = sandbox.spy();
Wyatt Allen64705582016-12-08 14:19:51 -0800156 Gerrit._setPluginsCount(1);
157 plugin.on(element.EventType.SHOW_CHANGE, spy);
158 element.handleEvent(element.EventType.SHOW_CHANGE,
159 {change: testChange, patchNum: 1});
160 assert.isFalse(spy.called);
161 Gerrit._setPluginsCount(0);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700162 flush(() => {
Wyatt Allen64705582016-12-08 14:19:51 -0800163 assert.isTrue(spy.called);
164 done();
165 });
166 });
167
Kasper Nilsson75214af2017-05-15 16:39:47 -0700168 test('comment event', done => {
169 const testCommentNode = {foo: 'bar'};
Andrew Bonventre033b9312016-06-15 14:09:32 -0700170 plugin.on(element.EventType.COMMENT, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700171 plugin.on(element.EventType.COMMENT, commentNode => {
Andrew Bonventred2b90432016-03-29 14:12:33 -0400172 assert.deepEqual(commentNode, testCommentNode);
Andrew Bonventre033b9312016-06-15 14:09:32 -0700173 assert.isTrue(errorStub.calledOnce);
Andrew Bonventred2b90432016-03-29 14:12:33 -0400174 done();
175 });
176 element.handleEvent(element.EventType.COMMENT, {node: testCommentNode});
177 });
178
Kasper Nilsson75214af2017-05-15 16:39:47 -0700179 test('revert event', () => {
Ravi Mistryc88cdda2016-10-10 16:06:01 -0400180 function appendToRevertMsg(c, revertMsg, originalMsg) {
181 return revertMsg + '\n' + originalMsg.replace(/^/gm, '> ') + '\ninfo';
Ravi Mistry7d64e782016-08-02 07:48:45 -0400182 }
Ravi Mistry7d64e782016-08-02 07:48:45 -0400183
Ravi Mistryc88cdda2016-10-10 16:06:01 -0400184 assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), 'test');
Ravi Mistry7d64e782016-08-02 07:48:45 -0400185 assert.equal(errorStub.callCount, 0);
186
187 plugin.on(element.EventType.REVERT, throwErrFn);
188 plugin.on(element.EventType.REVERT, appendToRevertMsg);
Ravi Mistryc88cdda2016-10-10 16:06:01 -0400189 assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'),
Kasper Nilsson75214af2017-05-15 16:39:47 -0700190 'test\n> origTest\ninfo');
Ravi Mistry7d64e782016-08-02 07:48:45 -0400191 assert.isTrue(errorStub.calledOnce);
192
193 plugin.on(element.EventType.REVERT, appendToRevertMsg);
Ravi Mistryc88cdda2016-10-10 16:06:01 -0400194 assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'),
Kasper Nilsson75214af2017-05-15 16:39:47 -0700195 'test\n> origTest\ninfo\n> origTest\ninfo');
Ravi Mistry7d64e782016-08-02 07:48:45 -0400196 assert.isTrue(errorStub.calledTwice);
197 });
198
Kasper Nilsson75214af2017-05-15 16:39:47 -0700199 test('postrevert event', () => {
Ravi Mistryae0ae482016-09-29 08:01:21 -0400200 function getLabels(c) {
201 return {'Code-Review': 1};
202 }
Ravi Mistryae0ae482016-09-29 08:01:21 -0400203
204 assert.deepEqual(element.getLabelValuesPostRevert(null), {});
205 assert.equal(errorStub.callCount, 0);
206
207 plugin.on(element.EventType.POST_REVERT, throwErrFn);
208 plugin.on(element.EventType.POST_REVERT, getLabels);
Viktar Donich5ad95122016-12-15 14:06:34 -0800209 assert.deepEqual(
210 element.getLabelValuesPostRevert(null), {'Code-Review': 1});
Ravi Mistryae0ae482016-09-29 08:01:21 -0400211 assert.isTrue(errorStub.calledOnce);
212 });
213
Kasper Nilsson75214af2017-05-15 16:39:47 -0700214 test('commitmsgedit event', done => {
215 const testMsg = 'Test CL commit message';
Ravi Mistry3bb2dd22016-12-02 14:57:47 -0500216 plugin.on(element.EventType.COMMIT_MSG_EDIT, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700217 plugin.on(element.EventType.COMMIT_MSG_EDIT, (change, msg) => {
Ravi Mistry3bb2dd22016-12-02 14:57:47 -0500218 assert.deepEqual(msg, testMsg);
219 assert.isTrue(errorStub.calledOnce);
220 done();
221 });
222 element.handleCommitMessage(null, testMsg);
223 });
224
Kasper Nilsson75214af2017-05-15 16:39:47 -0700225 test('labelchange event', done => {
226 const testChange = {_number: 42};
Andrew Bonventre5474de82016-06-27 16:34:12 -0400227 plugin.on(element.EventType.LABEL_CHANGE, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700228 plugin.on(element.EventType.LABEL_CHANGE, change => {
Andrew Bonventre5474de82016-06-27 16:34:12 -0400229 assert.deepEqual(change, testChange);
230 assert.isTrue(errorStub.calledOnce);
231 done();
232 });
233 element.handleEvent(element.EventType.LABEL_CHANGE, {change: testChange});
234 });
235
Kasper Nilsson75214af2017-05-15 16:39:47 -0700236 test('submitchange', () => {
Andrew Bonventre033b9312016-06-15 14:09:32 -0700237 plugin.on(element.EventType.SUBMIT_CHANGE, throwErrFn);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700238 plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; });
Andrew Bonventred2b90432016-03-29 14:12:33 -0400239 assert.isTrue(element.canSubmitChange());
Andrew Bonventre033b9312016-06-15 14:09:32 -0700240 assert.isTrue(errorStub.calledOnce);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700241 plugin.on(element.EventType.SUBMIT_CHANGE, () => { return false; });
242 plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; });
Andrew Bonventred2b90432016-03-29 14:12:33 -0400243 assert.isFalse(element.canSubmitChange());
Andrew Bonventre033b9312016-06-15 14:09:32 -0700244 assert.isTrue(errorStub.calledTwice);
Andrew Bonventred2b90432016-03-29 14:12:33 -0400245 });
246
Kasper Nilsson75214af2017-05-15 16:39:47 -0700247 test('versioning', () => {
248 const callback = sandbox.spy();
Andrew Bonventree665f082016-06-27 16:12:12 -0400249 Gerrit.install(callback, '0.0pre-alpha');
250 assert(callback.notCalled);
251 });
252
Kasper Nilsson75214af2017-05-15 16:39:47 -0700253 test('getAccount', done => {
254 Gerrit.getLoggedIn().then(loggedIn => {
Andrew Bonventre5c38e9a2016-06-29 14:45:23 -0400255 assert.isTrue(loggedIn);
256 done();
257 });
258 });
259
Kasper Nilsson75214af2017-05-15 16:39:47 -0700260 test('_setPluginsCount', done => {
Viktar Doniche8680ea2016-10-05 11:58:58 -0700261 stub('gr-reporting', {
Kasper Nilsson75214af2017-05-15 16:39:47 -0700262 pluginsLoaded() {
Viktar Doniche8680ea2016-10-05 11:58:58 -0700263 assert.equal(Gerrit._pluginsPending, 0);
264 done();
Kasper Nilsson75214af2017-05-15 16:39:47 -0700265 },
Viktar Doniche8680ea2016-10-05 11:58:58 -0700266 });
267 Gerrit._setPluginsCount(0);
268 });
269
Kasper Nilsson75214af2017-05-15 16:39:47 -0700270 test('_arePluginsLoaded', () => {
Wyatt Allen64705582016-12-08 14:19:51 -0800271 assert.isTrue(Gerrit._arePluginsLoaded());
Viktar Doniche8680ea2016-10-05 11:58:58 -0700272 Gerrit._setPluginsCount(1);
273 assert.isFalse(Gerrit._arePluginsLoaded());
274 Gerrit._setPluginsCount(0);
275 assert.isTrue(Gerrit._arePluginsLoaded());
276 });
277
Kasper Nilsson75214af2017-05-15 16:39:47 -0700278 test('_pluginInstalled', done => {
Viktar Doniche8680ea2016-10-05 11:58:58 -0700279 stub('gr-reporting', {
Kasper Nilsson75214af2017-05-15 16:39:47 -0700280 pluginsLoaded() {
Viktar Donich5ad95122016-12-15 14:06:34 -0800281 assert.equal(Gerrit._pluginsPending, 0);
Viktar Doniche8680ea2016-10-05 11:58:58 -0700282 done();
Kasper Nilsson75214af2017-05-15 16:39:47 -0700283 },
Viktar Doniche8680ea2016-10-05 11:58:58 -0700284 });
285 Gerrit._setPluginsCount(2);
286 Gerrit._pluginInstalled();
287 assert.equal(Gerrit._pluginsPending, 1);
288 Gerrit._pluginInstalled();
289 });
290
Kasper Nilsson75214af2017-05-15 16:39:47 -0700291 test('install calls _pluginInstalled', () => {
Viktar Donich5ad95122016-12-15 14:06:34 -0800292 sandbox.stub(Gerrit, '_pluginInstalled');
Kasper Nilsson75214af2017-05-15 16:39:47 -0700293 Gerrit.install(p => { plugin = p; }, '0.1',
Viktar Doniche8680ea2016-10-05 11:58:58 -0700294 'http://test.com/plugins/testplugin/static/test.js');
Viktar Donich5ad95122016-12-15 14:06:34 -0800295 assert.isTrue(Gerrit._pluginInstalled.calledOnce);
Viktar Doniche8680ea2016-10-05 11:58:58 -0700296 });
297
Kasper Nilsson75214af2017-05-15 16:39:47 -0700298 test('install calls _pluginInstalled on error', () => {
Viktar Donich5ad95122016-12-15 14:06:34 -0800299 sandbox.stub(Gerrit, '_pluginInstalled');
Kasper Nilsson75214af2017-05-15 16:39:47 -0700300 Gerrit.install(() => {}, '0.0pre-alpha');
Viktar Donich5ad95122016-12-15 14:06:34 -0800301 assert.isTrue(Gerrit._pluginInstalled.calledOnce);
302 });
303
Kasper Nilsson75214af2017-05-15 16:39:47 -0700304 test('installGwt calls _pluginInstalled', () => {
Viktar Donich5ad95122016-12-15 14:06:34 -0800305 sandbox.stub(Gerrit, '_pluginInstalled');
306 Gerrit.installGwt();
307 assert.isTrue(Gerrit._pluginInstalled.calledOnce);
308 });
309
Kasper Nilsson75214af2017-05-15 16:39:47 -0700310 test('installGwt returns a stub object', () => {
311 const plugin = Gerrit.installGwt();
Viktar Donich5ad95122016-12-15 14:06:34 -0800312 sandbox.stub(console, 'warn');
313 assert.isAbove(Object.keys(plugin).length, 0);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700314 for (const name of Object.keys(plugin)) {
Viktar Donich5ad95122016-12-15 14:06:34 -0800315 console.warn.reset();
316 plugin[name]();
317 assert.isTrue(console.warn.calledOnce);
Kasper Nilsson75214af2017-05-15 16:39:47 -0700318 }
Viktar Doniche8680ea2016-10-05 11:58:58 -0700319 });
Paladox none9b25fea2017-07-29 14:43:11 +0000320
Viktar Doniche7112572017-07-31 14:14:37 -0700321 test('attributeHelper', () => {
322 assert.isOk(plugin.attributeHelper());
323 });
324
Paladox none9b25fea2017-07-29 14:43:11 +0000325 suite('test plugin with base url', () => {
326 setup(() => {
327 sandbox.stub(Gerrit.BaseUrlBehavior, 'getBaseUrl').returns('/r');
328
329 Gerrit._setPluginsCount(1);
330 Gerrit.install(p => { plugin = p; }, '0.1',
331 'http://test.com/r/plugins/testplugin/static/test.js');
332 });
333
334 test('url', () => {
335 assert.notEqual(plugin.url(), 'http://test.com/plugins/testplugin/');
336 assert.equal(plugin.url(), 'http://test.com/r/plugins/testplugin/');
337 assert.equal(plugin.url('/static/test.js'),
338 'http://test.com/r/plugins/testplugin/static/test.js');
339 });
340 });
Andrew Bonventred2b90432016-03-29 14:12:33 -0400341 });
342</script>