|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Datatable } from 'src/plugins/expressions'; |
| 21 | +import { FieldFormat } from '../../common/field_formats'; |
| 22 | +import { datatableToCSV } from './export_csv'; |
| 23 | + |
| 24 | +function getDefaultOptions() { |
| 25 | + const formatFactory = jest.fn(); |
| 26 | + formatFactory.mockReturnValue({ convert: (v: unknown) => `Formatted_${v}` } as FieldFormat); |
| 27 | + return { |
| 28 | + csvSeparator: ',', |
| 29 | + quoteValues: true, |
| 30 | + formatFactory, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function getDataTable({ multipleColumns }: { multipleColumns?: boolean } = {}): Datatable { |
| 35 | + const layer1: Datatable = { |
| 36 | + type: 'datatable', |
| 37 | + columns: [{ id: 'col1', name: 'columnOne', meta: { type: 'string' } }], |
| 38 | + rows: [{ col1: 'value' }], |
| 39 | + }; |
| 40 | + if (multipleColumns) { |
| 41 | + layer1.columns.push({ id: 'col2', name: 'columnTwo', meta: { type: 'number' } }); |
| 42 | + layer1.rows[0].col2 = 5; |
| 43 | + } |
| 44 | + return layer1; |
| 45 | +} |
| 46 | + |
| 47 | +describe('CSV exporter', () => { |
| 48 | + test('should not break with empty data', () => { |
| 49 | + expect( |
| 50 | + datatableToCSV({ type: 'datatable', columns: [], rows: [] }, getDefaultOptions()) |
| 51 | + ).toMatch(''); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should export formatted values by default', () => { |
| 55 | + expect(datatableToCSV(getDataTable(), getDefaultOptions())).toMatch( |
| 56 | + 'columnOne\r\n"Formatted_value"\r\n' |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should not quote values when requested', () => { |
| 61 | + return expect( |
| 62 | + datatableToCSV(getDataTable(), { ...getDefaultOptions(), quoteValues: false }) |
| 63 | + ).toMatch('columnOne\r\nFormatted_value\r\n'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should use raw values when requested', () => { |
| 67 | + expect(datatableToCSV(getDataTable(), { ...getDefaultOptions(), raw: true })).toMatch( |
| 68 | + 'columnOne\r\nvalue\r\n' |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should use separator for multiple columns', () => { |
| 73 | + expect(datatableToCSV(getDataTable({ multipleColumns: true }), getDefaultOptions())).toMatch( |
| 74 | + 'columnOne,columnTwo\r\n"Formatted_value","Formatted_5"\r\n' |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('should escape values', () => { |
| 79 | + const datatable = getDataTable(); |
| 80 | + datatable.rows[0].col1 = '"value"'; |
| 81 | + expect(datatableToCSV(datatable, getDefaultOptions())).toMatch( |
| 82 | + 'columnOne\r\n"Formatted_""value"""\r\n' |
| 83 | + ); |
| 84 | + }); |
| 85 | +}); |
0 commit comments