diff options
Diffstat (limited to 'polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html')
-rw-r--r-- | polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html | 235 |
1 files changed, 205 insertions, 30 deletions
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html index 62fc1db653..e0c7c37f04 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html @@ -1,5 +1,6 @@ <!DOCTYPE html> <!-- +@license Copyright (C) 2016 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); @@ -59,9 +60,9 @@ limitations under the License. }); element = fixture('basic'); errorStub = sandbox.stub(console, 'error'); - Gerrit._setPluginsCount(1); Gerrit.install(p => { plugin = p; }, '0.1', 'http://test.com/plugins/testplugin/static/test.js'); + Gerrit._setPluginsPending([]); }); teardown(() => { @@ -77,6 +78,16 @@ limitations under the License. assert.strictEqual(plugin, otherPlugin); }); + test('flushes preinstalls if provided', () => { + assert.doesNotThrow(() => { + Gerrit._flushPreinstalls(); + }); + window.Gerrit.flushPreinstalls = sandbox.stub(); + Gerrit._flushPreinstalls(); + assert.isTrue(window.Gerrit.flushPreinstalls.calledOnce); + delete window.Gerrit.flushPreinstalls; + }); + test('url', () => { assert.equal(plugin.url(), 'http://test.com/plugins/testplugin/'); assert.equal(plugin.url('/static/test.js'), @@ -179,15 +190,17 @@ limitations under the License. _number: 42, revisions: {def: {_number: 2}, abc: {_number: 1}}, }; + const expectedChange = Object.assign({mergeable: false}, testChange); plugin.on(element.EventType.SHOW_CHANGE, throwErrFn); - plugin.on(element.EventType.SHOW_CHANGE, (change, revision) => { - assert.deepEqual(change, testChange); + plugin.on(element.EventType.SHOW_CHANGE, (change, revision, info) => { + assert.deepEqual(change, expectedChange); assert.deepEqual(revision, testChange.revisions.abc); + assert.deepEqual(info, {mergeable: false}); assert.isTrue(errorStub.calledOnce); done(); }); element.handleEvent(element.EventType.SHOW_CHANGE, - {change: testChange, patchNum: 1}); + {change: testChange, patchNum: 1, info: {mergeable: false}}); }); test('handleEvent awaits plugins load', done => { @@ -303,7 +316,6 @@ limitations under the License. test('_setPluginsCount', done => { stub('gr-reporting', { pluginsLoaded() { - assert.equal(Gerrit._pluginsPending, 0); done(); }, }); @@ -318,17 +330,19 @@ limitations under the License. assert.isTrue(Gerrit._arePluginsLoaded()); }); - test('_pluginInstalled', done => { + test('_pluginInstalled', () => { + const pluginsLoadedStub = sandbox.stub(); stub('gr-reporting', { - pluginsLoaded() { - assert.equal(Gerrit._pluginsPending, 0); - done(); - }, + pluginsLoaded: (...args) => pluginsLoadedStub(...args), }); - Gerrit._setPluginsCount(2); - Gerrit._pluginInstalled(); - assert.equal(Gerrit._pluginsPending, 1); - Gerrit._pluginInstalled(); + const plugins = [ + 'http://test.com/plugins/foo/static/test.js', + 'http://test.com/plugins/bar/static/test.js', + ]; + Gerrit._setPluginsPending(plugins); + Gerrit._pluginInstalled(plugins[0]); + Gerrit._pluginInstalled(plugins[1]); + assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar'])); }); test('install calls _pluginInstalled', () => { @@ -345,27 +359,42 @@ limitations under the License. assert.isTrue(Gerrit._pluginInstalled.calledOnce); }); - test('install calls _pluginInstalled on error', () => { - sandbox.stub(Gerrit, '_pluginInstalled'); + test('plugin install errors mark plugins as loaded', () => { + Gerrit._setPluginsCount(1); Gerrit.install(() => {}, '0.0pre-alpha'); - assert.isTrue(Gerrit._pluginInstalled.calledOnce); + return Gerrit.awaitPluginsLoaded(); + }); + + test('multiple ui plugins per java plugin', () => { + const file1 = 'http://test.com/plugins/qaz/static/foo.nocache.js'; + const file2 = 'http://test.com/plugins/qaz/static/bar.js'; + Gerrit._setPluginsPending([file1, file2]); + Gerrit.install(() => {}, '0.1', file1); + Gerrit.install(() => {}, '0.1', file2); + return Gerrit.awaitPluginsLoaded(); + }); + + test('plugin install errors shows toasts', () => { + const alertStub = sandbox.stub(); + document.addEventListener('show-alert', alertStub); + Gerrit._setPluginsCount(1); + Gerrit.install(() => {}, '0.0pre-alpha'); + return Gerrit.awaitPluginsLoaded().then(() => { + assert.isTrue(alertStub.calledOnce); + }); }); test('installGwt calls _pluginInstalled', () => { sandbox.stub(Gerrit, '_pluginInstalled'); - Gerrit.installGwt(); + Gerrit.installGwt('http://test.com/plugins/testplugin/static/test.js'); assert.isTrue(Gerrit._pluginInstalled.calledOnce); }); - test('installGwt returns a stub object', () => { - const plugin = Gerrit.installGwt(); - sandbox.stub(console, 'warn'); - assert.isAbove(Object.keys(plugin).length, 0); - for (const name of Object.keys(plugin)) { - console.warn.reset(); - plugin[name](); - assert.isTrue(console.warn.calledOnce); - } + test('installGwt returns a plugin', () => { + const plugin = Gerrit.installGwt( + 'http://test.com/plugins/testplugin/static/test.js'); + assert.isOk(plugin); + assert.isOk(plugin._loadedGwt); }); test('attributeHelper', () => { @@ -379,6 +408,47 @@ limitations under the License. assert.notStrictEqual(plugin.install, plugin.deprecated.install); }); + test('getAdminMenuLinks', () => { + const links = [{text: 'a', url: 'b'}, {text: 'c', url: 'd'}]; + const getCallbacksStub = sandbox.stub(element, '_getEventCallbacks') + .returns([ + {getMenuLinks: () => [links[0]]}, + {getMenuLinks: () => [links[1]]}, + ]); + const result = element.getAdminMenuLinks(); + assert.deepEqual(result, links); + assert.isTrue(getCallbacksStub.calledOnce); + assert.equal(getCallbacksStub.lastCall.args[0], + element.EventType.ADMIN_MENU_LINKS); + }); + + test('Gerrit._isPluginPreloaded', () => { + Gerrit._preloadedPlugins = {baz: ()=>{}}; + assert.isFalse(Gerrit._isPluginPreloaded('plugins/foo/bar')); + assert.isFalse(Gerrit._isPluginPreloaded('http://a.com/42')); + assert.isTrue(Gerrit._isPluginPreloaded('preloaded:baz')); + Gerrit._preloadedPlugins = null; + }); + + test('preloaded plugins are installed', () => { + const installStub = sandbox.stub(); + Gerrit._preloadedPlugins = {foo: installStub}; + Gerrit._installPreloadedPlugins(); + assert.isTrue(installStub.called); + const pluginApi = installStub.lastCall.args[0]; + assert.strictEqual(pluginApi.getPluginName(), 'foo'); + }); + + test('installing preloaded plugin', () => { + let plugin; + window.ASSETS_PATH = 'http://blips.com/chitz/'; + Gerrit.install(p => { plugin = p; }, '0.1', 'preloaded:foo'); + assert.strictEqual(plugin.getPluginName(), 'foo'); + assert.strictEqual(plugin.url('/some/thing.html'), + 'http://blips.com/plugins/foo/some/thing.html'); + delete window.ASSETS_PATH; + }); + suite('test plugin with base url', () => { setup(() => { sandbox.stub(Gerrit.BaseUrlBehavior, 'getBaseUrl').returns('/r'); @@ -398,9 +468,8 @@ limitations under the License. suite('popup', () => { test('popup(element) is deprecated', () => { - assert.throws(() => { - plugin.popup(document.createElement('div')); - }); + plugin.popup(document.createElement('div')); + assert.isTrue(console.error.calledOnce); }); test('popup(moduleName) creates popup with component', () => { @@ -463,5 +532,111 @@ limitations under the License. assert.isFalse(stub.called); }); }); + + suite('screen', () => { + test('screenUrl()', () => { + sandbox.stub(Gerrit.BaseUrlBehavior, 'getBaseUrl').returns('/base'); + assert.equal(plugin.screenUrl(), 'http://test.com/base/x/testplugin'); + assert.equal( + plugin.screenUrl('foo'), 'http://test.com/base/x/testplugin/foo'); + }); + + test('deprecated works', () => { + const stub = sandbox.stub(); + const hookStub = {onAttached: sandbox.stub()}; + sandbox.stub(plugin, 'hook').returns(hookStub); + plugin.deprecated.screen('foo', stub); + assert.isTrue(plugin.hook.calledWith('testplugin-screen-foo')); + const fakeEl = {style: {display: ''}}; + hookStub.onAttached.callArgWith(0, fakeEl); + assert.isTrue(stub.called); + assert.equal(fakeEl.style.display, 'none'); + }); + + test('works', () => { + sandbox.stub(plugin, 'registerCustomComponent'); + plugin.screen('foo', 'some-module'); + assert.isTrue(plugin.registerCustomComponent.calledWith( + 'testplugin-screen-foo', 'some-module')); + }); + }); + + suite('panel', () => { + let fakeEl; + let emulateAttached; + + setup(()=> { + fakeEl = {change: {}, revision: {}}; + const hookStub = {onAttached: sandbox.stub()}; + sandbox.stub(plugin, 'hook').returns(hookStub); + emulateAttached = () => hookStub.onAttached.callArgWith(0, fakeEl); + }); + + test('plugin.panel is deprecated', () => { + plugin.panel('rubbish'); + assert.isTrue(console.error.called); + }); + + [ + ['CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK', 'change-view-integration'], + ['CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK', 'change-metadata-item'], + ].forEach(([panelName, endpointName]) => { + test(`deprecated.panel works for ${panelName}`, () => { + const callback = sandbox.stub(); + plugin.deprecated.panel(panelName, callback); + assert.isTrue(plugin.hook.calledWith(endpointName)); + emulateAttached(); + assert.isTrue(callback.called); + const args = callback.args[0][0]; + assert.strictEqual(args.body, fakeEl); + assert.strictEqual(args.p.CHANGE_INFO, fakeEl.change); + assert.strictEqual(args.p.REVISION_INFO, fakeEl.revision); + }); + }); + }); + + suite('settingsScreen', () => { + test('plugin.settingsScreen is deprecated', () => { + plugin.settingsScreen('rubbish'); + assert.isTrue(console.error.called); + }); + + test('plugin.settings() returns GrSettingsApi', () => { + assert.isOk(plugin.settings()); + assert.isTrue(plugin.settings() instanceof GrSettingsApi); + }); + + test('plugin.deprecated.settingsScreen() works', () => { + const hookStub = {onAttached: sandbox.stub()}; + sandbox.stub(plugin, 'hook').returns(hookStub); + const fakeSettings = {}; + fakeSettings.title = sandbox.stub().returns(fakeSettings); + fakeSettings.token = sandbox.stub().returns(fakeSettings); + fakeSettings.module = sandbox.stub().returns(fakeSettings); + fakeSettings.build = sandbox.stub().returns(hookStub); + sandbox.stub(plugin, 'settings').returns(fakeSettings); + const callback = sandbox.stub(); + + plugin.deprecated.settingsScreen('path', 'menu', callback); + assert.isTrue(fakeSettings.title.calledWith('menu')); + assert.isTrue(fakeSettings.token.calledWith('path')); + assert.isTrue(fakeSettings.module.calledWith('div')); + assert.equal(fakeSettings.build.callCount, 1); + + const fakeBody = {}; + const fakeEl = { + style: { + display: '', + }, + querySelector: sandbox.stub().returns(fakeBody), + }; + // Emulate settings screen attached + hookStub.onAttached.callArgWith(0, fakeEl); + assert.isTrue(callback.called); + const args = callback.args[0][0]; + assert.strictEqual(args.body, fakeBody); + assert.equal(fakeEl.style.display, 'none'); + }); + }); }); </script> |