From 60a0816cc0b4d7c43dfb990cf0aeffe9177dd91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Queiruga?= Date: Wed, 22 Jan 2020 18:47:15 +0100 Subject: [PATCH] Fix: broken interface of the jenkins object yielded by the setupWizard - Callbacks registered with `onSetupWizardInitialized` are yielded a `wizard` parameter, that contained a reference to the jenkins object. - Some plugins called the method `jenkins.initHandlebars` from this callback, to receive a Handlebars intance. - Added a unit test to ensure this regression does not happen again --- war/src/main/js/util/jenkins.js | 12 ++++++-- war/src/test/js/pluginSetupWizard.spec.js | 36 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/war/src/main/js/util/jenkins.js b/war/src/main/js/util/jenkins.js index a16bd8df3c..b5aa8d0e76 100644 --- a/war/src/main/js/util/jenkins.js +++ b/war/src/main/js/util/jenkins.js @@ -1,8 +1,9 @@ /** * Jenkins JS Modules common utility functions */ -import $ from 'jquery' -import wh from 'window-handle' +import $ from 'jquery'; +import wh from 'window-handle'; +import Handlebars from 'handlebars'; var debug = false; var jenkins = {}; @@ -130,6 +131,13 @@ jenkins.post = function(url, data, success, options) { $.ajax(args); }; +/** + * handlebars setup, done for backwards compatibility because some plugins depend on it + */ +jenkins.initHandlebars = function() { + return Handlebars; +} + /** * Load translations for the given bundle ID, provide the message object to the handler. * Optional error handler as the last argument. diff --git a/war/src/test/js/pluginSetupWizard.spec.js b/war/src/test/js/pluginSetupWizard.spec.js index d1a707641d..6725c2333c 100644 --- a/war/src/test/js/pluginSetupWizard.spec.js +++ b/war/src/test/js/pluginSetupWizard.spec.js @@ -505,4 +505,40 @@ describe("pluginSetupWizard.js", function () { goButton.click(); }, { ajaxMappings, $, $body }); }); + + describe('running extension callbacks', function () { + beforeEach(() => { + global.setupWizardExtensions = []; + global.onSetupWizardInitialized = function(extension) { + setupWizardExtensions.push(extension); + }; + + }); + + afterEach(() => { + delete global.setupWizardExtensions; + delete global.onSetupWizardInitialized; + }); + + it ('yields a jenkins object with the initHandlebars method', function(done) { + jsTest.onPage(function() { + + $.ajax = ajaxMocks(); + + onSetupWizardInitialized(wizard => { + const { jenkins } = wizard; + expect(jenkins).toBe(getJenkins()); + + // Test that the initHandlebars method returns a Handlebars instance + const handlebars = jenkins.initHandlebars(); + expect(handlebars.registerHelper).toBeInstanceOf(Function) + + done(); + }) + + const pluginSetupWizard = getSetupWizardGui(); + pluginSetupWizard.init($body); + }); + }); + }); }); -- GitLab