patch pagination context test suites

This commit is contained in:
AllenFang 2018-12-01 15:46:51 +08:00
parent d4be1675db
commit 7dbdc1943b
4 changed files with 1052 additions and 728 deletions

View File

@ -1,711 +0,0 @@
import 'jsdom-global/register';
import React from 'react';
import { shallow } from 'enzyme';
import BootstrapTable from 'react-bootstrap-table-next/src/bootstrap-table';
import Pagination from '../src/pagination';
import Const from '../src/const';
import createPaginationContext from '../src/context';
import paginationFactory from '../index';
const data = [];
for (let i = 0; i < 100; i += 1) {
data.push({
id: i,
name: `itme name ${i}`
});
}
describe('PaginationContext', () => {
let wrapper;
let PaginationContext;
const columns = [{
dataField: 'id',
text: 'ID'
}, {
dataField: 'name',
text: 'Name'
}];
const defaultPagination = { options: {} };
const mockBase = jest.fn((props => (
<BootstrapTable
keyField="id"
data={ data }
columns={ columns }
{ ...props }
/>
)));
const handleRemotePaginationChange = jest.fn();
function shallowContext(
customPagination = defaultPagination,
enableRemote = false
) {
mockBase.mockReset();
handleRemotePaginationChange.mockReset();
PaginationContext = createPaginationContext(
jest.fn().mockReturnValue(enableRemote),
handleRemotePaginationChange
);
return (
<PaginationContext.Provider
pagination={ paginationFactory(customPagination) }
columns={ columns }
data={ data }
>
<PaginationContext.Consumer>
{
paginationProps => mockBase(paginationProps)
}
</PaginationContext.Consumer>
</PaginationContext.Provider>
);
}
describe('default render', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
wrapper.render();
});
it('should have correct Provider property after calling createPaginationContext', () => {
expect(PaginationContext.Provider).toBeDefined();
});
it('should have correct Consumer property after calling createPaginationContext', () => {
expect(PaginationContext.Consumer).toBeDefined();
});
it('should have correct currPage', () => {
expect(wrapper.instance().currPage).toEqual(Const.PAGE_START_INDEX);
});
it('should have correct currSizePerPage', () => {
expect(wrapper.instance().currSizePerPage).toEqual(Const.SIZE_PER_PAGE_LIST[0]);
});
it('should render Pagination component correctly', () => {
expect(wrapper.length).toBe(1);
const instance = wrapper.instance();
const pagination = wrapper.find(Pagination);
expect(pagination).toHaveLength(1);
expect(pagination.prop('dataSize')).toEqual(data.length);
expect(pagination.prop('currPage')).toEqual(instance.currPage);
expect(pagination.prop('currSizePerPage')).toEqual(instance.currSizePerPage);
expect(pagination.prop('onPageChange')).toEqual(instance.handleChangePage);
expect(pagination.prop('onSizePerPageChange')).toEqual(instance.handleChangeSizePerPage);
expect(pagination.prop('sizePerPageList')).toEqual(Const.SIZE_PER_PAGE_LIST);
expect(pagination.prop('paginationSize')).toEqual(Const.PAGINATION_SIZE);
expect(pagination.prop('pageStartIndex')).toEqual(Const.PAGE_START_INDEX);
expect(pagination.prop('withFirstAndLast')).toEqual(Const.With_FIRST_AND_LAST);
expect(pagination.prop('alwaysShowAllBtns')).toEqual(Const.SHOW_ALL_PAGE_BTNS);
expect(pagination.prop('firstPageText')).toEqual(Const.FIRST_PAGE_TEXT);
expect(pagination.prop('prePageText')).toEqual(Const.PRE_PAGE_TEXT);
expect(pagination.prop('nextPageText')).toEqual(Const.NEXT_PAGE_TEXT);
expect(pagination.prop('lastPageText')).toEqual(Const.LAST_PAGE_TEXT);
expect(pagination.prop('firstPageTitle')).toEqual(Const.FIRST_PAGE_TITLE);
expect(pagination.prop('prePageTitle')).toEqual(Const.PRE_PAGE_TITLE);
expect(pagination.prop('nextPageTitle')).toEqual(Const.NEXT_PAGE_TITLE);
expect(pagination.prop('lastPageTitle')).toEqual(Const.LAST_PAGE_TITLE);
expect(pagination.prop('hideSizePerPage')).toEqual(Const.HIDE_SIZE_PER_PAGE);
expect(pagination.prop('hideSizePerPage')).toEqual(Const.HIDE_SIZE_PER_PAGE);
expect(pagination.prop('paginationTotalRenderer')).toBeNull();
});
it('should pass correct cell editing props to children element', () => {
expect(mockBase.mock.calls[0][0].data).toHaveLength(Const.SIZE_PER_PAGE_LIST[0]);
});
});
describe('componentWillReceiveProps', () => {
let instance;
let nextProps;
describe('when nextProps.pagination.options.page is not existing', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
page: 3
}));
instance = wrapper.instance();
wrapper.render();
nextProps = { data, pagination: defaultPagination };
instance.componentWillReceiveProps(nextProps);
});
it('should not set currPage', () => {
expect(instance.currPage).toEqual(3);
});
});
describe('when nextProps.pagination.options.sizePerPage is not existing', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
sizePerPage: Const.SIZE_PER_PAGE_LIST[2]
}));
instance = wrapper.instance();
wrapper.render();
nextProps = { data, pagination: defaultPagination };
instance.componentWillReceiveProps(nextProps);
});
it('should not set currSizePerPage', () => {
expect(instance.currSizePerPage).toEqual(Const.SIZE_PER_PAGE_LIST[2]);
});
});
describe('when remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({ ...defaultPagination }, true));
instance = wrapper.instance();
wrapper.render();
nextProps = {
data,
pagination: { ...defaultPagination, options: { page: 3, sizePerPage: 5 } }
};
instance.componentWillReceiveProps(nextProps);
});
it('should always set currPage from nextProps.pagination.options.page', () => {
expect(instance.currPage).toEqual(nextProps.pagination.options.page);
});
it('should always set currSizePerPage from nextProps.pagination.options.sizePerPage', () => {
expect(instance.currSizePerPage).toEqual(nextProps.pagination.options.sizePerPage);
});
});
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', () => {
let instance;
const newPage = 3;
describe('should update component correctly', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
});
describe('if options.onPageChange is defined', () => {
const onPageChange = jest.fn();
beforeEach(() => {
onPageChange.mockClear();
wrapper = shallow(shallowContext({
...defaultPagination,
onPageChange
}));
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
it('should call options.onPageChange correctly', () => {
expect(onPageChange).toHaveBeenCalledTimes(1);
expect(onPageChange).toHaveBeenCalledWith(newPage, instance.currSizePerPage);
});
});
describe('if remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination
}, true));
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(0);
});
it('should call handleRemotePageChange correctly', () => {
expect(handleRemotePaginationChange).toHaveBeenCalledTimes(1);
expect(handleRemotePaginationChange)
.toHaveBeenCalledWith(newPage, instance.currSizePerPage);
});
});
});
describe('handleChangeSizePerPage', () => {
let instance;
const newPage = 2;
const newSizePerPage = 15;
describe('should update component correctly', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
});
describe('if options.onSizePerPageChange is defined', () => {
const onSizePerPageChange = jest.fn();
beforeEach(() => {
onSizePerPageChange.mockClear();
wrapper = shallow(shallowContext({
...defaultPagination,
onSizePerPageChange
}));
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
it('should call options.onSizePerPageChange correctly', () => {
expect(onSizePerPageChange).toHaveBeenCalledTimes(1);
expect(onSizePerPageChange).toHaveBeenCalledWith(newSizePerPage, newPage);
});
});
describe('if remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination
}, true));
instance = wrapper.instance();
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(0);
});
it('should call handleRemotePageChange correctly', () => {
expect(handleRemotePaginationChange).toHaveBeenCalledTimes(1);
expect(handleRemotePaginationChange)
.toHaveBeenCalledWith(newPage, newSizePerPage);
});
});
});
describe('when options.page is defined', () => {
const page = 3;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
page
}));
wrapper.render();
});
it('should set correct currPage', () => {
expect(wrapper.instance().currPage).toEqual(page);
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('currPage')).toEqual(page);
});
});
describe('when options.sizePerPage is defined', () => {
const sizePerPage = Const.SIZE_PER_PAGE_LIST[2];
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
sizePerPage
}));
wrapper.render();
});
it('should set correct currSizePerPage', () => {
expect(wrapper.instance().currSizePerPage).toEqual(sizePerPage);
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('currSizePerPage')).toEqual(sizePerPage);
});
});
describe('when options.totalSize is defined', () => {
const totalSize = 100;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
totalSize
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('dataSize')).toEqual(totalSize);
});
});
describe('when options.showTotal is defined', () => {
const showTotal = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
showTotal
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('showTotal')).toEqual(showTotal);
});
});
describe('when options.pageStartIndex is defined', () => {
const pageStartIndex = -1;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
pageStartIndex
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('pageStartIndex')).toEqual(pageStartIndex);
});
});
describe('when options.sizePerPageList is defined', () => {
const sizePerPageList = [10, 40];
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
sizePerPageList
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('sizePerPageList')).toEqual(sizePerPageList);
});
});
describe('when options.paginationSize is defined', () => {
const paginationSize = 10;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
paginationSize
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('paginationSize')).toEqual(paginationSize);
});
});
describe('when options.withFirstAndLast is defined', () => {
const withFirstAndLast = false;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
withFirstAndLast
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('withFirstAndLast')).toEqual(withFirstAndLast);
});
});
describe('when options.alwaysShowAllBtns is defined', () => {
const alwaysShowAllBtns = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
alwaysShowAllBtns
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('alwaysShowAllBtns')).toEqual(alwaysShowAllBtns);
});
});
describe('when options.firstPageText is defined', () => {
const firstPageText = '1st';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
firstPageText
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('firstPageText')).toEqual(firstPageText);
});
});
describe('when options.prePageText is defined', () => {
const prePageText = 'PRE';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
prePageText
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('prePageText')).toEqual(prePageText);
});
});
describe('when options.nextPageText is defined', () => {
const nextPageText = 'NEXT';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
nextPageText
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('nextPageText')).toEqual(nextPageText);
});
});
describe('when options.lastPageText is defined', () => {
const lastPageText = 'LAST';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
lastPageText
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('lastPageText')).toEqual(lastPageText);
});
});
describe('when options.firstPageTitle is defined', () => {
const firstPageTitle = '1st';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
firstPageTitle
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('firstPageTitle')).toEqual(firstPageTitle);
});
});
describe('when options.prePageTitle is defined', () => {
const prePageTitle = 'PRE';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
prePageTitle
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('prePageTitle')).toEqual(prePageTitle);
});
});
describe('when options.nextPageTitle is defined', () => {
const nextPageTitle = 'NEXT';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
nextPageTitle
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('nextPageTitle')).toEqual(nextPageTitle);
});
});
describe('when options.lastPageTitle is defined', () => {
const lastPageTitle = 'nth';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
lastPageTitle
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('lastPageTitle')).toEqual(lastPageTitle);
});
});
describe('when options.hideSizePerPage is defined', () => {
const hideSizePerPage = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
hideSizePerPage
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('hideSizePerPage')).toEqual(hideSizePerPage);
});
});
describe('when options.hidePageListOnlyOnePage is defined', () => {
const hidePageListOnlyOnePage = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
hidePageListOnlyOnePage
}));
wrapper.render();
});
it('should rendering Pagination correctly', () => {
const pagination = wrapper.find(Pagination);
expect(pagination.length).toBe(1);
expect(pagination.prop('hidePageListOnlyOnePage')).toEqual(hidePageListOnlyOnePage);
});
});
});

View File

@ -0,0 +1,208 @@
import 'jsdom-global/register';
import React from 'react';
import { shallow } from 'enzyme';
import paginationFactory from '../index';
import Const from '../src/const';
import createStateContext from '../src/data-context';
import Pagination from '../src/pagination';
import { getByCurrPage } from '../src/page';
const data = [];
for (let i = 0; i < 100; i += 1) {
data.push({
id: i,
name: `itme name ${i}`
});
}
describe('PaginationDataContext', () => {
let wrapper;
let PaginationDataContext;
const defaultPagination = { options: { totalSize: data.length }, createContext: jest.fn() };
const MockComponent = () => null;
const renderMockComponent = jest.fn((props => (
<MockComponent { ...props } />
)));
const handleRemotePaginationChange = jest.fn();
function shallowContext(
customPagination = defaultPagination,
remoteEnabled = false
) {
renderMockComponent.mockReset();
handleRemotePaginationChange.mockReset();
PaginationDataContext = createStateContext();
const isRemotePagination = jest.fn().mockReturnValue(remoteEnabled);
const remoteEmitter = { emit: jest.fn() };
return (
<PaginationDataContext.Provider
pagination={ paginationFactory(customPagination) }
data={ data }
remoteEmitter={ remoteEmitter }
isRemotePagination={ isRemotePagination }
>
<PaginationDataContext.Consumer>
{
paginationProps => renderMockComponent(paginationProps)
}
</PaginationDataContext.Consumer>
</PaginationDataContext.Provider>
);
}
describe('default render', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
wrapper.render();
});
it('should have correct Provider property after calling createPaginationDataContext', () => {
expect(PaginationDataContext.Provider).toBeDefined();
});
it('should have correct Consumer property after calling createPaginationDataContext', () => {
expect(PaginationDataContext.Consumer).toBeDefined();
});
it('should have correct currPage', () => {
expect(wrapper.instance().currPage).toEqual(Const.PAGE_START_INDEX);
});
it('should have correct currSizePerPage', () => {
expect(wrapper.instance().currSizePerPage).toEqual(Const.SIZE_PER_PAGE_LIST[0]);
});
it('should render correct data props to childrens', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledTimes(1);
expect(renderMockComponent).toHaveBeenCalledWith({
data: getByCurrPage(
data,
instance.currPage,
instance.currSizePerPage,
Const.PAGE_START_INDEX
),
setRemoteEmitter: instance.setRemoteEmitter
});
});
});
describe('default render', () => {
describe('when options.custom is negative', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
wrapper.render();
});
it('should render Pagination component correctly', () => {
const instance = wrapper.instance();
const pagination = wrapper.find(Pagination);
expect(pagination).toHaveLength(1);
expect(pagination.prop('dataSize')).toEqual(data.length);
expect(pagination.prop('currPage')).toEqual(instance.currPage);
expect(pagination.prop('currSizePerPage')).toEqual(instance.currSizePerPage);
expect(pagination.prop('onPageChange')).toEqual(instance.handleChangePage);
expect(pagination.prop('onSizePerPageChange')).toEqual(instance.handleChangeSizePerPage);
expect(pagination.prop('sizePerPageList')).toEqual(Const.SIZE_PER_PAGE_LIST);
expect(pagination.prop('paginationSize')).toEqual(Const.PAGINATION_SIZE);
expect(pagination.prop('pageStartIndex')).toEqual(Const.PAGE_START_INDEX);
expect(pagination.prop('withFirstAndLast')).toEqual(Const.With_FIRST_AND_LAST);
expect(pagination.prop('alwaysShowAllBtns')).toEqual(Const.SHOW_ALL_PAGE_BTNS);
expect(pagination.prop('firstPageText')).toEqual(Const.FIRST_PAGE_TEXT);
expect(pagination.prop('prePageText')).toEqual(Const.PRE_PAGE_TEXT);
expect(pagination.prop('nextPageText')).toEqual(Const.NEXT_PAGE_TEXT);
expect(pagination.prop('lastPageText')).toEqual(Const.LAST_PAGE_TEXT);
expect(pagination.prop('firstPageTitle')).toEqual(Const.FIRST_PAGE_TITLE);
expect(pagination.prop('prePageTitle')).toEqual(Const.PRE_PAGE_TITLE);
expect(pagination.prop('nextPageTitle')).toEqual(Const.NEXT_PAGE_TITLE);
expect(pagination.prop('lastPageTitle')).toEqual(Const.LAST_PAGE_TITLE);
expect(pagination.prop('hideSizePerPage')).toEqual(Const.HIDE_SIZE_PER_PAGE);
expect(pagination.prop('hideSizePerPage')).toEqual(Const.HIDE_SIZE_PER_PAGE);
expect(pagination.prop('paginationTotalRenderer')).toBeNull();
});
});
describe('when options.custom is positive', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
custom: true
}));
wrapper.render();
});
it('should not render Pagination component', () => {
const pagination = wrapper.find(Pagination);
expect(pagination).toHaveLength(0);
});
});
});
describe('when remote pagination enabled', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({}, true));
wrapper.render();
});
it('just pass data props to children', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledTimes(1);
expect(renderMockComponent).toHaveBeenCalledWith({
data: instance.props.data,
setRemoteEmitter: instance.setRemoteEmitter
});
});
});
describe('componentWillReceiveProps', () => {
let instance;
let nextProps;
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);
});
});
});
});
});

View File

@ -46,29 +46,15 @@ describe('Page Functions', () => {
const pageStartIndex = 1;
const sizePerPage = 10;
const page = 3;
describe('if the page does not fit the pages interval calculated from the length of store.data', () => {
beforeEach(() => {
data = [];
for (let i = 0; i < 15; i += 1) {
data.push({ id: i, name: `test_name${i}` });
}
});
describe('if the page does not fit the pages which calculated from the length of data', () => {
it('should return pageStartIndex argument', () => {
expect(alignPage(data, page, sizePerPage, pageStartIndex)).toEqual(pageStartIndex);
expect(alignPage(15, page, sizePerPage, pageStartIndex)).toEqual(pageStartIndex);
});
});
describe('if the length of store.data is large than the end page index', () => {
beforeEach(() => {
data = [];
for (let i = 0; i < 30; i += 1) {
data.push({ id: i, name: `test_name${i}` });
}
});
it('should return current page', () => {
expect(alignPage(data, page, sizePerPage, pageStartIndex)).toEqual(page);
expect(alignPage(30, page, sizePerPage, pageStartIndex)).toEqual(page);
});
});
});

View File

@ -0,0 +1,841 @@
/* eslint no-param-reassign: 0 */
import 'jsdom-global/register';
import React from 'react';
import { shallow } from 'enzyme';
import Const from '../src/const';
import createStateContext from '../src/state-context';
import paginationFactory from '../index';
const data = [];
for (let i = 0; i < 100; i += 1) {
data.push({
id: i,
name: `itme name ${i}`
});
}
describe('PaginationStateContext', () => {
let wrapper;
let remoteEmitter;
let PaginationStateContext;
const defaultPagination = { options: {}, createContext: jest.fn() };
const MockComponent = () => null;
const renderMockComponent = jest.fn((props => (
<MockComponent { ...props } />
)));
const handleRemotePaginationChange = jest.fn();
function shallowContext(
customPagination = defaultPagination
) {
const additionProps = {};
renderMockComponent.mockReset();
handleRemotePaginationChange.mockReset();
PaginationStateContext = createStateContext();
return (
<PaginationStateContext.Provider
pagination={ paginationFactory(customPagination) }
data={ data }
{ ...additionProps }
>
<PaginationStateContext.Consumer>
{
paginationProps => renderMockComponent(paginationProps)
}
</PaginationStateContext.Consumer>
</PaginationStateContext.Provider>
);
}
function setRemotePaginationEmitter(
instance,
remoteEnabled = false
) {
remoteEmitter = { emit: jest.fn() };
if (remoteEnabled) {
remoteEmitter.emit = jest.fn().mockImplementation((evtName, d = {}) => {
if (evtName === 'isRemotePagination') {
d.result = remoteEnabled;
}
});
}
instance.setPaginationRemoteEmitter(remoteEmitter);
}
describe('default render', () => {
const options = { totalSize: data.length };
beforeEach(() => {
wrapper = shallow(shallowContext(options));
wrapper.render();
});
it('should have correct Provider property after calling createPaginationStateContext', () => {
expect(PaginationStateContext.Provider).toBeDefined();
});
it('should have correct Consumer property after calling createPaginationStateContext', () => {
expect(PaginationStateContext.Consumer).toBeDefined();
});
it('should have correct currPage', () => {
expect(wrapper.instance().currPage).toEqual(Const.PAGE_START_INDEX);
});
it('should have correct currSizePerPage', () => {
expect(wrapper.instance().currSizePerPage).toEqual(Const.SIZE_PER_PAGE_LIST[0]);
});
it('should get correct pagination props', () => {
const instance = wrapper.instance();
expect(wrapper.length).toBe(1);
expect(renderMockComponent).toHaveBeenCalledTimes(1);
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
it('should return correct pagination states from getPaginationProps function', () => {
const instance = wrapper.instance();
const paginationProps = instance.getPaginationProps();
expect(paginationProps.dataSize).toEqual(data.length);
expect(paginationProps.page).toEqual(instance.currPage);
expect(paginationProps.sizePerPage).toEqual(instance.currSizePerPage);
expect(paginationProps.onPageChange).toEqual(instance.handleChangePage);
expect(paginationProps.onSizePerPageChange).toEqual(instance.handleChangeSizePerPage);
expect(paginationProps.sizePerPageList).toEqual(Const.SIZE_PER_PAGE_LIST);
expect(paginationProps.paginationSize).toEqual(Const.PAGINATION_SIZE);
expect(paginationProps.showTotal).toEqual(options.showTotal);
expect(paginationProps.hidePageListOnlyOnePage).toEqual(Const.HIDE_PAGE_LIST_ONLY_ONE_PAGE);
expect(paginationProps.pageStartIndex).toEqual(Const.PAGE_START_INDEX);
expect(paginationProps.withFirstAndLast).toEqual(Const.With_FIRST_AND_LAST);
expect(paginationProps.alwaysShowAllBtns).toEqual(Const.SHOW_ALL_PAGE_BTNS);
expect(paginationProps.firstPageText).toEqual(Const.FIRST_PAGE_TEXT);
expect(paginationProps.prePageText).toEqual(Const.PRE_PAGE_TEXT);
expect(paginationProps.nextPageText).toEqual(Const.NEXT_PAGE_TEXT);
expect(paginationProps.lastPageText).toEqual(Const.LAST_PAGE_TEXT);
expect(paginationProps.firstPageTitle).toEqual(Const.FIRST_PAGE_TITLE);
expect(paginationProps.prePageTitle).toEqual(Const.PRE_PAGE_TITLE);
expect(paginationProps.nextPageTitle).toEqual(Const.NEXT_PAGE_TITLE);
expect(paginationProps.lastPageTitle).toEqual(Const.LAST_PAGE_TITLE);
expect(paginationProps.hideSizePerPage).toEqual(Const.HIDE_SIZE_PER_PAGE);
expect(paginationProps.paginationTotalRenderer).toEqual(options.paginationTotalRenderer);
});
});
describe('compoientWillReceiveProps', () => {
let instance;
let nextProps;
describe('if remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination
}, true));
instance = wrapper.instance();
setRemotePaginationEmitter(instance, true);
nextProps = {
data,
pagination: { ...defaultPagination, options: { page: 3, sizePerPage: 5 } }
};
instance.componentWillReceiveProps(nextProps);
});
it('should always reset currPage and currSizePerPage', () => {
expect(instance.currPage).toEqual(nextProps.pagination.options.page);
expect(instance.currSizePerPage).toEqual(nextProps.pagination.options.sizePerPage);
});
});
describe('if options.custom is true', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
custom: true
}, true));
instance = wrapper.instance();
setRemotePaginationEmitter(instance, true);
nextProps = {
data,
pagination: { ...defaultPagination, options: { page: 3, sizePerPage: 5, custom: true } }
};
instance.componentWillReceiveProps(nextProps);
});
it('should always reset currPage and currSizePerPage', () => {
expect(instance.currPage).toEqual(nextProps.pagination.options.page);
expect(instance.currSizePerPage).toEqual(nextProps.pagination.options.sizePerPage);
});
});
});
describe('handleChangePage', () => {
let instance;
const newPage = 3;
describe('should update component correctly', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
instance = wrapper.instance();
setRemotePaginationEmitter(instance);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
});
describe('if options.onPageChange is defined', () => {
const onPageChange = jest.fn();
beforeEach(() => {
onPageChange.mockClear();
wrapper = shallow(shallowContext({
...defaultPagination,
onPageChange
}));
instance = wrapper.instance();
setRemotePaginationEmitter(instance);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
it('should call options.onPageChange correctly', () => {
expect(onPageChange).toHaveBeenCalledTimes(1);
expect(onPageChange).toHaveBeenCalledWith(newPage, instance.currSizePerPage);
});
});
describe('if remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination
}, true));
instance = wrapper.instance();
setRemotePaginationEmitter(instance, true);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangePage(newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(0);
});
it('should emit paginationChange event correctly', () => {
expect(remoteEmitter.emit).toHaveBeenLastCalledWith('paginationChange', instance.currPage, instance.currSizePerPage);
});
});
});
describe('handleChangeSizePerPage', () => {
let instance;
const newPage = 2;
const newSizePerPage = 15;
describe('should update component correctly', () => {
beforeEach(() => {
wrapper = shallow(shallowContext());
instance = wrapper.instance();
setRemotePaginationEmitter(instance);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
});
describe('if options.onSizePerPageChange is defined', () => {
const onSizePerPageChange = jest.fn();
beforeEach(() => {
onSizePerPageChange.mockClear();
wrapper = shallow(shallowContext({
...defaultPagination,
onSizePerPageChange
}));
instance = wrapper.instance();
setRemotePaginationEmitter(instance);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(1);
});
it('should call options.onSizePerPageChange correctly', () => {
expect(onSizePerPageChange).toHaveBeenCalledTimes(1);
expect(onSizePerPageChange).toHaveBeenCalledWith(newSizePerPage, newPage);
});
});
describe('if remote pagination is enable', () => {
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination
}, true));
instance = wrapper.instance();
setRemotePaginationEmitter(instance, true);
jest.spyOn(instance, 'forceUpdate');
instance.handleChangeSizePerPage(newSizePerPage, newPage);
});
it('should still update component correctly', () => {
expect(instance.currPage).toEqual(newPage);
expect(instance.currSizePerPage).toEqual(newSizePerPage);
expect(instance.forceUpdate).toHaveBeenCalledTimes(0);
});
it('should emit paginationChange event correctly', () => {
expect(remoteEmitter.emit).toHaveBeenLastCalledWith('paginationChange', instance.currPage, instance.currSizePerPage);
});
});
});
describe('when options.page is defined', () => {
const page = 3;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
page
}));
wrapper.render();
});
it('should set correct currPage', () => {
expect(wrapper.instance().currPage).toEqual(page);
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.sizePerPage is defined', () => {
const sizePerPage = Const.SIZE_PER_PAGE_LIST[2];
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
sizePerPage
}));
wrapper.render();
});
it('should set correct currSizePerPage', () => {
expect(wrapper.instance().currSizePerPage).toEqual(sizePerPage);
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.totalSize is defined', () => {
const totalSize = 100;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
totalSize
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.showTotal is defined', () => {
const showTotal = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
showTotal
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.pageStartIndex is defined', () => {
const pageStartIndex = -1;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
pageStartIndex
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.sizePerPageList is defined', () => {
const sizePerPageList = [10, 40];
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
sizePerPageList
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.paginationSize is defined', () => {
const paginationSize = 10;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
paginationSize
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.withFirstAndLast is defined', () => {
const withFirstAndLast = false;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
withFirstAndLast
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.alwaysShowAllBtns is defined', () => {
const alwaysShowAllBtns = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
alwaysShowAllBtns
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.firstPageText is defined', () => {
const firstPageText = '1st';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
firstPageText
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.prePageText is defined', () => {
const prePageText = 'PRE';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
prePageText
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.nextPageText is defined', () => {
const nextPageText = 'NEXT';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
nextPageText
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.lastPageText is defined', () => {
const lastPageText = 'LAST';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
lastPageText
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.firstPageTitle is defined', () => {
const firstPageTitle = '1st';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
firstPageTitle
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.prePageTitle is defined', () => {
const prePageTitle = 'PRE';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
prePageTitle
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.nextPageTitle is defined', () => {
const nextPageTitle = 'NEXT';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
nextPageTitle
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.lastPageTitle is defined', () => {
const lastPageTitle = 'nth';
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
lastPageTitle
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.hideSizePerPage is defined', () => {
const hideSizePerPage = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
hideSizePerPage
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
describe('when options.hidePageListOnlyOnePage is defined', () => {
const hidePageListOnlyOnePage = true;
beforeEach(() => {
wrapper = shallow(shallowContext({
...defaultPagination,
hidePageListOnlyOnePage
}));
wrapper.render();
});
it('should render correctly', () => {
const instance = wrapper.instance();
expect(renderMockComponent).toHaveBeenCalledWith({
paginationProps: instance.getPaginationProps(),
paginationBaseProps: {
pagination: {
createContext: expect.any(Function),
options: instance.getPaginationProps()
},
setPaginationRemoteEmitter: instance.setPaginationRemoteEmitter
}
});
});
});
});