Merge "Launch Instance Modal should be exist as a Service"

This commit is contained in:
Jenkins 2015-12-02 22:56:38 +00:00 committed by Gerrit Code Review
commit b51f0eef3a
4 changed files with 192 additions and 80 deletions

View File

@ -21,36 +21,17 @@
.controller('LaunchInstanceModalController', LaunchInstanceModalController); .controller('LaunchInstanceModalController', LaunchInstanceModalController);
LaunchInstanceModalController.$inject = [ LaunchInstanceModalController.$inject = [
'$modal', 'horizon.dashboard.project.workflow.launch-instance.modal.service'
'$window',
'horizon.dashboard.project.workflow.launch-instance.modal-spec'
]; ];
function LaunchInstanceModalController($modal, $window, modalSpec) { function LaunchInstanceModalController(modalService) {
var ctrl = this; var ctrl = this;
ctrl.openLaunchInstanceWizard = function (launchContext) { ctrl.openLaunchInstanceWizard = openLaunchInstanceWizard;
var localSpec = {
resolve: { function openLaunchInstanceWizard(launchContext) {
launchContext: function () { modalService.open(launchContext);
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')
);
};
} }
})(); })();

View File

@ -17,23 +17,19 @@
'use strict'; 'use strict';
describe('LaunchInstanceModalController tests', function() { describe('LaunchInstanceModalController tests', function() {
var ctrl, modal, $window; var ctrl, modalService;
beforeEach(module('horizon.dashboard.project')); beforeEach(module('horizon.dashboard.project'));
beforeEach(module(function($provide) { beforeEach(module(function($provide) {
modal = { modalService = {
open: function() { open: function() { }
return {
result: {
then: angular.noop
}
};
}
}; };
$window = { location: { href: '/' } };
$provide.value('$modal', modal); $provide.value(
$provide.value('$modalSpec', {}); 'horizon.dashboard.project.workflow.launch-instance.modal.service',
$provide.value('$window', $window); modalService
);
})); }));
beforeEach(inject(function($controller) { beforeEach(inject(function($controller) {
@ -56,55 +52,21 @@
launchContext = {}; launchContext = {};
}); });
it('calls modal.open', function() { it('calls modal.service.open', function() {
spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); spyOn(modalService, 'open').and.callThrough();
func(launchContext); func(launchContext);
expect(modal.open).toHaveBeenCalled(); expect(modalService.open).toHaveBeenCalled();
}); });
it('calls modal.open with expected values', function() { it('calls modalService.open with expected values', function() {
spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } }); spyOn(modalService, 'open').and.callThrough();
launchContext = { info: 'information' }; launchContext = { info: 'information' };
func(launchContext); func(launchContext);
var resolve = modal.open.calls.argsFor(0)[0].resolve; var args = modalService.open.calls.argsFor(0)[0];
expect(resolve).toBeDefined(); expect(args).toEqual(launchContext);
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('/');
});
}); });
}); });

View File

@ -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')
);
}
}
})();

View File

@ -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('/');
});
});
});
})();