react-csv-downloader
Version:
React csv downloader
260 lines • 14.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable sonarjs/no-duplicate-string,sonarjs/no-identical-functions */
const chai_1 = require("chai");
const csv_1 = require("./csv");
const newLine = '\r\n';
const columnSet1 = [{ id: 'cell1' }];
const columnSet2 = [{ id: 'cell1' }, { id: 'cell2' }];
const columnSet3 = [{ id: 'cell1', displayName: 'Cell name' }];
const columnSet4 = [{ id: 'cell2' }, { id: 'cell1' }];
const dataSet1 = [{ cell1: 'row1' }];
const dataSet2 = [{ cell1: 'row1', cell2: 'row1' }];
const dataSet3 = [['cell1', 'cell2']];
const dataSet4 = [
['cell1', 'cell2'],
['cell1', 'cell2'],
];
const dataSet5 = [{ cell1: 'row1' }, { cell1: 'row2' }];
const dataSet6 = [{ cell1: 'row1' }, { cell1: null }, { cell1: 'row3' }];
const dataSet7 = [{ cell1: 'row1' }, { cell1: undefined }, { cell1: 'row3' }];
describe('CSV Creator', () => {
it('Should work with empty data', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: [], datas: [] });
(0, chai_1.expect)(result).to.equal(``);
}));
describe('Default separator', () => {
const separator = ',';
it('Single cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet1, datas: dataSet1 });
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1`);
}));
it('Multiple cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet2, datas: dataSet2 });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}row1${separator}row1`);
}));
it('Header display name', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet3, datas: dataSet1 });
(0, chai_1.expect)(result).to.equal(`Cell name${newLine}row1`);
}));
it('Ordered cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet4, datas: dataSet2 });
(0, chai_1.expect)(result).to.equal(`cell2${separator}cell1${newLine}row1${separator}row1`);
}));
it('No header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet1,
separator,
noHeader: true,
});
(0, chai_1.expect)(result).to.equal('row1');
}));
it('Auto header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet2 });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}row1${separator}row1`);
}));
it('array of array datas - single row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet3 });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2`);
}));
it('array of array datas - multiple row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet4 });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}cell1${separator}cell2`);
}));
it('array of array datas - with header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet4, datas: dataSet4 });
(0, chai_1.expect)(result).to.equal(`cell2${separator}cell1${newLine}cell1${separator}cell2${newLine}cell1${separator}cell2`);
}));
});
describe('Column Wrap', () => {
const separator = ',';
const wrapColumnChar = '"';
it('Single cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet1,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell1${wrapColumnChar}${newLine}${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('Multiple cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet2,
datas: dataSet2,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}${newLine}${wrapColumnChar}row1${wrapColumnChar}${separator}${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('Header display name', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet3,
datas: dataSet1,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}Cell name${wrapColumnChar}${newLine}${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('Ordered cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet4,
datas: dataSet2,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell2${wrapColumnChar}${separator}${wrapColumnChar}cell1${wrapColumnChar}${newLine}${wrapColumnChar}row1${wrapColumnChar}${separator}${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('No header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet1,
separator,
noHeader: true,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('Auto header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: false,
datas: dataSet2,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}${newLine}${wrapColumnChar}row1${wrapColumnChar}${separator}${wrapColumnChar}row1${wrapColumnChar}`);
}));
it('array of array datas - single row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: false,
datas: dataSet3,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}`);
}));
it('array of array datas - multiple row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: false,
datas: dataSet4,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}${newLine}${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}`);
}));
it('array of array datas - with header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet4,
datas: dataSet4,
wrapColumnChar,
});
(0, chai_1.expect)(result).to.equal(`${wrapColumnChar}cell2${wrapColumnChar}${separator}${wrapColumnChar}cell1${wrapColumnChar}${newLine}${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}${newLine}${wrapColumnChar}cell1${wrapColumnChar}${separator}${wrapColumnChar}cell2${wrapColumnChar}`);
}));
});
describe('Semicolon separator', () => {
const separator = ';';
it('Single cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet1,
separator,
});
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1`);
}));
it('Multiple cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet2,
datas: dataSet2,
separator,
});
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}row1${separator}row1`);
}));
it('Header display name', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet3,
datas: dataSet1,
separator,
});
(0, chai_1.expect)(result).to.equal(`Cell name${newLine}row1`);
}));
it('Ordered cell', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet4,
datas: dataSet2,
separator,
});
(0, chai_1.expect)(result).to.equal(`cell2${separator}cell1${newLine}row1${separator}row1`);
}));
it('No header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet1,
separator,
noHeader: true,
});
(0, chai_1.expect)(result).to.equal('row1');
}));
it('Auto header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet2, separator });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}row1${separator}row1`);
}));
it('array of array datas - single row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet3, separator });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2`);
}));
it('array of array datas - multiple row', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: false, datas: dataSet4, separator });
(0, chai_1.expect)(result).to.equal(`cell1${separator}cell2${newLine}cell1${separator}cell2`);
}));
it('array of array datas - with header', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet4,
datas: dataSet4,
separator,
});
(0, chai_1.expect)(result).to.equal(`cell2${separator}cell1${newLine}cell1${separator}cell2${newLine}cell1${separator}cell2`);
}));
});
describe('New line at end', () => {
it('should not insert new line at end', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet5,
newLineAtEnd: false,
});
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1${newLine}row2`);
}));
it('should insert new line at end', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet5,
newLineAtEnd: true,
});
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1${newLine}row2${newLine}`);
}));
});
describe('Should process chunks', () => {
it('should process each line as a chunk', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({
columns: columnSet1,
datas: dataSet5,
chunkSize: 1,
});
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1${newLine}row2`);
}));
});
describe('Nulls and undefineds', () => {
it('should convert null to empty field', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet1, datas: dataSet6 });
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1${newLine}${newLine}row3`);
}));
it('should convert null to empty field', () => __awaiter(void 0, void 0, void 0, function* () {
const result = yield (0, csv_1.default)({ columns: columnSet1, datas: dataSet7 });
(0, chai_1.expect)(result).to.equal(`cell1${newLine}row1${newLine}${newLine}row3`);
}));
});
});
//# sourceMappingURL=csv.spec.js.map