From 28407599b84421e81224358be8d8e69a26d2c6af Mon Sep 17 00:00:00 2001 From: Gabriel Liwerant Date: Thu, 14 Nov 2019 21:43:09 -0500 Subject: [PATCH] Add tests and refactor to support tests Inspired by work done by @Skn0tt in #929 --- src/components/TableToolbar.js | 4 +- src/utils.js | 20 +++++--- test/utils.test.js | 94 +++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 10 deletions(-) diff --git a/src/components/TableToolbar.js b/src/components/TableToolbar.js index 40303cf0a..c26be05b2 100644 --- a/src/components/TableToolbar.js +++ b/src/components/TableToolbar.js @@ -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 => ({ @@ -131,7 +131,7 @@ class TableToolbar extends React.Component { }); } } - buildCSV(columnsToDownload, dataToDownload, options); + createCSVDownload(columnsToDownload, dataToDownload, options, downloadCSV); }; setActiveIcon = iconName => { diff --git a/src/utils.js b/src/utils.js index 7335da60a..3eb2c4605 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 */ @@ -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 }; diff --git a/test/utils.test.js b/test/utils.test.js index 0d2311aac..be5e26bc2 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -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', () => { @@ -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); + }); + }); });