diff --git a/package.json b/package.json index 795679c..da919a0 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "jest-babel": "^1.0.1", "lerna": "^2.0.0", "react-test-renderer": "^15.6.1", + "sinon": "^3.2.1", "webpack": "^3.5.4", "webpack-dev-server": "^2.7.1" }, diff --git a/packages/react-bootstrap-table2/src/body.js b/packages/react-bootstrap-table2/src/body.js index e9e968e..9b69cb9 100644 --- a/packages/react-bootstrap-table2/src/body.js +++ b/packages/react-bootstrap-table2/src/body.js @@ -6,8 +6,13 @@ import Row from './row'; const Body = ({ columns, data, keyField }) => ( { - data.map(row => ( - + data.map((row, index) => ( + )) } diff --git a/packages/react-bootstrap-table2/src/cell.js b/packages/react-bootstrap-table2/src/cell.js index 7a1c4f7..e5cf90e 100644 --- a/packages/react-bootstrap-table2/src/cell.js +++ b/packages/react-bootstrap-table2/src/cell.js @@ -2,12 +2,20 @@ import React from 'react'; import PropTypes from 'prop-types'; -const Cell = ({ value }) => ( - { value } -); +const Cell = ({ row, rowIndex, column }) => { + let content = row[column.dataField]; + if (column.formatter) { + content = column.formatter(content, row, rowIndex, column.formatExtraData); + } + return ( + { content } + ); +}; Cell.propTypes = { - value: PropTypes.any.isRequired + row: PropTypes.object.isRequired, + rowIndex: PropTypes.number.isRequired, + column: PropTypes.object.isRequired }; export default Cell; diff --git a/packages/react-bootstrap-table2/src/row.js b/packages/react-bootstrap-table2/src/row.js index 3feffd6..94a0aba 100644 --- a/packages/react-bootstrap-table2/src/row.js +++ b/packages/react-bootstrap-table2/src/row.js @@ -3,18 +3,25 @@ import PropTypes from 'prop-types'; import Cell from './cell'; -const Row = ({ row, columns }) => ( +const Row = ({ row, rowIndex, columns }) => ( { columns.map(column => - - ) + ( + + )) } ); Row.propTypes = { row: PropTypes.object.isRequired, + rowIndex: PropTypes.number.isRequired, columns: PropTypes.array.isRequired }; diff --git a/packages/react-bootstrap-table2/test/bootstrap-table.test.js b/packages/react-bootstrap-table2/test/bootstrap-table.test.js index 417074d..c712b72 100644 --- a/packages/react-bootstrap-table2/test/bootstrap-table.test.js +++ b/packages/react-bootstrap-table2/test/bootstrap-table.test.js @@ -41,7 +41,7 @@ describe('BootstrapTable', () => { }); }); - describe('when hover is true', () => { + describe('when hover props is true', () => { beforeEach(() => { wrapper = shallow(); }); @@ -51,7 +51,7 @@ describe('BootstrapTable', () => { }); }); - describe('when striped is true', () => { + describe('when striped props is true', () => { beforeEach(() => { wrapper = shallow(); }); @@ -61,7 +61,7 @@ describe('BootstrapTable', () => { }); }); - describe('when condensed is true', () => { + describe('when condensed props is true', () => { beforeEach(() => { wrapper = shallow(); }); @@ -71,7 +71,7 @@ describe('BootstrapTable', () => { }); }); - describe('when bordered is false', () => { + describe('when bordered props is false', () => { beforeEach(() => { wrapper = shallow(); }); diff --git a/packages/react-bootstrap-table2/test/cell.test.js b/packages/react-bootstrap-table2/test/cell.test.js index 078e5d8..0649ca5 100644 --- a/packages/react-bootstrap-table2/test/cell.test.js +++ b/packages/react-bootstrap-table2/test/cell.test.js @@ -1,20 +1,60 @@ import React from 'react'; +import sinon from 'sinon'; import { shallow } from 'enzyme'; import Cell from '../src/cell'; describe('Cell', () => { let wrapper; - const value = 'test'; + const row = { + id: 1, + name: 'A' + }; describe('simplest cell', () => { + const column = { + dataField: 'id', + text: 'ID' + }; + beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should render successfully', () => { expect(wrapper.length).toBe(1); - expect(wrapper.contains({ value })).toBe(true); + expect(wrapper.contains({ row[column.dataField] })).toBe(true); + }); + }); + + describe('when formatter prop is defined', () => { + const rowIndex = 1; + const column = { + dataField: 'id', + text: 'ID', + formatExtraData: [] + }; + const formatterResult = (

{ row[column.dataField] }

); + const formatter = sinon.stub() + .withArgs(row[column.dataField], row, rowIndex, column.formatExtraData) + .returns(formatterResult); + column.formatter = formatter; // defined column formatter + + beforeEach(() => { + wrapper = shallow(); + }); + + afterEach(() => { formatter.reset(); }); + + it('should render successfully', () => { + expect(wrapper.length).toBe(1); + expect(wrapper.contains(

{ row[column.dataField] }

)).toBe(true); + }); + + it('should call custom formatter correctly', () => { + expect(formatter.callCount).toBe(1); + expect(formatter.calledWith(row[column.dataField], + row, rowIndex, column.formatExtraData)).toBe(true); }); }); }); diff --git a/packages/react-bootstrap-table2/test/row.test.js b/packages/react-bootstrap-table2/test/row.test.js index dfe3f4b..0b111cc 100644 --- a/packages/react-bootstrap-table2/test/row.test.js +++ b/packages/react-bootstrap-table2/test/row.test.js @@ -21,7 +21,7 @@ describe('Row', () => { describe('simplest row', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should render successfully', () => {