Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright (C) 2016 The Android Open Source Project |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations 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 Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 21 | <script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script> |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 22 | <script src="../../../bower_components/web-component-tester/browser.js"></script> |
Mike Samuel | e07c4b2 | 2017-06-02 13:08:19 -0400 | [diff] [blame] | 23 | <link rel="import" href="../../../test/common-test-setup.html"/> |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 24 | <link rel="import" href="gr-js-api-interface.html"> |
| 25 | |
Viktar Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 26 | <script>void(0);</script> |
| 27 | |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 28 | <test-fixture id="basic"> |
| 29 | <template> |
| 30 | <gr-js-api-interface></gr-js-api-interface> |
| 31 | </template> |
| 32 | </test-fixture> |
| 33 | |
| 34 | <script> |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 35 | suite('gr-js-api-interface tests', () => { |
| 36 | let element; |
| 37 | let plugin; |
| 38 | let errorStub; |
| 39 | let sandbox; |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 40 | let getResponseObjectStub; |
| 41 | let sendStub; |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 42 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 43 | const throwErrFn = function() { |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 44 | throw Error('Unfortunately, this handler has stopped'); |
| 45 | }; |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 46 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 47 | setup(() => { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 48 | sandbox = sinon.sandbox.create(); |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 49 | getResponseObjectStub = sandbox.stub().returns(Promise.resolve()); |
Viktar Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 50 | sendStub = sandbox.stub().returns(Promise.resolve({status: 200})); |
Andrew Bonventre | 5c38e9a | 2016-06-29 14:45:23 -0400 | [diff] [blame] | 51 | stub('gr-rest-api-interface', { |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 52 | getAccount() { |
Andrew Bonventre | 5c38e9a | 2016-06-29 14:45:23 -0400 | [diff] [blame] | 53 | return Promise.resolve({name: 'Judy Hopps'}); |
| 54 | }, |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 55 | getResponseObject: getResponseObjectStub, |
| 56 | send(...args) { |
| 57 | return sendStub(...args); |
| 58 | }, |
Viktar Donich | 2603965 | 2016-06-29 17:05:14 -0700 | [diff] [blame] | 59 | }); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 60 | element = fixture('basic'); |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 61 | errorStub = sandbox.stub(console, 'error'); |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 62 | Gerrit._setPluginsCount(1); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 63 | Gerrit.install(p => { plugin = p; }, '0.1', |
Andrew Bonventre | 4adac42 | 2016-04-19 14:33:53 -0400 | [diff] [blame] | 64 | 'http://test.com/plugins/testplugin/static/test.js'); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 65 | }); |
| 66 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 67 | teardown(() => { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 68 | sandbox.restore(); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 69 | element._removeEventCallbacks(); |
| 70 | plugin = null; |
| 71 | }); |
| 72 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 73 | test('url', () => { |
Andrew Bonventre | 4adac42 | 2016-04-19 14:33:53 -0400 | [diff] [blame] | 74 | 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 Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 79 | test('_send on failure rejects with response text', () => { |
| 80 | sendStub.returns(Promise.resolve( |
Kasper Nilsson | ac8bea9 | 2017-07-20 13:07:48 -0700 | [diff] [blame] | 81 | {status: 400, text() { return Promise.resolve('text'); }})); |
Viktar Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 82 | return plugin._send().catch(r => { |
| 83 | assert.equal(r, 'text'); |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 84 | }); |
| 85 | }); |
| 86 | |
Viktar Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 87 | test('_send on failure without text rejects with code', () => { |
| 88 | sendStub.returns(Promise.resolve( |
Kasper Nilsson | ac8bea9 | 2017-07-20 13:07:48 -0700 | [diff] [blame] | 89 | {status: 400, text() { return Promise.resolve(null); }})); |
Viktar Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 90 | 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 Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 114 | const payload = {foo: 'foo'}; |
| 115 | const response = {bar: 'bar'}; |
| 116 | getResponseObjectStub.returns(Promise.resolve(response)); |
Viktar Donich | d6be68a | 2017-06-28 15:07:51 -0700 | [diff] [blame] | 117 | return plugin.post('/url', payload, r => { |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 118 | assert.isTrue(sendStub.calledWith('POST', '/url', payload)); |
| 119 | assert.strictEqual(r, response); |
Viktar Donich | 2389327 | 2017-05-24 16:03:42 -0700 | [diff] [blame] | 120 | }); |
| 121 | }); |
| 122 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 123 | test('history event', done => { |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 124 | plugin.on(element.EventType.HISTORY, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 125 | plugin.on(element.EventType.HISTORY, path => { |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 126 | assert.equal(path, '/path/to/awesomesauce'); |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 127 | assert.isTrue(errorStub.calledOnce); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 128 | done(); |
| 129 | }); |
| 130 | element.handleEvent(element.EventType.HISTORY, |
| 131 | {path: '/path/to/awesomesauce'}); |
| 132 | }); |
| 133 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 134 | test('showchange event', done => { |
| 135 | const testChange = { |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 136 | _number: 42, |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 137 | revisions: {def: {_number: 2}, abc: {_number: 1}}, |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 138 | }; |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 139 | plugin.on(element.EventType.SHOW_CHANGE, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 140 | plugin.on(element.EventType.SHOW_CHANGE, (change, revision) => { |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 141 | assert.deepEqual(change, testChange); |
| 142 | assert.deepEqual(revision, testChange.revisions.abc); |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 143 | assert.isTrue(errorStub.calledOnce); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 144 | done(); |
| 145 | }); |
| 146 | element.handleEvent(element.EventType.SHOW_CHANGE, |
| 147 | {change: testChange, patchNum: 1}); |
| 148 | }); |
| 149 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 150 | test('handleEvent awaits plugins load', done => { |
| 151 | const testChange = { |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 152 | _number: 42, |
| 153 | revisions: {def: {_number: 2}, abc: {_number: 1}}, |
| 154 | }; |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 155 | const spy = sandbox.spy(); |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 156 | 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 Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 162 | flush(() => { |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 163 | assert.isTrue(spy.called); |
| 164 | done(); |
| 165 | }); |
| 166 | }); |
| 167 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 168 | test('comment event', done => { |
| 169 | const testCommentNode = {foo: 'bar'}; |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 170 | plugin.on(element.EventType.COMMENT, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 171 | plugin.on(element.EventType.COMMENT, commentNode => { |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 172 | assert.deepEqual(commentNode, testCommentNode); |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 173 | assert.isTrue(errorStub.calledOnce); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 174 | done(); |
| 175 | }); |
| 176 | element.handleEvent(element.EventType.COMMENT, {node: testCommentNode}); |
| 177 | }); |
| 178 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 179 | test('revert event', () => { |
Ravi Mistry | c88cdda | 2016-10-10 16:06:01 -0400 | [diff] [blame] | 180 | function appendToRevertMsg(c, revertMsg, originalMsg) { |
| 181 | return revertMsg + '\n' + originalMsg.replace(/^/gm, '> ') + '\ninfo'; |
Ravi Mistry | 7d64e78 | 2016-08-02 07:48:45 -0400 | [diff] [blame] | 182 | } |
Ravi Mistry | 7d64e78 | 2016-08-02 07:48:45 -0400 | [diff] [blame] | 183 | |
Ravi Mistry | c88cdda | 2016-10-10 16:06:01 -0400 | [diff] [blame] | 184 | assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), 'test'); |
Ravi Mistry | 7d64e78 | 2016-08-02 07:48:45 -0400 | [diff] [blame] | 185 | assert.equal(errorStub.callCount, 0); |
| 186 | |
| 187 | plugin.on(element.EventType.REVERT, throwErrFn); |
| 188 | plugin.on(element.EventType.REVERT, appendToRevertMsg); |
Ravi Mistry | c88cdda | 2016-10-10 16:06:01 -0400 | [diff] [blame] | 189 | assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 190 | 'test\n> origTest\ninfo'); |
Ravi Mistry | 7d64e78 | 2016-08-02 07:48:45 -0400 | [diff] [blame] | 191 | assert.isTrue(errorStub.calledOnce); |
| 192 | |
| 193 | plugin.on(element.EventType.REVERT, appendToRevertMsg); |
Ravi Mistry | c88cdda | 2016-10-10 16:06:01 -0400 | [diff] [blame] | 194 | assert.equal(element.modifyRevertMsg(null, 'test', 'origTest'), |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 195 | 'test\n> origTest\ninfo\n> origTest\ninfo'); |
Ravi Mistry | 7d64e78 | 2016-08-02 07:48:45 -0400 | [diff] [blame] | 196 | assert.isTrue(errorStub.calledTwice); |
| 197 | }); |
| 198 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 199 | test('postrevert event', () => { |
Ravi Mistry | ae0ae48 | 2016-09-29 08:01:21 -0400 | [diff] [blame] | 200 | function getLabels(c) { |
| 201 | return {'Code-Review': 1}; |
| 202 | } |
Ravi Mistry | ae0ae48 | 2016-09-29 08:01:21 -0400 | [diff] [blame] | 203 | |
| 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 Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 209 | assert.deepEqual( |
| 210 | element.getLabelValuesPostRevert(null), {'Code-Review': 1}); |
Ravi Mistry | ae0ae48 | 2016-09-29 08:01:21 -0400 | [diff] [blame] | 211 | assert.isTrue(errorStub.calledOnce); |
| 212 | }); |
| 213 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 214 | test('commitmsgedit event', done => { |
| 215 | const testMsg = 'Test CL commit message'; |
Ravi Mistry | 3bb2dd2 | 2016-12-02 14:57:47 -0500 | [diff] [blame] | 216 | plugin.on(element.EventType.COMMIT_MSG_EDIT, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 217 | plugin.on(element.EventType.COMMIT_MSG_EDIT, (change, msg) => { |
Ravi Mistry | 3bb2dd2 | 2016-12-02 14:57:47 -0500 | [diff] [blame] | 218 | assert.deepEqual(msg, testMsg); |
| 219 | assert.isTrue(errorStub.calledOnce); |
| 220 | done(); |
| 221 | }); |
| 222 | element.handleCommitMessage(null, testMsg); |
| 223 | }); |
| 224 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 225 | test('labelchange event', done => { |
| 226 | const testChange = {_number: 42}; |
Andrew Bonventre | 5474de8 | 2016-06-27 16:34:12 -0400 | [diff] [blame] | 227 | plugin.on(element.EventType.LABEL_CHANGE, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 228 | plugin.on(element.EventType.LABEL_CHANGE, change => { |
Andrew Bonventre | 5474de8 | 2016-06-27 16:34:12 -0400 | [diff] [blame] | 229 | assert.deepEqual(change, testChange); |
| 230 | assert.isTrue(errorStub.calledOnce); |
| 231 | done(); |
| 232 | }); |
| 233 | element.handleEvent(element.EventType.LABEL_CHANGE, {change: testChange}); |
| 234 | }); |
| 235 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 236 | test('submitchange', () => { |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 237 | plugin.on(element.EventType.SUBMIT_CHANGE, throwErrFn); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 238 | plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; }); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 239 | assert.isTrue(element.canSubmitChange()); |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 240 | assert.isTrue(errorStub.calledOnce); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 241 | plugin.on(element.EventType.SUBMIT_CHANGE, () => { return false; }); |
| 242 | plugin.on(element.EventType.SUBMIT_CHANGE, () => { return true; }); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 243 | assert.isFalse(element.canSubmitChange()); |
Andrew Bonventre | 033b931 | 2016-06-15 14:09:32 -0700 | [diff] [blame] | 244 | assert.isTrue(errorStub.calledTwice); |
Andrew Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 245 | }); |
| 246 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 247 | test('versioning', () => { |
| 248 | const callback = sandbox.spy(); |
Andrew Bonventre | e665f08 | 2016-06-27 16:12:12 -0400 | [diff] [blame] | 249 | Gerrit.install(callback, '0.0pre-alpha'); |
| 250 | assert(callback.notCalled); |
| 251 | }); |
| 252 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 253 | test('getAccount', done => { |
| 254 | Gerrit.getLoggedIn().then(loggedIn => { |
Andrew Bonventre | 5c38e9a | 2016-06-29 14:45:23 -0400 | [diff] [blame] | 255 | assert.isTrue(loggedIn); |
| 256 | done(); |
| 257 | }); |
| 258 | }); |
| 259 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 260 | test('_setPluginsCount', done => { |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 261 | stub('gr-reporting', { |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 262 | pluginsLoaded() { |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 263 | assert.equal(Gerrit._pluginsPending, 0); |
| 264 | done(); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 265 | }, |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 266 | }); |
| 267 | Gerrit._setPluginsCount(0); |
| 268 | }); |
| 269 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 270 | test('_arePluginsLoaded', () => { |
Wyatt Allen | 6470558 | 2016-12-08 14:19:51 -0800 | [diff] [blame] | 271 | assert.isTrue(Gerrit._arePluginsLoaded()); |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 272 | Gerrit._setPluginsCount(1); |
| 273 | assert.isFalse(Gerrit._arePluginsLoaded()); |
| 274 | Gerrit._setPluginsCount(0); |
| 275 | assert.isTrue(Gerrit._arePluginsLoaded()); |
| 276 | }); |
| 277 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 278 | test('_pluginInstalled', done => { |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 279 | stub('gr-reporting', { |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 280 | pluginsLoaded() { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 281 | assert.equal(Gerrit._pluginsPending, 0); |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 282 | done(); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 283 | }, |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 284 | }); |
| 285 | Gerrit._setPluginsCount(2); |
| 286 | Gerrit._pluginInstalled(); |
| 287 | assert.equal(Gerrit._pluginsPending, 1); |
| 288 | Gerrit._pluginInstalled(); |
| 289 | }); |
| 290 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 291 | test('install calls _pluginInstalled', () => { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 292 | sandbox.stub(Gerrit, '_pluginInstalled'); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 293 | Gerrit.install(p => { plugin = p; }, '0.1', |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 294 | 'http://test.com/plugins/testplugin/static/test.js'); |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 295 | assert.isTrue(Gerrit._pluginInstalled.calledOnce); |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 296 | }); |
| 297 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 298 | test('install calls _pluginInstalled on error', () => { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 299 | sandbox.stub(Gerrit, '_pluginInstalled'); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 300 | Gerrit.install(() => {}, '0.0pre-alpha'); |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 301 | assert.isTrue(Gerrit._pluginInstalled.calledOnce); |
| 302 | }); |
| 303 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 304 | test('installGwt calls _pluginInstalled', () => { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 305 | sandbox.stub(Gerrit, '_pluginInstalled'); |
| 306 | Gerrit.installGwt(); |
| 307 | assert.isTrue(Gerrit._pluginInstalled.calledOnce); |
| 308 | }); |
| 309 | |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 310 | test('installGwt returns a stub object', () => { |
| 311 | const plugin = Gerrit.installGwt(); |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 312 | sandbox.stub(console, 'warn'); |
| 313 | assert.isAbove(Object.keys(plugin).length, 0); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 314 | for (const name of Object.keys(plugin)) { |
Viktar Donich | 5ad9512 | 2016-12-15 14:06:34 -0800 | [diff] [blame] | 315 | console.warn.reset(); |
| 316 | plugin[name](); |
| 317 | assert.isTrue(console.warn.calledOnce); |
Kasper Nilsson | 75214af | 2017-05-15 16:39:47 -0700 | [diff] [blame] | 318 | } |
Viktar Donich | e8680ea | 2016-10-05 11:58:58 -0700 | [diff] [blame] | 319 | }); |
Paladox none | 9b25fea | 2017-07-29 14:43:11 +0000 | [diff] [blame] | 320 | |
Viktar Donich | e711257 | 2017-07-31 14:14:37 -0700 | [diff] [blame] | 321 | test('attributeHelper', () => { |
| 322 | assert.isOk(plugin.attributeHelper()); |
| 323 | }); |
| 324 | |
Paladox none | 9b25fea | 2017-07-29 14:43:11 +0000 | [diff] [blame] | 325 | 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 Bonventre | d2b9043 | 2016-03-29 14:12:33 -0400 | [diff] [blame] | 341 | }); |
| 342 | </script> |