You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
7.8 KiB
206 lines
7.8 KiB
7 years ago
|
import React from 'react';
|
||
2 years ago
|
import type { ListProps } from '..';
|
||
7 years ago
|
import List from '..';
|
||
2 years ago
|
import { fireEvent, render } from '../../../tests/utils';
|
||
3 years ago
|
import { noop } from '../../_util/warning';
|
||
7 years ago
|
|
||
2 years ago
|
interface DataSourceItem {
|
||
|
name: string;
|
||
|
key: React.Key;
|
||
|
}
|
||
|
|
||
7 years ago
|
describe('List.pagination', () => {
|
||
2 years ago
|
const data: ListProps<DataSourceItem>['dataSource'] = [
|
||
7 years ago
|
{ key: 0, name: 'Jack' },
|
||
|
{ key: 1, name: 'Lucy' },
|
||
|
{ key: 2, name: 'Tom' },
|
||
|
{ key: 3, name: 'Jerry' },
|
||
|
];
|
||
|
|
||
|
const pagination = { className: 'my-page', pageSize: 2 };
|
||
|
|
||
2 years ago
|
function createList(props?: ListProps<DataSourceItem>) {
|
||
7 years ago
|
return (
|
||
|
<List
|
||
|
itemLayout="vertical"
|
||
|
pagination={pagination}
|
||
|
dataSource={data}
|
||
6 years ago
|
renderItem={item => <List.Item key={item.key}>{item.name}</List.Item>}
|
||
7 years ago
|
{...props}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
|
|
||
2 years ago
|
function renderedNames(container: ReturnType<typeof render>['container']) {
|
||
3 years ago
|
return Array.prototype.map.call(
|
||
2 years ago
|
container.querySelectorAll('.ant-list-item'),
|
||
|
(row: HTMLDivElement) => row.textContent,
|
||
3 years ago
|
);
|
||
7 years ago
|
}
|
||
|
|
||
|
it('renders pagination correctly', () => {
|
||
3 years ago
|
const { container } = render(createList());
|
||
|
expect(container.firstChild).toMatchSnapshot();
|
||
7 years ago
|
});
|
||
|
|
||
|
it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', () => {
|
||
3 years ago
|
const { container: wrapper, rerender } = render(
|
||
2 years ago
|
createList({ pagination: { pageSize: 3, hideOnSinglePage: true } }),
|
||
3 years ago
|
);
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
|
rerender(createList({ pagination: { pageSize: 3, hideOnSinglePage: false } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
|
rerender(createList({ pagination: { pageSize: 4, hideOnSinglePage: true } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(0);
|
||
|
rerender(createList({ pagination: { pageSize: 4, hideOnSinglePage: false } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
|
rerender(createList({ pagination: { pageSize: 5, hideOnSinglePage: true } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(0);
|
||
|
rerender(createList({ pagination: { pageSize: 5, hideOnSinglePage: false } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
7 years ago
|
});
|
||
|
|
||
|
it('paginate data', () => {
|
||
3 years ago
|
const { container: wrapper } = render(createList());
|
||
7 years ago
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']);
|
||
3 years ago
|
|
||
|
const paginationItems = wrapper.querySelectorAll('.ant-pagination-item');
|
||
|
fireEvent.click(paginationItems[paginationItems.length - 1]);
|
||
7 years ago
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
||
|
});
|
||
|
|
||
|
it('repaginates when pageSize change', () => {
|
||
3 years ago
|
const { container: wrapper, rerender } = render(createList());
|
||
7 years ago
|
|
||
3 years ago
|
rerender(createList({ pagination: { pageSize: 1 } }));
|
||
7 years ago
|
expect(renderedNames(wrapper)).toEqual(['Jack']);
|
||
|
});
|
||
|
|
||
|
it('fires change event', () => {
|
||
|
const handlePaginationChange = jest.fn();
|
||
3 years ago
|
const { container: wrapper } = render(
|
||
7 years ago
|
createList({
|
||
|
pagination: {
|
||
|
...pagination,
|
||
|
onChange: handlePaginationChange,
|
||
|
onShowSizeChange: noop,
|
||
|
},
|
||
6 years ago
|
}),
|
||
7 years ago
|
);
|
||
|
|
||
3 years ago
|
const paginationItems = wrapper.querySelectorAll('.ant-pagination-item');
|
||
|
fireEvent.click(paginationItems[paginationItems.length - 1]);
|
||
7 years ago
|
|
||
6 years ago
|
expect(handlePaginationChange).toHaveBeenCalledWith(2, 2);
|
||
7 years ago
|
});
|
||
|
|
||
|
// 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', () => {
|
||
3 years ago
|
const { container: wrapper, rerender } = render(createList());
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination-item')).toHaveLength(2);
|
||
|
|
||
|
rerender(createList({ pagination: false }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(0);
|
||
|
|
||
|
rerender(createList({ pagination }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination-item')).toHaveLength(2);
|
||
|
|
||
2 years ago
|
fireEvent.click(wrapper.querySelector('.ant-pagination-item-2')!);
|
||
7 years ago
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
||
3 years ago
|
|
||
|
rerender(createList({ pagination: false }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(0);
|
||
|
|
||
2 years ago
|
rerender(createList({ pagination: true as ListProps<DataSourceItem>['pagination'] }));
|
||
3 years ago
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
6 years ago
|
// Legacy code will make pageSize ping with 10, here we fixed to keep sync by current one
|
||
3 years ago
|
expect(wrapper.querySelectorAll('.ant-pagination-item')).toHaveLength(2);
|
||
6 years ago
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
||
7 years ago
|
});
|
||
|
|
||
|
// https://github.com/ant-design/ant-design/issues/5259
|
||
|
it('change to correct page when data source changes', () => {
|
||
3 years ago
|
const { container: wrapper, rerender } = render(createList({ pagination: { pageSize: 1 } }));
|
||
2 years ago
|
fireEvent.click(wrapper.querySelector('.ant-pagination-item-3')!);
|
||
3 years ago
|
rerender(createList({ dataSource: [data[0]] }));
|
||
|
expect(wrapper.querySelector('.ant-pagination-item-1')).toHaveClass(
|
||
|
'ant-pagination-item-active',
|
||
6 years ago
|
);
|
||
7 years ago
|
});
|
||
7 years ago
|
|
||
|
it('specify the position of pagination', () => {
|
||
3 years ago
|
const { container: wrapper, rerender } = render(
|
||
|
createList({ pagination: { position: 'top' } }),
|
||
|
);
|
||
2 years ago
|
expect(wrapper.querySelector('.ant-list')?.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
||
3 years ago
|
|
||
|
rerender(createList({ pagination: { position: 'bottom' } }));
|
||
|
expect(
|
||
2 years ago
|
wrapper.querySelector('.ant-list')?.lastElementChild?.querySelectorAll('.ant-pagination'),
|
||
3 years ago
|
).toHaveLength(1);
|
||
|
|
||
|
rerender(createList({ pagination: { position: 'both' } }));
|
||
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(2);
|
||
|
expect(
|
||
2 years ago
|
wrapper.querySelector('.ant-list')?.firstElementChild?.querySelectorAll('.ant-pagination'),
|
||
3 years ago
|
).toHaveLength(1);
|
||
|
expect(
|
||
2 years ago
|
wrapper.querySelector('.ant-list')?.lastElementChild?.querySelectorAll('.ant-pagination'),
|
||
3 years ago
|
).toHaveLength(1);
|
||
7 years ago
|
});
|
||
6 years ago
|
|
||
|
it('should change page size work', () => {
|
||
3 years ago
|
const { container: wrapper } = render(createList({ pagination: { showSizeChanger: true } }));
|
||
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
||
6 years ago
|
|
||
2 years ago
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
||
3 years ago
|
fireEvent.click(wrapper.querySelectorAll('.ant-select-item-option')[2]);
|
||
6 years ago
|
|
||
2 years ago
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
||
3 years ago
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
||
5 years ago
|
});
|
||
|
|
||
5 years ago
|
// https://github.com/ant-design/ant-design/issues/24913
|
||
5 years ago
|
// https://github.com/ant-design/ant-design/issues/24501
|
||
|
it('should onChange called when pageSize change', () => {
|
||
|
const handlePaginationChange = jest.fn();
|
||
|
const handlePageSizeChange = () => {};
|
||
3 years ago
|
const { container: wrapper } = render(
|
||
5 years ago
|
createList({
|
||
|
pagination: {
|
||
|
...pagination,
|
||
|
showSizeChanger: true,
|
||
|
onChange: handlePaginationChange,
|
||
|
onShowSizeChange: handlePageSizeChange,
|
||
|
},
|
||
|
}),
|
||
|
);
|
||
|
|
||
2 years ago
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
||
3 years ago
|
fireEvent.click(wrapper.querySelectorAll('.ant-select-item-option')[1]);
|
||
5 years ago
|
expect(handlePaginationChange).toHaveBeenCalledWith(1, 10);
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
it('should default work', () => {
|
||
3 years ago
|
const { container: wrapper } = render(
|
||
6 years ago
|
createList({
|
||
|
pagination: {
|
||
|
defaultPageSize: 3,
|
||
|
defaultCurrent: 2,
|
||
|
pageSizeOptions: ['1', '3'],
|
||
|
showSizeChanger: true,
|
||
|
},
|
||
|
}),
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
it('should not crash when pagination is null', () => {
|
||
2 years ago
|
render(createList({ pagination: null as unknown as ListProps<DataSourceItem>['pagination'] }));
|
||
6 years ago
|
});
|
||
7 years ago
|
});
|