patch tests for remote pagination

This commit is contained in:
AllenFang 2017-12-02 15:49:27 +08:00
parent f9abcf42f4
commit 28f1bdb49f
3 changed files with 260 additions and 86 deletions

View File

@ -39,15 +39,21 @@ describe('Wrapper', () => {
const pureTable = props => (<BootstrapTable { ...props } />);
const createPaginationWrapper = (props, renderFragment = true) => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
if (renderFragment) {
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
}
};
describe('default pagination', () => {
const props = createTableProps();
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering correctly', () => {
@ -92,15 +98,64 @@ describe('Wrapper', () => {
});
});
describe('when options.page is defined', () => {
const page = 3;
const props = createTableProps({ options: { page } });
beforeEach(() => {
createPaginationWrapper(props);
});
it('should setting correct state.currPage', () => {
expect(instance.state.currPage).toEqual(page);
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(wrapper.length).toBe(1);
expect(pagination.length).toBe(1);
expect(pagination.prop('currPage')).toEqual(page);
});
});
describe('when options.sizePerPage is defined', () => {
const sizePerPage = 30;
const props = createTableProps({ options: { sizePerPage } });
beforeEach(() => {
createPaginationWrapper(props);
});
it('should setting correct state.currPage', () => {
expect(instance.state.currSizePerPage).toEqual(sizePerPage);
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(wrapper.length).toBe(1);
expect(pagination.length).toBe(1);
expect(pagination.prop('currSizePerPage')).toEqual(sizePerPage);
});
});
describe('when options.totalSize is defined', () => {
const totalSize = 100;
const props = createTableProps({ options: { totalSize } });
beforeEach(() => {
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(wrapper.length).toBe(1);
expect(pagination.length).toBe(1);
expect(pagination.prop('dataSize')).toEqual(totalSize);
});
});
describe('when options.pageStartIndex is defined', () => {
const pageStartIndex = -1;
const props = createTableProps({ options: { pageStartIndex } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should setting correct state.currPage', () => {
@ -119,11 +174,7 @@ describe('Wrapper', () => {
const sizePerPageList = [10, 40];
const props = createTableProps({ options: { sizePerPageList } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -138,11 +189,7 @@ describe('Wrapper', () => {
const paginationSize = 10;
const props = createTableProps({ options: { paginationSize } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -157,11 +204,7 @@ describe('Wrapper', () => {
const withFirstAndLast = false;
const props = createTableProps({ options: { withFirstAndLast } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -176,11 +219,7 @@ describe('Wrapper', () => {
const alwaysShowAllBtns = true;
const props = createTableProps({ options: { alwaysShowAllBtns } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -195,11 +234,7 @@ describe('Wrapper', () => {
const firstPageText = '1st';
const props = createTableProps({ options: { firstPageText } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -214,11 +249,7 @@ describe('Wrapper', () => {
const prePageText = 'PRE';
const props = createTableProps({ options: { prePageText } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -233,11 +264,7 @@ describe('Wrapper', () => {
const nextPageText = 'NEXT';
const props = createTableProps({ options: { nextPageText } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -252,11 +279,7 @@ describe('Wrapper', () => {
const lastPageText = 'nth';
const props = createTableProps({ options: { lastPageText } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -271,11 +294,7 @@ describe('Wrapper', () => {
const firstPageTitle = '1st';
const props = createTableProps({ options: { firstPageTitle } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -290,11 +309,7 @@ describe('Wrapper', () => {
const prePageTitle = 'PRE';
const props = createTableProps({ options: { prePageTitle } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -309,11 +324,7 @@ describe('Wrapper', () => {
const nextPageTitle = 'NEXT';
const props = createTableProps({ options: { nextPageTitle } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -328,11 +339,7 @@ describe('Wrapper', () => {
const lastPageTitle = 'nth';
const props = createTableProps({ options: { lastPageTitle } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -347,11 +354,7 @@ describe('Wrapper', () => {
const hideSizePerPage = true;
const props = createTableProps({ options: { hideSizePerPage } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -366,11 +369,7 @@ describe('Wrapper', () => {
const hidePageListOnlyOnePage = true;
const props = createTableProps({ options: { hidePageListOnlyOnePage } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
const fragment = instance.render();
wrapper = shallow(<div>{ fragment }</div>);
createPaginationWrapper(props);
});
it('should rendering Pagination correctly', () => {
@ -385,9 +384,7 @@ describe('Wrapper', () => {
const newPage = 3;
const props = createTableProps({ options: { onPageChange: sinon.stub() } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
createPaginationWrapper(props, false);
instance.handleChangePage(newPage);
});
@ -404,6 +401,25 @@ describe('Wrapper', () => {
expect(onPageChange.calledOnce).toBeTruthy();
expect(onPageChange.calledWith(newPage, instance.state.currSizePerPage)).toBeTruthy();
});
describe('when pagination remote is enable', () => {
beforeEach(() => {
props.remote = true;
props.onRemotePageChange = sinon.stub();
createPaginationWrapper(props, false);
instance.handleChangePage(newPage);
});
it('should not setting state.currPage', () => {
expect(instance.state.currPage).not.toEqual(newPage);
});
it('should calling options.onRemotePageChange correctly', () => {
expect(props.onRemotePageChange.calledOnce).toBeTruthy();
expect(props.onRemotePageChange.calledWith(
newPage, instance.state.currSizePerPage)).toBeTruthy();
});
});
});
describe('handleChangeSizePerPage', () => {
@ -411,9 +427,7 @@ describe('Wrapper', () => {
const newSizePerPage = 30;
const props = createTableProps({ options: { onSizePerPageChange: sinon.stub() } });
beforeEach(() => {
PaginationWrapper = wrapperFactory(pureTable);
wrapper = shallow(<PaginationWrapper { ...props } />);
instance = wrapper.instance();
createPaginationWrapper(props, false);
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
@ -431,5 +445,85 @@ describe('Wrapper', () => {
expect(onSizePerPageChange.calledOnce).toBeTruthy();
expect(onSizePerPageChange.calledWith(newSizePerPage, newPage)).toBeTruthy();
});
describe('when pagination remote is enable', () => {
beforeEach(() => {
props.remote = true;
props.onRemotePageChange = sinon.stub();
createPaginationWrapper(props, false);
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('should not setting state.currPage', () => {
expect(instance.state.currPage).not.toEqual(newPage);
expect(instance.state.currSizePerPage).not.toEqual(newSizePerPage);
});
it('should calling options.onRemotePageChange correctly', () => {
expect(props.onRemotePageChange.calledOnce).toBeTruthy();
expect(props.onRemotePageChange.calledWith(newPage, newSizePerPage)).toBeTruthy();
});
});
});
describe('isRemote', () => {
let result;
describe('when options.remote is true', () => {
const props = createTableProps();
props.remote = true;
beforeEach(() => {
createPaginationWrapper(props, false);
result = instance.isRemote();
});
it('should return true', () => {
expect(result).toBeTruthy();
});
});
describe('when options.remote is false', () => {
const props = createTableProps();
props.remote = false;
beforeEach(() => {
createPaginationWrapper(props, false);
result = instance.isRemote();
});
it('should return false', () => {
expect(result).toBeFalsy();
});
});
describe('when options.remote.pagination is defined as true', () => {
const props = createTableProps();
props.remote = {};
props.remote.pagination = true;
beforeEach(() => {
createPaginationWrapper(props, false);
result = instance.isRemote();
});
it('should return true', () => {
expect(result).toBeTruthy();
});
});
describe('when options.remote.pagination is defined as false', () => {
const props = createTableProps();
props.remote = {};
props.remote.pagination = false;
beforeEach(() => {
createPaginationWrapper(props, false);
result = instance.isRemote();
});
it('should return false', () => {
expect(result).toBeFalsy();
});
});
});
});

View File

@ -152,6 +152,33 @@ describe('withDataStore', () => {
});
});
describe('when pagination prop is defined', () => {
const PaginationWrapper = () => <div>test</div>;
const pagination = {
wrapper: jest.fn().mockReturnValue(PaginationWrapper)
};
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField={ keyField }
data={ data }
columns={ columns }
pagination={ pagination }
/>
);
});
it('should render Pagination wrapper successfully', () => {
expect(wrapper.find(PaginationWrapper).length).toBe(1);
});
it('should injecting correct props to Pagination wrapper', () => {
const component = wrapper.find(PaginationWrapper);
expect(component.props().onRemotePageChange).toBeDefined();
});
});
describe('when any column.sort is defined', () => {
beforeEach(() => {
const columnsWithSort = [{
@ -172,4 +199,27 @@ describe('withDataStore', () => {
expect(wrapper.find(SortWrapper).length).toBe(1);
});
});
describe('onRemotePageChange', () => {
const page = 2;
const sizePerPage = 25;
const onTableChangeCallBack = sinon.stub();
beforeEach(() => {
wrapper = shallow(
<BootstrapTable
keyField={ keyField }
data={ data }
columns={ columns }
onTableChange={ onTableChangeCallBack }
/>
);
wrapper.instance().onRemotePageChange(page, sizePerPage);
});
it('should calling onTableChange correctly', () => {
expect(onTableChangeCallBack.calledOnce).toBeTruthy();
expect(onTableChangeCallBack.calledWith({ page, sizePerPage })).toBeTruthy();
});
});
});

View File

@ -212,4 +212,34 @@ describe('Store Base', () => {
expect(store.isAnySelectedRow()).not.toBeTruthy();
});
});
describe('getByCurrPage', () => {
beforeEach(() => {
data = [];
for (let i = 0; i < 100; i += 1) {
data.push({ id: i, name: `test_name${i}` });
}
store = new Base({ data, keyField: 'id' });
});
it('should always return correct data', () => {
[
// [page, sizePerPage, pageStartIndex]
[1, 10, 1],
[1, 25, 1],
[1, 30, 1],
[3, 30, 1],
[4, 30, 1],
[10, 10, 1],
[0, 10, 0],
[1, 10, 0],
[9, 10, 0]
].forEach(([page, sizePerPage, pageStartIndex]) => {
const rows = store.getByCurrPage(page, sizePerPage, pageStartIndex);
expect(rows).toBeDefined();
expect(Array.isArray(rows)).toBeTruthy();
expect(rows.every(row => !!row)).toBeTruthy();
});
});
});
});