step2_controller_test.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. App = require('app');
  19. require('controllers/main/service/widgets/create/step2_controller');
  20. describe('App.WidgetWizardStep2Controller', function () {
  21. var controller = App.WidgetWizardStep2Controller.create({
  22. content: Em.Object.create()
  23. });
  24. describe("#isEditWidget", function() {
  25. it("empty name", function() {
  26. controller.set('content.controllerName', '');
  27. controller.propertyDidChange('isEditWidget');
  28. expect(controller.get('isEditWidget')).to.be.false;
  29. });
  30. it("correct name", function() {
  31. controller.set('content.controllerName', 'widgetEditController');
  32. controller.propertyDidChange('isEditWidget');
  33. expect(controller.get('isEditWidget')).to.be.true;
  34. });
  35. });
  36. describe("#filteredMetrics", function() {
  37. var testCases = [
  38. {
  39. metric: {
  40. point_in_time: false
  41. },
  42. type: null,
  43. result: []
  44. },
  45. {
  46. metric: {
  47. point_in_time: true
  48. },
  49. type: null,
  50. result: [
  51. {
  52. point_in_time: true
  53. }
  54. ]
  55. },
  56. {
  57. metric: {
  58. temporal: false
  59. },
  60. type: 'GRAPH',
  61. result: []
  62. },
  63. {
  64. metric: {
  65. temporal: true
  66. },
  67. type: 'GRAPH',
  68. result: [
  69. {
  70. temporal: true
  71. }
  72. ]
  73. }
  74. ];
  75. testCases.forEach(function (test) {
  76. it("type=" + test.type + "; temporal=" + test.metric.temporal + "; point_in_time=" + test.metric.point_in_time, function () {
  77. controller.get('content').setProperties({
  78. widgetType: test.type,
  79. allMetrics: [test.metric]
  80. });
  81. controller.propertyDidChange('filteredMetrics');
  82. expect(controller.get('filteredMetrics')).to.eql(test.result);
  83. });
  84. });
  85. });
  86. describe("#isSubmitDisabled", function () {
  87. beforeEach(function () {
  88. this.expressionFunc = sinon.stub(controller, 'isExpressionComplete');
  89. this.graphFunc = sinon.stub(controller, 'isGraphDataComplete');
  90. this.templateFunc = sinon.stub(controller, 'isTemplateDataComplete');
  91. controller.set('expressions', ['']);
  92. });
  93. afterEach(function () {
  94. this.expressionFunc.restore();
  95. this.graphFunc.restore();
  96. this.templateFunc.restore();
  97. controller.get('expressions').clear();
  98. });
  99. it("invalid property", function () {
  100. controller.set('widgetPropertiesViews', [Em.Object.create({isValid: false})]);
  101. controller.propertyDidChange('isSubmitDisabled');
  102. expect(controller.get('isSubmitDisabled')).to.be.true;
  103. });
  104. it("valid number widget", function () {
  105. controller.set('widgetPropertiesViews', []);
  106. controller.set('content.widgetType', 'NUMBER');
  107. this.expressionFunc.returns(true);
  108. controller.propertyDidChange('isSubmitDisabled');
  109. expect(controller.get('isSubmitDisabled')).to.be.false;
  110. });
  111. it("invalid number widget", function () {
  112. controller.set('widgetPropertiesViews', []);
  113. controller.set('content.widgetType', 'NUMBER');
  114. this.expressionFunc.returns(false);
  115. controller.propertyDidChange('isSubmitDisabled');
  116. expect(controller.get('isSubmitDisabled')).to.be.true;
  117. });
  118. it("valid graph widget", function () {
  119. controller.set('widgetPropertiesViews', []);
  120. controller.set('content.widgetType', 'GRAPH');
  121. this.graphFunc.returns(true);
  122. controller.propertyDidChange('isSubmitDisabled');
  123. expect(controller.get('isSubmitDisabled')).to.be.false;
  124. });
  125. it("invalid graph widget", function () {
  126. controller.set('widgetPropertiesViews', []);
  127. controller.set('content.widgetType', 'GRAPH');
  128. this.graphFunc.returns(false);
  129. controller.propertyDidChange('isSubmitDisabled');
  130. expect(controller.get('isSubmitDisabled')).to.be.true;
  131. });
  132. it("valid template widget", function () {
  133. controller.set('widgetPropertiesViews', []);
  134. controller.set('content.widgetType', 'TEMPLATE');
  135. this.templateFunc.returns(true);
  136. controller.propertyDidChange('isSubmitDisabled');
  137. expect(controller.get('isSubmitDisabled')).to.be.false;
  138. });
  139. it("invalid template widget", function () {
  140. controller.set('widgetPropertiesViews', []);
  141. controller.set('content.widgetType', 'TEMPLATE');
  142. this.templateFunc.returns(false);
  143. controller.propertyDidChange('isSubmitDisabled');
  144. expect(controller.get('isSubmitDisabled')).to.be.true;
  145. });
  146. it("unknown widget type", function () {
  147. controller.set('widgetPropertiesViews', []);
  148. controller.set('content.widgetType', '');
  149. controller.propertyDidChange('isSubmitDisabled');
  150. expect(controller.get('isSubmitDisabled')).to.be.false;
  151. });
  152. });
  153. describe("#isExpressionComplete()", function() {
  154. var testCases = [
  155. {
  156. expression: null,
  157. result: false
  158. },
  159. {
  160. expression: Em.Object.create({isInvalid: true}),
  161. result: false
  162. },
  163. {
  164. expression: Em.Object.create({isInvalid: false, isEmpty: false}),
  165. result: true
  166. },
  167. {
  168. expression: Em.Object.create({isInvalid: false, isEmpty: true}),
  169. result: false
  170. }
  171. ];
  172. testCases.forEach(function (test) {
  173. it("expression = " + test.expression, function () {
  174. expect(controller.isExpressionComplete(test.expression)).to.equal(test.result);
  175. });
  176. });
  177. });
  178. describe("#isGraphDataComplete()", function() {
  179. beforeEach(function () {
  180. this.mock = sinon.stub(controller, 'isExpressionComplete');
  181. });
  182. afterEach(function () {
  183. this.mock.restore();
  184. });
  185. it("dataSets is empty", function() {
  186. expect(controller.isGraphDataComplete([])).to.be.false;
  187. });
  188. it("label is empty", function() {
  189. expect(controller.isGraphDataComplete([Em.Object.create({label: ''})])).to.be.false;
  190. });
  191. it("expression is not complete", function() {
  192. this.mock.returns(false);
  193. expect(controller.isGraphDataComplete([Em.Object.create({label: 'abc'})])).to.be.false;
  194. });
  195. it("expression is complete", function() {
  196. this.mock.returns(true);
  197. expect(controller.isGraphDataComplete([Em.Object.create({label: 'abc'})])).to.be.true;
  198. });
  199. });
  200. describe("#isTemplateDataComplete()", function() {
  201. beforeEach(function () {
  202. this.mock = sinon.stub(controller, 'isExpressionComplete');
  203. });
  204. afterEach(function () {
  205. this.mock.restore();
  206. });
  207. it("expressions is empty", function() {
  208. expect(controller.isTemplateDataComplete([])).to.be.false;
  209. });
  210. it("templateValue is empty", function() {
  211. expect(controller.isTemplateDataComplete([{}], '')).to.be.false;
  212. });
  213. it("expression is not complete", function() {
  214. this.mock.returns(false);
  215. expect(controller.isTemplateDataComplete([{}], 'abc')).to.be.false;
  216. });
  217. it("expression is complete", function() {
  218. this.mock.returns(true);
  219. expect(controller.isTemplateDataComplete([{}], 'abc')).to.be.true;
  220. });
  221. });
  222. describe("#addDataSet()", function() {
  223. it("", function() {
  224. controller.get('dataSets').clear();
  225. controller.addDataSet(null, true);
  226. expect(controller.get('dataSets').objectAt(0).get('id')).to.equal(1);
  227. expect(controller.get('dataSets').objectAt(0).get('isRemovable')).to.equal(false);
  228. controller.addDataSet(null);
  229. expect(controller.get('dataSets').objectAt(1).get('id')).to.equal(2);
  230. expect(controller.get('dataSets').objectAt(1).get('isRemovable')).to.equal(true);
  231. controller.get('dataSets').clear();
  232. });
  233. });
  234. describe("#removeDataSet()", function () {
  235. it("", function () {
  236. var dataSet = Em.Object.create();
  237. controller.get('dataSets').pushObject(dataSet);
  238. controller.removeDataSet({context: dataSet});
  239. expect(controller.get('dataSets')).to.be.empty;
  240. });
  241. });
  242. describe("#addExpression()", function() {
  243. it("", function() {
  244. controller.get('expressions').clear();
  245. controller.addExpression(null, true);
  246. expect(controller.get('expressions').objectAt(0).get('id')).to.equal(1);
  247. expect(controller.get('expressions').objectAt(0).get('isRemovable')).to.equal(false);
  248. controller.addExpression(null);
  249. expect(controller.get('expressions').objectAt(1).get('id')).to.equal(2);
  250. expect(controller.get('expressions').objectAt(1).get('isRemovable')).to.equal(true);
  251. controller.get('expressions').clear();
  252. });
  253. });
  254. describe("#removeExpression()", function () {
  255. it("", function () {
  256. var expression = Em.Object.create();
  257. controller.get('expressions').pushObject(expression);
  258. controller.removeExpression({context: expression});
  259. expect(controller.get('expressions')).to.be.empty;
  260. });
  261. });
  262. });