utils.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import {mount} from 'enzyme';
  2. import {
  3. getChartData,
  4. getChartDataForWidget,
  5. getChartDataByDay,
  6. getDisplayValue,
  7. getDisplayText,
  8. downloadAsCsv,
  9. } from 'app/views/discover/result/utils';
  10. describe('Utils', function() {
  11. describe('getChartData()', function() {
  12. const raw = [
  13. {count: 2, uniq_id: 1, 'project.id': 5, environment: null},
  14. {count: 2, uniq_id: 3, 'project.id': 5, environment: 'staging'},
  15. {count: 2, uniq_id: 4, 'project.id': 5, environment: 'alpha'},
  16. {count: 6, uniq_id: 10, 'project.id': 5, environment: 'production'},
  17. ];
  18. const query = {
  19. aggregations: [['count()', null, 'count'], ['uniq', 'id', 'uniq_id']],
  20. fields: ['project.id', 'environment'],
  21. };
  22. it('returns chart data', function() {
  23. const expected = [
  24. {
  25. seriesName: 'count',
  26. data: [
  27. {value: 2, name: 'project.id 5 environment null'},
  28. {value: 2, name: 'project.id 5 environment staging'},
  29. {value: 2, name: 'project.id 5 environment alpha'},
  30. {value: 6, name: 'project.id 5 environment production'},
  31. ],
  32. animation: false,
  33. },
  34. {
  35. seriesName: 'uniq_id',
  36. data: [
  37. {value: 1, name: 'project.id 5 environment null'},
  38. {value: 3, name: 'project.id 5 environment staging'},
  39. {value: 4, name: 'project.id 5 environment alpha'},
  40. {value: 10, name: 'project.id 5 environment production'},
  41. ],
  42. animation: false,
  43. },
  44. ];
  45. expect(getChartData(raw, query)).toEqual(expected);
  46. });
  47. });
  48. describe('getChartDataForWidget()', function() {
  49. const raw = [
  50. {count: 2, uniq_id: 1, 'project.id': 5, environment: null},
  51. {count: 2, uniq_id: 3, 'project.id': 5, environment: 'staging'},
  52. {count: 2, uniq_id: 4, 'project.id': 5, environment: 'alpha'},
  53. {count: 6, uniq_id: 10, 'project.id': 5, environment: 'production'},
  54. ];
  55. const query = {
  56. aggregations: [['count()', null, 'count'], ['uniq', 'id', 'uniq_id']],
  57. fields: ['project.id', 'environment'],
  58. };
  59. it('returns chart data for widgets with percentages', function() {
  60. const expected = [
  61. {
  62. seriesName: 'count',
  63. data: [
  64. {value: 2, percentage: 16.67, name: '5, null', fieldValues: [5, null]},
  65. {
  66. value: 2,
  67. percentage: 16.67,
  68. name: '5, staging',
  69. fieldValues: [5, 'staging'],
  70. },
  71. {value: 2, percentage: 16.67, name: '5, alpha', fieldValues: [5, 'alpha']},
  72. {
  73. value: 6,
  74. percentage: 50,
  75. name: '5, production',
  76. fieldValues: [5, 'production'],
  77. },
  78. ],
  79. },
  80. {
  81. seriesName: 'uniq_id',
  82. data: [
  83. {value: 1, percentage: 5.56, name: '5, null', fieldValues: [5, null]},
  84. {
  85. value: 3,
  86. percentage: 16.67,
  87. name: '5, staging',
  88. fieldValues: [5, 'staging'],
  89. },
  90. {value: 4, percentage: 22.22, name: '5, alpha', fieldValues: [5, 'alpha']},
  91. {
  92. value: 10,
  93. percentage: 55.56,
  94. name: '5, production',
  95. fieldValues: [5, 'production'],
  96. },
  97. ],
  98. },
  99. ];
  100. expect(getChartDataForWidget(raw, query, {includePercentages: true})).toEqual(
  101. expected
  102. );
  103. });
  104. });
  105. describe('getChartDataByDay()', function() {
  106. const raw = [
  107. {
  108. 'error.type': 'Type Error',
  109. platform: 'javascript',
  110. count: 5,
  111. time: 1532070000,
  112. },
  113. {
  114. 'error.type': 'Exception',
  115. platform: 'php',
  116. count: 8,
  117. time: 1532070000,
  118. },
  119. {
  120. 'error.type': 'SnubaError',
  121. platform: 'python',
  122. count: 30,
  123. time: 1532070000,
  124. },
  125. {
  126. 'error.type': 'ZeroDivisionError',
  127. platform: 'python',
  128. count: 20,
  129. time: 1531180800,
  130. },
  131. {
  132. 'error.type': 'ZeroDivisionError',
  133. platform: 'python',
  134. count: 6,
  135. time: 1531094400,
  136. },
  137. {
  138. 'error.type': 'Type Error',
  139. platform: 'javascript',
  140. count: 6,
  141. time: 1531094400,
  142. },
  143. {
  144. 'error.type': 'Exception',
  145. platform: 'php',
  146. count: 6,
  147. time: 1531094400,
  148. },
  149. {
  150. 'error.type': 'SnubaError',
  151. platform: 'python',
  152. count: 14,
  153. time: 1531094400,
  154. },
  155. ];
  156. const query = {
  157. aggregations: [['count()', null, 'count']],
  158. fields: ['platform', 'error.type'],
  159. };
  160. it('returns chart data grouped by day', function() {
  161. const expected = [
  162. {
  163. data: [
  164. {name: 1531094400000, value: 14},
  165. {name: 1531180800000, value: 0},
  166. {name: 1532070000000, value: 30},
  167. ],
  168. seriesName: 'python,SnubaError',
  169. },
  170. {
  171. data: [
  172. {name: 1531094400000, value: 6},
  173. {name: 1531180800000, value: 0},
  174. {name: 1532070000000, value: 8},
  175. ],
  176. seriesName: 'php,Exception',
  177. },
  178. {
  179. data: [
  180. {name: 1531094400000, value: 6},
  181. {name: 1531180800000, value: 0},
  182. {name: 1532070000000, value: 5},
  183. ],
  184. seriesName: 'javascript,Type Error',
  185. },
  186. {
  187. data: [
  188. {name: 1531094400000, value: 6},
  189. {name: 1531180800000, value: 20},
  190. {name: 1532070000000, value: 0},
  191. ],
  192. seriesName: 'python,ZeroDivisionError',
  193. },
  194. ];
  195. expect(getChartDataByDay(raw, query)).toEqual(expected);
  196. });
  197. it('returns chart data with zero filled dates', function() {
  198. const zeroFilledRaw = [
  199. {
  200. 'error.type': 'Type Error',
  201. platform: 'javascript',
  202. count: 5,
  203. time: 1531465200,
  204. },
  205. {
  206. 'error.type': 'Exception',
  207. platform: 'php',
  208. count: 8,
  209. time: 1531465200,
  210. },
  211. {
  212. 'error.type': 'SnubaError',
  213. platform: 'python',
  214. count: 30,
  215. time: 1531465200,
  216. },
  217. {time: 1531378800},
  218. {time: 1531292400},
  219. ...raw.slice(-5),
  220. ];
  221. const expected = [
  222. {
  223. data: [
  224. {name: 1531094400000, value: 14},
  225. {name: 1531180800000, value: 0},
  226. {name: 1531292400000, value: 0},
  227. {name: 1531378800000, value: 0},
  228. {name: 1531465200000, value: 30},
  229. ],
  230. seriesName: 'python,SnubaError',
  231. },
  232. {
  233. data: [
  234. {name: 1531094400000, value: 6},
  235. {name: 1531180800000, value: 0},
  236. {name: 1531292400000, value: 0},
  237. {name: 1531378800000, value: 0},
  238. {name: 1531465200000, value: 8},
  239. ],
  240. seriesName: 'php,Exception',
  241. },
  242. {
  243. data: [
  244. {name: 1531094400000, value: 6},
  245. {name: 1531180800000, value: 0},
  246. {name: 1531292400000, value: 0},
  247. {name: 1531378800000, value: 0},
  248. {name: 1531465200000, value: 5},
  249. ],
  250. seriesName: 'javascript,Type Error',
  251. },
  252. {
  253. data: [
  254. {name: 1531094400000, value: 6},
  255. {name: 1531180800000, value: 20},
  256. {name: 1531292400000, value: 0},
  257. {name: 1531378800000, value: 0},
  258. {name: 1531465200000, value: 0},
  259. ],
  260. seriesName: 'python,ZeroDivisionError',
  261. },
  262. ];
  263. expect(getChartDataByDay(zeroFilledRaw, query)).toEqual(expected);
  264. });
  265. it('shows only top 10 series by default', function() {
  266. expect(
  267. getChartDataByDay(
  268. [
  269. ...raw,
  270. ...[...new Array(10)].map(() => ({
  271. 'error.type': 'Exeption',
  272. platform: `${Math.random()}`,
  273. count: 10,
  274. time: 1532070000,
  275. })),
  276. ],
  277. query
  278. )
  279. ).toHaveLength(10);
  280. });
  281. it('shows all series', function() {
  282. expect(
  283. getChartDataByDay(
  284. [
  285. ...raw,
  286. ...[...new Array(10)].map(() => ({
  287. 'error.type': 'Exeption',
  288. platform: `${Math.random()}`,
  289. count: 10,
  290. time: 1532070000,
  291. })),
  292. ],
  293. query,
  294. {allSeries: true}
  295. )
  296. ).toHaveLength(14);
  297. });
  298. it('maps field value to label', function() {
  299. const expected = [
  300. {
  301. data: [
  302. {name: 1531094400000, value: 14},
  303. {name: 1531180800000, value: 0},
  304. {name: 1532070000000, value: 30},
  305. ],
  306. seriesName: 'SNAKES,SnubaError',
  307. },
  308. {
  309. data: [
  310. {name: 1531094400000, value: 6},
  311. {name: 1531180800000, value: 0},
  312. {name: 1532070000000, value: 8},
  313. ],
  314. seriesName: 'PHP,Exception',
  315. },
  316. {
  317. data: [
  318. {name: 1531094400000, value: 6},
  319. {name: 1531180800000, value: 0},
  320. {name: 1532070000000, value: 5},
  321. ],
  322. seriesName: 'NOT JAVA,Type Error',
  323. },
  324. {
  325. data: [
  326. {name: 1531094400000, value: 6},
  327. {name: 1531180800000, value: 20},
  328. {name: 1532070000000, value: 0},
  329. ],
  330. seriesName: 'SNAKES,ZeroDivisionError',
  331. },
  332. ];
  333. expect(
  334. getChartDataByDay(raw, query, {
  335. fieldLabelMap: {python: 'SNAKES', php: 'PHP', javascript: 'NOT JAVA'},
  336. })
  337. ).toEqual(expected);
  338. });
  339. });
  340. it('getDisplayValue()', function() {
  341. const testData = [
  342. {input: null, expectedText: 'null'},
  343. {
  344. input: 'some thing',
  345. expectedText: '"some thing"',
  346. },
  347. {
  348. input: 12,
  349. expectedText: '12',
  350. },
  351. {
  352. input: ['one', 'two', 'three'],
  353. expectedText: '["one","two","three"]',
  354. },
  355. {
  356. input: 1000000,
  357. expectedText: '1,000,000',
  358. },
  359. ];
  360. testData.forEach(({input, expectedText}) => {
  361. expect(mount(getDisplayValue(input)).text()).toBe(expectedText);
  362. });
  363. });
  364. it('getTextValue()', function() {
  365. const testData = [
  366. {input: null, expectedText: 'null'},
  367. {
  368. input: 'some thing',
  369. expectedText: '"some thing"',
  370. },
  371. {
  372. input: 12,
  373. expectedText: '12',
  374. },
  375. {
  376. input: ['one', 'two', 'three'],
  377. expectedText: '["one","two","three"]',
  378. },
  379. {
  380. input: 1000000,
  381. expectedText: '1,000,000',
  382. },
  383. ];
  384. testData.forEach(({input, expectedText}) => {
  385. expect(getDisplayText(input)).toBe(expectedText);
  386. });
  387. });
  388. describe('downloadAsCsv()', function() {
  389. let locationSpy;
  390. beforeEach(function() {
  391. locationSpy = jest.spyOn(window.location, 'assign').mockImplementation(_ => _);
  392. });
  393. afterEach(function() {
  394. jest.restoreAllMocks();
  395. });
  396. it('handles raw data', function() {
  397. const result = {
  398. meta: [{name: 'message'}, {name: 'environment'}],
  399. data: [
  400. {message: 'test 1', environment: 'prod'},
  401. {message: 'test 2', environment: 'test'},
  402. ],
  403. };
  404. downloadAsCsv(result);
  405. expect(locationSpy).toHaveBeenCalledWith(
  406. expect.stringContaining(
  407. encodeURI('message,environment\r\ntest 1,prod\r\ntest 2,test')
  408. )
  409. );
  410. });
  411. it('handles aggregations', function() {
  412. const result = {
  413. meta: [{type: 'UInt64', name: 'count'}],
  414. data: [{count: 3}],
  415. };
  416. downloadAsCsv(result);
  417. expect(locationSpy).toHaveBeenCalledWith(
  418. expect.stringContaining(encodeURI('count\r\n3'))
  419. );
  420. });
  421. it('quotes unsafe strings', function() {
  422. const result = {
  423. meta: [{name: 'message'}],
  424. data: [{message: '=HYPERLINK(http://some-bad-website)'}],
  425. };
  426. downloadAsCsv(result);
  427. expect(locationSpy).toHaveBeenCalledWith(
  428. expect.stringContaining(
  429. encodeURI("message\r\n'=HYPERLINK(http://some-bad-website)")
  430. )
  431. );
  432. });
  433. });
  434. });