Launch Instance Modal should be exist as a Service
Extracted openLaunchInstanceWizard from the LaunchInstanceModalController to a Service named horizon.dashboard.project.workflow.launch-instance.modal.service This service is required for the new Images panel to create the launch instance modal pop up. Partially-Implements: blueprint angularize-images-table Change-Id: Id7177cbafbfb9094d03908b5f8eba52d00241817
This commit is contained in:
parent
a48491b3e2
commit
222bd716fa
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
@ -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('/');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
})();
|
|
@ -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('/');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
})();
|
Loading…
Reference in New Issue