export_metrics_mixin_test.js 7.2 KB

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