diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.js index 801a5afae0..bc2edf9690 100644 --- a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.js +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.js @@ -21,36 +21,17 @@ .controller('LaunchInstanceModalController', LaunchInstanceModalController); LaunchInstanceModalController.$inject = [ - '$modal', - '$window', - 'horizon.dashboard.project.workflow.launch-instance.modal-spec' + 'horizon.dashboard.project.workflow.launch-instance.modal.service' ]; - function LaunchInstanceModalController($modal, $window, modalSpec) { + function LaunchInstanceModalController(modalService) { var ctrl = this; - ctrl.openLaunchInstanceWizard = function (launchContext) { - var localSpec = { - resolve: { - launchContext: function () { - return launchContext; - } - } - }; - angular.extend(localSpec, modalSpec); - var launchInstanceModal = $modal.open(localSpec); - var handleModalClose = function (redirectPropertyName) { - return function () { - if (launchContext && launchContext[redirectPropertyName]) { - $window.location.href = launchContext[redirectPropertyName]; - } - }; - }; - launchInstanceModal.result.then( - handleModalClose('successUrl'), - handleModalClose('dismissUrl') - ); - }; + ctrl.openLaunchInstanceWizard = openLaunchInstanceWizard; + + function openLaunchInstanceWizard(launchContext) { + modalService.open(launchContext); + } } })(); diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.spec.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.spec.js index 5d7fa2bf77..3cdd94ff18 100644 --- a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.spec.js +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.spec.js @@ -17,23 +17,19 @@ 'use strict'; describe('LaunchInstanceModalController tests', function() { - var ctrl, modal, $window; + var ctrl, modalService; beforeEach(module('horizon.dashboard.project')); + beforeEach(module(function($provide) { - modal = { - open: function() { - return { - result: { - then: angular.noop - } - }; - } + modalService = { + open: function() { } }; - $window = { location: { href: '/' } }; - $provide.value('$modal', modal); - $provide.value('$modalSpec', {}); - $provide.value('$window', $window); + + $provide.value( + 'horizon.dashboard.project.workflow.launch-instance.modal.service', + modalService + ); })); beforeEach(inject(function($controller) { @@ -56,55 +52,21 @@ launchContext = {}; }); - it('calls modal.open', function() { - spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); + it('calls modal.service.open', function() { + spyOn(modalService, 'open').and.callThrough(); func(launchContext); - expect(modal.open).toHaveBeenCalled(); + expect(modalService.open).toHaveBeenCalled(); }); - it('calls modal.open with expected values', function() { - spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); + it('calls modalService.open with expected values', function() { + spyOn(modalService, 'open').and.callThrough(); launchContext = { info: 'information' }; func(launchContext); - var resolve = modal.open.calls.argsFor(0)[0].resolve; - expect(resolve).toBeDefined(); - expect(resolve.launchContext).toBeDefined(); - expect(resolve.launchContext()).toEqual({ info: 'information' }); + var args = modalService.open.calls.argsFor(0)[0]; + expect(args).toEqual(launchContext); }); - it('sets up the correct success and failure paths', function() { - var successFunc, errFunc; - - launchContext = { successUrl: '/good/path', dismissUrl: '/bad/path' }; - spyOn(modal, 'open').and - .returnValue({ - result: { - then: function(x, y) { successFunc = x; errFunc = y; } - } - }); - func(launchContext); - successFunc('successUrl'); - expect($window.location.href).toBe('/good/path'); - errFunc('dismissUrl'); - expect($window.location.href).toBe('/bad/path'); - }); - - it("doesn't redirect if not configured to", function() { - var successFunc, errFunc; - launchContext = {}; - spyOn(modal, 'open').and - .returnValue({ - result: { - then: function(x, y) { successFunc = x; errFunc = y; } - } - }); - func(launchContext); - successFunc('successUrl'); - expect($window.location.href).toBe('/'); - errFunc('dismissUrl'); - expect($window.location.href).toBe('/'); - }); }); }); diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.js new file mode 100644 index 0000000000..8bc497945f --- /dev/null +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.js @@ -0,0 +1,66 @@ +/* + * (c) Copyright 2015 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + 'use strict'; + + angular + .module('horizon.dashboard.project.workflow.launch-instance') + .factory( + 'horizon.dashboard.project.workflow.launch-instance.modal.service', + LaunchInstanceModalService + ); + + LaunchInstanceModalService.$inject = [ + '$modal', + '$window', + 'horizon.dashboard.project.workflow.launch-instance.modal-spec' + ]; + + function LaunchInstanceModalService($modal, $window, modalSpec) { + var service = { + open: open + }; + + return service; + + function open(launchContext) { + var localSpec = { + resolve: { + launchContext: function () { + return launchContext; + } + } + }; + + angular.extend(localSpec, modalSpec); + + var launchInstanceModal = $modal.open(localSpec); + var handleModalClose = function (redirectPropertyName) { + return function () { + if (launchContext && launchContext[redirectPropertyName]) { + $window.location.href = launchContext[redirectPropertyName]; + } + }; + }; + + launchInstanceModal.result.then( + handleModalClose('successUrl'), + handleModalClose('dismissUrl') + ); + } + } + +})(); diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.spec.js b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.spec.js new file mode 100644 index 0000000000..2cdb639ebf --- /dev/null +++ b/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.spec.js @@ -0,0 +1,103 @@ +/* + * (c) Copyright 2015 Hewlett-Packard Development Company, L.P. + * + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + 'use strict'; + + describe('LaunchInstanceModalController tests', function() { + var service, modal, $window; + + beforeEach(module('horizon.dashboard.project')); + beforeEach(module(function($provide) { + modal = { + open: function() { + return { + result: { + then: angular.noop + } + }; + } + }; + $window = { location: { href: '/' } }; + $provide.value('$modal', modal); + $provide.value('$modalSpec', {}); + $provide.value('$window', $window); + })); + + beforeEach(inject(function($injector) { + service = $injector.get('horizon.dashboard.project.workflow.launch-instance.modal.service'); + })); + + describe('open function tests', function() { + var func, launchContext; + + beforeEach(function() { + func = service.open; + launchContext = {}; + }); + + it('calls modal.open', function() { + spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); + func(launchContext); + expect(modal.open).toHaveBeenCalled(); + }); + + it('calls modal.open with expected values', function() { + spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); + launchContext = { info: 'information' }; + func(launchContext); + + var resolve = modal.open.calls.argsFor(0)[0].resolve; + expect(resolve).toBeDefined(); + expect(resolve.launchContext).toBeDefined(); + expect(resolve.launchContext()).toEqual({ info: 'information' }); + }); + + it('sets up the correct success and failure paths', function() { + var successFunc, errFunc; + + launchContext = { successUrl: '/good/path', dismissUrl: '/bad/path' }; + spyOn(modal, 'open').and + .returnValue({ + result: { + then: function(x, y) { successFunc = x; errFunc = y; } + } + }); + func(launchContext); + successFunc('successUrl'); + expect($window.location.href).toBe('/good/path'); + errFunc('dismissUrl'); + expect($window.location.href).toBe('/bad/path'); + }); + + it("doesn't redirect if not configured to", function() { + var successFunc, errFunc; + launchContext = {}; + spyOn(modal, 'open').and + .returnValue({ + result: { + then: function(x, y) { successFunc = x; errFunc = y; } + } + }); + func(launchContext); + successFunc('successUrl'); + expect($window.location.href).toBe('/'); + errFunc('dismissUrl'); + expect($window.location.href).toBe('/'); + }); + }); + }); + +})();