From 1e816c3f2bf8dbe70a3a8b60c29ca26f08cb23e1 Mon Sep 17 00:00:00 2001 From: Chun-MingChen Date: Sun, 8 Oct 2017 16:30:46 +0800 Subject: [PATCH] require mock data from factory for each unit test --- .../react-bootstrap-table2/test/body.test.js | 51 ++++--------------- .../test/bootstrap-table.test.js | 45 ++++------------ .../react-bootstrap-table2/test/cell.test.js | 51 +++++-------------- .../test/editing-cell.test.js | 11 ++-- .../test/factory/resolved-props.js | 25 +-------- .../test/header-cell.test.js | 16 +++--- .../test/header.test.js | 10 +--- .../test/props-resolver/index.test.js | 21 ++------ .../react-bootstrap-table2/test/row.test.js | 19 ++----- .../test/store/base.test.js | 5 +- 10 files changed, 59 insertions(+), 195 deletions(-) diff --git a/packages/react-bootstrap-table2/test/body.test.js b/packages/react-bootstrap-table2/test/body.test.js index 554641c..63beffd 100644 --- a/packages/react-bootstrap-table2/test/body.test.js +++ b/packages/react-bootstrap-table2/test/body.test.js @@ -6,31 +6,18 @@ import Body from 'src/body'; import Row from 'src/row'; import Const from 'src/const'; import RowSection from 'src/row-section'; -import { bodyResolvedProps } from 'test/factory'; +import { baseColumns, baseData, baseProps, bodyResolvedProps } from 'test/factory'; +const columns = baseColumns(); +const data = baseData(); const mockBodyResolvedProps = bodyResolvedProps(); describe('Body', () => { let wrapper; - const columns = [{ - dataField: 'id', - text: 'ID' - }, { - dataField: 'name', - text: 'Name' - }]; - - const data = [{ - id: 1, - name: 'A' - }, { - id: 2, - name: 'B' - }]; describe('simplest body', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should render successfully', () => { @@ -44,10 +31,8 @@ describe('Body', () => { beforeEach(() => { wrapper = shallow( ); @@ -69,10 +54,8 @@ describe('Body', () => { emptyIndication = 'Table is empty'; wrapper = shallow( { emptyIndicationCallBack = sinon.stub().returns(content); wrapper = shallow( { beforeEach(() => { wrapper = shallow( ); @@ -152,16 +131,13 @@ describe('Body', () => { }); describe('when selectRow.mode is checkbox or radio (row was selectable)', () => { - const keyField = 'id'; const selectRow = { mode: 'checkbox' }; it('props selected should be true if all rows were selected', () => { wrapper = shallow( @@ -173,10 +149,8 @@ describe('Body', () => { it('props selected should be false if all rows were not selected', () => { wrapper = shallow( @@ -188,13 +162,10 @@ describe('Body', () => { describe('when selectRow.mode is ROW_SELECT_DISABLED (row was un-selectable)', () => { beforeEach(() => { - const keyField = 'id'; wrapper = shallow( ); diff --git a/packages/react-bootstrap-table2/test/bootstrap-table.test.js b/packages/react-bootstrap-table2/test/bootstrap-table.test.js index 332c097..844acea 100644 --- a/packages/react-bootstrap-table2/test/bootstrap-table.test.js +++ b/packages/react-bootstrap-table2/test/bootstrap-table.test.js @@ -7,28 +7,15 @@ import BootstrapTable from 'src/bootstrap-table'; import Header from 'src/header'; import Body from 'src/body'; import Const from 'src/const'; +import { baseData, baseProps } from 'test/factory'; describe('BootstrapTable', () => { let wrapper; - const columns = [{ - dataField: 'id', - text: 'ID' - }, { - dataField: 'name', - text: 'Name' - }]; - - const data = [{ - id: 1, - name: 'A' - }, { - id: 2, - name: 'B' - }]; + const data = baseData(); describe('simplest table', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should render successfully', () => { @@ -52,7 +39,7 @@ describe('BootstrapTable', () => { describe('when hover props is true', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should have table-hover class on table', () => { @@ -62,7 +49,7 @@ describe('BootstrapTable', () => { describe('when striped props is true', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should have table-striped class on table', () => { @@ -72,7 +59,7 @@ describe('BootstrapTable', () => { describe('when condensed props is true', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should have table-condensed class on table', () => { @@ -82,7 +69,7 @@ describe('BootstrapTable', () => { describe('when bordered props is false', () => { beforeEach(() => { - wrapper = shallow(); + wrapper = shallow(); }); it('should not have table-condensed class on table', () => { @@ -120,9 +107,7 @@ describe('BootstrapTable', () => { beforeEach(() => { wrapper = shallow( @@ -148,9 +133,7 @@ describe('BootstrapTable', () => { beforeEach(() => { wrapper = shallow( ); @@ -169,9 +152,7 @@ describe('BootstrapTable', () => { beforeEach(() => { wrapper = shallow( ); @@ -197,11 +178,7 @@ describe('BootstrapTable', () => { describe('handleAllRowsSelect', () => { beforeEach(() => { wrapper = shallow( - + ); }); diff --git a/packages/react-bootstrap-table2/test/cell.test.js b/packages/react-bootstrap-table2/test/cell.test.js index 4b5d43c..8c641f3 100644 --- a/packages/react-bootstrap-table2/test/cell.test.js +++ b/packages/react-bootstrap-table2/test/cell.test.js @@ -5,18 +5,14 @@ import { shallow } from 'enzyme'; import Const from 'src/const'; import Cell from 'src/cell'; +import { baseColumn, baseRow } from 'test/factory'; + describe('Cell', () => { let wrapper; - const row = { - id: 1, - name: 'A' - }; + const row = baseRow(); describe('simplest cell', () => { - const column = { - dataField: 'id', - text: 'ID' - }; + const column = baseColumn(); beforeEach(() => { wrapper = shallow(); @@ -30,8 +26,7 @@ describe('Cell', () => { describe('when column.hidden prop is true', () => { const column = { - dataField: 'id', - text: 'ID', + ...baseColumn(), hidden: true }; @@ -49,8 +44,7 @@ describe('Cell', () => { describe('when column.formatter prop is defined', () => { const rowIndex = 1; const column = { - dataField: 'id', - text: 'ID', + ...baseColumn(), formatExtraData: [] }; const formatterResult = (

{ row[column.dataField] }

); @@ -85,10 +79,7 @@ describe('Cell', () => { const rowIndex = 1; beforeEach(() => { - column = { - dataField: 'id', - text: 'ID' - }; + column = baseColumn(); }); describe('when style is an object', () => { @@ -139,10 +130,7 @@ describe('Cell', () => { const rowIndex = 1; beforeEach(() => { - column = { - dataField: 'id', - text: 'ID' - }; + column = baseColumn(); }); describe('when classes is an object', () => { @@ -193,10 +181,7 @@ describe('Cell', () => { const rowIndex = 1; beforeEach(() => { - column = { - dataField: 'id', - text: 'ID' - }; + column = baseColumn(); }); describe('when title is boolean', () => { @@ -246,8 +231,7 @@ describe('Cell', () => { beforeEach(() => { column = { - dataField: 'id', - text: 'ID', + ...baseColumn(), events: { onClick: sinon.stub() } @@ -274,10 +258,7 @@ describe('Cell', () => { const rowIndex = 1; beforeEach(() => { - column = { - dataField: 'id', - text: 'ID' - }; + column = baseColumn(); }); describe('when align is string', () => { @@ -326,10 +307,7 @@ describe('Cell', () => { const rowIndex = 1; beforeEach(() => { - column = { - dataField: 'id', - text: 'ID' - }; + column = baseColumn(); }); describe('when attrs is an object', () => { @@ -453,10 +431,7 @@ describe('Cell', () => { let onStartCallBack; const rowIndex = 1; const columnIndex = 1; - const column = { - dataField: 'id', - text: 'ID' - }; + const column = baseColumn(); beforeEach(() => { onStartCallBack = sinon.stub().withArgs(rowIndex, columnIndex); diff --git a/packages/react-bootstrap-table2/test/editing-cell.test.js b/packages/react-bootstrap-table2/test/editing-cell.test.js index 6394f4f..f0296f2 100644 --- a/packages/react-bootstrap-table2/test/editing-cell.test.js +++ b/packages/react-bootstrap-table2/test/editing-cell.test.js @@ -8,20 +8,15 @@ import TextEditor from 'src/text-editor'; import EditorIndicator from 'src/editor-indicator'; import { TableRowWrapper } from 'test/test-helpers/table-wrapper'; +import { baseColumn, baseRow } from 'test/factory'; describe('EditingCell', () => { let wrapper; let onEscape; let onComplete; - const row = { - id: 1, - name: 'A' - }; + const row = baseRow(); - let column = { - dataField: 'id', - text: 'ID' - }; + let column = baseColumn(); beforeEach(() => { onComplete = sinon.stub(); diff --git a/packages/react-bootstrap-table2/test/factory/resolved-props.js b/packages/react-bootstrap-table2/test/factory/resolved-props.js index bfdbadd..860ca2e 100644 --- a/packages/react-bootstrap-table2/test/factory/resolved-props.js +++ b/packages/react-bootstrap-table2/test/factory/resolved-props.js @@ -1,28 +1,5 @@ import BootstrapTable from 'src/bootstrap-table'; - -const columns = [{ - dataField: 'id', - text: 'ID' -}, { - dataField: 'name', - text: 'Name' -}]; - -const data = [{ - id: 1, - name: 'A' -}, { - id: 2, - name: 'B' -}]; - -const keyField = 'id'; - -const props = { - columns, - data, - keyField -}; +import { baseProps as props } from './base-props'; const bootstrapTable = new BootstrapTable(props); diff --git a/packages/react-bootstrap-table2/test/header-cell.test.js b/packages/react-bootstrap-table2/test/header-cell.test.js index e9f6b88..31dc66f 100644 --- a/packages/react-bootstrap-table2/test/header-cell.test.js +++ b/packages/react-bootstrap-table2/test/header-cell.test.js @@ -7,15 +7,14 @@ import SortCaret from 'src/sort-caret'; import SortSymbol from 'src/sort-symbol'; import HeaderCell from 'src/header-cell'; +import { baseColumn } from 'test/factory'; + describe('HeaderCell', () => { let wrapper; const index = 1; describe('simplest header cell', () => { - const column = { - dataField: 'id', - text: 'ID' - }; + const column = baseColumn(); beforeEach(() => { wrapper = shallow(); @@ -35,8 +34,7 @@ describe('HeaderCell', () => { describe('when column.hidden props is true', () => { const column = { - dataField: 'id', - text: 'ID', + ...baseColumn(), hidden: true }; @@ -142,10 +140,8 @@ describe('HeaderCell', () => { }); describe('when column.headerFormatter prop is defined', () => { - const column = { - dataField: 'id', - text: 'ID' - }; + const column = baseColumn(); + const formatterResult = (

{ column.text }

); const formatter = sinon.stub() .withArgs(column, index) diff --git a/packages/react-bootstrap-table2/test/header.test.js b/packages/react-bootstrap-table2/test/header.test.js index e751209..93f2c6a 100644 --- a/packages/react-bootstrap-table2/test/header.test.js +++ b/packages/react-bootstrap-table2/test/header.test.js @@ -5,19 +5,13 @@ import HeaderCell from 'src/header-cell'; import SelectionHeaderCell from 'src/row-selection/selection-header-cell'; import Header from 'src/header'; import Const from 'src/const'; -import { headerResolvedProps } from 'test/factory'; +import { headerResolvedProps, baseColumns } from 'test/factory'; const mockHeaderResolvedProps = headerResolvedProps(); describe('Header', () => { let wrapper; - const columns = [{ - dataField: 'id', - text: 'ID' - }, { - dataField: 'name', - text: 'Name' - }]; + const columns = baseColumns(); describe('simplest header', () => { beforeEach(() => { diff --git a/packages/react-bootstrap-table2/test/props-resolver/index.test.js b/packages/react-bootstrap-table2/test/props-resolver/index.test.js index dec588a..a39724b 100644 --- a/packages/react-bootstrap-table2/test/props-resolver/index.test.js +++ b/packages/react-bootstrap-table2/test/props-resolver/index.test.js @@ -5,24 +5,13 @@ import { shallow } from 'enzyme'; import baseResolver from 'src/props-resolver/index'; import Const from 'src/const'; import { extendTo } from 'test/test-helpers/mock-component'; +import { baseColumns, baseData, baseKeyField } from 'test/factory'; + +const columns = baseColumns(); +const data = baseData(); +const keyField = baseKeyField(); describe('TableResolver', () => { - const keyField = 'id'; - const columns = [{ - dataField: keyField, - text: 'ID' - }, { - dataField: 'name', - text: 'Name' - }]; - const data = [{ - id: 1, - name: 'A' - }, { - id: 2, - name: 'B' - }]; - const ExtendBase = baseResolver(Component); const BootstrapTableMock = extendTo(ExtendBase); let wrapper; diff --git a/packages/react-bootstrap-table2/test/row.test.js b/packages/react-bootstrap-table2/test/row.test.js index 7eec0f0..81afedd 100644 --- a/packages/react-bootstrap-table2/test/row.test.js +++ b/packages/react-bootstrap-table2/test/row.test.js @@ -7,29 +7,16 @@ import Row from 'src/row'; import Const from 'src/const'; import EditingCell from 'src/editing-cell'; import SelectionCell from 'src/row-selection/selection-cell'; -import { bodyResolvedProps } from 'test/factory'; +import { bodyResolvedProps, baseColumns, baseRow } from 'test/factory'; const mockBodyResolvedProps = bodyResolvedProps(); -const defaultColumns = [{ - dataField: 'id', - text: 'ID' -}, { - dataField: 'name', - text: 'Name' -}, { - dataField: 'price', - text: 'Price' -}]; +const defaultColumns = baseColumns(); describe('Row', () => { let wrapper; - const row = { - id: 1, - name: 'A', - price: 1000 - }; + const row = baseRow(); describe('simplest row', () => { beforeEach(() => { diff --git a/packages/react-bootstrap-table2/test/store/base.test.js b/packages/react-bootstrap-table2/test/store/base.test.js index c7e7052..f4cc152 100644 --- a/packages/react-bootstrap-table2/test/store/base.test.js +++ b/packages/react-bootstrap-table2/test/store/base.test.js @@ -1,6 +1,9 @@ import Base from 'src/store/base'; import Const from 'src/const'; import _ from 'src/utils'; +import { baseKeyField } from 'test/factory'; + +const keyField = baseKeyField(); describe('Store Base', () => { let store; @@ -13,7 +16,7 @@ describe('Store Base', () => { { id: 4, name: '123tester' }, { id: 1, name: '!@#' } ]; - store = new Base({ data, keyField: 'id' }); + store = new Base({ data, keyField }); }); describe('initialize', () => {