widget_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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('views/main/dashboard/widget');
  20. describe('App.DashboardWidgetView', function () {
  21. var dashboardWidgetView = App.DashboardWidgetView.create({
  22. parentView: Em.Object.create({
  23. widgetsMapper: Em.K,
  24. getUserPref: Em.K,
  25. postUserPref: Em.K,
  26. translateToReal: Em.K,
  27. visibleWidgets: [],
  28. hiddenWidgets: []
  29. })
  30. });
  31. describe('#viewID', function () {
  32. it('viewID is computed with id', function () {
  33. dashboardWidgetView.set('id', 5);
  34. expect(dashboardWidgetView.get('viewID')).to.equal('widget-5');
  35. });
  36. });
  37. describe('#model', function () {
  38. it('model_type is null', function () {
  39. dashboardWidgetView.set('model_type', null);
  40. dashboardWidgetView.propertyDidChange('model');
  41. expect(dashboardWidgetView.get('model')).to.eql({});
  42. });
  43. it('model_type is valid', function () {
  44. dashboardWidgetView.set('model_type', 's');
  45. dashboardWidgetView.propertyDidChange('model');
  46. dashboardWidgetView.set('parentView.s_model', {'s': {}});
  47. expect(dashboardWidgetView.get('model')).to.eql({'s': {}});
  48. });
  49. });
  50. describe("#didInsertElement()", function () {
  51. before(function () {
  52. sinon.stub(App, 'tooltip', Em.K);
  53. });
  54. after(function () {
  55. App.tooltip.restore();
  56. });
  57. it("call App.tooltip", function () {
  58. dashboardWidgetView.didInsertElement();
  59. expect(App.tooltip.calledOnce).to.be.true;
  60. });
  61. });
  62. describe("#deleteWidget()", function () {
  63. beforeEach(function () {
  64. sinon.stub(dashboardWidgetView.get('parentView'), 'widgetsMapper').returns({});
  65. sinon.stub(dashboardWidgetView.get('parentView'), 'getUserPref').returns({
  66. complete: Em.K
  67. });
  68. });
  69. afterEach(function () {
  70. dashboardWidgetView.get('parentView').widgetsMapper.restore();
  71. dashboardWidgetView.get('parentView').getUserPref.restore();
  72. });
  73. it("testMode is on", function () {
  74. App.set('testMode', true);
  75. dashboardWidgetView.set('id', '1');
  76. dashboardWidgetView.deleteWidget();
  77. expect(dashboardWidgetView.get('parentView').widgetsMapper.calledWith('1')).to.be.true;
  78. expect(dashboardWidgetView.get('parentView.visibleWidgets')).to.be.empty;
  79. expect(dashboardWidgetView.get('parentView.hiddenWidgets')).to.not.be.empty;
  80. });
  81. it("testMode is off", function () {
  82. App.set('testMode', false);
  83. dashboardWidgetView.set('parentView.persistKey', 'key');
  84. dashboardWidgetView.deleteWidget();
  85. expect(dashboardWidgetView.get('parentView').getUserPref.calledWith('key')).to.be.true;
  86. });
  87. });
  88. describe("#deleteWidgetComplete()", function () {
  89. before(function () {
  90. sinon.spy(dashboardWidgetView.get('parentView'), 'postUserPref');
  91. sinon.spy(dashboardWidgetView.get('parentView'), 'translateToReal');
  92. });
  93. after(function () {
  94. dashboardWidgetView.get('parentView').postUserPref.restore();
  95. dashboardWidgetView.get('parentView').translateToReal.restore();
  96. });
  97. it("", function () {
  98. dashboardWidgetView.set('parentView.currentPrefObject', {
  99. dashboardVersion: 'new',
  100. visible: ['1', '2'],
  101. hidden: [],
  102. threshold: 'threshold'
  103. });
  104. dashboardWidgetView.set('parentView.persistKey', 'key');
  105. dashboardWidgetView.deleteWidgetComplete();
  106. expect(dashboardWidgetView.get('parentView').postUserPref.calledWith('key', {
  107. dashboardVersion: 'new',
  108. visible: ['2'],
  109. hidden: ['1'],
  110. threshold: 'threshold'
  111. }));
  112. expect(dashboardWidgetView.get('parentView').translateToReal.calledWith({
  113. dashboardVersion: 'new',
  114. visible: ['2'],
  115. hidden: ['1'],
  116. threshold: 'threshold'
  117. }));
  118. });
  119. });
  120. describe("#editWidget()", function () {
  121. before(function () {
  122. sinon.stub(dashboardWidgetView, 'showEditDialog', Em.K);
  123. });
  124. after(function () {
  125. dashboardWidgetView.showEditDialog.restore();
  126. });
  127. it("call showEditDialog", function () {
  128. dashboardWidgetView.editWidget();
  129. expect(dashboardWidgetView.showEditDialog.calledOnce).to.be.true;
  130. });
  131. });
  132. describe("#showEditDialog()", function () {
  133. var obj = Em.Object.create({
  134. observeThresh1Value: Em.K,
  135. observeThresh2Value: Em.K,
  136. thresh1: '1',
  137. thresh2: '2'
  138. });
  139. before(function () {
  140. sinon.spy(obj, 'observeThresh1Value');
  141. sinon.spy(obj, 'observeThresh2Value');
  142. sinon.stub(dashboardWidgetView.get('parentView'), 'getUserPref').returns({
  143. complete: Em.K
  144. });
  145. });
  146. after(function () {
  147. obj.observeThresh1Value.restore();
  148. obj.observeThresh2Value.restore();
  149. dashboardWidgetView.get('parentView').getUserPref.restore();
  150. });
  151. it("open popup", function () {
  152. var popup = dashboardWidgetView.showEditDialog(obj);
  153. popup.onPrimary();
  154. expect(obj.observeThresh1Value.calledOnce).to.be.true;
  155. expect(obj.observeThresh2Value.calledOnce).to.be.true;
  156. expect(dashboardWidgetView.get('thresh1')).to.equal(1);
  157. expect(dashboardWidgetView.get('thresh2')).to.equal(2);
  158. expect(dashboardWidgetView.get('parentView').getUserPref.calledOnce).to.be.true;
  159. });
  160. });
  161. describe('#model', function () {
  162. it('model_type is null', function () {
  163. dashboardWidgetView.set('model_type', null);
  164. dashboardWidgetView.propertyDidChange('model');
  165. expect(dashboardWidgetView.get('model')).to.eql({});
  166. });
  167. it('model_type is valid', function () {
  168. dashboardWidgetView.set('model_type', 's');
  169. dashboardWidgetView.propertyDidChange('model');
  170. dashboardWidgetView.set('parentView.s_model', {'s': {}});
  171. expect(dashboardWidgetView.get('model')).to.eql({'s': {}});
  172. });
  173. });
  174. describe('#hoverContentTopClass', function () {
  175. var tests = [
  176. {
  177. h: ['', ''],
  178. e: 'content-hidden-two-line',
  179. m: '2 lines'
  180. },
  181. {
  182. h: ['', '', ''],
  183. e: 'content-hidden-three-line',
  184. m: '3 lines'
  185. },
  186. {
  187. h: [''],
  188. e: '',
  189. m: '1 line'
  190. },
  191. {
  192. h: [],
  193. e: '',
  194. m: '0 lines'
  195. },
  196. {
  197. h: ['', '', '', '', ''],
  198. e: 'content-hidden-five-line',
  199. m: '5 lines'
  200. },
  201. {
  202. h: ['', '', '', ''],
  203. e: 'content-hidden-four-line',
  204. m: '4 lines'
  205. },
  206. {
  207. h: ['', '', '', '', '', ''],
  208. e: 'content-hidden-six-line',
  209. m: '6 lines'
  210. }
  211. ];
  212. tests.forEach(function (test) {
  213. it(test.m, function () {
  214. dashboardWidgetView.set('hiddenInfo', test.h);
  215. expect(dashboardWidgetView.get('hoverContentTopClass')).to.equal(test.e);
  216. });
  217. });
  218. });
  219. describe("#widgetConfig", function() {
  220. var widget = dashboardWidgetView.get('widgetConfig').create();
  221. describe("#hintInfo", function() {
  222. it("", function() {
  223. widget.set('maxValue', 1);
  224. widget.propertyDidChange('hintInfo');
  225. expect(widget.get('hintInfo')).to.equal(Em.I18n.t('dashboard.widgets.hintInfo.common').format(1));
  226. });
  227. });
  228. describe("#observeThresh1Value", function() {
  229. beforeEach(function () {
  230. sinon.stub(widget, 'updateSlider', Em.K);
  231. });
  232. afterEach(function () {
  233. widget.updateSlider.restore();
  234. });
  235. var testCases = [
  236. {
  237. data: {
  238. thresh1: '',
  239. maxValue: 0
  240. },
  241. result: {
  242. isThresh1Error: true,
  243. errorMessage1: Em.I18n.t('admin.users.editError.requiredField')
  244. }
  245. },
  246. {
  247. data: {
  248. thresh1: 'NaN',
  249. maxValue: 0
  250. },
  251. result: {
  252. isThresh1Error: true,
  253. errorMessage1: Em.I18n.t('dashboard.widgets.error.invalid').format(0)
  254. }
  255. },
  256. {
  257. data: {
  258. thresh1: '-1',
  259. maxValue: 0
  260. },
  261. result: {
  262. isThresh1Error: true,
  263. errorMessage1: Em.I18n.t('dashboard.widgets.error.invalid').format(0)
  264. }
  265. },
  266. {
  267. data: {
  268. thresh1: '2',
  269. maxValue: 1
  270. },
  271. result: {
  272. isThresh1Error: true,
  273. errorMessage1: Em.I18n.t('dashboard.widgets.error.invalid').format(1)
  274. }
  275. },
  276. {
  277. data: {
  278. thresh1: '1',
  279. thresh2: '1',
  280. maxValue: 2
  281. },
  282. result: {
  283. isThresh1Error: true,
  284. errorMessage1: Em.I18n.t('dashboard.widgets.error.smaller')
  285. }
  286. },
  287. {
  288. data: {
  289. thresh1: '1',
  290. thresh2: '0',
  291. maxValue: 2
  292. },
  293. result: {
  294. isThresh1Error: true,
  295. errorMessage1: Em.I18n.t('dashboard.widgets.error.smaller')
  296. }
  297. },
  298. {
  299. data: {
  300. thresh1: '1',
  301. thresh2: '2',
  302. maxValue: 2
  303. },
  304. result: {
  305. isThresh1Error: false,
  306. errorMessage1: ''
  307. }
  308. }
  309. ];
  310. testCases.forEach(function (test) {
  311. it("thresh1 - " + test.data.thresh1 + ', maxValue - ' + test.data.maxValue, function () {
  312. widget.set('isThresh2Error', false);
  313. widget.set('thresh2', test.data.thresh2 || "");
  314. widget.set('thresh1', test.data.thresh1);
  315. widget.set('maxValue', test.data.maxValue);
  316. widget.observeThresh1Value();
  317. expect(widget.get('isThresh1Error')).to.equal(test.result.isThresh1Error);
  318. expect(widget.get('errorMessage1')).to.equal(test.result.errorMessage1);
  319. expect(widget.updateSlider.called).to.be.true;
  320. });
  321. });
  322. });
  323. describe("#observeThresh2Value", function() {
  324. beforeEach(function () {
  325. sinon.stub(widget, 'updateSlider', Em.K);
  326. });
  327. afterEach(function () {
  328. widget.updateSlider.restore();
  329. });
  330. var testCases = [
  331. {
  332. data: {
  333. thresh2: '',
  334. maxValue: 0
  335. },
  336. result: {
  337. isThresh2Error: true,
  338. errorMessage2: Em.I18n.t('admin.users.editError.requiredField')
  339. }
  340. },
  341. {
  342. data: {
  343. thresh2: 'NaN',
  344. maxValue: 0
  345. },
  346. result: {
  347. isThresh2Error: true,
  348. errorMessage2: Em.I18n.t('dashboard.widgets.error.invalid').format(0)
  349. }
  350. },
  351. {
  352. data: {
  353. thresh2: '-1',
  354. maxValue: 0
  355. },
  356. result: {
  357. isThresh2Error: true,
  358. errorMessage2: Em.I18n.t('dashboard.widgets.error.invalid').format(0)
  359. }
  360. },
  361. {
  362. data: {
  363. thresh2: '2',
  364. maxValue: 1
  365. },
  366. result: {
  367. isThresh2Error: true,
  368. errorMessage2: Em.I18n.t('dashboard.widgets.error.invalid').format(1)
  369. }
  370. },
  371. {
  372. data: {
  373. thresh2: '2',
  374. maxValue: 2
  375. },
  376. result: {
  377. isThresh2Error: false,
  378. errorMessage2: ''
  379. }
  380. }
  381. ];
  382. testCases.forEach(function (test) {
  383. it("thresh2 - " + test.data.thresh2 + ', maxValue - ' + test.data.maxValue, function () {
  384. widget.set('thresh2', test.data.thresh2 || "");
  385. widget.set('maxValue', test.data.maxValue);
  386. widget.observeThresh2Value();
  387. expect(widget.get('isThresh2Error')).to.equal(test.result.isThresh2Error);
  388. expect(widget.get('errorMessage2')).to.equal(test.result.errorMessage2);
  389. expect(widget.updateSlider.called).to.be.true;
  390. });
  391. });
  392. });
  393. });
  394. });