Skip to content

Commit

Permalink
Add tests and refactor to support tests
Browse files Browse the repository at this point in the history
Inspired by work done by @Skn0tt in #929
  • Loading branch information
gabrielliwerant committed Nov 15, 2019
1 parent 0aec5e0 commit 2840759
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/components/TableToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import FilterIcon from '@material-ui/icons/FilterList';
import ReactToPrint from 'react-to-print';
import find from 'lodash.find';
import { withStyles } from '@material-ui/core/styles';
import { buildCSV } from '../utils';
import { createCSVDownload, downloadCSV } from '../utils';
import cloneDeep from 'lodash.clonedeep';

export const defaultToolbarStyles = theme => ({
Expand Down Expand Up @@ -131,7 +131,7 @@ class TableToolbar extends React.Component {
});
}
}
buildCSV(columnsToDownload, dataToDownload, options);
createCSVDownload(columnsToDownload, dataToDownload, options, downloadCSV);
};

setActiveIcon = iconName => {
Expand Down
20 changes: 13 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,10 @@ function buildCSV(columns, data, options) {
? options.onDownload(buildHead, buildBody, columns, data)
: `${CSVHead}${CSVBody}`.trim();

if (options.onDownload && csv === false) {
return;
}

createCSVDownload(csv, options.downloadOptions.filename);
return csv;
}

function createCSVDownload(csv, filename) {
function downloadCSV(csv, filename) {
const blob = new Blob([csv], { type: 'text/csv' });

/* taken from react-csv */
Expand All @@ -102,4 +98,14 @@ function createCSVDownload(csv, filename) {
}
}

export { buildMap, getPageValue, getCollatorComparator, sortCompare, createCSVDownload, buildCSV };
function createCSVDownload(columns, data, options, downloadCSV) {
const csv = buildCSV(columns, data, options);

if (options.onDownload && csv === false) {
return;
}

downloadCSV(csv, options.downloadOptions.filename);
}

export { buildMap, getPageValue, getCollatorComparator, sortCompare, createCSVDownload, buildCSV, downloadCSV };
94 changes: 93 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getPageValue } from '../src/utils';
import { getPageValue, buildCSV, createCSVDownload, downloadCSV } from '../src/utils';
import { spy } from 'sinon';
import { assert } from 'chai';

describe('utils.js', () => {
Expand Down Expand Up @@ -57,4 +58,95 @@ describe('utils.js', () => {
assert.strictEqual(actualResult, 0);
});
});

describe('buildCSV', () => {
const options = {
downloadOptions: {
separator: ';',
},
onDownload: null,
};
const columns = [
{
name: 'firstname',
download: true,
},
{
name: 'lastname',
download: true,
},
];

it('properly builds a csv when given a non-empty dataset', () => {
const data = [
{ data: ['anton', 'abraham'] },
{ data: ['berta', 'buchel'] }
];
const csv = buildCSV(columns, data, options);

assert.strictEqual(csv, '"firstname";"lastname"\r\n' + '"anton";"abraham"\r\n' + '"berta";"buchel"');
});

it('returns an empty csv with header when given an empty dataset', () => {
const columns = [
{
name: 'firstname',
download: true,
},
{
name: 'lastname',
download: true,
},
];
const data = [];
const csv = buildCSV(columns, data, options);

assert.strictEqual(csv, '"firstname";"lastname"');
});
});

describe('createCSVDownload', () => {
const columns = [
{
name: 'firstname',
download: true,
},
{
name: 'lastname',
download: true,
},
];
const data = [
{ data: ['anton', 'abraham'] },
{ data: ['berta', 'buchel'] }
];

it("does not call download function if download callback returns `false`", () => {
const options = {
downloadOptions: {
separator: ";"
},
onDownload: () => false
};
const downloadCSV = spy();

createCSVDownload(columns, data, options, downloadCSV);

assert.strictEqual(downloadCSV.callCount, 0);
});

it("calls download function if download callback returns truthy", () => {
const options = {
downloadOptions: {
separator: ";"
},
onDownload: () => true
};
const downloadCSV = spy();

createCSVDownload(columns, data, options, downloadCSV);

assert.strictEqual(downloadCSV.callCount, 1);
});
});
});

0 comments on commit 2840759

Please sign in to comment.