Browse Source

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

tags/9.0.0.0b1
Jenkins 3 years ago
parent
commit
b51f0eef3a

+ 7
- 26
openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.js View File

@@ -21,36 +21,17 @@
21 21
     .controller('LaunchInstanceModalController', LaunchInstanceModalController);
22 22
 
23 23
   LaunchInstanceModalController.$inject = [
24
-    '$modal',
25
-    '$window',
26
-    'horizon.dashboard.project.workflow.launch-instance.modal-spec'
24
+    'horizon.dashboard.project.workflow.launch-instance.modal.service'
27 25
   ];
28 26
 
29
-  function LaunchInstanceModalController($modal, $window, modalSpec) {
27
+  function LaunchInstanceModalController(modalService) {
30 28
     var ctrl = this;
31 29
 
32
-    ctrl.openLaunchInstanceWizard = function (launchContext) {
33
-      var localSpec = {
34
-        resolve: {
35
-          launchContext: function () {
36
-            return launchContext;
37
-          }
38
-        }
39
-      };
40
-      angular.extend(localSpec, modalSpec);
41
-      var launchInstanceModal = $modal.open(localSpec);
42
-      var handleModalClose = function (redirectPropertyName) {
43
-        return function () {
44
-          if (launchContext && launchContext[redirectPropertyName]) {
45
-            $window.location.href = launchContext[redirectPropertyName];
46
-          }
47
-        };
48
-      };
49
-      launchInstanceModal.result.then(
50
-        handleModalClose('successUrl'),
51
-        handleModalClose('dismissUrl')
52
-      );
53
-    };
30
+    ctrl.openLaunchInstanceWizard = openLaunchInstanceWizard;
31
+
32
+    function openLaunchInstanceWizard(launchContext) {
33
+      modalService.open(launchContext);
34
+    }
54 35
   }
55 36
 
56 37
 })();

+ 16
- 54
openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.controller.spec.js View File

@@ -17,23 +17,19 @@
17 17
   'use strict';
18 18
 
19 19
   describe('LaunchInstanceModalController tests', function() {
20
-    var ctrl, modal, $window;
20
+    var ctrl, modalService;
21 21
 
22 22
     beforeEach(module('horizon.dashboard.project'));
23
+
23 24
     beforeEach(module(function($provide) {
24
-      modal = {
25
-        open: function() {
26
-          return {
27
-            result: {
28
-              then: angular.noop
29
-            }
30
-          };
31
-        }
25
+      modalService = {
26
+        open: function() { }
32 27
       };
33
-      $window = { location: { href: '/' } };
34
-      $provide.value('$modal', modal);
35
-      $provide.value('$modalSpec', {});
36
-      $provide.value('$window', $window);
28
+
29
+      $provide.value(
30
+        'horizon.dashboard.project.workflow.launch-instance.modal.service',
31
+        modalService
32
+      );
37 33
     }));
38 34
 
39 35
     beforeEach(inject(function($controller) {
@@ -56,55 +52,21 @@
56 52
         launchContext = {};
57 53
       });
58 54
 
59
-      it('calls modal.open', function() {
60
-        spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } });
55
+      it('calls modal.service.open', function() {
56
+        spyOn(modalService, 'open').and.callThrough();
61 57
         func(launchContext);
62
-        expect(modal.open).toHaveBeenCalled();
58
+        expect(modalService.open).toHaveBeenCalled();
63 59
       });
64 60
 
65
-      it('calls modal.open with expected values', function() {
66
-        spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } });
61
+      it('calls modalService.open with expected values', function() {
62
+        spyOn(modalService, 'open').and.callThrough();
67 63
         launchContext = { info: 'information' };
68 64
         func(launchContext);
69 65
 
70
-        var resolve = modal.open.calls.argsFor(0)[0].resolve;
71
-        expect(resolve).toBeDefined();
72
-        expect(resolve.launchContext).toBeDefined();
73
-        expect(resolve.launchContext()).toEqual({ info: 'information' });
66
+        var args = modalService.open.calls.argsFor(0)[0];
67
+        expect(args).toEqual(launchContext);
74 68
       });
75 69
 
76
-      it('sets up the correct success and failure paths', function() {
77
-        var successFunc, errFunc;
78
-
79
-        launchContext = { successUrl: '/good/path', dismissUrl: '/bad/path' };
80
-        spyOn(modal, 'open').and
81
-          .returnValue({
82
-            result: {
83
-              then: function(x, y) { successFunc = x; errFunc = y; }
84
-            }
85
-          });
86
-        func(launchContext);
87
-        successFunc('successUrl');
88
-        expect($window.location.href).toBe('/good/path');
89
-        errFunc('dismissUrl');
90
-        expect($window.location.href).toBe('/bad/path');
91
-      });
92
-
93
-      it("doesn't redirect if not configured to", function() {
94
-        var successFunc, errFunc;
95
-        launchContext = {};
96
-        spyOn(modal, 'open').and
97
-          .returnValue({
98
-            result: {
99
-              then: function(x, y) { successFunc = x; errFunc = y; }
100
-            }
101
-          });
102
-        func(launchContext);
103
-        successFunc('successUrl');
104
-        expect($window.location.href).toBe('/');
105
-        errFunc('dismissUrl');
106
-        expect($window.location.href).toBe('/');
107
-      });
108 70
     });
109 71
   });
110 72
 

+ 66
- 0
openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.js View File

@@ -0,0 +1,66 @@
1
+/*
2
+ *    (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the 'License');
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *    http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an 'AS IS' BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+(function () {
17
+  'use strict';
18
+
19
+  angular
20
+    .module('horizon.dashboard.project.workflow.launch-instance')
21
+    .factory(
22
+      'horizon.dashboard.project.workflow.launch-instance.modal.service',
23
+      LaunchInstanceModalService
24
+    );
25
+
26
+  LaunchInstanceModalService.$inject = [
27
+    '$modal',
28
+    '$window',
29
+    'horizon.dashboard.project.workflow.launch-instance.modal-spec'
30
+  ];
31
+
32
+  function LaunchInstanceModalService($modal, $window, modalSpec) {
33
+    var service = {
34
+      open: open
35
+    };
36
+
37
+    return service;
38
+
39
+    function open(launchContext) {
40
+      var localSpec = {
41
+        resolve: {
42
+          launchContext: function () {
43
+            return launchContext;
44
+          }
45
+        }
46
+      };
47
+
48
+      angular.extend(localSpec, modalSpec);
49
+
50
+      var launchInstanceModal = $modal.open(localSpec);
51
+      var handleModalClose = function (redirectPropertyName) {
52
+        return function () {
53
+          if (launchContext && launchContext[redirectPropertyName]) {
54
+            $window.location.href = launchContext[redirectPropertyName];
55
+          }
56
+        };
57
+      };
58
+
59
+      launchInstanceModal.result.then(
60
+        handleModalClose('successUrl'),
61
+        handleModalClose('dismissUrl')
62
+      );
63
+    }
64
+  }
65
+
66
+})();

+ 103
- 0
openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/launch-instance-modal.service.spec.js View File

@@ -0,0 +1,103 @@
1
+/*
2
+ *    (c) Copyright 2015 Hewlett-Packard Development Company, L.P.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the 'License');
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *    http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an 'AS IS' BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+(function () {
17
+  'use strict';
18
+
19
+  describe('LaunchInstanceModalController tests', function() {
20
+    var service, modal, $window;
21
+
22
+    beforeEach(module('horizon.dashboard.project'));
23
+    beforeEach(module(function($provide) {
24
+      modal = {
25
+        open: function() {
26
+          return {
27
+            result: {
28
+              then: angular.noop
29
+            }
30
+          };
31
+        }
32
+      };
33
+      $window = { location: { href: '/' } };
34
+      $provide.value('$modal', modal);
35
+      $provide.value('$modalSpec', {});
36
+      $provide.value('$window', $window);
37
+    }));
38
+
39
+    beforeEach(inject(function($injector) {
40
+      service = $injector.get('horizon.dashboard.project.workflow.launch-instance.modal.service');
41
+    }));
42
+
43
+    describe('open function tests', function() {
44
+      var func, launchContext;
45
+
46
+      beforeEach(function() {
47
+        func = service.open;
48
+        launchContext = {};
49
+      });
50
+
51
+      it('calls modal.open', function() {
52
+        spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } });
53
+        func(launchContext);
54
+        expect(modal.open).toHaveBeenCalled();
55
+      });
56
+
57
+      it('calls modal.open with expected values', function() {
58
+        spyOn(modal, 'open').and.returnValue({ result: { then: angular.noop } });
59
+        launchContext = { info: 'information' };
60
+        func(launchContext);
61
+
62
+        var resolve = modal.open.calls.argsFor(0)[0].resolve;
63
+        expect(resolve).toBeDefined();
64
+        expect(resolve.launchContext).toBeDefined();
65
+        expect(resolve.launchContext()).toEqual({ info: 'information' });
66
+      });
67
+
68
+      it('sets up the correct success and failure paths', function() {
69
+        var successFunc, errFunc;
70
+
71
+        launchContext = { successUrl: '/good/path', dismissUrl: '/bad/path' };
72
+        spyOn(modal, 'open').and
73
+          .returnValue({
74
+            result: {
75
+              then: function(x, y) { successFunc = x; errFunc = y; }
76
+            }
77
+          });
78
+        func(launchContext);
79
+        successFunc('successUrl');
80
+        expect($window.location.href).toBe('/good/path');
81
+        errFunc('dismissUrl');
82
+        expect($window.location.href).toBe('/bad/path');
83
+      });
84
+
85
+      it("doesn't redirect if not configured to", function() {
86
+        var successFunc, errFunc;
87
+        launchContext = {};
88
+        spyOn(modal, 'open').and
89
+          .returnValue({
90
+            result: {
91
+              then: function(x, y) { successFunc = x; errFunc = y; }
92
+            }
93
+          });
94
+        func(launchContext);
95
+        successFunc('successUrl');
96
+        expect($window.location.href).toBe('/');
97
+        errFunc('dismissUrl');
98
+        expect($window.location.href).toBe('/');
99
+      });
100
+    });
101
+  });
102
+
103
+})();

Loading…
Cancel
Save