diff --git a/packages/react-bootstrap-table2-paginator/src/context.js b/packages/react-bootstrap-table2-paginator/src/context.js index 10d72ff..eb58ceb 100644 --- a/packages/react-bootstrap-table2-paginator/src/context.js +++ b/packages/react-bootstrap-table2-paginator/src/context.js @@ -54,31 +54,22 @@ export default ( componentWillReceiveProps(nextProps) { let needNewState = false; - let { currPage, currSizePerPage } = this; - const { page, sizePerPage, onPageChange } = nextProps.pagination.options; + let { currPage } = this; + const { currSizePerPage } = this; + const { onPageChange } = nextProps.pagination.options; const pageStartIndex = typeof nextProps.pagination.options.pageStartIndex !== 'undefined' ? nextProps.pagination.options.pageStartIndex : Const.PAGE_START_INDEX; - if (typeof page !== 'undefined' && currPage !== page) { // user defined page - currPage = page; - needNewState = true; - } else { - // user should align the page when the page is not fit to the data size when remote enable - if (!isRemotePagination()) { - const newPage = alignPage(nextProps.data, currPage, currSizePerPage, pageStartIndex); - if (currPage !== newPage) { - currPage = newPage; - needNewState = true; - } + // user should align the page when the page is not fit to the data size when remote enable + if (!isRemotePagination()) { + const newPage = alignPage(nextProps.data, currPage, currSizePerPage, pageStartIndex); + if (currPage !== newPage) { + currPage = newPage; + needNewState = true; } } - if (typeof sizePerPage !== 'undefined' && currSizePerPage !== sizePerPage) { - currSizePerPage = sizePerPage; - needNewState = true; - } - if (needNewState) { if (onPageChange) { onPageChange(currPage, currSizePerPage); diff --git a/packages/react-bootstrap-table2-paginator/test/context.test.js b/packages/react-bootstrap-table2-paginator/test/context.test.js index 4b9c30d..9d13658 100644 --- a/packages/react-bootstrap-table2-paginator/test/context.test.js +++ b/packages/react-bootstrap-table2-paginator/test/context.test.js @@ -126,69 +126,6 @@ describe('PaginationContext', () => { let instance; let nextProps; - describe('when nextProps.pagination.options.page is existing', () => { - const onPageChange = jest.fn(); - afterEach(() => { - onPageChange.mockReset(); - }); - - describe('and if it is different with currPage', () => { - beforeEach(() => { - wrapper = shallow(shallowContext()); - instance = wrapper.instance(); - wrapper.render(); - nextProps = { - data, - pagination: { - options: { - page: 2, - onPageChange - } - } - }; - instance.componentWillReceiveProps(nextProps); - }); - - it('should call options.onPageChange', () => { - expect(onPageChange).toHaveBeenCalledTimes(1); - expect(onPageChange).toHaveBeenCalledWith( - instance.currPage, - instance.currSizePerPage - ); - }); - - it('should set correct currPage', () => { - expect(instance.currPage).toEqual(nextProps.pagination.options.page); - }); - }); - - describe('and if it is same as currPage', () => { - beforeEach(() => { - wrapper = shallow(shallowContext()); - instance = wrapper.instance(); - wrapper.render(); - nextProps = { - data, - pagination: { - options: { - page: 1, - onPageChange - } - } - }; - instance.componentWillReceiveProps(nextProps); - }); - - it('shouldn\'t call options.onPageChange', () => { - expect(onPageChange).toHaveBeenCalledTimes(0); - }); - - it('should have correct currPage', () => { - expect(instance.currPage).toEqual(nextProps.pagination.options.page); - }); - }); - }); - describe('when nextProps.pagination.options.page is not existing', () => { beforeEach(() => { wrapper = shallow(shallowContext({ @@ -206,69 +143,6 @@ describe('PaginationContext', () => { }); }); - describe('when nextProps.pagination.options.sizePerPage is existing', () => { - const onPageChange = jest.fn(); - afterEach(() => { - onPageChange.mockReset(); - }); - - describe('and if it is different with currSizePerPage', () => { - beforeEach(() => { - wrapper = shallow(shallowContext()); - instance = wrapper.instance(); - wrapper.render(); - nextProps = { - data, - pagination: { - options: { - sizePerPage: Const.SIZE_PER_PAGE_LIST[2], - onPageChange - } - } - }; - instance.componentWillReceiveProps(nextProps); - }); - - it('should call options.onPageChange', () => { - expect(onPageChange).toHaveBeenCalledTimes(1); - expect(onPageChange).toHaveBeenCalledWith( - instance.currPage, - instance.currSizePerPage - ); - }); - - it('should set correct currSizePerPage', () => { - expect(instance.currSizePerPage).toEqual(nextProps.pagination.options.sizePerPage); - }); - }); - - describe('and if it is same as currSizePerPage', () => { - beforeEach(() => { - wrapper = shallow(shallowContext()); - instance = wrapper.instance(); - wrapper.render(); - nextProps = { - data, - pagination: { - options: { - sizePerPage: Const.SIZE_PER_PAGE_LIST[0], - onPageChange - } - } - }; - instance.componentWillReceiveProps(nextProps); - }); - - it('shouldn\'t call options.onPageChange', () => { - expect(onPageChange).toHaveBeenCalledTimes(0); - }); - - it('should have correct currSizePerPage', () => { - expect(instance.currSizePerPage).toEqual(nextProps.pagination.options.sizePerPage); - }); - }); - }); - describe('when nextProps.pagination.options.sizePerPage is not existing', () => { beforeEach(() => { wrapper = shallow(shallowContext({ @@ -281,10 +155,53 @@ describe('PaginationContext', () => { instance.componentWillReceiveProps(nextProps); }); - it('should not set currPage', () => { + it('should not set currSizePerPage', () => { expect(instance.currSizePerPage).toEqual(Const.SIZE_PER_PAGE_LIST[2]); }); }); + + describe('when page is not align', () => { + beforeEach(() => { + wrapper = shallow(shallowContext({ + ...defaultPagination, + page: 2 + })); + instance = wrapper.instance(); + wrapper.render(); + nextProps = { + data: [], + pagination: { ...defaultPagination } + }; + instance.componentWillReceiveProps(nextProps); + }); + + it('should reset currPage to first page', () => { + expect(instance.currPage).toEqual(1); + }); + + describe('if options.onPageChange is defined', () => { + const onPageChange = jest.fn(); + beforeEach(() => { + onPageChange.mockClear(); + wrapper = shallow(shallowContext({ + ...defaultPagination, + page: 2 + })); + instance = wrapper.instance(); + wrapper.render(); + nextProps = { + data: [], + pagination: { ...defaultPagination, options: { onPageChange } } + }; + instance.componentWillReceiveProps(nextProps); + }); + + it('should call options.onPageChange correctly', () => { + expect(onPageChange).toHaveBeenCalledTimes(1); + expect(onPageChange).toHaveBeenCalledWith(instance.currPage, instance.currSizePerPage); + }); + }); + }); }); describe('handleChangePage', () => {