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',
|
|
|
|
sorter: sorterFn,
|
|
|
|
};
|
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
function createTable(props) {
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={[column]}
|
|
|
|
dataSource={data}
|
|
|
|
pagination={false}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderedNames(wrapper) {
|
|
|
|
return wrapper.find('TableRow').map(row => row.props().record.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-20 16:26:18 +08:00
|
|
|
it.only('sort records', () => {
|
2016-12-14 14:48:09 +08:00
|
|
|
const wrapper = mount(createTable());
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorter-up').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorter-down').simulate('click');
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be controlled by sortOrder', () => {
|
|
|
|
const wrapper = mount(createTable({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
}));
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change event', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(createTable({ onChange: handleChange }));
|
|
|
|
|
|
|
|
wrapper.find('.ant-table-column-sorter-up').simulate('click');
|
|
|
|
|
|
|
|
const sorter = handleChange.mock.calls[0][2];
|
|
|
|
expect(sorter.column.dataIndex).toBe('name');
|
|
|
|
expect(sorter.order).toBe('ascend');
|
|
|
|
expect(sorter.field).toBe('name');
|
|
|
|
expect(sorter.columnKey).toBe('name');
|
|
|
|
});
|
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 },
|
|
|
|
];
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table columns={columns} dataSource={testData} />
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|