From 7b0be310d1602329b19093a1fd223177d36028e1 Mon Sep 17 00:00:00 2001 From: Patrick Gillespie Date: Sun, 8 Sep 2019 02:08:41 -0400 Subject: [PATCH] New test --- examples/csv-export/index.js | 2 +- src/utils.js | 15 +++++------ test/MUIDataTableBodyCell.test.js | 43 +++++++++++++++++++++++++++++++ test/MUIDataTableHeadCell.test.js | 6 +++-- 4 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 test/MUIDataTableBodyCell.test.js diff --git a/examples/csv-export/index.js b/examples/csv-export/index.js index 7915f74..99a67d2 100644 --- a/examples/csv-export/index.js +++ b/examples/csv-export/index.js @@ -73,7 +73,7 @@ class Example extends React.Component { rowsPerPage: 10, downloadOptions: { filename: 'excel-format.csv', - separator: ';', + separator: ',', filterOptions: { useDisplayedColumnsOnly: true, useDisplayedRowsOnly: true, diff --git a/src/utils.js b/src/utils.js index e376eb1..fe82b46 100644 --- a/src/utils.js +++ b/src/utils.js @@ -27,19 +27,16 @@ function sortCompare(order) { } function createCSVDownload(columns, data, options) { - const replaceDoubleQuoteInString = columnData => - typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData; + const replaceDoubleQuoteInString = columnData => { + return typeof columnData === 'string' ? columnData.replace(/\"/g, '""') : columnData; + }; const buildHead = columns => { return ( columns - .reduce( - (soFar, column) => - column.download - ? soFar + '"' + replaceDoubleQuoteInString(column.name) + '"' + options.downloadOptions.separator - : soFar, - '', - ) + .reduce((soFar, column) => { + return column.download ? soFar + '"' + replaceDoubleQuoteInString(column.name) + '"' + options.downloadOptions.separator : soFar; + }, '') .slice(0, -1) + '\r\n' ); }; diff --git a/test/MUIDataTableBodyCell.test.js b/test/MUIDataTableBodyCell.test.js new file mode 100644 index 0000000..0edf929 --- /dev/null +++ b/test/MUIDataTableBodyCell.test.js @@ -0,0 +1,43 @@ +import React from 'react'; +import { spy, stub } from 'sinon'; +import { mount, shallow } from 'enzyme'; +import { assert, expect, should } from 'chai'; +import textLabels from '../src/textLabels'; +import TableBodyCell from '../src/components/TableBodyCell'; + +describe('', function() { + let classes; + + before(() => { + classes = { + root: {}, + }; + }); + + it('should trigger onCellClick prop callback when clicking cell', () => { + const options = { sort: true, textLabels, onCellClick: spy() }; + + const fullWrapper = mount( + + some content + , + ); + + // The test should click on both TDs that are rendered, but only 1 onCellClick + // should fire because the first TD represents the column header (due to responsive design). + fullWrapper.find('td').forEach(item => { + item.simulate('click'); + }); + + assert.strictEqual(options.onCellClick.callCount, 1); + + }); +}); diff --git a/test/MUIDataTableHeadCell.test.js b/test/MUIDataTableHeadCell.test.js index 3870282..50b2f98 100644 --- a/test/MUIDataTableHeadCell.test.js +++ b/test/MUIDataTableHeadCell.test.js @@ -70,7 +70,7 @@ describe('', function() { assert.strictEqual(actualResult.length, 0); }); - it('should trigger toggleSort prop callback when calling method handleSortClick', () => { + it('should trigger toggleSort prop callback when clicking or pressing enter on column label', () => { const options = { sort: true, textLabels }; const toggleSort = spy(); @@ -87,7 +87,9 @@ describe('', function() { ); fullWrapper.find('span[data-column-label="true"]').simulate('click'); - assert.strictEqual(toggleSort.callCount, 1); + + fullWrapper.find('span[data-column-label="true"]').simulate('keyUp', {key: 'Enter'}); + assert.strictEqual(toggleSort.callCount, 2); }); });