2018-04-20 15:41:40 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { render, mount } from 'enzyme';
|
|
|
|
import List from '..';
|
|
|
|
|
|
|
|
describe('List.pagination', () => {
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const pagination = { className: 'my-page', pageSize: 2 };
|
|
|
|
|
|
|
|
function createList(props) {
|
|
|
|
return (
|
|
|
|
<List
|
|
|
|
itemLayout="vertical"
|
|
|
|
pagination={pagination}
|
|
|
|
dataSource={data}
|
2018-12-07 16:17:45 +08:00
|
|
|
renderItem={item => <List.Item key={item.key}>{item.name}</List.Item>}
|
2018-04-20 15:41:40 +08:00
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderedNames(wrapper) {
|
2019-03-06 16:36:40 +08:00
|
|
|
return wrapper.find('.ant-list-item').map(row => row.text());
|
2018-04-20 15:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('renders pagination correctly', () => {
|
|
|
|
const wrapper = render(createList());
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(createList({ pagination: { pageSize: 3, hideOnSinglePage: true } }));
|
2018-04-20 15:41:40 +08:00
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 3, hideOnSinglePage: false } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: true } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(0);
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: false } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: true } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(0);
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: false } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('paginate data', () => {
|
|
|
|
const wrapper = mount(createList());
|
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']);
|
|
|
|
wrapper
|
|
|
|
.find('Pager')
|
|
|
|
.last()
|
|
|
|
.simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('repaginates when pageSize change', () => {
|
|
|
|
const wrapper = mount(createList());
|
|
|
|
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 1 } });
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change event', () => {
|
|
|
|
const handlePaginationChange = jest.fn();
|
|
|
|
const noop = () => {};
|
|
|
|
const wrapper = mount(
|
|
|
|
createList({
|
|
|
|
pagination: {
|
|
|
|
...pagination,
|
|
|
|
onChange: handlePaginationChange,
|
|
|
|
onShowSizeChange: noop,
|
|
|
|
},
|
2018-12-07 16:17:45 +08:00
|
|
|
}),
|
2018-04-20 15:41:40 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
wrapper
|
|
|
|
.find('Pager')
|
|
|
|
.last()
|
|
|
|
.simulate('click');
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handlePaginationChange).toHaveBeenCalledWith(2, 2);
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4532
|
|
|
|
// https://codepen.io/afc163/pen/pWVRJV?editors=001
|
|
|
|
it('should display pagination as prop pagination change between true and false', () => {
|
|
|
|
const wrapper = mount(createList());
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.ant-pagination-item')).toHaveLength(2);
|
|
|
|
wrapper.setProps({ pagination: false });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(0);
|
|
|
|
wrapper.setProps({ pagination });
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.ant-pagination-item')).toHaveLength(2);
|
|
|
|
wrapper.find('.ant-pagination-item-2').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
|
|
|
wrapper.setProps({ pagination: false });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(0);
|
|
|
|
wrapper.setProps({ pagination: true });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
2019-04-05 17:30:24 +08:00
|
|
|
// Legacy code will make pageSize ping with 10, here we fixed to keep sync by current one
|
|
|
|
expect(wrapper.find('.ant-pagination-item')).toHaveLength(2);
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/5259
|
|
|
|
it('change to correct page when data source changes', () => {
|
|
|
|
const wrapper = mount(createList({ pagination: { pageSize: 1 } }));
|
|
|
|
wrapper.find('.ant-pagination-item-3').simulate('click');
|
|
|
|
wrapper.setProps({ dataSource: [data[0]] });
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(wrapper.find('.ant-pagination-item-1').hasClass('ant-pagination-item-active')).toBe(
|
|
|
|
true,
|
|
|
|
);
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
2018-05-21 21:52:18 +08:00
|
|
|
|
|
|
|
it('specify the position of pagination', () => {
|
|
|
|
const wrapper = mount(createList({ pagination: { position: 'top' } }));
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-list')
|
|
|
|
.childAt(0)
|
|
|
|
.find('.ant-pagination'),
|
|
|
|
).toHaveLength(1);
|
2018-05-21 21:52:18 +08:00
|
|
|
wrapper.setProps({ pagination: { position: 'bottom' } });
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-list')
|
|
|
|
.children()
|
|
|
|
.last()
|
|
|
|
.find('.ant-pagination'),
|
|
|
|
).toHaveLength(1);
|
2018-05-21 21:52:18 +08:00
|
|
|
wrapper.setProps({ pagination: { position: 'both' } });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(2);
|
2018-12-07 16:17:45 +08:00
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-list')
|
|
|
|
.childAt(0)
|
|
|
|
.find('.ant-pagination'),
|
|
|
|
).toHaveLength(1);
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-list')
|
|
|
|
.children()
|
|
|
|
.last()
|
|
|
|
.find('.ant-pagination'),
|
|
|
|
).toHaveLength(1);
|
2018-05-21 21:52:18 +08:00
|
|
|
});
|
2019-04-05 17:30:24 +08:00
|
|
|
|
|
|
|
it('should change page size work', () => {
|
|
|
|
const wrapper = mount(createList({ pagination: { showSizeChanger: true } }));
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('Pagination')
|
|
|
|
.first()
|
|
|
|
.render(),
|
|
|
|
).toMatchSnapshot();
|
|
|
|
|
|
|
|
wrapper.find('.ant-select-selection-selected-value').simulate('click');
|
|
|
|
wrapper
|
|
|
|
.find('.ant-select-dropdown-menu .ant-select-dropdown-menu-item')
|
|
|
|
.at(2)
|
|
|
|
.simulate('click');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('Pagination')
|
|
|
|
.first()
|
|
|
|
.render(),
|
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|