You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1081 lines
34 KiB
1081 lines
34 KiB
6 years ago
|
/* eslint-disable react/no-multi-comp */
|
||
8 years ago
|
import React from 'react';
|
||
2 years ago
|
import type { ColumnType, TableProps } from '..';
|
||
8 years ago
|
import Table from '..';
|
||
2 years ago
|
import { act, fireEvent, render } from '../../../tests/utils';
|
||
2 years ago
|
import type { ColumnsType, SortOrder } from '../interface';
|
||
8 years ago
|
|
||
|
describe('Table.sorter', () => {
|
||
2 years ago
|
const sorterFn: ColumnType<any>['sorter'] = (a, b) =>
|
||
|
a.name[0].charCodeAt() - b.name[0].charCodeAt();
|
||
8 years ago
|
|
||
2 years ago
|
const column: ColumnType<any> = {
|
||
8 years ago
|
title: 'Name',
|
||
|
dataIndex: 'name',
|
||
5 years ago
|
key: 'name',
|
||
8 years ago
|
sorter: sorterFn,
|
||
|
};
|
||
|
|
||
|
const data = [
|
||
|
{ key: 0, name: 'Jack' },
|
||
|
{ key: 1, name: 'Lucy' },
|
||
|
{ key: 2, name: 'Tom' },
|
||
|
{ key: 3, name: 'Jerry' },
|
||
|
];
|
||
|
|
||
2 years ago
|
function createTable(tableProps: TableProps<any> = {}, columnProps = {}) {
|
||
8 years ago
|
return (
|
||
|
<Table
|
||
2 years ago
|
columns={[{ ...column, ...columnProps }]}
|
||
8 years ago
|
dataSource={data}
|
||
|
pagination={false}
|
||
7 years ago
|
{...tableProps}
|
||
8 years ago
|
/>
|
||
|
);
|
||
|
}
|
||
|
|
||
2 years ago
|
function renderedNames(container: ReturnType<typeof render>['container']) {
|
||
|
const namesList: (Node['textContent'] | undefined)[] = [];
|
||
|
container
|
||
|
?.querySelector('.ant-table-tbody')
|
||
|
?.querySelectorAll('tr')
|
||
|
?.forEach(tr => {
|
||
|
namesList.push(tr.querySelector('td')?.textContent);
|
||
3 years ago
|
});
|
||
|
return namesList;
|
||
8 years ago
|
}
|
||
|
|
||
|
it('renders sorter icon correctly', () => {
|
||
3 years ago
|
const { container } = render(createTable());
|
||
|
expect(container.querySelector('thead')).toMatchSnapshot();
|
||
8 years ago
|
});
|
||
|
|
||
7 years ago
|
it('default sort order ascend', () => {
|
||
2 years ago
|
const { container } = render(createTable({}, { defaultSortOrder: 'ascend' }));
|
||
7 years ago
|
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
7 years ago
|
});
|
||
|
|
||
|
it('default sort order descend', () => {
|
||
2 years ago
|
const { container } = render(createTable({}, { defaultSortOrder: 'descend' }));
|
||
7 years ago
|
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(container.querySelector('th')?.getAttribute('aria-sort')).toEqual('descending');
|
||
3 years ago
|
});
|
||
|
|
||
|
it('should change aria-sort when default sort order is set to descend', () => {
|
||
3 years ago
|
const { container } = render(
|
||
2 years ago
|
createTable({ sortDirections: ['descend', 'ascend'] }, { defaultSortOrder: 'descend' }),
|
||
3 years ago
|
);
|
||
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
3 years ago
|
|
||
|
// Test that it cycles through the order of sortDirections
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
3 years ago
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
3 years ago
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
7 years ago
|
});
|
||
|
|
||
2 years ago
|
it('should have aria-lable if the column is sortable and is not sorted', () => {
|
||
|
const { container } = render(
|
||
|
createTable(
|
||
|
{
|
||
|
sortDirections: ['descend', 'ascend'],
|
||
|
},
|
||
|
{
|
||
|
defaultSortOrder: 'descend',
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
|
||
|
const getNameColumn = () => container.querySelector('th');
|
||
|
|
||
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
|
expect(getNameColumn()?.getAttribute('aria-label')).toEqual(null);
|
||
2 years ago
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
|
expect(getNameColumn()?.getAttribute('aria-label')).toEqual(null);
|
||
2 years ago
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
|
expect(getNameColumn()?.getAttribute('aria-label')).toEqual('Name sortable');
|
||
2 years ago
|
});
|
||
|
|
||
7 years ago
|
it('sort records', () => {
|
||
3 years ago
|
const { container } = render(createTable());
|
||
|
const getNameColumn = () => container.querySelector('th');
|
||
3 years ago
|
|
||
|
// first assert default state
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
3 years ago
|
|
||
6 years ago
|
// ascend
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
6 years ago
|
|
||
6 years ago
|
// descend
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
8 years ago
|
});
|
||
|
|
||
3 years ago
|
it('sort records with keydown', () => {
|
||
3 years ago
|
const { container } = render(createTable());
|
||
3 years ago
|
|
||
|
// ascend
|
||
2 years ago
|
fireEvent.keyDown(container.querySelector('.ant-table-column-sorters')!, { keyCode: 13 });
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
3 years ago
|
|
||
|
// descend
|
||
2 years ago
|
fireEvent.keyDown(container.querySelector('.ant-table-column-sorters')!, { keyCode: 13 });
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
3 years ago
|
});
|
||
|
|
||
5 years ago
|
describe('can be controlled by sortOrder', () => {
|
||
|
it('single', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
createTable({
|
||
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
||
|
}),
|
||
|
);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
5 years ago
|
});
|
||
|
|
||
|
it('invalidate mix with single & multiple sorters', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
createTable({
|
||
|
columns: [
|
||
|
{
|
||
|
title: 'Name',
|
||
|
dataIndex: 'name',
|
||
|
sortOrder: 'ascend',
|
||
|
sorter: {
|
||
|
multiple: 1,
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
title: 'Name',
|
||
|
dataIndex: 'name',
|
||
|
sortOrder: 'ascend',
|
||
|
sorter: {},
|
||
|
},
|
||
|
],
|
||
|
}),
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
5 years ago
|
});
|
||
8 years ago
|
});
|
||
|
|
||
7 years ago
|
it('provides sortOrder in the sorterFn', () => {
|
||
2 years ago
|
let actualSortOrder: SortOrder;
|
||
3 years ago
|
render(
|
||
6 years ago
|
createTable(
|
||
|
{},
|
||
|
{
|
||
|
sortOrder: 'ascend',
|
||
2 years ago
|
sorter: (a: any, b: any, sortOrder: SortOrder) => {
|
||
6 years ago
|
actualSortOrder = sortOrder;
|
||
|
return sorterFn(a, b);
|
||
|
},
|
||
|
},
|
||
|
),
|
||
|
);
|
||
2 years ago
|
expect(actualSortOrder!).toEqual('ascend');
|
||
7 years ago
|
});
|
||
|
|
||
5 years ago
|
it('can update column sortOrder', () => {
|
||
3 years ago
|
const { container, rerender } = render(
|
||
5 years ago
|
createTable({
|
||
|
columns: [column],
|
||
|
}),
|
||
|
);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
|
rerender(
|
||
|
createTable({
|
||
|
columns: [{ ...column, sortOrder: 'ascend' }],
|
||
|
}),
|
||
|
);
|
||
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
5 years ago
|
});
|
||
|
|
||
8 years ago
|
it('fires change event', () => {
|
||
|
const handleChange = jest.fn();
|
||
3 years ago
|
const { container } = render(createTable({ onChange: handleChange }));
|
||
8 years ago
|
|
||
6 years ago
|
// ascent
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
6 years ago
|
const sorter1 = handleChange.mock.calls[0][2];
|
||
|
expect(sorter1.column.dataIndex).toBe('name');
|
||
6 years ago
|
expect(sorter1.order).toBe('ascend');
|
||
6 years ago
|
expect(sorter1.field).toBe('name');
|
||
|
expect(sorter1.columnKey).toBe('name');
|
||
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
6 years ago
|
const sorter2 = handleChange.mock.calls[1][2];
|
||
|
expect(sorter2.column.dataIndex).toBe('name');
|
||
6 years ago
|
expect(sorter2.order).toBe('descend');
|
||
6 years ago
|
expect(sorter2.field).toBe('name');
|
||
|
expect(sorter2.columnKey).toBe('name');
|
||
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('.ant-table-column-sorters')!);
|
||
6 years ago
|
const sorter3 = handleChange.mock.calls[2][2];
|
||
|
expect(sorter3.column).toBe(undefined);
|
||
|
expect(sorter3.order).toBe(undefined);
|
||
5 years ago
|
expect(sorter3.field).toBe('name');
|
||
|
expect(sorter3.columnKey).toBe('name');
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
5 years ago
|
it('hover header show sorter tooltip', () => {
|
||
|
// tooltip has delay
|
||
|
jest.useFakeTimers();
|
||
3 years ago
|
const { container, rerender } = render(createTable());
|
||
|
|
||
5 years ago
|
// default show sorter tooltip
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
|
||
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
5 years ago
|
|
||
5 years ago
|
// set table props showSorterTooltip is false
|
||
3 years ago
|
rerender(createTable({ showSorterTooltip: false }));
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
|
||
5 years ago
|
// set table props showSorterTooltip is false, column showSorterTooltip is true
|
||
3 years ago
|
rerender(
|
||
|
createTable({ showSorterTooltip: true, columns: [{ ...column, showSorterTooltip: true }] }),
|
||
|
);
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
5 years ago
|
});
|
||
3 years ago
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
|
||
5 years ago
|
// set table props showSorterTooltip is true, column showSorterTooltip is false
|
||
3 years ago
|
rerender(
|
||
|
createTable({
|
||
|
showSorterTooltip: true,
|
||
|
columns: [{ ...column, showSorterTooltip: false }],
|
||
|
}),
|
||
|
);
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
5 years ago
|
});
|
||
3 years ago
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
5 years ago
|
});
|
||
|
|
||
4 years ago
|
it('should show correct tooltip when showSorterTooltip is an object', () => {
|
||
|
// basically copied from 'hover header show sorter tooltip'
|
||
|
jest.useFakeTimers();
|
||
3 years ago
|
const { container, rerender } = render(
|
||
4 years ago
|
createTable({ showSorterTooltip: { placement: 'bottom', title: 'static title' } }),
|
||
|
);
|
||
3 years ago
|
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
|
});
|
||
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
|
||
|
// 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 }],
|
||
|
}),
|
||
|
);
|
||
2 years ago
|
fireEvent.mouseEnter(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
4 years ago
|
});
|
||
3 years ago
|
expect(container.querySelector('.ant-tooltip-open')).toBeTruthy();
|
||
2 years ago
|
fireEvent.mouseOut(container.querySelector('.ant-table-column-sorters')!);
|
||
3 years ago
|
|
||
|
// Column to false
|
||
|
rerender(
|
||
|
createTable({
|
||
|
showSorterTooltip: true,
|
||
|
columns: [{ ...column, showSorterTooltip: false }],
|
||
|
}),
|
||
|
);
|
||
|
act(() => {
|
||
|
jest.runAllTimers();
|
||
4 years ago
|
});
|
||
3 years ago
|
expect(container.querySelector('.ant-tooltip-open')).toBeFalsy();
|
||
4 years ago
|
});
|
||
|
|
||
8 years ago
|
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 },
|
||
|
];
|
||
3 years ago
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
||
8 years ago
|
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
8 years ago
|
});
|
||
6 years ago
|
|
||
|
// https://github.com/ant-design/ant-design/issues/11246#issuecomment-405009167
|
||
|
it('Allow column title as render props with sortOrder argument', () => {
|
||
2 years ago
|
const title = ({ sortOrder }: { sortOrder: SortOrder }) => (
|
||
|
<div className="custom-title">{sortOrder}</div>
|
||
|
);
|
||
|
const columns = [{ title, key: 'group', sorter: true }];
|
||
6 years ago
|
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 },
|
||
|
];
|
||
3 years ago
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
||
2 years ago
|
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');
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
// https://github.com/ant-design/ant-design/pull/12264#discussion_r218053034
|
||
5 years ago
|
it('should sort from beginning state when toggle from different columns', () => {
|
||
6 years ago
|
const columns = [
|
||
2 years ago
|
{ title: 'name', dataIndex: 'name', sorter: true },
|
||
|
{ title: 'age', dataIndex: 'age', sorter: true },
|
||
6 years ago
|
];
|
||
|
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 },
|
||
|
];
|
||
3 years ago
|
const { container } = render(<Table columns={columns} dataSource={testData} />);
|
||
5 years ago
|
|
||
2 years ago
|
const getNameColumn = () =>
|
||
|
container.querySelectorAll<HTMLElement>('.ant-table-column-has-sorters')[0];
|
||
|
const getAgeColumn = () =>
|
||
|
container.querySelectorAll<HTMLElement>('.ant-table-column-has-sorters')[1];
|
||
|
const getNameIcon = (name: string) =>
|
||
|
getNameColumn()?.querySelector(`.ant-table-column-sorter-${name}`);
|
||
|
const getAgeIcon = (name: string) =>
|
||
|
getAgeColumn().querySelector(`.ant-table-column-sorter-${name}`);
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getNameIcon('up')?.className.includes('active')).toBeTruthy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
|
expect(getAgeIcon('up')?.className.includes('active')).toBeFalsy();
|
||
3 years ago
|
expect(getAgeColumn().getAttribute('aria-sort')).toEqual(null);
|
||
5 years ago
|
|
||
6 years ago
|
// sort age
|
||
3 years ago
|
fireEvent.click(getAgeColumn());
|
||
2 years ago
|
expect(getNameIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
|
expect(getAgeIcon('up')?.className.includes('active')).toBeTruthy();
|
||
3 years ago
|
expect(getAgeColumn().getAttribute('aria-sort')).toEqual('ascending');
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
// 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 {
|
||
2 years ago
|
state = { pagination: {} };
|
||
6 years ago
|
|
||
2 years ago
|
onChange: TableProps<any>['onChange'] = pagination => {
|
||
6 years ago
|
this.setState({ pagination });
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
|
render() {
|
||
2 years ago
|
const columns = [{ title: 'name', dataIndex: 'name', sorter: true }];
|
||
6 years ago
|
const { pagination } = this.state;
|
||
|
return (
|
||
|
<Table
|
||
|
columns={columns}
|
||
|
pagination={pagination}
|
||
|
dataSource={testData}
|
||
|
onChange={this.onChange}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
const { container } = render(<TableTest />);
|
||
5 years ago
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
2 years ago
|
const getIcon = (name: string) =>
|
||
|
getNameColumn()?.querySelector(`.ant-table-column-sorter-${name}`);
|
||
5 years ago
|
|
||
2 years ago
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeTruthy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeTruthy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
|
|
||
|
// https://github.com/ant-design/ant-design/issues/12737
|
||
5 years ago
|
// https://github.com/ant-design/ant-design/issues/19398
|
||
6 years ago
|
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: {},
|
||
|
};
|
||
|
|
||
2 years ago
|
onChange: TableProps<any>['onChange'] = pagination => {
|
||
6 years ago
|
this.setState({ pagination });
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
|
render() {
|
||
6 years ago
|
const columns = [
|
||
|
{
|
||
|
title: 'name',
|
||
|
dataIndex: 'name',
|
||
|
sorter: true,
|
||
2 years ago
|
render: (text: string) => text,
|
||
5 years ago
|
array: ['1', '2', 3],
|
||
6 years ago
|
},
|
||
2 years ago
|
] as unknown as TableProps<any>['columns'];
|
||
6 years ago
|
const { pagination } = this.state;
|
||
|
return (
|
||
|
<Table
|
||
|
columns={columns}
|
||
|
pagination={pagination}
|
||
|
dataSource={testData}
|
||
|
onChange={this.onChange}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
const { container } = render(<TableTest />);
|
||
5 years ago
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
2 years ago
|
const getIcon = (name: string) =>
|
||
|
getNameColumn()?.querySelector(`.ant-table-column-sorter-${name}`);
|
||
5 years ago
|
|
||
2 years ago
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeTruthy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeTruthy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
|
expect(getIcon('up')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getIcon('down')?.className.includes('active')).toBeFalsy();
|
||
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
|
|
||
|
// 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: {},
|
||
|
};
|
||
|
|
||
2 years ago
|
onChange: TableProps<any>['onChange'] = pagination => {
|
||
6 years ago
|
this.setState({ pagination });
|
||
6 years ago
|
};
|
||
6 years ago
|
|
||
|
render() {
|
||
6 years ago
|
const columns = [
|
||
|
{
|
||
|
title: 'name',
|
||
|
dataIndex: 'name',
|
||
|
sorter: true,
|
||
|
key: 'a',
|
||
|
style: {
|
||
|
fontSize: 18,
|
||
|
},
|
||
6 years ago
|
},
|
||
6 years ago
|
];
|
||
6 years ago
|
const { pagination } = this.state;
|
||
|
return (
|
||
|
<Table
|
||
|
columns={columns}
|
||
|
pagination={pagination}
|
||
|
dataSource={testData}
|
||
|
onChange={this.onChange}
|
||
|
/>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
const { container } = render(<TableTest />);
|
||
|
const getNameColumn = () => container.querySelector('th');
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-up')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-down')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-up')?.className.includes('active'),
|
||
5 years ago
|
).toBeTruthy();
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-down')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-up')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-down')?.className.includes('active'),
|
||
5 years ago
|
).toBeTruthy();
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
5 years ago
|
|
||
6 years ago
|
// sort name
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-up')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
6 years ago
|
expect(
|
||
2 years ago
|
getNameColumn()?.querySelector('.ant-table-column-sorter-down')?.className.includes('active'),
|
||
5 years ago
|
).toBeFalsy();
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
it('should first sort by descend, then ascend, then cancel sort', () => {
|
||
2 years ago
|
const { container } = render(createTable({ sortDirections: ['descend', 'ascend'] }));
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
6 years ago
|
|
||
|
// descend
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
6 years ago
|
|
||
|
// ascend
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Jerry', 'Lucy', 'Tom']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
6 years ago
|
|
||
|
// cancel sort
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
|
|
||
|
it('should first sort by descend, then cancel sort', () => {
|
||
3 years ago
|
const { container } = render(
|
||
6 years ago
|
createTable({
|
||
|
sortDirections: ['descend'],
|
||
|
}),
|
||
|
);
|
||
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
3 years ago
|
|
||
|
// default
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
3 years ago
|
|
||
6 years ago
|
// descend
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
6 years ago
|
|
||
|
// cancel sort
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
|
|
||
|
it('should first sort by descend, then cancel sort. (column prop)', () => {
|
||
3 years ago
|
const { container } = render(
|
||
6 years ago
|
createTable(
|
||
|
{},
|
||
|
{
|
||
|
sortDirections: ['descend'],
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
3 years ago
|
|
||
|
// default
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
3 years ago
|
|
||
6 years ago
|
// descend
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Tom', 'Lucy', 'Jack', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('descending');
|
||
6 years ago
|
|
||
|
// cancel sort
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
6 years ago
|
});
|
||
6 years ago
|
|
||
|
it('pagination back', () => {
|
||
|
const onPageChange = jest.fn();
|
||
|
const onChange = jest.fn();
|
||
|
|
||
3 years ago
|
const { container } = render(
|
||
6 years ago
|
createTable({
|
||
|
pagination: {
|
||
|
pageSize: 2,
|
||
5 years ago
|
defaultCurrent: 2,
|
||
6 years ago
|
onChange: onPageChange,
|
||
|
},
|
||
|
onChange,
|
||
|
}),
|
||
|
);
|
||
|
|
||
3 years ago
|
const getNameColumn = () => container.querySelector('th');
|
||
3 years ago
|
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual(null);
|
||
3 years ago
|
|
||
2 years ago
|
fireEvent.click(getNameColumn()!);
|
||
5 years ago
|
expect(onChange.mock.calls[0][0].current).toBe(2);
|
||
|
expect(onPageChange).not.toHaveBeenCalled();
|
||
2 years ago
|
expect(getNameColumn()?.getAttribute('aria-sort')).toEqual('ascending');
|
||
6 years ago
|
});
|
||
5 years ago
|
|
||
|
it('should support onHeaderCell in sort column', () => {
|
||
|
const onClick = jest.fn();
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
<Table columns={[{ title: 'title', onHeaderCell: () => ({ onClick }), sorter: true }]} />,
|
||
|
);
|
||
2 years ago
|
fireEvent.click(container.querySelector('th')!);
|
||
5 years ago
|
expect(onClick).toHaveBeenCalled();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('could sort data with children', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
createTable(
|
||
|
{
|
||
5 years ago
|
defaultExpandAllRows: true,
|
||
5 years ago
|
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',
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
2 years ago
|
{ defaultSortOrder: 'ascend' },
|
||
5 years ago
|
),
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(renderedNames(container)).toEqual(['Brown', 'Green', 'Mike', 'Alex', 'Petter', 'Zoe']);
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
// 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(() => {
|
||
3 years ago
|
render(<Demo />);
|
||
5 years ago
|
}).not.toThrow();
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should support defaultOrder in Column', () => {
|
||
3 years ago
|
const { asFragment } = render(
|
||
5 years ago
|
<Table dataSource={[{ key: '1', age: 1 }]}>
|
||
|
<Table.Column title="Age" dataIndex="age" sorter defaultSortOrder="ascend" key="age" />
|
||
|
</Table>,
|
||
|
);
|
||
3 years ago
|
expect(asFragment().firstChild).toMatchSnapshot();
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
// https://github.com/ant-design/ant-design/issues/20096
|
||
|
it('invalidate sorter should not display sorter button', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
<Table
|
||
|
columns={[
|
||
|
{
|
||
|
title: 'Name',
|
||
|
dataIndex: 'name',
|
||
|
key: 'name',
|
||
|
sorter: false,
|
||
|
},
|
||
|
{
|
||
|
title: 'Age',
|
||
|
dataIndex: 'age',
|
||
|
key: 'age',
|
||
2 years ago
|
sorter: null as unknown as ColumnType<any>['sorter'],
|
||
5 years ago
|
},
|
||
|
{
|
||
|
title: 'Address',
|
||
|
dataIndex: 'address',
|
||
|
key: 'address',
|
||
|
sorter: undefined,
|
||
|
},
|
||
|
]}
|
||
|
/>,
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(container.querySelectorAll('.ant-table-column-sorter-inner')).toHaveLength(0);
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
// https://github.com/ant-design/ant-design/issues/21193
|
||
|
it('table with sugar column', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
<Table>
|
||
|
<Table.Column
|
||
|
title="Chinese Score"
|
||
|
dataIndex="chinese"
|
||
|
sorter={{
|
||
2 years ago
|
compare: (a: any, b: any) => a.chinese - b.chinese,
|
||
5 years ago
|
multiple: 3,
|
||
|
}}
|
||
|
/>
|
||
|
<Table.Column
|
||
|
title="Math Score"
|
||
|
dataIndex="math"
|
||
|
sorter={{
|
||
2 years ago
|
compare: (a: any, b: any) => a.math - b.math,
|
||
5 years ago
|
multiple: 2,
|
||
|
}}
|
||
|
/>
|
||
|
</Table>,
|
||
|
);
|
||
|
|
||
2 years ago
|
fireEvent.click(container.querySelector('th')!);
|
||
5 years ago
|
|
||
3 years ago
|
expect(container.querySelectorAll('th.ant-table-column-sort')).toHaveLength(1);
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
it('surger should support sorterOrder', () => {
|
||
3 years ago
|
const { container } = render(
|
||
5 years ago
|
<Table>
|
||
|
<Table.Column key="name" title="Name" dataIndex="name" sortOrder="ascend" sorter />
|
||
|
</Table>,
|
||
|
);
|
||
|
|
||
3 years ago
|
expect(
|
||
2 years ago
|
container.querySelector('.ant-table-column-sorter-up')?.className.includes('active'),
|
||
3 years ago
|
).toBeTruthy();
|
||
|
expect(
|
||
2 years ago
|
container.querySelector('.ant-table-column-sorter-down')?.className.includes('active'),
|
||
3 years ago
|
).toBeFalsy();
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
it('controlled multiple group', () => {
|
||
2 years ago
|
const groupColumns: ColumnsType = [
|
||
5 years ago
|
{
|
||
|
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,
|
||
|
},
|
||
|
];
|
||
2 years ago
|
const dataProp = { data: groupData };
|
||
|
const { container } = render(<Table columns={groupColumns} {...dataProp} />);
|
||
3 years ago
|
|
||
5 years ago
|
expect(
|
||
3 years ago
|
container
|
||
2 years ago
|
?.querySelectorAll('.ant-table-column-sorter-full')?.[0]
|
||
|
?.querySelector('.ant-table-column-sorter-up')
|
||
|
?.className.includes('active'),
|
||
5 years ago
|
).toBeTruthy();
|
||
|
expect(
|
||
3 years ago
|
container
|
||
2 years ago
|
?.querySelectorAll('.ant-table-column-sorter-full')?.[1]
|
||
|
?.querySelector('.ant-table-column-sorter-down')
|
||
|
?.className.includes('active'),
|
||
5 years ago
|
).toBeTruthy();
|
||
|
});
|
||
5 years ago
|
|
||
|
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();
|
||
2 years ago
|
const dataProp = { data: groupData };
|
||
3 years ago
|
const { container } = render(
|
||
2 years ago
|
<Table columns={groupColumns} {...dataProp} onChange={onChange} />,
|
||
3 years ago
|
);
|
||
5 years ago
|
|
||
2 years ago
|
function clickToMatchExpect(index: number, sorter: { field: string; order: SortOrder }) {
|
||
3 years ago
|
fireEvent.click(container.querySelectorAll('.ant-table-column-sorters')[index]);
|
||
5 years ago
|
|
||
|
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' });
|
||
2 years ago
|
clickToMatchExpect(0, { field: 'math', order: undefined as unknown as SortOrder });
|
||
5 years ago
|
|
||
|
// Last
|
||
|
clickToMatchExpect(1, { field: 'english', order: 'ascend' });
|
||
|
clickToMatchExpect(1, { field: 'english', order: 'descend' });
|
||
2 years ago
|
clickToMatchExpect(1, { field: 'english', order: undefined as unknown as SortOrder });
|
||
5 years ago
|
});
|
||
2 years ago
|
|
||
|
// https://github.com/ant-design/ant-design/issues/37024
|
||
|
it('multiple sort should pass array sorter as onChange param', () => {
|
||
2 years ago
|
const columns: TableProps<any>['columns'] = [
|
||
2 years ago
|
{
|
||
|
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(),
|
||
|
);
|
||
|
});
|
||
8 years ago
|
});
|