2018-11-01 13:42:53 +08:00
|
|
|
/* eslint-disable react/no-multi-comp */
|
2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { render, mount } from 'enzyme';
|
2016-12-14 15:38:26 +08:00
|
|
|
import Table from '..';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Table.sorter', () => {
|
|
|
|
const sorterFn = (a, b) => a.name[0].charCodeAt() - b.name[0].charCodeAt();
|
|
|
|
|
|
|
|
const column = {
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
2019-11-15 14:35:25 +08:00
|
|
|
key: 'name',
|
2016-12-14 14:48:09 +08:00
|
|
|
sorter: sorterFn,
|
|
|
|
};
|
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
2017-11-09 19:30:24 +08:00
|
|
|
function createTable(tableProps, columnProps = {}) {
|
2016-12-14 14:48:09 +08:00
|
|
|
return (
|
|
|
|
<Table
|
2018-12-07 20:02:01 +08:00
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
...column,
|
|
|
|
...columnProps,
|
|
|
|
},
|
|
|
|
]}
|
2016-12-14 14:48:09 +08:00
|
|
|
dataSource={data}
|
|
|
|
pagination={false}
|
2017-11-09 19:30:24 +08:00
|
|
|
{...tableProps}
|
2016-12-14 14:48:09 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderedNames(wrapper) {
|
2019-11-15 14:35:25 +08:00
|
|
|
return wrapper.find('BodyRow').map(row => row.props().record.name);
|
2016-12-14 14:48:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('renders sorter icon correctly', () => {
|
|
|
|
const wrapper = render(createTable());
|
2017-04-02 18:09:23 +08:00
|
|
|
expect(wrapper.find('thead')).toMatchSnapshot();
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2017-11-09 19:30:24 +08:00
|
|
|
it('default sort order ascend', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'ascend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2017-11-09 19:30:24 +08:00
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default sort order descend', () => {
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'descend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2017-11-09 19:30:24 +08:00
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sort records', () => {
|
2016-12-14 14:48:09 +08:00
|
|
|
const wrapper = mount(createTable());
|
|
|
|
|
2018-11-21 00:27:11 +08:00
|
|
|
// ascend
|
2018-09-17 19:03:06 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
2018-11-21 00:27:11 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
2018-09-17 19:03:06 +08:00
|
|
|
|
2018-11-21 00:27:11 +08:00
|
|
|
// descend
|
2018-09-17 19:03:06 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
2018-11-21 00:27:11 +08:00
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2020-05-05 21:08:56 +08:00
|
|
|
describe('can be controlled by sortOrder', () => {
|
|
|
|
it('single', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invalidate mix with single & multiple sorters', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: {
|
|
|
|
multiple: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: {},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2018-04-29 22:08:28 +08:00
|
|
|
it('provides sortOrder in the sorterFn', () => {
|
|
|
|
let actualSortOrder;
|
2018-12-07 20:02:01 +08:00
|
|
|
mount(
|
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: (a, b, sortOrder) => {
|
|
|
|
actualSortOrder = sortOrder;
|
|
|
|
return sorterFn(a, b);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2018-04-29 22:08:28 +08:00
|
|
|
expect(actualSortOrder).toEqual('ascend');
|
|
|
|
});
|
|
|
|
|
2019-09-28 15:22:27 +08:00
|
|
|
it('can update column sortOrder', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
columns: [column],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
wrapper.setProps({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('fires change event', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(createTable({ onChange: handleChange }));
|
|
|
|
|
2018-09-17 19:03:06 +08:00
|
|
|
// ascent
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
const sorter1 = handleChange.mock.calls[0][2];
|
|
|
|
expect(sorter1.column.dataIndex).toBe('name');
|
2018-11-21 00:27:11 +08:00
|
|
|
expect(sorter1.order).toBe('ascend');
|
2018-09-17 19:03:06 +08:00
|
|
|
expect(sorter1.field).toBe('name');
|
|
|
|
expect(sorter1.columnKey).toBe('name');
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
const sorter2 = handleChange.mock.calls[1][2];
|
|
|
|
expect(sorter2.column.dataIndex).toBe('name');
|
2018-11-21 00:27:11 +08:00
|
|
|
expect(sorter2.order).toBe('descend');
|
2018-09-17 19:03:06 +08:00
|
|
|
expect(sorter2.field).toBe('name');
|
|
|
|
expect(sorter2.columnKey).toBe('name');
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
const sorter3 = handleChange.mock.calls[2][2];
|
|
|
|
expect(sorter3.column).toBe(undefined);
|
|
|
|
expect(sorter3.order).toBe(undefined);
|
2019-10-15 16:33:37 +08:00
|
|
|
expect(sorter3.field).toBe('name');
|
|
|
|
expect(sorter3.columnKey).toBe('name');
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2017-03-10 14:52:28 +08:00
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
it('hover header show sorter tooltip', () => {
|
|
|
|
// tooltip has delay
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const wrapper = mount(createTable({}));
|
|
|
|
// default show sorter tooltip
|
2020-04-02 18:53:49 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseenter');
|
2020-02-29 22:01:13 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-tooltip-open').length).toBeTruthy();
|
2020-04-02 18:53:49 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseout');
|
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is false
|
|
|
|
wrapper.setProps({ showSorterTooltip: false });
|
2020-04-02 18:53:49 +08:00
|
|
|
expect(wrapper.find('.ant-table-column-sorters-with-tooltip')).toHaveLength(0);
|
2020-02-29 22:01:13 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
2020-04-02 18:53:49 +08:00
|
|
|
expect(wrapper.find('.ant-tooltip-open')).toHaveLength(0);
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is false, column showSorterTooltip is true
|
|
|
|
wrapper.setProps({
|
|
|
|
showSorterTooltip: false,
|
|
|
|
columns: [{ ...column, showSorterTooltip: true }],
|
|
|
|
});
|
2020-04-02 18:53:49 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseenter');
|
2020-02-29 22:01:13 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
expect(wrapper.find('.ant-tooltip-open').length).toBeTruthy();
|
2020-04-02 18:53:49 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters-with-tooltip').simulate('mouseout');
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is true, column showSorterTooltip is false
|
|
|
|
wrapper.setProps({
|
|
|
|
showSorterTooltip: true,
|
|
|
|
columns: [{ ...column, showSorterTooltip: false }],
|
|
|
|
});
|
2020-04-02 18:53:49 +08:00
|
|
|
expect(wrapper.find('.ant-table-column-sorters-with-tooltip')).toHaveLength(0);
|
2020-02-29 22:01:13 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
2020-04-02 18:53:49 +08:00
|
|
|
expect(wrapper.find('.ant-tooltip-open')).toHaveLength(0);
|
2020-02-29 22:01:13 +08:00
|
|
|
});
|
|
|
|
|
2017-03-10 14:52:28 +08:00
|
|
|
it('works with grouping columns in controlled mode', () => {
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'group',
|
|
|
|
key: 'group',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
sorter: sorterFn,
|
|
|
|
sortOrder: 'descend',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
key: 'age',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
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 },
|
|
|
|
];
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<Table columns={columns} dataSource={testData} />);
|
2017-03-10 14:52:28 +08:00
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
});
|
2018-09-17 19:52:24 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11246#issuecomment-405009167
|
|
|
|
it('Allow column title as render props with sortOrder argument', () => {
|
2019-08-02 17:59:42 +08:00
|
|
|
const title = ({ sortOrder }) => <div className="custom-title">{sortOrder}</div>;
|
2018-09-17 19:52:24 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
2019-08-02 17:59:42 +08:00
|
|
|
title,
|
2018-09-17 19:52:24 +08:00
|
|
|
key: 'group',
|
|
|
|
sorter: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
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 },
|
|
|
|
];
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<Table columns={columns} dataSource={testData} />);
|
2018-09-17 19:52:24 +08:00
|
|
|
expect(wrapper.find('.custom-title').text()).toEqual('');
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(wrapper.find('.custom-title').text()).toEqual('ascend');
|
2018-11-21 00:27:11 +08:00
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(wrapper.find('.custom-title').text()).toEqual('descend');
|
2018-09-17 19:52:24 +08:00
|
|
|
});
|
2018-09-21 16:22:29 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/pull/12264#discussion_r218053034
|
|
|
|
it('should sort from begining state when toggle from different columns', () => {
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
sorter: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
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 },
|
|
|
|
];
|
2018-12-07 20:02:01 +08:00
|
|
|
const wrapper = mount(<Table columns={columns} dataSource={testData} />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
const getNameColumn = () => wrapper.find('.ant-table-column-has-sorters').at(0);
|
|
|
|
const getAgeColumn = () => wrapper.find('.ant-table-column-has-sorters').at(1);
|
2020-05-05 21:08:56 +08:00
|
|
|
const getNameIcon = name => getNameColumn().find(`.ant-table-column-sorter-${name}`).first();
|
|
|
|
const getAgeIcon = name => getAgeColumn().find(`.ant-table-column-sorter-${name}`).first();
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-09-21 16:22:29 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getNameIcon('up').hasClass('active')).toBeTruthy();
|
|
|
|
expect(getAgeIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
|
2018-09-21 16:22:29 +08:00
|
|
|
// sort age
|
2019-11-15 14:35:25 +08:00
|
|
|
getAgeColumn().simulate('click');
|
|
|
|
expect(getNameIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getAgeIcon('up').hasClass('active')).toBeTruthy();
|
2018-09-21 16:22:29 +08:00
|
|
|
});
|
2018-10-10 11:23:44 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12571
|
|
|
|
it('should toggle sort state when columns are put in render', () => {
|
|
|
|
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 },
|
|
|
|
];
|
|
|
|
class TableTest extends React.Component {
|
|
|
|
state = {
|
|
|
|
pagination: {},
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
onChange = pagination => {
|
2018-10-10 11:23:44 +08:00
|
|
|
this.setState({ pagination });
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-10-10 11:23:44 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: true,
|
|
|
|
},
|
|
|
|
];
|
2018-10-10 11:23:44 +08:00
|
|
|
const { pagination } = this.state;
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
pagination={pagination}
|
|
|
|
dataSource={testData}
|
|
|
|
onChange={this.onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(<TableTest />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
const getNameColumn = () => wrapper.find('.ant-table-column-has-sorters').at(0);
|
2020-05-05 21:08:56 +08:00
|
|
|
const getIcon = name => getNameColumn().find(`.ant-table-column-sorter-${name}`).first();
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeTruthy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeTruthy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
2018-11-01 13:42:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12737
|
2019-10-24 17:29:49 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/19398
|
2018-11-01 13:42:53 +08:00
|
|
|
it('should toggle sort state when columns with non primitive properties are put in render', () => {
|
|
|
|
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 },
|
|
|
|
];
|
|
|
|
class TableTest extends React.Component {
|
|
|
|
state = {
|
|
|
|
pagination: {},
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
onChange = pagination => {
|
2018-11-01 13:42:53 +08:00
|
|
|
this.setState({ pagination });
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 13:42:53 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: true,
|
|
|
|
render: text => text,
|
2019-10-24 17:29:49 +08:00
|
|
|
array: ['1', '2', 3],
|
2018-12-07 20:02:01 +08:00
|
|
|
},
|
|
|
|
];
|
2018-11-01 13:42:53 +08:00
|
|
|
const { pagination } = this.state;
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
pagination={pagination}
|
|
|
|
dataSource={testData}
|
|
|
|
onChange={this.onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(<TableTest />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
const getNameColumn = () => wrapper.find('.ant-table-column-has-sorters').at(0);
|
2020-05-05 21:08:56 +08:00
|
|
|
const getIcon = name => getNameColumn().find(`.ant-table-column-sorter-${name}`).first();
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeTruthy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeTruthy();
|
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
|
|
|
expect(getIcon('up').hasClass('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').hasClass('active')).toBeFalsy();
|
2018-11-01 13:42:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/12870
|
|
|
|
it('should toggle sort state when columns with key are put in render', () => {
|
|
|
|
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 },
|
|
|
|
];
|
|
|
|
class TableTest extends React.Component {
|
|
|
|
state = {
|
|
|
|
pagination: {},
|
|
|
|
};
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
onChange = pagination => {
|
2018-11-01 13:42:53 +08:00
|
|
|
this.setState({ pagination });
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-01 13:42:53 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sorter: true,
|
|
|
|
key: 'a',
|
|
|
|
style: {
|
|
|
|
fontSize: 18,
|
|
|
|
},
|
2018-11-01 13:42:53 +08:00
|
|
|
},
|
2018-12-07 20:02:01 +08:00
|
|
|
];
|
2018-11-01 13:42:53 +08:00
|
|
|
const { pagination } = this.state;
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
pagination={pagination}
|
|
|
|
dataSource={testData}
|
|
|
|
onChange={this.onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(<TableTest />);
|
2019-11-15 14:35:25 +08:00
|
|
|
const getNameColumn = () => wrapper.find('.ant-table-column-has-sorters').at(0);
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-up').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-down').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-up').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeTruthy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-down').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-up').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-down').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeTruthy();
|
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2019-11-15 14:35:25 +08:00
|
|
|
getNameColumn().simulate('click');
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-up').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2020-05-05 21:08:56 +08:00
|
|
|
getNameColumn().find('.ant-table-column-sorter-down').at(0).hasClass('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-10-10 11:23:44 +08:00
|
|
|
});
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
it('should first sort by descend, then ascend, then cancel sort', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
sortDirections: ['descend', 'ascend'],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// descend
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
|
|
|
|
// ascend
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
|
|
|
|
// cancel sort
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should first sort by descend, then cancel sort', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
sortDirections: ['descend'],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// descend
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
|
|
|
|
// cancel sort
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should first sort by descend, then cancel sort. (column prop)', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
sortDirections: ['descend'],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// descend
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
|
|
|
|
// cancel sort
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
});
|
2019-06-10 11:59:46 +08:00
|
|
|
|
|
|
|
it('pagination back', () => {
|
|
|
|
const onPageChange = jest.fn();
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable({
|
|
|
|
pagination: {
|
|
|
|
pageSize: 2,
|
2019-12-28 21:52:09 +08:00
|
|
|
defaultCurrent: 2,
|
2019-06-10 11:59:46 +08:00
|
|
|
onChange: onPageChange,
|
|
|
|
},
|
|
|
|
onChange,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorters').simulate('click');
|
2019-12-28 21:52:09 +08:00
|
|
|
expect(onChange.mock.calls[0][0].current).toBe(2);
|
|
|
|
expect(onPageChange).not.toHaveBeenCalled();
|
2019-06-10 11:59:46 +08:00
|
|
|
});
|
2019-09-28 15:22:27 +08:00
|
|
|
|
|
|
|
it('should support onHeaderCell in sort column', () => {
|
|
|
|
const onClick = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table columns={[{ title: 'title', onHeaderCell: () => ({ onClick }), sorter: true }]} />,
|
|
|
|
);
|
|
|
|
wrapper.find('th').simulate('click');
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2019-09-28 18:14:28 +08:00
|
|
|
|
|
|
|
it('could sort data with children', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
createTable(
|
|
|
|
{
|
2019-11-15 14:35:25 +08:00
|
|
|
defaultExpandAllRows: true,
|
2019-09-28 18:14:28 +08:00
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
name: 'Brown',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
name: 'Zoe',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
name: 'Mike',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: '3-1',
|
|
|
|
name: 'Petter',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3-2',
|
|
|
|
name: 'Alex',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
name: 'Green',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'ascend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Brown', 'Green', 'Mike', 'Alex', 'Petter', 'Zoe']);
|
|
|
|
});
|
2019-11-05 00:40:03 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/19443
|
|
|
|
it('should not being inifinite loop when using Table.Column with sortOrder', () => {
|
|
|
|
class Demo extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
this.setState({});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Table dataSource={[]}>
|
|
|
|
<Table.Column title="Age" dataIndex="age" sorter sortOrder="ascend" key="age" />
|
|
|
|
</Table>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(() => {
|
|
|
|
mount(<Demo />);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-11-05 01:03:22 +08:00
|
|
|
|
|
|
|
it('should support defaultOrder in Column', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table dataSource={[{ key: '1', age: 1 }]}>
|
|
|
|
<Table.Column title="Age" dataIndex="age" sorter defaultSortOrder="ascend" key="age" />
|
|
|
|
</Table>,
|
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-12-11 14:14:52 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/20096
|
|
|
|
it('invalidate sorter should not display sorter button', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
sorter: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
|
|
|
key: 'age',
|
|
|
|
sorter: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address',
|
|
|
|
dataIndex: 'address',
|
|
|
|
key: 'address',
|
|
|
|
sorter: undefined,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('.ant-table-column-sorter-inner')).toHaveLength(0);
|
|
|
|
});
|
2020-02-03 16:48:13 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/21193
|
|
|
|
it('table with sugar column', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table>
|
|
|
|
<Table.Column
|
|
|
|
title="Chinese Score"
|
|
|
|
dataIndex="chinese"
|
|
|
|
sorter={{
|
|
|
|
compare: (a, b) => a.chinese - b.chinese,
|
|
|
|
multiple: 3,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Table.Column
|
|
|
|
title="Math Score"
|
|
|
|
dataIndex="math"
|
|
|
|
sorter={{
|
|
|
|
compare: (a, b) => a.math - b.math,
|
|
|
|
multiple: 2,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Table>,
|
|
|
|
);
|
|
|
|
|
2020-05-05 21:08:56 +08:00
|
|
|
wrapper.find('th').first().simulate('click');
|
2020-02-03 16:48:13 +08:00
|
|
|
|
|
|
|
expect(wrapper.find('th.ant-table-column-sort')).toHaveLength(1);
|
|
|
|
});
|
2020-03-01 13:11:28 +08:00
|
|
|
|
|
|
|
it('surger should support sorterOrder', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table>
|
|
|
|
<Table.Column key="name" title="Name" dataIndex="name" sortOrder="ascend" sorter />
|
|
|
|
</Table>,
|
|
|
|
);
|
|
|
|
|
2020-05-05 21:08:56 +08:00
|
|
|
expect(wrapper.find('.ant-table-column-sorter-up').last().hasClass('active')).toBeTruthy();
|
|
|
|
expect(wrapper.find('.ant-table-column-sorter-down').last().hasClass('active')).toBeFalsy();
|
2020-03-01 13:11:28 +08:00
|
|
|
});
|
2020-03-20 23:33:14 +08:00
|
|
|
|
|
|
|
it('controlled multiple group', () => {
|
|
|
|
const groupColumns = [
|
|
|
|
{
|
|
|
|
title: 'Math Score',
|
|
|
|
dataIndex: 'math1',
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: { multiple: 1 },
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
title: 'math',
|
|
|
|
dataIndex: 'math',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'English Score',
|
|
|
|
dataIndex: 'english',
|
|
|
|
sortOrder: 'descend',
|
|
|
|
sorter: { multiple: 2 },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const groupData = [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
name: 'John Brown',
|
|
|
|
chinese: 98,
|
|
|
|
math: 60,
|
|
|
|
english: 70,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const wrapper = mount(<Table columns={groupColumns} data={groupData} />);
|
|
|
|
wrapper.update();
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-table-column-sorter-full')
|
|
|
|
.first()
|
|
|
|
.find('.ant-table-column-sorter-up')
|
|
|
|
.first()
|
|
|
|
.hasClass('active'),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
wrapper
|
|
|
|
.find('.ant-table-column-sorter-full')
|
|
|
|
.last()
|
|
|
|
.find('.ant-table-column-sorter-down')
|
|
|
|
.first()
|
|
|
|
.hasClass('active'),
|
|
|
|
).toBeTruthy();
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|