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';
|
2016-12-14 15:38:26 +08:00
|
|
|
import Table from '..';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { act, fireEvent, render } from '../../../tests/utils';
|
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' },
|
|
|
|
];
|
|
|
|
|
2022-06-07 10:14:32 +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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
function renderedNames(contain) {
|
|
|
|
const namesList = [];
|
|
|
|
contain
|
|
|
|
.querySelector('.ant-table-tbody')
|
|
|
|
.querySelectorAll('tr')
|
|
|
|
.forEach(tr => {
|
|
|
|
namesList.push(tr.querySelector('td').textContent);
|
|
|
|
});
|
|
|
|
return namesList;
|
2016-12-14 14:48:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
it('renders sorter icon correctly', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(createTable());
|
|
|
|
expect(container.querySelector('thead')).toMatchSnapshot();
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2017-11-09 19:30:24 +08:00
|
|
|
it('default sort order ascend', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2018-12-07 20:02:01 +08:00
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'ascend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2017-11-09 19:30:24 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
2017-11-09 19:30:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('default sort order descend', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2018-12-07 20:02:01 +08:00
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'descend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2017-11-09 19:30:24 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(container.querySelector('th').getAttribute('aria-sort')).toEqual('descending');
|
2022-01-11 13:18:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should change aria-sort when default sort order is set to descend', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2022-01-11 13:18:42 +08:00
|
|
|
createTable(
|
|
|
|
{
|
|
|
|
sortDirections: ['descend', 'ascend'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
defaultSortOrder: 'descend',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
|
|
|
// Test that it cycles through the order of sortDirections
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2017-11-09 19:30:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('sort records', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(createTable());
|
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
|
|
|
// first assert default state
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2018-11-21 00:27:11 +08:00
|
|
|
// ascend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2018-09-17 19:03:06 +08:00
|
|
|
|
2018-11-21 00:27:11 +08:00
|
|
|
// descend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2022-04-27 16:27:13 +08:00
|
|
|
it('sort records with keydown', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(createTable());
|
2022-04-27 16:27:13 +08:00
|
|
|
|
|
|
|
// ascend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.keyDown(container.querySelector('.ant-table-column-sorters'), { keyCode: 13 });
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
2022-04-27 16:27:13 +08:00
|
|
|
|
|
|
|
// descend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.keyDown(container.querySelector('.ant-table-column-sorters'), { keyCode: 13 });
|
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
2022-04-27 16:27:13 +08:00
|
|
|
});
|
|
|
|
|
2020-05-05 21:08:56 +08:00
|
|
|
describe('can be controlled by sortOrder', () => {
|
|
|
|
it('single', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2020-05-05 21:08:56 +08:00
|
|
|
createTable({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
}),
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
2020-05-05 21:08:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invalidate mix with single & multiple sorters', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2020-05-05 21:08:56 +08:00
|
|
|
createTable({
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: {
|
|
|
|
multiple: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
sortOrder: 'ascend',
|
|
|
|
sorter: {},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
2020-05-05 21:08:56 +08:00
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2018-04-29 22:08:28 +08:00
|
|
|
it('provides sortOrder in the sorterFn', () => {
|
|
|
|
let actualSortOrder;
|
2022-06-07 10:14:32 +08:00
|
|
|
render(
|
2018-12-07 20:02:01 +08:00
|
|
|
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', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container, rerender } = render(
|
2019-09-28 15:22:27 +08:00
|
|
|
createTable({
|
|
|
|
columns: [column],
|
|
|
|
}),
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
rerender(
|
|
|
|
createTable({
|
|
|
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
2019-09-28 15:22:27 +08:00
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('fires change event', () => {
|
|
|
|
const handleChange = jest.fn();
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(createTable({ onChange: handleChange }));
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2018-09-17 19:03:06 +08:00
|
|
|
// ascent
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
2018-09-17 19:03:06 +08:00
|
|
|
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');
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
2018-09-17 19:03:06 +08:00
|
|
|
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');
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
2018-09-17 19:03:06 +08:00
|
|
|
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();
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container, rerender } = render(createTable());
|
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
// default show sorter tooltip
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
2020-04-02 18:53:49 +08:00
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is false
|
2022-06-07 10:14:32 +08:00
|
|
|
rerender(createTable({ showSorterTooltip: false }));
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is false, column showSorterTooltip is true
|
2022-06-07 10:14:32 +08:00
|
|
|
rerender(
|
|
|
|
createTable({ showSorterTooltip: true, columns: [{ ...column, showSorterTooltip: true }] }),
|
|
|
|
);
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
2020-02-29 22:01:13 +08:00
|
|
|
});
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
// set table props showSorterTooltip is true, column showSorterTooltip is false
|
2022-06-07 10:14:32 +08:00
|
|
|
rerender(
|
|
|
|
createTable({
|
|
|
|
showSorterTooltip: true,
|
|
|
|
columns: [{ ...column, showSorterTooltip: false }],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
2020-02-29 22:01:13 +08:00
|
|
|
});
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
2020-02-29 22:01:13 +08:00
|
|
|
});
|
|
|
|
|
2021-01-26 09:33:29 +08:00
|
|
|
it('should show correct tooltip when showSorterTooltip is an object', () => {
|
|
|
|
// basically copied from 'hover header show sorter tooltip'
|
|
|
|
jest.useFakeTimers();
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container, rerender } = render(
|
2021-01-26 09:33:29 +08:00
|
|
|
createTable({ showSorterTooltip: { placement: 'bottom', title: 'static title' } }),
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
|
|
|
|
// Root to false
|
|
|
|
rerender(createTable({ showSorterTooltip: false }));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
|
|
|
|
|
|
|
// Column to true
|
|
|
|
rerender(
|
|
|
|
createTable({
|
|
|
|
showSorterTooltip: false,
|
|
|
|
columns: [{ ...column, showSorterTooltip: true }],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
2021-01-26 09:33:29 +08:00
|
|
|
});
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
|
|
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
|
|
|
|
// Column to false
|
|
|
|
rerender(
|
|
|
|
createTable({
|
|
|
|
showSorterTooltip: true,
|
|
|
|
columns: [{ ...column, showSorterTooltip: false }],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
2021-01-26 09:33:29 +08:00
|
|
|
});
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
2021-01-26 09:33:29 +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 },
|
|
|
|
];
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
2017-03-10 14:52:28 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
2017-03-10 14:52:28 +08:00
|
|
|
});
|
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 },
|
|
|
|
];
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
|
|
|
expect(container.querySelector('.custom-title').textContent).toEqual('');
|
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(container.querySelector('.custom-title').textContent).toEqual('ascend');
|
|
|
|
fireEvent.click(container.querySelector('.ant-table-column-sorters'));
|
|
|
|
expect(container.querySelector('.custom-title').textContent).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
|
2020-06-08 22:45:53 +08:00
|
|
|
it('should sort from beginning state when toggle from different columns', () => {
|
2018-09-21 16:22:29 +08:00
|
|
|
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 },
|
|
|
|
];
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelectorAll('.ant-table-column-has-sorters')[0];
|
|
|
|
const getAgeColumn = () => container.querySelectorAll('.ant-table-column-has-sorters')[1];
|
|
|
|
const getNameIcon = name => getNameColumn().querySelector(`.ant-table-column-sorter-${name}`);
|
|
|
|
const getAgeIcon = name => getAgeColumn().querySelector(`.ant-table-column-sorter-${name}`);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-09-21 16:22:29 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getNameIcon('up').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
|
|
|
expect(getAgeIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getAgeColumn().getAttribute('aria-sort')).toEqual(null);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-09-21 16:22:29 +08:00
|
|
|
// sort age
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getAgeColumn());
|
|
|
|
expect(getNameIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
|
|
|
expect(getAgeIcon('up').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getAgeColumn().getAttribute('aria-sort')).toEqual('ascending');
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<TableTest />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
|
|
|
const getIcon = name => getNameColumn().querySelector(`.ant-table-column-sorter-${name}`);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<TableTest />);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
|
|
|
const getIcon = name => getNameColumn().querySelector(`.ant-table-column-sorter-${name}`);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeTruthy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-11-01 13:42:53 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(getIcon('up').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getIcon('down').className.includes('active')).toBeFalsy();
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<TableTest />);
|
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-up').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-down').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-up').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeTruthy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-down').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-up').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-down').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeTruthy();
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2018-10-10 11:23:44 +08:00
|
|
|
// sort name
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-up').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2018-12-07 20:02:01 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
getNameColumn().querySelector('.ant-table-column-sorter-down').className.includes('active'),
|
2019-11-15 14:35:25 +08:00
|
|
|
).toBeFalsy();
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
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', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2018-12-22 18:16:02 +08:00
|
|
|
createTable({
|
|
|
|
sortDirections: ['descend', 'ascend'],
|
|
|
|
}),
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
// descend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
// ascend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
// cancel sort
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2018-12-22 18:16:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should first sort by descend, then cancel sort', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2018-12-22 18:16:02 +08:00
|
|
|
createTable({
|
|
|
|
sortDirections: ['descend'],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
|
|
|
// default
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2018-12-22 18:16:02 +08:00
|
|
|
// descend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
// cancel sort
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2018-12-22 18:16:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should first sort by descend, then cancel sort. (column prop)', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2018-12-22 18:16:02 +08:00
|
|
|
createTable(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
sortDirections: ['descend'],
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
|
|
|
// default
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2018-12-22 18:16:02 +08:00
|
|
|
// descend
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('descending');
|
2018-12-22 18:16:02 +08:00
|
|
|
|
|
|
|
// cancel sort
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
|
|
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2018-12-22 18:16:02 +08:00
|
|
|
});
|
2019-06-10 11:59:46 +08:00
|
|
|
|
|
|
|
it('pagination back', () => {
|
|
|
|
const onPageChange = jest.fn();
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2019-06-10 11:59:46 +08:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const getNameColumn = () => container.querySelector('th');
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual(null);
|
2022-01-11 13:18:42 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(getNameColumn());
|
2019-12-28 21:52:09 +08:00
|
|
|
expect(onChange.mock.calls[0][0].current).toBe(2);
|
|
|
|
expect(onPageChange).not.toHaveBeenCalled();
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(getNameColumn().getAttribute('aria-sort')).toEqual('ascending');
|
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();
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2019-09-28 15:22:27 +08:00
|
|
|
<Table columns={[{ title: 'title', onHeaderCell: () => ({ onClick }), sorter: true }]} />,
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('th'));
|
2019-09-28 15:22:27 +08:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
2019-09-28 18:14:28 +08:00
|
|
|
|
|
|
|
it('could sort data with children', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2019-09-28 18:14:28 +08:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(renderedNames(container)).toEqual(['Brown', 'Green', 'Mike', 'Alex', 'Petter', 'Zoe']);
|
2019-09-28 18:14:28 +08:00
|
|
|
});
|
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(() => {
|
2022-06-07 10:14:32 +08:00
|
|
|
render(<Demo />);
|
2019-11-05 00:40:03 +08:00
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-11-05 01:03:22 +08:00
|
|
|
|
|
|
|
it('should support defaultOrder in Column', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { asFragment } = render(
|
2019-11-05 01:03:22 +08:00
|
|
|
<Table dataSource={[{ key: '1', age: 1 }]}>
|
|
|
|
<Table.Column title="Age" dataIndex="age" sorter defaultSortOrder="ascend" key="age" />
|
|
|
|
</Table>,
|
|
|
|
);
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-11-05 01:03:22 +08:00
|
|
|
});
|
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', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2019-12-11 14:14:52 +08:00
|
|
|
<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,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelectorAll('.ant-table-column-sorter-inner')).toHaveLength(0);
|
2019-12-11 14:14:52 +08:00
|
|
|
});
|
2020-02-03 16:48:13 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/21193
|
|
|
|
it('table with sugar column', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2020-02-03 16:48:13 +08:00
|
|
|
<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>,
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelector('th'));
|
2020-02-03 16:48:13 +08:00
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(container.querySelectorAll('th.ant-table-column-sort')).toHaveLength(1);
|
2020-02-03 16:48:13 +08:00
|
|
|
});
|
2020-03-01 13:11:28 +08:00
|
|
|
|
|
|
|
it('surger should support sorterOrder', () => {
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
2020-03-01 13:11:28 +08:00
|
|
|
<Table>
|
|
|
|
<Table.Column key="name" title="Name" dataIndex="name" sortOrder="ascend" sorter />
|
|
|
|
</Table>,
|
|
|
|
);
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
expect(
|
|
|
|
container.querySelector('.ant-table-column-sorter-up').className.includes('active'),
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
|
|
|
container.querySelector('.ant-table-column-sorter-down').className.includes('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,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(<Table columns={groupColumns} data={groupData} />);
|
|
|
|
|
2020-03-20 23:33:14 +08:00
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
container
|
|
|
|
.querySelectorAll('.ant-table-column-sorter-full')[0]
|
|
|
|
.querySelector('.ant-table-column-sorter-up')
|
|
|
|
.className.includes('active'),
|
2020-03-20 23:33:14 +08:00
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
2022-06-07 10:14:32 +08:00
|
|
|
container
|
|
|
|
.querySelectorAll('.ant-table-column-sorter-full')[1]
|
|
|
|
.querySelector('.ant-table-column-sorter-down')
|
|
|
|
.className.includes('active'),
|
2020-03-20 23:33:14 +08:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
2020-06-08 22:45:53 +08:00
|
|
|
|
|
|
|
it('onChange with correct sorter for multiple', () => {
|
|
|
|
const groupColumns = [
|
|
|
|
{
|
|
|
|
title: 'Math Score',
|
|
|
|
dataIndex: 'math',
|
|
|
|
sorter: { multiple: 1 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'English Score',
|
|
|
|
dataIndex: 'english',
|
|
|
|
sorter: { multiple: 2 },
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const groupData = [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
name: 'John Brown',
|
|
|
|
chinese: 98,
|
|
|
|
math: 60,
|
|
|
|
english: 70,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const onChange = jest.fn();
|
2022-06-07 10:14:32 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Table columns={groupColumns} data={groupData} onChange={onChange} />,
|
|
|
|
);
|
2020-06-08 22:45:53 +08:00
|
|
|
|
|
|
|
function clickToMatchExpect(index, sorter) {
|
2022-06-07 10:14:32 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-table-column-sorters')[index]);
|
2020-06-08 22:45:53 +08:00
|
|
|
|
|
|
|
expect(onChange).toHaveBeenCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
expect.objectContaining(sorter),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
|
|
|
|
onChange.mockReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// First
|
|
|
|
clickToMatchExpect(0, { field: 'math', order: 'ascend' });
|
|
|
|
clickToMatchExpect(0, { field: 'math', order: 'descend' });
|
|
|
|
clickToMatchExpect(0, { field: 'math', order: undefined });
|
|
|
|
|
|
|
|
// Last
|
|
|
|
clickToMatchExpect(1, { field: 'english', order: 'ascend' });
|
|
|
|
clickToMatchExpect(1, { field: 'english', order: 'descend' });
|
|
|
|
clickToMatchExpect(1, { field: 'english', order: undefined });
|
|
|
|
});
|
2022-08-17 13:21:44 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/37024
|
|
|
|
it('multiple sort should pass array sorter as onChange param', () => {
|
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Chinese Score',
|
|
|
|
dataIndex: 'chinese',
|
|
|
|
sorter: {
|
|
|
|
compare: (a, b) => a.chinese - b.chinese,
|
|
|
|
multiple: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Math Score',
|
|
|
|
dataIndex: 'math',
|
|
|
|
sorter: {
|
|
|
|
compare: (a, b) => a.math - b.math,
|
|
|
|
multiple: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'English Score',
|
|
|
|
dataIndex: 'english',
|
|
|
|
sorter: {
|
|
|
|
compare: (a, b) => a.english - b.english,
|
|
|
|
multiple: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const tableData = [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
name: 'John Brown',
|
|
|
|
chinese: 98,
|
|
|
|
math: 60,
|
|
|
|
english: 70,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
name: 'Jim Green',
|
|
|
|
chinese: 98,
|
|
|
|
math: 66,
|
|
|
|
english: 89,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
name: 'Joe Black',
|
|
|
|
chinese: 98,
|
|
|
|
math: 90,
|
|
|
|
english: 70,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
name: 'Jim Red',
|
|
|
|
chinese: 88,
|
|
|
|
math: 99,
|
|
|
|
english: 89,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<Table columns={columns} dataSource={tableData} onChange={onChange} />,
|
|
|
|
);
|
|
|
|
const sorterColumns = Array.from(container.querySelectorAll('.ant-table-column-has-sorters'));
|
|
|
|
expect(sorterColumns.length).toBe(3);
|
|
|
|
fireEvent.click(sorterColumns[0]);
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
expect.objectContaining({ field: 'chinese' }),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
fireEvent.click(sorterColumns[1]);
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({ field: 'chinese' }),
|
|
|
|
expect.objectContaining({ field: 'math' }),
|
|
|
|
]),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
fireEvent.click(sorterColumns[2]);
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith(
|
|
|
|
expect.anything(),
|
|
|
|
expect.anything(),
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({ field: 'chinese' }),
|
|
|
|
expect.objectContaining({ field: 'math' }),
|
|
|
|
expect.objectContaining({ field: 'english' }),
|
|
|
|
]),
|
|
|
|
expect.anything(),
|
|
|
|
);
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|