2019-01-12 11:33:27 +08:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Table from '..';
|
2018-05-27 14:01:18 +08:00
|
|
|
|
|
|
|
describe('Table.sorter', () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sorterFn = (a, b) => a.name[0].charCodeAt() - b.name[0].charCodeAt();
|
2018-05-27 14:01:18 +08:00
|
|
|
|
|
|
|
const column = {
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: sorterFn,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-05-27 14:01:18 +08:00
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
2019-01-12 11:33:27 +08:00
|
|
|
];
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getTableOptions(props = {}, columnProps = {}, listeners = {}) {
|
2018-05-27 14:01:18 +08:00
|
|
|
return {
|
|
|
|
propsData: {
|
2019-01-12 11:33:27 +08:00
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
...column,
|
|
|
|
...columnProps,
|
|
|
|
},
|
|
|
|
],
|
2018-05-27 14:01:18 +08:00
|
|
|
dataSource: data,
|
|
|
|
pagination: false,
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
listeners: {
|
|
|
|
...listeners,
|
|
|
|
},
|
|
|
|
sync: false,
|
|
|
|
attachedToDocument: true,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-05-27 14:01:18 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function renderedNames(wrapper) {
|
2018-05-27 14:01:18 +08:00
|
|
|
return wrapper.findAll({ name: 'TableRow' }).wrappers.map(row => {
|
2019-01-12 11:33:27 +08:00
|
|
|
return row.props().record.name;
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
it('renders sorter icon correctly', done => {
|
|
|
|
const wrapper = mount(Table, getTableOptions());
|
2018-05-27 14:01:18 +08:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(wrapper.find('thead').html()).toMatchSnapshot();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
it('default sort order ascend', done => {
|
|
|
|
const wrapper = mount(
|
|
|
|
Table,
|
|
|
|
getTableOptions(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'ascend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2018-05-27 14:01:18 +08:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
it('default sort order descend', done => {
|
|
|
|
const wrapper = mount(
|
|
|
|
Table,
|
|
|
|
getTableOptions(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'descend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2018-05-27 14:01:18 +08:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2018-06-06 10:48:15 +08:00
|
|
|
it('sort records', async () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const wrapper = mount(Table, getTableOptions());
|
2018-06-06 10:48:15 +08:00
|
|
|
await asyncExpect(() => {
|
2018-12-12 10:15:00 +08:00
|
|
|
// descent
|
2019-01-12 11:33:27 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').trigger('click');
|
|
|
|
});
|
2018-06-06 10:48:15 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(wrapper.find('.ant-table-tbody').text()).toEqual(
|
|
|
|
['Jack', 'Jerry', 'Lucy', 'Tom'].join(''),
|
|
|
|
);
|
2018-12-12 10:15:00 +08:00
|
|
|
|
|
|
|
// ascent
|
2019-01-12 11:33:27 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').trigger('click');
|
|
|
|
});
|
2018-06-06 10:48:15 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(wrapper.find('.ant-table-tbody').text()).toEqual(
|
|
|
|
['Tom', 'Lucy', 'Jack', 'Jerry'].join(''),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
it('can be controlled by sortOrder', done => {
|
|
|
|
const wrapper = mount(
|
|
|
|
Table,
|
|
|
|
getTableOptions({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
}),
|
|
|
|
);
|
2018-05-27 14:01:18 +08:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2018-12-12 10:15:00 +08:00
|
|
|
it('fires change event', async () => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(Table, getTableOptions({}, {}, { change: handleChange }));
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').trigger('click');
|
2018-12-12 10:15:00 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sorter1 = handleChange.mock.calls[0][2];
|
|
|
|
expect(sorter1.column.dataIndex).toBe('name');
|
|
|
|
expect(sorter1.order).toBe('ascend');
|
|
|
|
expect(sorter1.field).toBe('name');
|
|
|
|
expect(sorter1.columnKey).toBe('name');
|
|
|
|
});
|
|
|
|
wrapper.find('.ant-table-column-sorters').trigger('click');
|
2018-12-12 10:15:00 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sorter2 = handleChange.mock.calls[1][2];
|
|
|
|
expect(sorter2.column.dataIndex).toBe('name');
|
|
|
|
expect(sorter2.order).toBe('descend');
|
|
|
|
expect(sorter2.field).toBe('name');
|
|
|
|
expect(sorter2.columnKey).toBe('name');
|
|
|
|
});
|
2018-12-12 10:15:00 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').trigger('click');
|
2018-12-12 10:15:00 +08:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const sorter3 = handleChange.mock.calls[2][2];
|
|
|
|
expect(sorter3.column).toBe(undefined);
|
|
|
|
expect(sorter3.order).toBe(undefined);
|
2020-03-07 19:45:13 +08:00
|
|
|
expect(sorter3.field).toBe('name');
|
|
|
|
expect(sorter3.columnKey).toBe('name');
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
it('works with grouping columns in controlled mode', done => {
|
2018-05-27 14:01:18 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'group',
|
|
|
|
key: 'group',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
sorter: sorterFn,
|
|
|
|
sortOrder: 'descend',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
key: 'age',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
];
|
2018-05-27 14:01:18 +08:00
|
|
|
const testData = [
|
|
|
|
{ key: 0, name: 'Jack', age: 11 },
|
|
|
|
{ key: 1, name: 'Lucy', age: 20 },
|
|
|
|
{ key: 2, name: 'Tom', age: 21 },
|
|
|
|
{ key: 3, name: 'Jerry', age: 22 },
|
2019-01-12 11:33:27 +08:00
|
|
|
];
|
2018-05-27 14:01:18 +08:00
|
|
|
const wrapper = mount(Table, {
|
|
|
|
propsData: {
|
2019-01-12 11:33:27 +08:00
|
|
|
columns,
|
|
|
|
dataSource: testData,
|
2018-05-27 14:01:18 +08:00
|
|
|
},
|
|
|
|
sync: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-05-27 14:01:18 +08:00
|
|
|
Vue.nextTick(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|