widget_property_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. var App = require('app');
  19. require('models/widget_property');
  20. describe('App.WidgetProperty', function () {
  21. var widgetProperty,
  22. unit = App.WidgetPropertyTypes.findProperty('name', 'display_unit'),
  23. threshold = App.WidgetPropertyTypes.findProperty('name', 'threshold'),
  24. validate = function (value) {
  25. return !isNaN(value);
  26. };
  27. beforeEach(function () {
  28. widgetProperty = App.WidgetProperty.create();
  29. });
  30. describe('#viewClass', function () {
  31. var cases = [
  32. {
  33. displayType: 'textField',
  34. viewClass: App.WidgetPropertyTextFieldView
  35. },
  36. {
  37. displayType: 'threshold',
  38. viewClass: App.WidgetPropertyThresholdView
  39. },
  40. {
  41. displayType: 'select',
  42. viewClass: App.WidgetPropertySelectView
  43. },
  44. {
  45. displayType: 'none',
  46. viewClass: undefined
  47. }
  48. ];
  49. cases.forEach(function (item) {
  50. it(item.displayType, function () {
  51. widgetProperty.set('displayType', item.displayType);
  52. expect(widgetProperty.get('viewClass')).to.eql(item.viewClass);
  53. });
  54. });
  55. });
  56. describe('#isValid', function () {
  57. describe('display_unit', function () {
  58. var cases = [
  59. {
  60. isRequired: true,
  61. value: 'MB',
  62. isValid: true,
  63. title: 'valid value'
  64. },
  65. {
  66. isRequired: true,
  67. value: '0',
  68. isValid: true,
  69. title: 'non-empty value'
  70. },
  71. {
  72. isRequired: true,
  73. value: '',
  74. isValid: false,
  75. title: 'empty value'
  76. },
  77. {
  78. isRequired: false,
  79. value: '',
  80. isValid: true,
  81. title: 'value not required'
  82. }
  83. ];
  84. beforeEach(function () {
  85. widgetProperty.reopen(unit);
  86. });
  87. cases.forEach(function (item) {
  88. it(item.title, function () {
  89. widgetProperty.setProperties({
  90. isRequired: item.isRequired,
  91. value: item.value
  92. });
  93. expect(widgetProperty.get('isValid')).to.equal(item.isValid);
  94. });
  95. });
  96. });
  97. describe('threshold', function () {
  98. var cases = [
  99. {
  100. isSmallValueValid: true,
  101. isBigValueValid: true,
  102. isValid: true,
  103. title: 'both threshold values are valid'
  104. },
  105. {
  106. isSmallValueValid: false,
  107. isBigValueValid: true,
  108. isValid: false,
  109. title: 'warning threshold value is invalid'
  110. },
  111. {
  112. isSmallValueValid: true,
  113. isBigValueValid: false,
  114. isValid: false,
  115. title: 'error threshold value is invalid'
  116. },
  117. {
  118. isSmallValueValid: false,
  119. isBigValueValid: false,
  120. isValid: false,
  121. title: 'both threshold values are invalid'
  122. }
  123. ];
  124. cases.forEach(function (item) {
  125. it(item.title, function () {
  126. widgetProperty.reopen(threshold, {
  127. isSmallValueValid: item.isSmallValueValid,
  128. isBigValueValid: item.isBigValueValid
  129. });
  130. expect(widgetProperty.get('isValid')).to.equal(item.isValid);
  131. });
  132. });
  133. });
  134. });
  135. describe('#isSmallValueValid', function () {
  136. var cases = [
  137. {
  138. smallValue: '1',
  139. isSmallValueValid: true,
  140. title: 'valid value'
  141. },
  142. {
  143. smallValue: 'value',
  144. isSmallValueValid: false,
  145. title: 'invalid value'
  146. }
  147. ];
  148. beforeEach(function () {
  149. widgetProperty.reopen(threshold);
  150. sinon.stub(widgetProperty, 'validate', validate);
  151. });
  152. afterEach(function () {
  153. widgetProperty.validate.restore();
  154. });
  155. cases.forEach(function (item) {
  156. it(item.title, function () {
  157. widgetProperty.set('smallValue', item.smallValue);
  158. expect(widgetProperty.get('isSmallValueValid')).to.equal(item.isSmallValueValid);
  159. });
  160. });
  161. });
  162. describe('#isBigValueValid', function () {
  163. var cases = [
  164. {
  165. bigValue: '1',
  166. isBigValueValid: true,
  167. title: 'valid value'
  168. },
  169. {
  170. bigValue: 'value',
  171. isBigValueValid: false,
  172. title: 'invalid value'
  173. }
  174. ];
  175. beforeEach(function () {
  176. widgetProperty.reopen(threshold);
  177. sinon.stub(widgetProperty, 'validate', validate);
  178. });
  179. afterEach(function () {
  180. widgetProperty.validate.restore();
  181. });
  182. cases.forEach(function (item) {
  183. it(item.title, function () {
  184. widgetProperty.set('bigValue', item.bigValue);
  185. expect(widgetProperty.get('isBigValueValid')).to.equal(item.isBigValueValid);
  186. });
  187. });
  188. });
  189. describe('#validate', function () {
  190. var cases = [
  191. {
  192. value: '',
  193. validateResult: true,
  194. title: 'empty value'
  195. },
  196. {
  197. value: ' \r\n\t ',
  198. validateResult: true,
  199. title: 'spaces only'
  200. },
  201. {
  202. value: 'v',
  203. validateResult: false,
  204. title: 'invalid value'
  205. },
  206. {
  207. value: ' v \r\n\t',
  208. validateResult: false,
  209. title: 'invalid value with spaces'
  210. },
  211. {
  212. value: '-1',
  213. validateResult: false,
  214. title: 'value below the minimum'
  215. },
  216. {
  217. value: ' -1 \r\n\t',
  218. validateResult: false,
  219. title: 'value below the minimum with spaces'
  220. },
  221. {
  222. value: '2',
  223. validateResult: false,
  224. title: 'value above the minimum'
  225. },
  226. {
  227. value: ' 2 \r\n\t',
  228. validateResult: false,
  229. title: 'value above the minimum with spaces'
  230. },
  231. {
  232. value: '0,5',
  233. validateResult: false,
  234. title: 'malformed number'
  235. },
  236. {
  237. value: ' 0,5 \r\n\t',
  238. validateResult: false,
  239. title: 'malformed number with spaces'
  240. },
  241. {
  242. value: '0.5',
  243. validateResult: true,
  244. title: 'valid value'
  245. },
  246. {
  247. value: ' 0.5 \r\n\t',
  248. validateResult: true,
  249. title: 'valid value with spaces'
  250. },
  251. {
  252. value: '2E-1',
  253. validateResult: true,
  254. title: 'exponentially formatted value'
  255. },
  256. {
  257. value: ' 2E-1 \r\n\t',
  258. validateResult: true,
  259. title: 'exponentially formatted value with spaces'
  260. }
  261. ];
  262. beforeEach(function () {
  263. widgetProperty.reopen(threshold);
  264. });
  265. cases.forEach(function (item) {
  266. it(item.title, function () {
  267. expect(widgetProperty.validate(item.value)).to.equal(item.validateResult);
  268. });
  269. });
  270. });
  271. });