export_metrics_mixin_test.js 6.6 KB

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