export_metrics_mixin_test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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('mixins/common/widgets/export_metrics_mixin');
  20. var fileUtils = require('utils/file_utils');
  21. describe('App.ExportMetricsMixin', function () {
  22. var obj;
  23. beforeEach(function () {
  24. obj = Em.Object.create(App.ExportMetricsMixin);
  25. });
  26. describe('#toggleFormatsList', function () {
  27. var cases = [
  28. {
  29. isMenuHidden: true,
  30. title: 'menu should be visible'
  31. },
  32. {
  33. isMenuHidden: false,
  34. title: 'menu should be hidden'
  35. }
  36. ];
  37. cases.forEach(function (item) {
  38. it(item.title, function () {
  39. obj.set('isMenuHidden', !item.isMenuHidden);
  40. obj.toggleFormatsList();
  41. expect(obj.get('isMenuHidden')).to.equal(item.isMenuHidden);
  42. });
  43. });
  44. });
  45. describe('#exportGraphData', function () {
  46. var cases = [
  47. {
  48. isMenuHidden: true,
  49. title: 'menu should remain hidden'
  50. },
  51. {
  52. isMenuHidden: false,
  53. title: 'menu should become hidden'
  54. }
  55. ];
  56. cases.forEach(function (item) {
  57. it(item.title, function () {
  58. obj.set('isMenuHidden', item.isMenuHidden);
  59. obj.exportGraphData();
  60. expect(obj.get('isMenuHidden')).to.be.true;
  61. });
  62. });
  63. });
  64. describe('#exportGraphDataSuccessCallback', function () {
  65. var cases = [
  66. {
  67. response: null,
  68. showAlertPopupCallCount: 1,
  69. prepareCSVCallCount: 0,
  70. prepareJSONCallCount: 0,
  71. downloadTextFileCallCount: 0,
  72. title: 'no response'
  73. },
  74. {
  75. response: {
  76. metrics: null
  77. },
  78. showAlertPopupCallCount: 1,
  79. prepareCSVCallCount: 0,
  80. prepareJSONCallCount: 0,
  81. downloadTextFileCallCount: 0,
  82. title: 'no metrics object in response'
  83. },
  84. {
  85. response: {
  86. metrics: {}
  87. },
  88. showAlertPopupCallCount: 1,
  89. prepareCSVCallCount: 0,
  90. prepareJSONCallCount: 0,
  91. downloadTextFileCallCount: 0,
  92. title: 'empty metrics object'
  93. },
  94. {
  95. response: {
  96. metrics: {
  97. m0: [0, 1]
  98. }
  99. },
  100. params: {
  101. isCSV: true
  102. },
  103. showAlertPopupCallCount: 0,
  104. prepareCSVCallCount: 1,
  105. prepareJSONCallCount: 0,
  106. downloadTextFileCallCount: 1,
  107. fileType: 'csv',
  108. fileName: 'data.csv',
  109. title: 'export to CSV'
  110. },
  111. {
  112. response: {
  113. metrics: {
  114. m0: [0, 1]
  115. }
  116. },
  117. params: {
  118. isCSV: false
  119. },
  120. showAlertPopupCallCount: 0,
  121. prepareCSVCallCount: 0,
  122. prepareJSONCallCount: 1,
  123. downloadTextFileCallCount: 1,
  124. fileType: 'json',
  125. fileName: 'data.json',
  126. title: 'export to JSON'
  127. }
  128. ];
  129. beforeEach(function () {
  130. sinon.stub(App, 'showAlertPopup', Em.K);
  131. sinon.stub(fileUtils, 'downloadTextFile', Em.K);
  132. sinon.stub(obj, 'prepareCSV', Em.K);
  133. sinon.stub(obj, 'prepareJSON', Em.K);
  134. });
  135. afterEach(function () {
  136. App.showAlertPopup.restore();
  137. fileUtils.downloadTextFile.restore();
  138. obj.prepareCSV.restore();
  139. obj.prepareJSON.restore();
  140. });
  141. cases.forEach(function (item) {
  142. it(item.title, function () {
  143. obj.exportGraphDataSuccessCallback(item.response, null, item.params);
  144. expect(obj.prepareCSV.callCount).to.equal(item.prepareCSVCallCount);
  145. expect(obj.prepareJSON.callCount).to.equal(item.prepareJSONCallCount);
  146. expect(fileUtils.downloadTextFile.callCount).to.equal(item.downloadTextFileCallCount);
  147. if (item.downloadTextFileCallCount) {
  148. expect(fileUtils.downloadTextFile.firstCall.args[1]).to.equal(item.fileType);
  149. expect(fileUtils.downloadTextFile.firstCall.args[2]).to.equal(item.fileName);
  150. }
  151. });
  152. });
  153. });
  154. describe('#exportGraphDataErrorCallback', function () {
  155. beforeEach(function () {
  156. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  157. });
  158. afterEach(function () {
  159. App.ajax.defaultErrorHandler.restore();
  160. });
  161. it('should display error popup', function () {
  162. obj.exportGraphDataErrorCallback({
  163. status: 404
  164. }, null, '', {
  165. url: 'url',
  166. method: 'GET'
  167. });
  168. expect(App.ajax.defaultErrorHandler.calledOnce).to.be.true;
  169. expect(App.ajax.defaultErrorHandler.calledWith({
  170. status: 404
  171. }, 'url', 'GET', 404)).to.be.true;
  172. });
  173. });
  174. describe('#setMetricsArrays', function () {
  175. var metrics = [],
  176. titles = [],
  177. data = {
  178. key0: {
  179. key1: {
  180. key2: [[0, 1], [2, 3]],
  181. key3: [[4, 5], [6, 7]]
  182. }
  183. }
  184. };
  185. it('should construct arrays with metrics info', function () {
  186. obj.setMetricsArrays(data, metrics, titles);
  187. expect(metrics).to.eql([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]);
  188. expect(titles).to.eql(['key2', 'key3']);
  189. })
  190. });
  191. describe('#prepareCSV', function () {
  192. var cases = [
  193. {
  194. data: {
  195. metrics: {
  196. key0: [[0, 1], [2, 3]],
  197. key1: [[4, 1], [5, 3]]
  198. }
  199. },
  200. result: 'Timestamp,key0,key1\n1,0,4\n3,2,5\n',
  201. title: 'old style widget metrics'
  202. },
  203. {
  204. data: [
  205. {
  206. data: [[6, 7], [8, 9]]
  207. },
  208. {
  209. data: [[10, 7], [11, 9]]
  210. }
  211. ],
  212. result: 'Timestamp,,\n7,6,10\n9,8,11\n',
  213. title: 'enhanced widget metrics'
  214. }
  215. ];
  216. cases.forEach(function (item) {
  217. it(item.title, function () {
  218. expect(obj.prepareCSV(item.data)).to.equal(item.result);
  219. });
  220. });
  221. });
  222. describe('#prepareJSON', function () {
  223. var cases = [
  224. {
  225. data: {
  226. metrics: {
  227. key0: [[0, 1], [2, 3]],
  228. key1: [[4, 1], [5, 3]]
  229. }
  230. },
  231. result: "{\"key0\":[[0,1],[2,3]],\"key1\":[[4,1],[5,3]]}",
  232. title: 'old style widget metrics'
  233. },
  234. {
  235. data: [
  236. {
  237. name: 'n0',
  238. data: [[6, 7], [8, 9]]
  239. },
  240. {
  241. name: 'n1',
  242. data: [[10, 7], [11, 9]]
  243. }
  244. ],
  245. result: "[{\"name\":\"n0\",\"data\":[[6,7],[8,9]]},{\"name\":\"n1\",\"data\":[[10,7],[11,9]]}]",
  246. title: 'enhanced widget metrics'
  247. }
  248. ];
  249. cases.forEach(function (item) {
  250. it(item.title, function () {
  251. expect(obj.prepareJSON(item.data).replace(/\s/g, '')).to.equal(item.result);
  252. });
  253. });
  254. });
  255. });