export_metrics_mixin_test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. var testHelpers = require('test/helpers');
  22. describe('App.ExportMetricsMixin', function () {
  23. var obj;
  24. beforeEach(function () {
  25. obj = Em.Object.create(App.ExportMetricsMixin);
  26. });
  27. describe('#toggleFormatsList', function () {
  28. var cases = [
  29. {
  30. isExportMenuHidden: true,
  31. title: 'menu should be visible'
  32. },
  33. {
  34. isExportMenuHidden: false,
  35. title: 'menu should be hidden'
  36. }
  37. ];
  38. cases.forEach(function (item) {
  39. it(item.title, function () {
  40. obj.set('isExportMenuHidden', !item.isExportMenuHidden);
  41. obj.toggleFormatsList();
  42. expect(obj.get('isExportMenuHidden')).to.equal(item.isExportMenuHidden);
  43. });
  44. });
  45. });
  46. describe('#exportGraphData', function () {
  47. var cases = [
  48. {
  49. isExportMenuHidden: true,
  50. event: {
  51. context: true
  52. },
  53. isCSV: true,
  54. title: 'CSV, menu should remain hidden'
  55. },
  56. {
  57. isExportMenuHidden: false,
  58. event: {},
  59. isCSV: false,
  60. title: 'JSON, menu should become hidden'
  61. }
  62. ];
  63. beforeEach(function () {
  64. obj.reopen({
  65. targetView: {
  66. ajaxIndex: 'index',
  67. getDataForAjaxRequest: function () {
  68. return {
  69. p: 'v'
  70. };
  71. }
  72. }
  73. });
  74. });
  75. cases.forEach(function (item) {
  76. describe(item.title, function () {
  77. beforeEach(function () {
  78. obj.set('isExportMenuHidden', item.isExportMenuHidden);
  79. obj.exportGraphData(item.event);
  80. this.ajaxParams = testHelpers.findAjaxRequest('name', 'index');
  81. });
  82. it('isExportMenuHidden is true', function () {
  83. expect(obj.get('isExportMenuHidden')).to.be.true;
  84. });
  85. it('one request was done', function () {
  86. expect(this.ajaxParams[0]).exists;
  87. });
  88. it('ajax-request with correct data', function () {
  89. expect(this.ajaxParams[0].data).to.eql({
  90. p: 'v',
  91. isCSV: item.isCSV
  92. });
  93. });
  94. });
  95. });
  96. });
  97. describe('#exportGraphDataSuccessCallback', function () {
  98. var cases = [
  99. {
  100. response: null,
  101. showAlertPopupCallCount: 1,
  102. downloadTextFileCallCount: 0,
  103. title: 'no response'
  104. },
  105. {
  106. response: {
  107. metrics: null
  108. },
  109. showAlertPopupCallCount: 1,
  110. downloadTextFileCallCount: 0,
  111. title: 'no metrics object in response'
  112. },
  113. {
  114. response: {
  115. metrics: {}
  116. },
  117. showAlertPopupCallCount: 1,
  118. downloadTextFileCallCount: 0,
  119. title: 'empty metrics object'
  120. },
  121. {
  122. response: {
  123. metrics: {
  124. m0: [0, 1]
  125. }
  126. },
  127. params: {
  128. isCSV: true
  129. },
  130. showAlertPopupCallCount: 0,
  131. downloadTextFileCallCount: 1,
  132. data: '0,1',
  133. fileType: 'csv',
  134. fileName: 'data.csv',
  135. title: 'export to CSV'
  136. },
  137. {
  138. response: {
  139. metrics: {
  140. m0: [0, 1]
  141. }
  142. },
  143. params: {
  144. isCSV: false
  145. },
  146. showAlertPopupCallCount: 0,
  147. downloadTextFileCallCount: 1,
  148. data: '[{"name":"m0","data":[0,1]}]',
  149. fileType: 'json',
  150. fileName: 'data.json',
  151. title: 'export to JSON'
  152. }
  153. ];
  154. beforeEach(function () {
  155. sinon.stub(App, 'showAlertPopup', Em.K);
  156. sinon.stub(fileUtils, 'downloadTextFile', Em.K);
  157. sinon.stub(obj, 'prepareCSV').returns('0,1');
  158. obj.reopen({
  159. targetView: {
  160. getData: function (response) {
  161. var data = [];
  162. if (response && response.metrics) {
  163. var name = Em.keys(response.metrics)[0];
  164. if (name && response.metrics[name]) {
  165. data = [
  166. {
  167. name: name,
  168. data: response.metrics[name]
  169. }
  170. ];
  171. }
  172. }
  173. return data;
  174. }
  175. }
  176. });
  177. });
  178. afterEach(function () {
  179. App.showAlertPopup.restore();
  180. fileUtils.downloadTextFile.restore();
  181. obj.prepareCSV.restore();
  182. });
  183. cases.forEach(function (item) {
  184. describe(item.title, function () {
  185. beforeEach(function () {
  186. obj.exportGraphDataSuccessCallback(item.response, null, item.params);
  187. });
  188. it('downloadTextFile was called needed number of times', function () {
  189. expect(fileUtils.downloadTextFile.callCount).to.equal(item.downloadTextFileCallCount);
  190. });
  191. if (item.downloadTextFileCallCount) {
  192. it('data is valid', function () {
  193. expect(fileUtils.downloadTextFile.firstCall.args[0].replace(/\s/g, '')).to.equal(item.data);
  194. });
  195. it('fileType is valid', function () {
  196. expect(fileUtils.downloadTextFile.firstCall.args[1]).to.equal(item.fileType);
  197. });
  198. it('fileName is valid', function () {
  199. expect(fileUtils.downloadTextFile.firstCall.args[2]).to.equal(item.fileName);
  200. });
  201. }
  202. });
  203. });
  204. });
  205. describe('#exportGraphDataErrorCallback', function () {
  206. beforeEach(function () {
  207. sinon.stub(App.ajax, 'defaultErrorHandler', Em.K);
  208. });
  209. afterEach(function () {
  210. App.ajax.defaultErrorHandler.restore();
  211. });
  212. it('should display error popup', function () {
  213. obj.exportGraphDataErrorCallback({
  214. status: 404
  215. }, null, '', {
  216. url: 'url',
  217. method: 'GET'
  218. });
  219. expect(App.ajax.defaultErrorHandler.calledOnce).to.be.true;
  220. expect(App.ajax.defaultErrorHandler.calledWith({
  221. status: 404
  222. }, 'url', 'GET', 404)).to.be.true;
  223. });
  224. });
  225. describe('#prepareCSV', function () {
  226. var cases = [
  227. {
  228. displayUnit: 'B',
  229. result: 'Timestamp,n0 (B),n1 (B)\n1,0,4\n3,2,5\n',
  230. title: 'display unit set'
  231. },
  232. {
  233. result: 'Timestamp,n0,n1\n1,0,4\n3,2,5\n',
  234. title: 'display unit not set'
  235. }
  236. ],
  237. data = [
  238. {
  239. name: 'n0',
  240. data: [[0, 1], [2, 3]]
  241. },
  242. {
  243. name: 'n1',
  244. data: [[4, 1], [5, 3]]
  245. }
  246. ];
  247. cases.forEach(function (item) {
  248. it(item.title, function () {
  249. obj.reopen({
  250. targetView: {
  251. displayUnit: item.displayUnit
  252. }
  253. });
  254. expect(obj.prepareCSV(data)).to.equal(item.result);
  255. });
  256. });
  257. });
  258. describe('#hideMenuForNoData', function () {
  259. var cases = [
  260. {
  261. isExportButtonHidden: true,
  262. isExportMenuHidden: true,
  263. title: 'menu should be hidden'
  264. },
  265. {
  266. isExportButtonHidden: false,
  267. isExportMenuHidden: false,
  268. title: 'menu should be visible'
  269. }
  270. ];
  271. cases.forEach(function (item) {
  272. it(item.title, function () {
  273. obj.setProperties({
  274. isExportButtonHidden: item.isExportButtonHidden,
  275. isExportMenuHidden: false
  276. });
  277. expect(obj.get('isExportMenuHidden')).to.equal(item.isExportMenuHidden);
  278. });
  279. });
  280. });
  281. describe('#jsonReplacer', function () {
  282. var cases = [
  283. {
  284. json: [
  285. {
  286. name: 'n0',
  287. data: [
  288. [0, 1],
  289. [1, 2]
  290. ]
  291. }
  292. ],
  293. result: '[{"name":"n0","data":[[0,1],[1,2]]}]',
  294. title: 'valid object'
  295. },
  296. {
  297. json: [
  298. {
  299. name: 'n1',
  300. data: [
  301. [0, 1],
  302. [1, 2]
  303. ],
  304. p1: 'v1'
  305. }
  306. ],
  307. result: '[{"name":"n1","data":[[0,1],[1,2]]}]',
  308. title: 'object with redundant property'
  309. },
  310. {
  311. json: [
  312. {
  313. name: 'n1',
  314. data: {
  315. p2: 'v2'
  316. }
  317. }
  318. ],
  319. result: '[{"name":"n1","data":{}}]',
  320. title: 'object with malformed data'
  321. }
  322. ];
  323. cases.forEach(function (item) {
  324. it(item.title, function () {
  325. expect(JSON.stringify(item.json, obj.jsonReplacer())).to.equal(item.result);
  326. });
  327. });
  328. });
  329. });