2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { render, mount } from 'enzyme';
|
|
|
|
import { renderToJson } from 'enzyme-to-json';
|
2016-12-14 15:38:26 +08:00
|
|
|
import Table from '..';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Table.pagination', () => {
|
|
|
|
const columns = [{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
}];
|
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const pagination = { pageSize: 2 };
|
|
|
|
|
|
|
|
function createTable(props) {
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
pagination={pagination}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderedNames(wrapper) {
|
|
|
|
return wrapper.find('TableRow').map(row => row.props().record.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('renders pagination correctly', () => {
|
|
|
|
const wrapper = render(createTable());
|
|
|
|
expect(renderToJson(wrapper)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('paginate data', () => {
|
|
|
|
const wrapper = mount(createTable());
|
|
|
|
|
|
|
|
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(createTable());
|
|
|
|
|
|
|
|
wrapper.setProps({ pagination: { pageSize: 1 } });
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change event', () => {
|
|
|
|
const handleChange = jest.fn();
|
2017-02-19 13:49:22 +08:00
|
|
|
const handlePaginationChange = jest.fn();
|
2016-12-14 14:48:09 +08:00
|
|
|
const noop = () => {};
|
|
|
|
const wrapper = mount(createTable({
|
2017-02-19 13:49:22 +08:00
|
|
|
pagination: { ...pagination, onChange: handlePaginationChange, onShowSizeChange: noop },
|
2016-12-14 14:48:09 +08:00
|
|
|
onChange: handleChange,
|
|
|
|
}));
|
|
|
|
|
|
|
|
wrapper.find('Pager').last().simulate('click');
|
|
|
|
|
|
|
|
expect(handleChange).toBeCalledWith(
|
|
|
|
{
|
|
|
|
current: 2,
|
|
|
|
pageSize: 2,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
{}
|
|
|
|
);
|
2017-02-19 13:49:22 +08:00
|
|
|
|
|
|
|
expect(handlePaginationChange).toBeCalledWith(2, 2);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2017-01-09 20:49:00 +08:00
|
|
|
|
2017-02-07 21:09:51 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/4532
|
|
|
|
// http://codepen.io/anon/pen/NdGgga?editors=001
|
2017-02-07 21:21:51 +08:00
|
|
|
it('should have pager when change pagination from false to undefined', () => {
|
2017-02-07 21:09:51 +08:00
|
|
|
const wrapper = mount(createTable({ pagination: false }));
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(0);
|
|
|
|
wrapper.setProps({ pagination: undefined });
|
|
|
|
expect(wrapper.find('.ant-pagination')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.ant-pagination-item-active')).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4532
|
|
|
|
// http://codepen.io/anon/pen/ZLbyjV?editors=001
|
|
|
|
it('should display pagination as prop pagination change between true and false', () => {
|
2017-01-09 20:49:00 +08:00
|
|
|
const wrapper = mount(createTable());
|
|
|
|
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 });
|
|
|
|
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);
|
|
|
|
expect(wrapper.find('.ant-pagination-item')).toHaveLength(1); // pageSize will be 10
|
2017-01-09 21:06:04 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
2017-01-09 20:49:00 +08:00
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|