file_export.jest.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import * as fileExport from '../utils/file_export';
  2. import { beforeEach, expect } from 'test/lib/common';
  3. describe('file_export', () => {
  4. let ctx: any = {};
  5. beforeEach(() => {
  6. ctx.seriesList = [
  7. {
  8. alias: 'series_1',
  9. datapoints: [
  10. [1, 1500026100000],
  11. [2, 1500026200000],
  12. [null, 1500026300000],
  13. [null, 1500026400000],
  14. [null, 1500026500000],
  15. [6, 1500026600000],
  16. ],
  17. },
  18. {
  19. alias: 'series_2',
  20. datapoints: [[11, 1500026100000], [12, 1500026200000], [13, 1500026300000], [15, 1500026500000]],
  21. },
  22. ];
  23. ctx.timeFormat = 'X'; // Unix timestamp (seconds)
  24. });
  25. describe('when exporting series as rows', () => {
  26. it('should export points in proper order', () => {
  27. let text = fileExport.convertSeriesListToCsv(ctx.seriesList, ctx.timeFormat);
  28. const expectedText =
  29. '"Series";"Time";"Value"\r\n' +
  30. '"series_1";"1500026100";1\r\n' +
  31. '"series_1";"1500026200";2\r\n' +
  32. '"series_1";"1500026300";null\r\n' +
  33. '"series_1";"1500026400";null\r\n' +
  34. '"series_1";"1500026500";null\r\n' +
  35. '"series_1";"1500026600";6\r\n' +
  36. '"series_2";"1500026100";11\r\n' +
  37. '"series_2";"1500026200";12\r\n' +
  38. '"series_2";"1500026300";13\r\n' +
  39. '"series_2";"1500026500";15';
  40. expect(text).toBe(expectedText);
  41. });
  42. });
  43. describe('when exporting series as columns', () => {
  44. it('should export points in proper order', () => {
  45. let text = fileExport.convertSeriesListToCsvColumns(ctx.seriesList, ctx.timeFormat);
  46. const expectedText =
  47. '"Time";"series_1";"series_2"\r\n' +
  48. '"1500026100";1;11\r\n' +
  49. '"1500026200";2;12\r\n' +
  50. '"1500026300";null;13\r\n' +
  51. '"1500026400";null;null\r\n' +
  52. '"1500026500";null;15\r\n' +
  53. '"1500026600";6;null';
  54. expect(text).toBe(expectedText);
  55. });
  56. });
  57. describe('when exporting table data to csv', () => {
  58. it('should properly escape special characters and quote all string values', () => {
  59. const inputTable = {
  60. columns: [
  61. { title: 'integer_value' },
  62. { text: 'string_value' },
  63. { title: 'float_value' },
  64. { text: 'boolean_value' },
  65. ],
  66. rows: [
  67. [123, 'some_string', 1.234, true],
  68. [0o765, 'some string with " in the middle', 1e-2, false],
  69. [0o765, 'some string with "" in the middle', 1e-2, false],
  70. [0o765, 'some string with """ in the middle', 1e-2, false],
  71. [0o765, '"some string with " at the beginning', 1e-2, false],
  72. [0o765, 'some string with " at the end"', 1e-2, false],
  73. [0x123, 'some string with \n in the middle', 10.01, false],
  74. [0b1011, 'some string with ; in the middle', -12.34, true],
  75. [123, 'some string with ;; in the middle', -12.34, true],
  76. ],
  77. };
  78. const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
  79. const expectedText =
  80. '"integer_value";"string_value";"float_value";"boolean_value"\r\n' +
  81. '123;"some_string";1.234;true\r\n' +
  82. '501;"some string with "" in the middle";0.01;false\r\n' +
  83. '501;"some string with """" in the middle";0.01;false\r\n' +
  84. '501;"some string with """""" in the middle";0.01;false\r\n' +
  85. '501;"""some string with "" at the beginning";0.01;false\r\n' +
  86. '501;"some string with "" at the end""";0.01;false\r\n' +
  87. '291;"some string with \n in the middle";10.01;false\r\n' +
  88. '11;"some string with ; in the middle";-12.34;true\r\n' +
  89. '123;"some string with ;; in the middle";-12.34;true';
  90. expect(returnedText).toBe(expectedText);
  91. });
  92. it('should decode HTML encoded characters', function() {
  93. const inputTable = {
  94. columns: [{ text: 'string_value' }],
  95. rows: [
  96. ['"&ä'],
  97. ['<strong>&quot;some html&quot;</strong>'],
  98. ['<a href="http://something/index.html">some text</a>'],
  99. ],
  100. };
  101. const returnedText = fileExport.convertTableDataToCsv(inputTable, false);
  102. const expectedText =
  103. '"string_value"\r\n' +
  104. '"""&ä"\r\n' +
  105. '"<strong>""some html""</strong>"\r\n' +
  106. '"<a href=""http://something/index.html"">some text</a>"';
  107. expect(returnedText).toBe(expectedText);
  108. });
  109. });
  110. });