require mock data from factory for each unit test

This commit is contained in:
Chun-MingChen
2017-10-08 16:30:46 +08:00
parent 2a7ec721d7
commit 1e816c3f2b
10 changed files with 59 additions and 195 deletions

View File

@@ -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(<Body {...mockBodyResolvedProps} keyField="id" columns={ columns } data={ data } />);
wrapper = shallow(<Body {...baseProps} {...mockBodyResolvedProps} />);
});
it('should render successfully', () => {
@@ -44,10 +31,8 @@ describe('Body', () => {
beforeEach(() => {
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
keyField="id"
columns={ columns }
data={ data }
visibleColumnSize={ columns.length }
isEmpty
/>);
@@ -69,10 +54,8 @@ describe('Body', () => {
emptyIndication = 'Table is empty';
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
keyField="id"
columns={ columns }
data={ data }
visibleColumnSize={ columns.length }
noDataIndication={ emptyIndication }
isEmpty
@@ -95,10 +78,8 @@ describe('Body', () => {
emptyIndicationCallBack = sinon.stub().returns(content);
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
keyField="id"
columns={ columns }
data={ data }
visibleColumnSize={ columns.length }
noDataIndication={ emptyIndicationCallBack }
isEmpty
@@ -129,10 +110,8 @@ describe('Body', () => {
beforeEach(() => {
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
data={ data }
columns={ columns }
keyField={ keyField }
cellEdit={ cellEdit }
/>
);
@@ -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(
<Body
{...baseProps}
{...mockBodyResolvedProps}
data={ data }
columns={ columns }
keyField={ keyField }
selectedRowKeys={[1, 2]}
selectRow={selectRow}
/>
@@ -173,10 +149,8 @@ describe('Body', () => {
it('props selected should be false if all rows were not selected', () => {
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
data={ data }
columns={ columns }
keyField={ keyField }
selectedRowKeys={[]}
selectRow={selectRow}
/>
@@ -188,13 +162,10 @@ describe('Body', () => {
describe('when selectRow.mode is ROW_SELECT_DISABLED (row was un-selectable)', () => {
beforeEach(() => {
const keyField = 'id';
wrapper = shallow(
<Body
{...baseProps}
{...mockBodyResolvedProps}
data={ data }
columns={ columns }
keyField={ keyField }
selectedRowKeys={[]}
/>
);

View File

@@ -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(<BootstrapTable keyField="id" columns={ columns } data={ data } />);
wrapper = shallow(<BootstrapTable {...baseProps} />);
});
it('should render successfully', () => {
@@ -52,7 +39,7 @@ describe('BootstrapTable', () => {
describe('when hover props is true', () => {
beforeEach(() => {
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } hover />);
wrapper = shallow(<BootstrapTable {...baseProps} hover />);
});
it('should have table-hover class on table', () => {
@@ -62,7 +49,7 @@ describe('BootstrapTable', () => {
describe('when striped props is true', () => {
beforeEach(() => {
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } striped />);
wrapper = shallow(<BootstrapTable {...baseProps} striped />);
});
it('should have table-striped class on table', () => {
@@ -72,7 +59,7 @@ describe('BootstrapTable', () => {
describe('when condensed props is true', () => {
beforeEach(() => {
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } condensed />);
wrapper = shallow(<BootstrapTable {...baseProps} condensed />);
});
it('should have table-condensed class on table', () => {
@@ -82,7 +69,7 @@ describe('BootstrapTable', () => {
describe('when bordered props is false', () => {
beforeEach(() => {
wrapper = shallow(<BootstrapTable keyField="id" columns={ columns } data={ data } bordered={ false } />);
wrapper = shallow(<BootstrapTable {...baseProps} bordered={ false } />);
});
it('should not have table-condensed class on table', () => {
@@ -120,9 +107,7 @@ describe('BootstrapTable', () => {
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField="id"
columns={ columns }
data={ data }
{...baseProps}
bordered={ false }
cellEdit={ cellEdit }
/>
@@ -148,9 +133,7 @@ describe('BootstrapTable', () => {
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField="id"
columns={ columns }
data={ data }
{...baseProps}
selectRow={{ mode: 'radio' }}
/>
);
@@ -169,9 +152,7 @@ describe('BootstrapTable', () => {
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField="id"
columns={ columns }
data={ data }
{...baseProps}
selectRow={{ mode: 'checkbox' }}
/>
);
@@ -197,11 +178,7 @@ describe('BootstrapTable', () => {
describe('handleAllRowsSelect', () => {
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField="id"
columns={ columns }
data={ data }
/>
<BootstrapTable {...baseProps} />
);
});

View File

@@ -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(<Cell row={ row } columnIndex={ 1 } rowIndex={ 1 } column={ column } />);
@@ -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 = (<h3>{ row[column.dataField] }</h3>);
@@ -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);

View File

@@ -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();

View File

@@ -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);

View File

@@ -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(<HeaderCell column={ column } index={ index } />);
@@ -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 = (<h3>{ column.text }</h3>);
const formatter = sinon.stub()
.withArgs(column, index)

View File

@@ -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(() => {

View File

@@ -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;

View File

@@ -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(() => {

View File

@@ -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', () => {