2018-04-20 15:41:40 +08:00
|
|
|
import React from 'react';
|
2022-09-05 19:41:32 +08:00
|
|
|
import type { ListProps } from '..';
|
2018-04-20 15:41:40 +08:00
|
|
|
import List from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2022-05-10 15:43:29 +08:00
|
|
|
import { noop } from '../../_util/warning';
|
2018-04-20 15:41:40 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
interface DataSourceItem {
|
|
|
|
name: string;
|
|
|
|
key: React.Key;
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:41:40 +08:00
|
|
|
describe('List.pagination', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
const data: ListProps<DataSourceItem>['dataSource'] = [
|
2018-04-20 15:41:40 +08:00
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const pagination = { className: 'my-page', pageSize: 2 };
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
function createList(props?: ListProps<DataSourceItem>) {
|
2018-04-20 15:41:40 +08:00
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
function renderedNames(container: ReturnType<typeof render>['container']) {
|
2022-06-01 13:45:51 +08:00
|
|
|
return Array.prototype.map.call(
|
2022-09-05 19:41:32 +08:00
|
|
|
container.querySelectorAll('.ant-list-item'),
|
|
|
|
(row: HTMLDivElement) => row.textContent,
|
2022-06-01 13:45:51 +08:00
|
|
|
);
|
2018-04-20 15:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('renders pagination correctly', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container } = render(createList());
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper, rerender } = render(
|
2022-09-05 19:41:32 +08:00
|
|
|
createList({ pagination: { pageSize: 3, hideOnSinglePage: true } }),
|
2022-06-01 13:45:51 +08:00
|
|
|
);
|
|
|
|
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);
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('paginate data', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper } = render(createList());
|
2018-04-20 15:41:40 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']);
|
2022-06-01 13:45:51 +08:00
|
|
|
|
|
|
|
const paginationItems = wrapper.querySelectorAll('.ant-pagination-item');
|
|
|
|
fireEvent.click(paginationItems[paginationItems.length - 1]);
|
2018-04-20 15:41:40 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('repaginates when pageSize change', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper, rerender } = render(createList());
|
2018-04-20 15:41:40 +08:00
|
|
|
|
2022-06-01 13:45:51 +08:00
|
|
|
rerender(createList({ pagination: { pageSize: 1 } }));
|
2018-04-20 15:41:40 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change event', () => {
|
|
|
|
const handlePaginationChange = jest.fn();
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper } = render(
|
2018-04-20 15:41:40 +08:00
|
|
|
createList({
|
|
|
|
pagination: {
|
|
|
|
...pagination,
|
|
|
|
onChange: handlePaginationChange,
|
|
|
|
onShowSizeChange: noop,
|
|
|
|
},
|
2018-12-07 16:17:45 +08:00
|
|
|
}),
|
2018-04-20 15:41:40 +08:00
|
|
|
);
|
|
|
|
|
2022-06-01 13:45:51 +08:00
|
|
|
const paginationItems = wrapper.querySelectorAll('.ant-pagination-item');
|
|
|
|
fireEvent.click(paginationItems[paginationItems.length - 1]);
|
2018-04-20 15:41:40 +08:00
|
|
|
|
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', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
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);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(wrapper.querySelector('.ant-pagination-item-2')!);
|
2018-04-20 15:41:40 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
|
2022-06-01 13:45:51 +08:00
|
|
|
|
|
|
|
rerender(createList({ pagination: false }));
|
|
|
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(0);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
rerender(createList({ pagination: true as ListProps<DataSourceItem>['pagination'] }));
|
2022-06-01 13:45:51 +08:00
|
|
|
expect(wrapper.querySelectorAll('.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
|
2022-06-01 13:45:51 +08:00
|
|
|
expect(wrapper.querySelectorAll('.ant-pagination-item')).toHaveLength(2);
|
2019-04-05 17:30:24 +08:00
|
|
|
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', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper, rerender } = render(createList({ pagination: { pageSize: 1 } }));
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.click(wrapper.querySelector('.ant-pagination-item-3')!);
|
2022-06-01 13:45:51 +08:00
|
|
|
rerender(createList({ dataSource: [data[0]] }));
|
|
|
|
expect(wrapper.querySelector('.ant-pagination-item-1')).toHaveClass(
|
|
|
|
'ant-pagination-item-active',
|
2018-12-07 16:17:45 +08:00
|
|
|
);
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|
2018-05-21 21:52:18 +08:00
|
|
|
|
|
|
|
it('specify the position of pagination', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper, rerender } = render(
|
|
|
|
createList({ pagination: { position: 'top' } }),
|
|
|
|
);
|
2022-09-05 19:41:32 +08:00
|
|
|
expect(wrapper.querySelector('.ant-list')?.querySelectorAll('.ant-pagination')).toHaveLength(1);
|
2022-06-01 13:45:51 +08:00
|
|
|
|
|
|
|
rerender(createList({ pagination: { position: 'bottom' } }));
|
|
|
|
expect(
|
2022-09-05 19:41:32 +08:00
|
|
|
wrapper.querySelector('.ant-list')?.lastElementChild?.querySelectorAll('.ant-pagination'),
|
2022-06-01 13:45:51 +08:00
|
|
|
).toHaveLength(1);
|
|
|
|
|
|
|
|
rerender(createList({ pagination: { position: 'both' } }));
|
|
|
|
expect(wrapper.querySelectorAll('.ant-pagination')).toHaveLength(2);
|
|
|
|
expect(
|
2022-09-05 19:41:32 +08:00
|
|
|
wrapper.querySelector('.ant-list')?.firstElementChild?.querySelectorAll('.ant-pagination'),
|
2022-06-01 13:45:51 +08:00
|
|
|
).toHaveLength(1);
|
|
|
|
expect(
|
2022-09-05 19:41:32 +08:00
|
|
|
wrapper.querySelector('.ant-list')?.lastElementChild?.querySelectorAll('.ant-pagination'),
|
2022-06-01 13:45:51 +08:00
|
|
|
).toHaveLength(1);
|
2018-05-21 21:52:18 +08:00
|
|
|
});
|
2019-04-05 17:30:24 +08:00
|
|
|
|
|
|
|
it('should change page size work', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper } = render(createList({ pagination: { showSizeChanger: true } }));
|
|
|
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
2019-04-05 17:30:24 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
2022-06-01 13:45:51 +08:00
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-select-item-option')[2]);
|
2019-04-05 17:30:24 +08:00
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
2022-06-01 13:45:51 +08:00
|
|
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
2020-06-06 15:09:59 +08:00
|
|
|
});
|
|
|
|
|
2020-06-13 14:26:29 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/24913
|
2020-06-06 15:09:59 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/24501
|
|
|
|
it('should onChange called when pageSize change', () => {
|
|
|
|
const handlePaginationChange = jest.fn();
|
|
|
|
const handlePageSizeChange = () => {};
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper } = render(
|
2020-06-06 15:09:59 +08:00
|
|
|
createList({
|
|
|
|
pagination: {
|
|
|
|
...pagination,
|
|
|
|
showSizeChanger: true,
|
|
|
|
onChange: handlePaginationChange,
|
|
|
|
onShowSizeChange: handlePageSizeChange,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-09-05 19:41:32 +08:00
|
|
|
fireEvent.mouseDown(wrapper.querySelector('.ant-select-selector')!);
|
2022-06-01 13:45:51 +08:00
|
|
|
fireEvent.click(wrapper.querySelectorAll('.ant-select-item-option')[1]);
|
2020-06-06 15:09:59 +08:00
|
|
|
expect(handlePaginationChange).toHaveBeenCalledWith(1, 10);
|
2019-04-05 17:30:24 +08:00
|
|
|
});
|
2019-04-15 23:35:31 +08:00
|
|
|
|
|
|
|
it('should default work', () => {
|
2022-06-01 13:45:51 +08:00
|
|
|
const { container: wrapper } = render(
|
2019-04-15 23:35:31 +08:00
|
|
|
createList({
|
|
|
|
pagination: {
|
|
|
|
defaultPageSize: 3,
|
|
|
|
defaultCurrent: 2,
|
|
|
|
pageSizeOptions: ['1', '3'],
|
|
|
|
showSizeChanger: true,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-01 13:45:51 +08:00
|
|
|
expect(wrapper.querySelector('.ant-pagination')).toMatchSnapshot();
|
2019-04-15 23:35:31 +08:00
|
|
|
});
|
2019-04-22 18:03:06 +08:00
|
|
|
|
|
|
|
it('should not crash when pagination is null', () => {
|
2022-09-05 19:41:32 +08:00
|
|
|
render(createList({ pagination: null as unknown as ListProps<DataSourceItem>['pagination'] }));
|
2019-04-22 18:03:06 +08:00
|
|
|
});
|
2018-04-20 15:41:40 +08:00
|
|
|
});
|