ant-design-vue/components/table/__tests__/Table.pagination.test.js

232 lines
7.5 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import Table from '..';
2020-07-25 13:27:58 +00:00
import * as Vue from 'vue';
2019-01-12 03:33:27 +00:00
import { asyncExpect } from '@/tests/utils';
2018-05-26 14:48:16 +00:00
describe('Table.pagination', () => {
2019-01-12 03:33:27 +00:00
const columns = [
{
title: 'Name',
dataIndex: 'name',
},
];
2018-05-26 14:48:16 +00:00
const data = [
{ key: 0, name: 'Jack' },
{ key: 1, name: 'Lucy' },
{ key: 2, name: 'Tom' },
{ key: 3, name: 'Jerry' },
2019-01-12 03:33:27 +00:00
];
2018-05-26 14:48:16 +00:00
2019-01-12 03:33:27 +00:00
const pagination = { class: 'my-page', pageSize: 2 };
2018-05-26 14:48:16 +00:00
2020-08-12 08:30:13 +00:00
function getTableOptions(props = {}) {
2018-05-26 14:48:16 +00:00
return {
2020-07-25 07:46:49 +00:00
props: {
2018-05-26 14:48:16 +00:00
columns,
dataSource: data,
pagination,
...props,
},
sync: false,
2019-01-12 03:33:27 +00:00
};
2018-05-26 14:48:16 +00:00
}
2019-01-12 03:33:27 +00:00
function renderedNames(wrapper) {
2020-08-12 08:30:13 +00:00
return wrapper.findAllComponents({ name: 'TableRow' }).map(row => {
2019-01-12 03:33:27 +00:00
return row.props().record.name;
});
2018-05-26 14:48:16 +00:00
}
2019-01-12 03:33:27 +00:00
it('renders pagination correctly', done => {
const wrapper = mount(Table, getTableOptions());
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.html()).toMatchSnapshot();
done();
});
});
2018-05-26 14:48:16 +00:00
it('should not show pager if pagination.hideOnSinglePage is true and only 1 page', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(
Table,
getTableOptions({ pagination: { pageSize: 3, hideOnSinglePage: true } }),
);
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { pageSize: 3, hideOnSinglePage: false } });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: true } });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(0);
wrapper.setProps({ pagination: { pageSize: 4, hideOnSinglePage: false } });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: true } });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(0);
wrapper.setProps({ pagination: { pageSize: 5, hideOnSinglePage: false } });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
});
});
2018-05-26 14:48:16 +00:00
2020-08-12 08:30:13 +00:00
xit('paginate data', done => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Table, getTableOptions());
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy']);
2020-08-12 08:30:13 +00:00
const pager = wrapper.findAllComponents({ name: 'Pager' });
2019-01-12 03:33:27 +00:00
pager.at(pager.length - 1).trigger('click');
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
done();
});
});
});
2018-05-26 14:48:16 +00:00
2020-08-12 08:30:13 +00:00
xit('repaginates when pageSize change', () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Table, getTableOptions());
wrapper.setProps({ pagination: { pageSize: 1 } });
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(renderedNames(wrapper)).toEqual(['Jack']);
});
});
2020-08-12 08:30:13 +00:00
xit('fires change event', done => {
2019-01-12 03:33:27 +00:00
const handleChange = jest.fn();
const handlePaginationChange = jest.fn();
const noop = () => {};
const wrapper = mount(
Table,
2020-08-12 08:30:13 +00:00
getTableOptions({
pagination: {
...pagination,
onChange: handlePaginationChange,
onShowSizeChange: noop,
onChange: handleChange,
2019-01-12 03:33:27 +00:00
},
2020-08-12 08:30:13 +00:00
}),
2019-01-12 03:33:27 +00:00
);
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2020-08-12 08:30:13 +00:00
const pager = wrapper.findAllComponents({ name: 'Pager' });
pager[pager.length - 1].trigger('click');
2018-05-26 14:48:16 +00:00
expect(handleChange).toBeCalledWith(
{
class: 'my-page',
current: 2,
pageSize: 2,
},
{},
2018-12-12 02:15:00 +00:00
{},
{
currentDataSource: [
{ key: 0, name: 'Jack' },
{ key: 1, name: 'Lucy' },
{ key: 2, name: 'Tom' },
{ key: 3, name: 'Jerry' },
],
},
2019-01-12 03:33:27 +00:00
);
2018-05-26 14:48:16 +00:00
2019-01-12 03:33:27 +00:00
expect(handlePaginationChange).toBeCalledWith(2, 2);
done();
});
});
2018-05-26 14:48:16 +00:00
// https://github.com/ant-design/ant-design/issues/4532
// https://codepen.io/afc163/pen/dVeNoP?editors=001
2019-01-12 03:33:27 +00:00
it('should have pager when change pagination from false to undefined', done => {
const wrapper = mount(Table, getTableOptions({ pagination: false }));
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(0);
wrapper.setProps({ pagination: undefined });
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
expect(wrapper.findAll('.ant-pagination-item-active')).toHaveLength(1);
done();
});
});
});
2018-05-26 14:48:16 +00:00
// https://github.com/ant-design/ant-design/issues/4532
// https://codepen.io/afc163/pen/pWVRJV?editors=001
2020-08-12 08:30:13 +00:00
xit('should display pagination as prop pagination change between true and false', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Table, getTableOptions());
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(2);
wrapper.setProps({ pagination: false });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(0);
wrapper.setProps({ pagination });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(2);
wrapper.find('.ant-pagination-item-2').trigger('click');
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(renderedNames(wrapper)).toEqual(['Tom', 'Jerry']);
wrapper.setProps({ pagination: false });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(0);
wrapper.setProps({ pagination: true });
});
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-pagination')).toHaveLength(1);
expect(wrapper.findAll('.ant-pagination-item')).toHaveLength(1); // pageSize will be 10
expect(renderedNames(wrapper)).toHaveLength(4);
});
});
2018-05-26 14:48:16 +00:00
// https://github.com/ant-design/ant-design/issues/5259
2019-01-12 03:33:27 +00:00
it('change to correct page when data source changes', done => {
const wrapper = mount(Table, getTableOptions({ pagination: { pageSize: 1 } }));
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
wrapper.find('.ant-pagination-item-3').trigger('click');
wrapper.setProps({ dataSource: [data[0]] });
2018-05-26 14:48:16 +00:00
Vue.nextTick(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.find('.ant-pagination-item-1').classes()).toContain(
'ant-pagination-item-active',
);
done();
});
});
});
2018-05-26 14:48:16 +00:00
2020-08-12 08:30:13 +00:00
xit('specify the position of pagination', async () => {
2019-01-12 03:33:27 +00:00
const wrapper = mount(Table, getTableOptions({ pagination: { position: 'top' } }));
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(2);
2020-08-12 08:30:13 +00:00
expect(wrapper.findAll('.ant-spin-container > *')[0].findAll('.ant-pagination')).toHaveLength(
1,
);
2019-01-12 03:33:27 +00:00
wrapper.setProps({ pagination: { position: 'bottom' } });
}, 0);
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(2);
2020-08-12 08:30:13 +00:00
expect(wrapper.findAll('.ant-spin-container > *')[1].findAll('.ant-pagination')).toHaveLength(
1,
);
2019-01-12 03:33:27 +00:00
wrapper.setProps({ pagination: { position: 'both' } });
}, 0);
await asyncExpect(() => {
2019-01-12 03:33:27 +00:00
expect(wrapper.findAll('.ant-spin-container > *')).toHaveLength(3);
2020-08-12 08:30:13 +00:00
expect(wrapper.findAll('.ant-spin-container > *')[0].findAll('.ant-pagination')).toHaveLength(
1,
);
expect(wrapper.findAll('.ant-spin-container > *')[2].findAll('.ant-pagination')).toHaveLength(
1,
);
2019-01-12 03:33:27 +00:00
}, 0);
});
});