ant-design/components/table/__tests__/Table.sorter.test.js

154 lines
4.1 KiB
JavaScript
Raw Normal View History

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(tableProps, columnProps = {}) {
2016-12-14 14:48:09 +08:00
return (
<Table
columns={[{
...column,
...columnProps,
}]}
2016-12-14 14:48:09 +08:00
dataSource={data}
pagination={false}
{...tableProps}
2016-12-14 14:48:09 +08:00
/>
);
}
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
});
it('default sort order ascend', () => {
const wrapper = mount(createTable({}, {
defaultSortOrder: 'ascend',
}));
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
});
it('default sort order descend', () => {
const wrapper = mount(createTable({}, {
defaultSortOrder: 'descend',
}));
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
});
it('sort records', () => {
2016-12-14 14:48:09 +08:00
const wrapper = mount(createTable());
2018-09-17 19:03:06 +08:00
// descent
wrapper.find('.ant-table-column-sorters').simulate('click');
2016-12-14 14:48:09 +08:00
expect(renderedNames(wrapper)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
2018-09-17 19:03:06 +08:00
// ascent
wrapper.find('.ant-table-column-sorters').simulate('click');
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
2016-12-14 14:48:09 +08:00
});
it('can be controlled by sortOrder', () => {
const wrapper = mount(createTable({
columns: [{ ...column, sortOrder: 'ascend' }],
}));
expect(renderedNames(wrapper)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
});
it('provides sortOrder in the sorterFn', () => {
let actualSortOrder;
mount(createTable({ },
{
sortOrder: 'ascend',
sorter: (a, b, sortOrder) => {
actualSortOrder = sortOrder;
return sorterFn(a, b);
},
},
));
expect(actualSortOrder).toEqual('ascend');
});
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');
expect(sorter1.order).toBe('descend');
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');
expect(sorter2.order).toBe('ascend');
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);
expect(sorter3.field).toBe(undefined);
expect(sorter3.columnKey).toBe(undefined);
2016-12-14 14:48:09 +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
});