2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
2021-05-24 16:24:00 +08:00
|
|
|
import { act } from 'react-dom/test-utils';
|
2016-12-14 15:38:26 +08:00
|
|
|
import Table from '..';
|
2022-06-13 11:26:16 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
2020-04-01 17:14:53 +08:00
|
|
|
import ConfigProvider from '../../config-provider';
|
2022-06-13 11:26:16 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Table.rowSelection', () => {
|
2021-05-24 16:24:00 +08:00
|
|
|
window.requestAnimationFrame = callback => window.setTimeout(callback, 16);
|
|
|
|
window.cancelAnimationFrame = window.clearTimeout;
|
|
|
|
|
2019-03-06 16:48:14 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
},
|
|
|
|
];
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
function createTable(props = {}) {
|
2018-12-07 16:17:45 +08:00
|
|
|
return <Table columns={columns} dataSource={data} rowSelection={{}} {...props} />;
|
2016-12-14 14:48:09 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
function renderedNames(contain) {
|
|
|
|
const namesList = [];
|
|
|
|
contain
|
|
|
|
.querySelector('.ant-table-tbody')
|
|
|
|
.querySelectorAll('tr')
|
|
|
|
.forEach(tr => {
|
|
|
|
namesList.push(tr.querySelectorAll('td')[1].textContent);
|
|
|
|
});
|
|
|
|
return namesList;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
function getSelections(container) {
|
|
|
|
const keys = [];
|
|
|
|
container.querySelectorAll('.ant-table-tbody tr').forEach(row => {
|
|
|
|
const key = row.getAttribute('data-row-key');
|
|
|
|
if (row.querySelector('input').checked) {
|
|
|
|
if (isNaN(Number(key))) {
|
|
|
|
// rowKey
|
|
|
|
keys.push(key);
|
|
|
|
} else {
|
|
|
|
keys.push(Number(key));
|
2020-06-28 22:41:59 +08:00
|
|
|
}
|
2022-06-13 11:26:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return keys;
|
2020-06-28 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
function getIndeterminateSelection(container) {
|
|
|
|
const keys = [];
|
|
|
|
container.querySelectorAll('.ant-table-tbody tr').forEach(row => {
|
|
|
|
const key = row.getAttribute('data-row-key');
|
|
|
|
if (row.querySelector('.ant-checkbox-indeterminate')) {
|
|
|
|
if (isNaN(Number(key))) {
|
|
|
|
// rowKey
|
|
|
|
keys.push(key);
|
|
|
|
} else {
|
|
|
|
keys.push(Number(key));
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
2022-06-13 11:26:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return keys;
|
2017-02-07 21:09:51 +08:00
|
|
|
}
|
|
|
|
|
2021-03-25 09:11:39 +08:00
|
|
|
it('select default row', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection: { defaultSelectedRowKeys: [0] } }));
|
|
|
|
const checkboxes = container.querySelectorAll('input[type="checkbox"]');
|
|
|
|
expect(getSelections(container)).toEqual([0]);
|
2021-03-25 09:11:39 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
expect(getSelections(container)).toEqual([]);
|
2021-03-25 09:11:39 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[0]);
|
|
|
|
expect(getSelections(container)).toEqual([0, 1, 2, 3]);
|
2021-03-25 09:11:39 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[0]);
|
|
|
|
expect(getSelections(container)).toEqual([]);
|
2021-03-25 09:11:39 +08:00
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('select by checkbox', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable());
|
|
|
|
const checkboxes = container.querySelectorAll('input[type="checkbox"]');
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[0]);
|
|
|
|
expect(getSelections(container)).toEqual([0, 1, 2, 3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
expect(getSelections(container)).toEqual([1, 2, 3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
expect(getSelections(container)).toEqual([0, 1, 2, 3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('select by radio', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection: { type: 'radio' } }));
|
|
|
|
const radios = container.querySelectorAll('input[type="radio"]');
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
expect(radios.length).toBe(4);
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(radios[0]);
|
|
|
|
expect(getSelections(container)).toEqual([0]);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(radios[radios.length - 1]);
|
|
|
|
expect(getSelections(container)).toEqual([3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('pass getCheckboxProps to checkbox', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
disabled: record.name === 'Lucy',
|
2020-07-24 18:46:56 +08:00
|
|
|
indeterminate: record.name === 'Tom',
|
2018-01-31 15:07:53 +08:00
|
|
|
name: record.name,
|
2016-12-14 14:48:09 +08:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
const checkboxes = container.querySelectorAll('input[type="checkbox"]');
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(checkboxes[1].disabled).toBe(false);
|
|
|
|
expect(checkboxes[1].name).toEqual(data[0].name);
|
|
|
|
expect(checkboxes[2].disabled).toBe(true);
|
|
|
|
expect(checkboxes[2].name).toEqual(data[1].name);
|
2020-07-24 18:46:56 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(getIndeterminateSelection(container)).toEqual([2]);
|
2020-07-24 18:46:56 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("make getCheckboxProps's `indeterminate` override selectedRowKeys' effect", () => {
|
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
disabled: record.name === 'Lucy',
|
|
|
|
indeterminate: record.name === 'Tom',
|
|
|
|
name: record.name,
|
|
|
|
}),
|
|
|
|
selectedRowKeys: [2],
|
|
|
|
};
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([2]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works with pagination', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ pagination: { pageSize: 2 } }));
|
|
|
|
|
|
|
|
const pagers = container.querySelectorAll('.ant-pagination-item');
|
|
|
|
const checkboxAll = container.querySelector('input[type="checkbox"]');
|
|
|
|
|
|
|
|
const objectContaining = {};
|
|
|
|
fireEvent.click(checkboxAll);
|
|
|
|
objectContaining.checked = checkboxAll.checked; // true
|
|
|
|
objectContaining.indeterminate = getIndeterminateSelection(container).length > 0; // false
|
|
|
|
expect.objectContaining(objectContaining);
|
|
|
|
|
|
|
|
fireEvent.click(pagers[1]);
|
|
|
|
objectContaining.checked = checkboxAll.checked; // false
|
|
|
|
objectContaining.indeterminate = getIndeterminateSelection(container).length > 0; // false
|
|
|
|
expect.objectContaining(objectContaining);
|
|
|
|
|
|
|
|
fireEvent.click(pagers[0]);
|
|
|
|
objectContaining.checked = checkboxAll.checked; // true
|
|
|
|
objectContaining.indeterminate = getIndeterminateSelection(container).length > 0; // false
|
|
|
|
expect.objectContaining(objectContaining);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4020
|
|
|
|
it('handles defaultChecked', () => {
|
2019-11-15 14:35:25 +08:00
|
|
|
resetWarned();
|
2016-12-14 14:48:09 +08:00
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
defaultChecked: record.key === 0,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
render(createTable({ rowSelection }));
|
2019-03-06 16:48:14 +08:00
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
2019-03-06 16:48:14 +08:00
|
|
|
'Warning: [antd: Table] Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead.',
|
|
|
|
);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can be controlled', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(createTable({ rowSelection: { selectedRowKeys: [0] } }));
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(getSelections(container)).toEqual([0]);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(createTable({ rowSelection: { selectedRowKeys: [1] } }));
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(getSelections(container)).toEqual([1]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change & select events', () => {
|
2021-01-29 13:22:42 +08:00
|
|
|
const order = [];
|
|
|
|
const handleChange = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onChange');
|
|
|
|
});
|
|
|
|
const handleSelect = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelect');
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
const rowSelection = {
|
|
|
|
onChange: handleChange,
|
|
|
|
onSelect: handleSelect,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('input[type="checkbox"]')[4]);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(handleChange).toHaveBeenCalledWith([3], [{ key: 3, name: 'Jerry' }], {
|
|
|
|
type: 'single',
|
|
|
|
});
|
2018-03-09 15:16:57 +08:00
|
|
|
expect(handleSelect.mock.calls.length).toBe(1);
|
|
|
|
expect(handleSelect.mock.calls[0][0]).toEqual({ key: 3, name: 'Jerry' });
|
|
|
|
expect(handleSelect.mock.calls[0][1]).toEqual(true);
|
|
|
|
expect(handleSelect.mock.calls[0][2]).toEqual([{ key: 3, name: 'Jerry' }]);
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(handleSelect.mock.calls[0][3].type).toBe('click');
|
2021-01-29 13:22:42 +08:00
|
|
|
expect(order).toEqual(['onSelect', 'onChange']);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:49:23 +08:00
|
|
|
it('fires selectMulti event', () => {
|
2021-01-29 13:22:42 +08:00
|
|
|
const order = [];
|
|
|
|
const handleSelectMulti = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelectMultiple');
|
|
|
|
});
|
|
|
|
const handleSelect = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelect');
|
|
|
|
});
|
|
|
|
const handleChange = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onChange');
|
|
|
|
});
|
2018-07-24 14:49:23 +08:00
|
|
|
const rowSelection = {
|
2021-01-29 13:22:42 +08:00
|
|
|
onChange: handleChange,
|
2018-07-24 14:49:23 +08:00
|
|
|
onSelect: handleSelect,
|
2018-08-04 19:20:28 +08:00
|
|
|
onSelectMultiple: handleSelectMulti,
|
2018-07-24 14:49:23 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
fireEvent.click(container.querySelectorAll('tbody input[type="checkbox"]')[0], {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelect).toHaveBeenCalled();
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(handleChange).toHaveBeenLastCalledWith([0], [{ key: 0, name: 'Jack' }], {
|
|
|
|
type: 'single',
|
|
|
|
});
|
2018-07-24 14:49:23 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('tbody input[type="checkbox"]')[2], {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectMulti).toHaveBeenCalledWith(
|
|
|
|
true,
|
|
|
|
[data[0], data[1], data[2]],
|
|
|
|
[data[1], data[2]],
|
|
|
|
);
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(handleChange).toHaveBeenLastCalledWith(
|
|
|
|
[0, 1, 2],
|
|
|
|
[
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
],
|
|
|
|
{ type: 'multiple' },
|
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('tbody input[type="checkbox"]')[0], {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectMulti).toHaveBeenCalledWith(false, [], [data[0], data[1], data[2]]);
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(handleChange).toHaveBeenLastCalledWith([], [], { type: 'multiple' });
|
2021-01-29 13:22:42 +08:00
|
|
|
|
|
|
|
expect(order).toEqual([
|
|
|
|
'onSelect',
|
|
|
|
'onChange',
|
|
|
|
'onSelectMultiple',
|
|
|
|
'onChange',
|
|
|
|
'onSelectMultiple',
|
|
|
|
'onChange',
|
|
|
|
]);
|
2018-07-24 14:49:23 +08:00
|
|
|
});
|
|
|
|
|
2022-06-23 16:04:55 +08:00
|
|
|
it('reset last select key after performing select and bulk operations', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const { container, baseElement } = render(
|
|
|
|
createTable({
|
|
|
|
checkbox: true,
|
|
|
|
rowSelection: {
|
|
|
|
selections: [Table.SELECTION_NONE],
|
|
|
|
onChange: keys => onChange(keys),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
const last = () => {
|
|
|
|
const elements = container.querySelectorAll('td input');
|
|
|
|
return elements[elements.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
const first = () => {
|
|
|
|
const elements = container.querySelectorAll('td input');
|
|
|
|
return elements[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
const allElement = () => container.querySelector('th input');
|
|
|
|
|
|
|
|
// Multiple select normal
|
|
|
|
fireEvent.click(last());
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([3]);
|
|
|
|
fireEvent.click(first(), {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([3, 0, 1, 2]);
|
|
|
|
fireEvent.click(allElement());
|
|
|
|
console.log(baseElement.innerHTML);
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([]);
|
|
|
|
|
|
|
|
// Reset last select key when select all
|
|
|
|
fireEvent.click(last());
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([3]);
|
|
|
|
fireEvent.click(allElement());
|
|
|
|
fireEvent.click(allElement());
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([]);
|
|
|
|
fireEvent.click(first(), {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([0]);
|
|
|
|
|
|
|
|
// Reset last select key when deselect
|
|
|
|
fireEvent.click(last());
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([0, 3]);
|
|
|
|
fireEvent.click(first());
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([3]);
|
|
|
|
fireEvent.click(first(), {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([3, 0]);
|
|
|
|
|
|
|
|
// Reset last select key when bulk operations
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
fireEvent.click(baseElement.querySelector('li.ant-dropdown-menu-item'));
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([]);
|
|
|
|
fireEvent.click(first(), {
|
|
|
|
shiftKey: true,
|
|
|
|
});
|
|
|
|
expect(onChange).toHaveBeenLastCalledWith([0]);
|
|
|
|
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('fires selectAll event', () => {
|
2021-01-29 13:22:42 +08:00
|
|
|
const order = [];
|
|
|
|
const handleSelectAll = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelectAll');
|
|
|
|
});
|
|
|
|
const handleChange = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onChange');
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
const rowSelection = {
|
2021-01-29 13:22:42 +08:00
|
|
|
onChange: handleChange,
|
2016-12-14 14:48:09 +08:00
|
|
|
onSelectAll: handleSelectAll,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
|
|
|
|
const checkAll = container.querySelector('input[type="checkbox"]');
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkAll);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectAll).toHaveBeenCalledWith(true, data, data);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2021-01-29 13:22:42 +08:00
|
|
|
expect(order).toEqual(['onSelectAll', 'onChange']);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkAll);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectAll).toHaveBeenCalledWith(false, [], data);
|
2017-02-23 19:29:47 +08:00
|
|
|
});
|
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
it('works with selectAll option inside selection menu', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2020-06-28 22:41:59 +08:00
|
|
|
const handleChange = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onChange: handleChange,
|
|
|
|
selections: true,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2021-04-22 17:15:37 +08:00
|
|
|
// Open
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelectorAll('.ant-dropdown-menu-item')[0]);
|
2021-04-22 17:15:37 +08:00
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(handleChange.mock.calls[0][0]).toEqual([0, 1, 2, 3]);
|
|
|
|
});
|
|
|
|
|
2017-03-24 11:44:36 +08:00
|
|
|
it('render with default selection correctly', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2017-03-24 11:44:36 +08:00
|
|
|
const rowSelection = {
|
|
|
|
selections: true,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(container.querySelector('.ant-dropdown')).toMatchSnapshot();
|
2017-02-23 19:29:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fires selectInvert event', () => {
|
2021-05-24 16:24:00 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2021-01-29 13:22:42 +08:00
|
|
|
const order = [];
|
|
|
|
const handleSelectInvert = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelectInvert');
|
|
|
|
});
|
|
|
|
const handleChange = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onChange');
|
|
|
|
});
|
2017-02-23 19:29:47 +08:00
|
|
|
const rowSelection = {
|
2021-01-29 13:22:42 +08:00
|
|
|
onChange: handleChange,
|
2017-02-23 19:29:47 +08:00
|
|
|
onSelectInvert: handleSelectInvert,
|
2017-03-24 11:44:36 +08:00
|
|
|
selections: true,
|
2017-02-23 19:29:47 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox')[1]);
|
2021-04-22 17:15:37 +08:00
|
|
|
// Open
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
2021-05-24 16:24:00 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2021-04-22 17:15:37 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('li.ant-dropdown-menu-item')[1]);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectInvert).toHaveBeenCalledWith([1, 2, 3]);
|
2021-01-29 13:22:42 +08:00
|
|
|
expect(order).toEqual(['onChange', 'onSelectInvert', 'onChange']);
|
2021-05-24 16:24:00 +08:00
|
|
|
|
|
|
|
jest.useRealTimers();
|
2017-02-23 19:29:47 +08:00
|
|
|
});
|
|
|
|
|
2020-12-29 10:38:43 +08:00
|
|
|
it('fires selectNone event', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2021-01-29 13:22:42 +08:00
|
|
|
const order = [];
|
|
|
|
const handleChange = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onChange');
|
|
|
|
});
|
|
|
|
const handleSelectNone = jest.fn().mockImplementation(() => {
|
|
|
|
order.push('onSelectNone');
|
|
|
|
});
|
2020-12-29 10:38:43 +08:00
|
|
|
const rowSelection = {
|
2021-01-29 13:22:42 +08:00
|
|
|
onChange: handleChange,
|
2020-12-29 10:38:43 +08:00
|
|
|
onSelectNone: handleSelectNone,
|
|
|
|
selections: true,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2020-12-29 10:38:43 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-checkbox')[1]);
|
2021-04-22 17:15:37 +08:00
|
|
|
// Open
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
const dropdownMenuItems = container.querySelectorAll('.ant-dropdown-menu-item');
|
|
|
|
fireEvent.click(dropdownMenuItems[dropdownMenuItems.length - 1]);
|
2020-12-29 10:38:43 +08:00
|
|
|
|
|
|
|
expect(handleSelectNone).toHaveBeenCalled();
|
2021-01-29 13:22:42 +08:00
|
|
|
expect(order).toEqual(['onChange', 'onSelectNone', 'onChange']);
|
2020-12-29 10:38:43 +08:00
|
|
|
});
|
|
|
|
|
2017-02-23 19:29:47 +08:00
|
|
|
it('fires selection event', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2017-02-23 19:29:47 +08:00
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
|
|
|
const rowSelection = {
|
2018-12-07 16:17:45 +08:00
|
|
|
selections: [
|
2019-11-15 14:35:25 +08:00
|
|
|
Table.SELECTION_ALL,
|
|
|
|
Table.SELECTION_INVERT,
|
2018-12-07 16:17:45 +08:00
|
|
|
{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
},
|
|
|
|
],
|
2017-02-23 19:29:47 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2021-04-22 17:15:37 +08:00
|
|
|
// Open
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
const dropdownMenuItems = container.querySelectorAll('.ant-dropdown-menu-item');
|
2021-04-22 17:15:37 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(dropdownMenuItems.length).toBe(4);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(dropdownMenuItems[2]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectOdd).toHaveBeenCalledWith([0, 1, 2, 3]);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(dropdownMenuItems[3]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectEven).toHaveBeenCalledWith([0, 1, 2, 3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2016-12-19 21:10:06 +08:00
|
|
|
|
2021-09-03 19:47:23 +08:00
|
|
|
describe('preset selection options', () => {
|
|
|
|
const presetData = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy', disabled: true },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
];
|
|
|
|
|
|
|
|
const getCheckboxProps = record => record;
|
|
|
|
|
|
|
|
it('SELECTION_ALL', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2021-09-03 19:47:23 +08:00
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-09-03 19:47:23 +08:00
|
|
|
createTable({
|
|
|
|
dataSource: presetData,
|
|
|
|
rowSelection: {
|
|
|
|
onChange,
|
|
|
|
defaultSelectedRowKeys: [2],
|
|
|
|
getCheckboxProps,
|
|
|
|
selections: [Table.SELECTION_ALL],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
2021-09-03 19:47:23 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('li.ant-dropdown-menu-item'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([0, 2], expect.anything(), { type: 'all' });
|
2021-09-03 19:47:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('SELECTION_INVERT', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2021-09-03 19:47:23 +08:00
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-09-03 19:47:23 +08:00
|
|
|
createTable({
|
|
|
|
dataSource: presetData,
|
|
|
|
rowSelection: {
|
|
|
|
onChange,
|
|
|
|
defaultSelectedRowKeys: [2],
|
|
|
|
getCheckboxProps,
|
|
|
|
selections: [Table.SELECTION_INVERT],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('li.ant-dropdown-menu-item'));
|
2021-09-03 19:47:23 +08:00
|
|
|
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([0], expect.anything(), { type: 'invert' });
|
2021-09-03 19:47:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('SELECTION_NONE', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2021-09-03 19:47:23 +08:00
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-09-03 19:47:23 +08:00
|
|
|
createTable({
|
|
|
|
dataSource: presetData,
|
|
|
|
rowSelection: {
|
|
|
|
onChange,
|
|
|
|
defaultSelectedRowKeys: [1, 2],
|
|
|
|
getCheckboxProps,
|
|
|
|
selections: [Table.SELECTION_NONE],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('li.ant-dropdown-menu-item'));
|
2021-09-03 19:47:23 +08:00
|
|
|
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith([1], expect.anything(), { type: 'none' });
|
2021-09-03 19:47:23 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-29 23:50:56 +08:00
|
|
|
it('could hide selectAll checkbox and custom selection', () => {
|
2017-08-27 15:44:22 +08:00
|
|
|
const rowSelection = {
|
2020-05-29 23:50:56 +08:00
|
|
|
hideSelectAll: true,
|
2017-08-27 15:44:22 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
|
|
|
expect(container.querySelector('.ant-selection')).toBeFalsy();
|
2017-08-27 15:44:22 +08:00
|
|
|
});
|
|
|
|
|
2017-09-19 09:56:19 +08:00
|
|
|
it('handle custom selection onSelect correctly when hide default selection options', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
jest.useFakeTimers();
|
2017-09-19 09:56:19 +08:00
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
|
|
|
const rowSelection = {
|
2018-12-07 16:17:45 +08:00
|
|
|
selections: [
|
|
|
|
{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
},
|
|
|
|
],
|
2017-09-19 09:56:19 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable({ rowSelection }));
|
2017-09-19 09:56:19 +08:00
|
|
|
|
2021-04-22 17:15:37 +08:00
|
|
|
// Open
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
2021-04-22 17:15:37 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const dropdownMenuItems = container.querySelectorAll('li.ant-dropdown-menu-item');
|
|
|
|
expect(dropdownMenuItems.length).toBe(2);
|
2017-09-19 09:56:19 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(dropdownMenuItems[0]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectOdd).toHaveBeenCalledWith([0, 1, 2, 3]);
|
2017-09-19 09:56:19 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(dropdownMenuItems[1]);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(handleSelectEven).toHaveBeenCalledWith([0, 1, 2, 3]);
|
2017-09-19 09:56:19 +08:00
|
|
|
});
|
|
|
|
|
2016-12-19 21:10:06 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/4245
|
|
|
|
it('handles disabled checkbox correctly when dataSource changes', () => {
|
|
|
|
const rowSelection = {
|
2017-01-08 17:04:09 +08:00
|
|
|
getCheckboxProps: record => ({ disabled: record.disabled }),
|
2016-12-19 21:10:06 +08:00
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(createTable({ rowSelection }));
|
2016-12-19 21:10:06 +08:00
|
|
|
const newData = [
|
|
|
|
{ key: 0, name: 'Jack', disabled: true },
|
|
|
|
{ key: 1, name: 'Lucy', disabled: true },
|
|
|
|
];
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
rerender(createTable({ rowSelection, dataSource: newData }));
|
|
|
|
container.querySelectorAll('input').forEach(checkbox => {
|
|
|
|
expect(checkbox.disabled).toBe(true);
|
2016-12-19 21:10:06 +08:00
|
|
|
});
|
|
|
|
});
|
2017-02-07 21:09:51 +08:00
|
|
|
|
2018-04-22 15:19:04 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/4245
|
|
|
|
it('should allow dynamic getCheckboxProps', () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{
|
|
|
|
getCheckboxProps: record => ({ disabled: record.name === 'Jack' }),
|
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
let checkboxList = container.querySelectorAll('input');
|
|
|
|
expect(checkboxList[1]).toHaveAttribute('disabled');
|
|
|
|
expect(checkboxList[2]).not.toHaveAttribute('disabled');
|
|
|
|
|
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{
|
|
|
|
getCheckboxProps: record => ({ disabled: record.name === 'Lucy' }),
|
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
checkboxList = container.querySelectorAll('input');
|
|
|
|
expect(checkboxList[1]).not.toHaveAttribute('disabled');
|
|
|
|
expect(checkboxList[2]).toHaveAttribute('disabled');
|
2018-04-22 15:19:04 +08:00
|
|
|
});
|
|
|
|
|
2017-02-07 21:09:51 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/4779
|
|
|
|
it('should not switch pagination when select record', () => {
|
|
|
|
const newData = [];
|
|
|
|
for (let i = 0; i < 20; i += 1) {
|
|
|
|
newData.push({
|
|
|
|
key: i.toString(),
|
|
|
|
name: i.toString(),
|
|
|
|
});
|
|
|
|
}
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {},
|
|
|
|
dataSource: newData,
|
|
|
|
}),
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('.ant-pagination-item')[1]); // switch to second page
|
|
|
|
fireEvent.click(container.querySelector('.ant-checkbox'));
|
|
|
|
|
|
|
|
expect(renderedNames(container)).toEqual([
|
2018-12-07 16:17:45 +08:00
|
|
|
'10',
|
|
|
|
'11',
|
|
|
|
'12',
|
|
|
|
'13',
|
|
|
|
'14',
|
|
|
|
'15',
|
|
|
|
'16',
|
|
|
|
'17',
|
|
|
|
'18',
|
|
|
|
'19',
|
|
|
|
]);
|
2017-02-07 21:09:51 +08:00
|
|
|
});
|
2017-11-18 00:03:30 +08:00
|
|
|
|
|
|
|
it('highlight selected row', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable());
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelectorAll('input')[1]);
|
|
|
|
expect(
|
|
|
|
container.querySelectorAll('tbody tr')[0].className.includes('ant-table-row-selected'),
|
|
|
|
).toBe(true);
|
2017-11-18 00:03:30 +08:00
|
|
|
});
|
2017-11-18 23:46:24 +08:00
|
|
|
|
|
|
|
it('fix selection column on the left', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { asFragment } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: { fixed: true },
|
2020-04-01 20:57:35 +08:00
|
|
|
scroll: { x: 903 },
|
2018-12-07 16:17:45 +08:00
|
|
|
}),
|
|
|
|
);
|
2017-11-18 23:46:24 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2017-11-18 23:46:24 +08:00
|
|
|
});
|
2018-06-05 20:45:19 +08:00
|
|
|
|
2020-03-10 23:12:37 +08:00
|
|
|
it('fix expand on th left when selection column fixed on the left', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { asFragment } = render(
|
2020-03-10 23:12:37 +08:00
|
|
|
createTable({
|
|
|
|
expandable: {
|
|
|
|
expandedRowRender() {
|
|
|
|
return <div />;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rowSelection: { fixed: true },
|
2020-04-01 20:57:35 +08:00
|
|
|
scroll: { x: 903 },
|
2020-03-10 23:12:37 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-03-10 23:12:37 +08:00
|
|
|
});
|
|
|
|
|
2019-09-28 15:22:27 +08:00
|
|
|
it('fix selection column on the left when any other column is fixed', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { asFragment } = render(
|
2019-09-28 15:22:27 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {},
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
fixed: 'left',
|
|
|
|
},
|
|
|
|
],
|
2020-04-01 20:57:35 +08:00
|
|
|
scroll: { x: 903 },
|
2019-09-28 15:22:27 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-09-28 15:22:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('use column as selection column when key is `selection-column`', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { asFragment } = render(
|
2019-09-28 15:22:27 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {},
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'selection-column',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2019-09-28 15:22:27 +08:00
|
|
|
});
|
|
|
|
|
2018-06-05 20:45:19 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/10629
|
|
|
|
it('should keep all checked state when remove item from dataSource', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2018-06-05 20:45:19 +08:00
|
|
|
<Table
|
|
|
|
rowSelection={{
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
}}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-06-05 20:45:19 +08:00
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxs = container.querySelectorAll('.ant-checkbox');
|
|
|
|
expect(checkboxs.length).toBe(5);
|
|
|
|
checkboxs.forEach(checkbox => {
|
|
|
|
expect(checkbox.querySelector('input').checked).toBe(true);
|
|
|
|
expect(checkbox.className.includes('ant-checkbox-indeterminate')).toBe(false);
|
2018-06-05 20:45:19 +08:00
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
rowSelection={{
|
|
|
|
selectedRowKeys: [1, 2, 3],
|
|
|
|
}}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data.slice(1)}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('.ant-checkbox').length).toBe(4);
|
|
|
|
container.querySelectorAll('.ant-checkbox').forEach(checkbox => {
|
|
|
|
expect(checkbox.querySelector('input').checked).toBe(true);
|
|
|
|
expect(checkbox.className.includes('ant-checkbox-indeterminate')).toBe(false);
|
2018-06-05 20:45:19 +08:00
|
|
|
});
|
|
|
|
});
|
2018-07-28 14:57:00 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11042
|
2018-09-21 15:55:00 +08:00
|
|
|
it('add columnTitle for rowSelection', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2018-07-28 14:57:00 +08:00
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{
|
|
|
|
columnTitle: '多选',
|
|
|
|
}}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-07-28 14:57:00 +08:00
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead tr th').textContent).toBe('多选');
|
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{
|
|
|
|
type: 'radio',
|
|
|
|
columnTitle: '单选',
|
|
|
|
}}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(container.querySelector('thead tr th').textContent).toBe('单选');
|
2018-07-28 14:57:00 +08:00
|
|
|
});
|
2018-12-06 11:03:56 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11384
|
|
|
|
it('should keep item even if in filter', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const filterColumns = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
text: 'Jack',
|
|
|
|
value: 'Jack',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'Lucy',
|
|
|
|
value: 'Lucy',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
filterDropdownVisible: true,
|
|
|
|
onFilter: (value, record) => record.name.indexOf(value) === 0,
|
|
|
|
},
|
|
|
|
];
|
2018-12-06 11:03:56 +08:00
|
|
|
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onChange,
|
|
|
|
};
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Table columns={filterColumns} dataSource={data} rowSelection={rowSelection} />,
|
2018-12-06 11:03:56 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
function clickFilter(indexList) {
|
2018-12-07 16:17:45 +08:00
|
|
|
indexList.forEach(index => {
|
2022-06-13 11:26:16 +08:00
|
|
|
// wrapper.find('.ant-dropdown-menu-item .ant-checkbox-wrapper').at(index).simulate('click');
|
|
|
|
fireEvent.click(
|
|
|
|
container.querySelectorAll('.ant-dropdown-menu-item .ant-checkbox-wrapper')[index],
|
|
|
|
);
|
2018-12-06 11:03:56 +08:00
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
// wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click');
|
|
|
|
fireEvent.click(container.querySelector('.ant-table-filter-dropdown-btns .ant-btn-primary'));
|
2018-12-06 11:03:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function clickItem() {
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(
|
|
|
|
container.querySelectorAll('tbody .ant-table-selection-column .ant-checkbox-input')[0],
|
|
|
|
);
|
|
|
|
// wrapper
|
|
|
|
// .find('tbody .ant-table-selection-column .ant-checkbox-input')
|
|
|
|
// .at(0)
|
|
|
|
// .simulate('change', {
|
|
|
|
// target: { checked: true },
|
|
|
|
// });
|
2018-12-06 11:03:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check Jack
|
|
|
|
clickFilter([0]);
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelectorAll('tbody tr').length).toBe(1);
|
2018-12-06 11:03:56 +08:00
|
|
|
clickItem();
|
|
|
|
expect(onChange.mock.calls[0][0].length).toBe(1);
|
|
|
|
expect(onChange.mock.calls[0][1].length).toBe(1);
|
|
|
|
|
|
|
|
// Check Lucy
|
|
|
|
clickFilter([0, 1]);
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelectorAll('tbody tr').length).toBe(1);
|
2018-12-06 11:03:56 +08:00
|
|
|
clickItem();
|
|
|
|
expect(onChange.mock.calls[1][0].length).toBe(2);
|
|
|
|
expect(onChange.mock.calls[1][1].length).toBe(2);
|
|
|
|
});
|
2018-12-20 14:44:45 +08:00
|
|
|
|
|
|
|
it('render correctly when set childrenColumnName', () => {
|
|
|
|
const newDatas = [
|
|
|
|
{
|
|
|
|
key: 1,
|
|
|
|
name: 'Jack',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 11,
|
|
|
|
name: 'John Brown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 2,
|
|
|
|
name: 'Lucy',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 21,
|
|
|
|
name: 'Lucy Brown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2018-12-20 14:44:45 +08:00
|
|
|
<Table columns={columns} dataSource={newDatas} childrenColumnName="test" rowSelection={{}} />,
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxes = container.querySelectorAll('input');
|
|
|
|
const objectContaining = {};
|
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
objectContaining.checked = checkboxes[0].checked; // false
|
|
|
|
objectContaining.indeterminate = getIndeterminateSelection(container).length > 0; // true
|
|
|
|
expect.objectContaining(objectContaining);
|
|
|
|
|
|
|
|
fireEvent.click(checkboxes[2]);
|
|
|
|
objectContaining.checked = checkboxes[0].checked; // true
|
|
|
|
objectContaining.indeterminate = getIndeterminateSelection(container).length > 0; // false
|
|
|
|
expect.objectContaining(objectContaining);
|
2018-12-20 14:44:45 +08:00
|
|
|
});
|
2019-02-11 01:00:33 +08:00
|
|
|
|
2019-05-16 14:50:24 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/16614
|
|
|
|
it('should get selectedRows correctly when set childrenColumnName', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const newDatas = [
|
|
|
|
{
|
|
|
|
key: 1,
|
|
|
|
name: 'Jack',
|
|
|
|
list: [
|
|
|
|
{
|
|
|
|
key: 11,
|
|
|
|
name: 'John Brown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2019-05-16 14:50:24 +08:00
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={newDatas}
|
|
|
|
childrenColumnName="list"
|
|
|
|
rowSelection={{ onChange }}
|
|
|
|
expandedRowKeys={[1]}
|
|
|
|
/>,
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxes = container.querySelectorAll('input');
|
|
|
|
|
|
|
|
fireEvent.click(checkboxes[2]);
|
|
|
|
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenLastCalledWith([11], [newDatas[0].list[0]], { type: 'single' });
|
2019-11-15 14:35:25 +08:00
|
|
|
onChange.mockReset();
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[1]);
|
2019-11-15 14:35:25 +08:00
|
|
|
const item0 = newDatas[0];
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenLastCalledWith([11, 1], [newDatas[0].list[0], item0], {
|
|
|
|
type: 'single',
|
|
|
|
});
|
2019-05-16 14:50:24 +08:00
|
|
|
});
|
|
|
|
|
2019-02-11 01:00:33 +08:00
|
|
|
it('clear selection className when remove `rowSelection`', () => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const dataSource = [
|
|
|
|
{ id: 1, name: 'Hello', age: 10 },
|
|
|
|
{ id: 2, name: 'World', age: 30 },
|
|
|
|
];
|
2019-02-11 01:00:33 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2019-10-16 10:44:56 +08:00
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={{}}
|
|
|
|
expandedRowRender={() => null}
|
2019-11-15 14:35:25 +08:00
|
|
|
rowKey="id"
|
2019-10-16 10:44:56 +08:00
|
|
|
/>,
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxes = container.querySelectorAll('input');
|
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
|
|
|
|
expect(container.querySelectorAll('tr.ant-table-row-selected').length).toBe(1);
|
2019-02-11 01:00:33 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={null}
|
|
|
|
expandedRowRender={() => null}
|
|
|
|
rowKey="id"
|
|
|
|
/>,
|
|
|
|
);
|
2019-02-11 01:00:33 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelectorAll('tr.ant-table-row-selected').length).toBe(0);
|
2019-02-11 01:00:33 +08:00
|
|
|
});
|
2019-08-27 17:11:00 +08:00
|
|
|
|
|
|
|
it('select by checkbox to trigger stopPropagation', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(createTable());
|
2019-08-27 17:11:00 +08:00
|
|
|
expect(() => {
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelectorAll('span')[10]);
|
2019-08-27 17:11:00 +08:00
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2020-01-17 14:02:07 +08:00
|
|
|
|
|
|
|
it('all disabled should not make select all checked', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2020-01-17 14:02:07 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {
|
|
|
|
getCheckboxProps: () => ({
|
|
|
|
disabled: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').disabled).toBeTruthy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').checked).toBeFalsy();
|
2020-01-17 14:02:07 +08:00
|
|
|
});
|
2020-02-22 22:19:33 +08:00
|
|
|
|
2021-06-02 12:15:00 +08:00
|
|
|
it('should make select all checked when each item is checked and disabled', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-06-02 12:15:00 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
getCheckboxProps: () => ({
|
|
|
|
disabled: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').disabled).toBeTruthy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').checked).toBeTruthy();
|
2021-06-02 12:15:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should make select all indeterminated when each item is disabled and some item is checked', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-06-02 12:15:00 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [0],
|
|
|
|
getCheckboxProps: () => ({
|
|
|
|
disabled: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').disabled).toBeTruthy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').checked).toBeFalsy();
|
2021-06-02 12:15:00 +08:00
|
|
|
expect(
|
2022-06-13 11:26:16 +08:00
|
|
|
container.querySelector('thead .ant-checkbox-indeterminate.ant-checkbox-disabled'),
|
2021-06-02 12:15:00 +08:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should make select all checked when each item is checked and some item is disabled', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-06-02 12:15:00 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
disabled: record.key === 0,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').disabled).toBeFalsy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').checked).toBeTruthy();
|
2021-06-02 12:15:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not make select all checked when some item is checked and disabled', () => {
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2021-06-02 12:15:00 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [1],
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
disabled: record.key === 0,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').disabled).toBeFalsy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-input').checked).toBeFalsy();
|
|
|
|
expect(container.querySelector('thead .ant-checkbox-indeterminate')).toBeTruthy();
|
2021-06-02 12:15:00 +08:00
|
|
|
});
|
|
|
|
|
2020-03-25 10:51:49 +08:00
|
|
|
it('should onRowClick not called when checkbox clicked', () => {
|
|
|
|
const onRowClick = jest.fn();
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2020-03-25 10:51:49 +08:00
|
|
|
createTable({
|
|
|
|
onRow: () => ({
|
|
|
|
onClick: onRowClick,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxes = container.querySelectorAll('input');
|
|
|
|
fireEvent.click(checkboxes[checkboxes.length - 1]);
|
2020-03-25 10:51:49 +08:00
|
|
|
|
|
|
|
expect(onRowClick).not.toHaveBeenCalled();
|
|
|
|
});
|
2020-04-01 17:14:53 +08:00
|
|
|
|
|
|
|
it('should support getPopupContainer', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
selections: true,
|
|
|
|
};
|
|
|
|
const getPopupContainer = jest.fn(node => node);
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2020-04-01 17:14:53 +08:00
|
|
|
createTable({
|
|
|
|
rowSelection,
|
|
|
|
getPopupContainer,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
jest.useFakeTimers();
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-04-01 17:14:53 +08:00
|
|
|
expect(getPopupContainer).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support getPopupContainer from ConfigProvider', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
selections: true,
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2020-04-01 17:14:53 +08:00
|
|
|
<ConfigProvider getPopupContainer={node => node.parentNode}>
|
|
|
|
{createTable({
|
|
|
|
rowSelection,
|
|
|
|
})}
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
jest.useFakeTimers();
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.mouseEnter(container.querySelector('.ant-dropdown-trigger'));
|
|
|
|
act(() => {
|
|
|
|
jest.runAllTimers();
|
|
|
|
});
|
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2020-04-01 17:14:53 +08:00
|
|
|
});
|
2020-04-13 22:56:26 +08:00
|
|
|
|
|
|
|
it('Table selection should check', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(
|
2020-04-13 22:56:26 +08:00
|
|
|
<Table
|
|
|
|
dataSource={[{ name: 'light', sub: [{ name: 'bamboo' }] }]}
|
|
|
|
expandable={{ expandedRowKeys: ['light'], childrenColumnName: 'sub' }}
|
|
|
|
rowSelection={{ onChange }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
const checkboxs = container.querySelectorAll('input');
|
|
|
|
fireEvent.click(checkboxs[checkboxs.length - 1]);
|
|
|
|
|
2020-04-13 22:56:26 +08:00
|
|
|
expect(onChange.mock.calls[0][1]).toEqual([expect.objectContaining({ name: 'bamboo' })]);
|
|
|
|
});
|
2020-05-21 11:02:12 +08:00
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
describe('supports children', () => {
|
|
|
|
const dataWithChildren = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{
|
|
|
|
key: 3,
|
|
|
|
name: 'Jerry',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 4,
|
|
|
|
name: 'Jerry Jack',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 5,
|
|
|
|
name: 'Jerry Lucy',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 6,
|
|
|
|
name: 'Jerry Tom',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 7,
|
|
|
|
name: 'Jerry Tom Jack',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 8,
|
|
|
|
name: 'Jerry Tom Lucy',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 9,
|
|
|
|
name: 'Jerry Tom Tom',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
describe('supports checkStrictly', () => {
|
|
|
|
it('use data entity key', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
},
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
|
|
|
|
expect(getSelections(container)).toEqual([3, 4, 5, 6, 7, 8, 9]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[0][0]).toEqual([3, 4, 5, 6, 7, 8, 9]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[7]);
|
|
|
|
|
|
|
|
expect(getSelections(container)).toEqual([4, 5]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([3]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[1][0]).toEqual([4, 5]);
|
|
|
|
});
|
|
|
|
it('use function rowkey', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
},
|
|
|
|
rowKey: entity => entity.name,
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom',
|
|
|
|
'Jerry Tom Jack',
|
|
|
|
'Jerry Tom Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[0][0]).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom',
|
|
|
|
'Jerry Tom Jack',
|
|
|
|
'Jerry Tom Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[7]);
|
|
|
|
expect(getSelections(container)).toEqual(['Jerry Jack', 'Jerry Lucy']);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual(['Jerry']);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[1][0]).toEqual(['Jerry Jack', 'Jerry Lucy']);
|
|
|
|
});
|
|
|
|
it('use string rowkey', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
},
|
|
|
|
rowKey: 'name',
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom',
|
|
|
|
'Jerry Tom Jack',
|
|
|
|
'Jerry Tom Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[0][0]).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom',
|
|
|
|
'Jerry Tom Jack',
|
|
|
|
'Jerry Tom Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[7]);
|
|
|
|
expect(getSelections(container)).toEqual(['Jerry Jack', 'Jerry Lucy']);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual(['Jerry']);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[1][0]).toEqual(['Jerry Jack', 'Jerry Lucy']);
|
|
|
|
});
|
|
|
|
it('initialized correctly', () => {
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
selectedRowKeys: [7, 8, 9],
|
|
|
|
},
|
|
|
|
rowKey: 'key',
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
expect(getSelections(container)).toEqual([6, 7, 8, 9]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([3]);
|
2020-06-28 22:41:59 +08:00
|
|
|
});
|
|
|
|
it('works with disabled checkbox', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
getCheckboxProps(record) {
|
|
|
|
return {
|
|
|
|
disabled: record.name === 'Jerry Tom',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[10]);
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
|
|
|
|
expect(getSelections(container).sort()).toEqual([3, 4, 5, 9]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(Array.from(onChange.mock.calls[1][0]).sort()).toEqual([3, 4, 5, 9]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual([9]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[2][0]).toEqual([9]);
|
|
|
|
});
|
|
|
|
it('works with disabled checkbox and function rowkey', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
getCheckboxProps(record) {
|
|
|
|
return {
|
|
|
|
disabled: record.name === 'Jerry Tom',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rowKey: entity => entity.name,
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[10]);
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(Array.from(onChange.mock.calls[1][0]).sort()).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual(['Jerry Tom Tom']);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[2][0]).toEqual(['Jerry Tom Tom']);
|
|
|
|
});
|
|
|
|
it('works with disabled checkbox and string rowkey', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
getCheckboxProps(record) {
|
|
|
|
return {
|
|
|
|
disabled: record.name === 'Jerry Tom',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
rowKey: 'name',
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[10]);
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(Array.from(onChange.mock.calls[1][0]).sort()).toEqual([
|
|
|
|
'Jerry',
|
|
|
|
'Jerry Jack',
|
|
|
|
'Jerry Lucy',
|
|
|
|
'Jerry Tom Tom',
|
|
|
|
]);
|
2022-06-13 11:26:16 +08:00
|
|
|
|
|
|
|
fireEvent.click(checkboxes[4]);
|
|
|
|
expect(getSelections(container)).toEqual(['Jerry Tom Tom']);
|
|
|
|
expect(getIndeterminateSelection(container)).toEqual([]);
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange.mock.calls[2][0]).toEqual(['Jerry Tom Tom']);
|
|
|
|
});
|
2020-12-28 15:18:04 +08:00
|
|
|
|
|
|
|
it('should support `childrenColumnName`', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
|
|
|
|
const table = createTable({
|
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: 0,
|
|
|
|
name: 'Jack',
|
|
|
|
childList: [
|
|
|
|
{ key: 1, name: 'Light' },
|
|
|
|
{ key: 2, name: 'Bamboo' },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
expandable: {
|
|
|
|
childrenColumnName: 'childList',
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
},
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
onChange,
|
|
|
|
},
|
|
|
|
});
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container } = render(table);
|
|
|
|
const checkboxes = container.querySelectorAll('input');
|
2020-12-28 15:18:04 +08:00
|
|
|
expect(checkboxes).toHaveLength(1 + 3);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(checkboxes[1]);
|
|
|
|
expect(getSelections(container)).toEqual([0, 1, 2]);
|
2020-12-28 15:18:04 +08:00
|
|
|
});
|
2020-06-28 22:41:59 +08:00
|
|
|
});
|
2020-07-24 18:46:56 +08:00
|
|
|
it('warns when set `indeterminate` using `rowSelection.getCheckboxProps` is not allowed with tree structured data.', () => {
|
|
|
|
resetWarned();
|
2022-06-13 11:26:16 +08:00
|
|
|
render(
|
2020-07-24 18:46:56 +08:00
|
|
|
createTable({
|
|
|
|
dataSource: dataWithChildren,
|
|
|
|
defaultExpandAllRows: true,
|
|
|
|
rowSelection: {
|
|
|
|
checkStrictly: false,
|
|
|
|
getCheckboxProps() {
|
|
|
|
return {
|
|
|
|
indeterminate: true,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: Table] set `indeterminate` using `rowSelection.getCheckboxProps` is not allowed with tree structured dataSource.',
|
|
|
|
);
|
|
|
|
});
|
2020-06-28 22:41:59 +08:00
|
|
|
});
|
2020-05-21 11:02:12 +08:00
|
|
|
|
2020-06-28 22:41:59 +08:00
|
|
|
describe('cache with selected keys', () => {
|
|
|
|
it('default not cache', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2020-06-28 22:41:59 +08:00
|
|
|
<Table
|
|
|
|
dataSource={[{ name: 'light' }, { name: 'bamboo' }]}
|
|
|
|
rowSelection={{ onChange }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
2020-05-21 11:02:12 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['light'], [{ name: 'light' }], { type: 'single' });
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table dataSource={[{ name: 'bamboo' }]} rowSelection={{ onChange }} rowKey="name" />,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['bamboo'], [{ name: 'bamboo' }], { type: 'single' });
|
2020-06-28 22:41:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('cache with preserveSelectedRowKeys', () => {
|
|
|
|
const onChange = jest.fn();
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2020-06-28 22:41:59 +08:00
|
|
|
<Table
|
|
|
|
dataSource={[{ name: 'light' }, { name: 'bamboo' }]}
|
|
|
|
rowSelection={{ onChange, preserveSelectedRowKeys: true }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['light'], [{ name: 'light' }], { type: 'single' });
|
2020-06-28 22:41:59 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
dataSource={[{ name: 'bamboo' }]}
|
|
|
|
rowSelection={{ onChange, preserveSelectedRowKeys: true }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2020-06-28 22:41:59 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(
|
|
|
|
['light', 'bamboo'],
|
|
|
|
[{ name: 'light' }, { name: 'bamboo' }],
|
2022-05-18 11:59:00 +08:00
|
|
|
{ type: 'single' },
|
2020-06-28 22:41:59 +08:00
|
|
|
);
|
|
|
|
});
|
2020-12-04 14:26:23 +08:00
|
|
|
|
|
|
|
it('works with receive selectedRowKeys fron [] to undefined', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const dataSource = [{ name: 'Jack' }];
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2020-12-04 14:26:23 +08:00
|
|
|
<Table
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={{ onChange, selectedRowKeys: [0] }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={{ onChange, selectedRowKeys: undefined }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Jack'], [{ name: 'Jack' }], { type: 'single' });
|
2020-12-04 14:26:23 +08:00
|
|
|
});
|
|
|
|
|
2020-12-04 15:27:23 +08:00
|
|
|
it('works with selectionType radio receive selectedRowKeys from [] to undefined', () => {
|
2020-12-04 14:26:23 +08:00
|
|
|
const onChange = jest.fn();
|
|
|
|
const dataSource = [{ name: 'Jack' }];
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2020-12-04 14:26:23 +08:00
|
|
|
<Table
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={{ onChange, selectedRowKeys: [0], type: 'radio' }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
dataSource={dataSource}
|
|
|
|
rowSelection={{ onChange, selectedRowKeys: undefined, type: 'radio' }}
|
|
|
|
rowKey="name"
|
|
|
|
/>,
|
|
|
|
);
|
2020-12-04 14:26:23 +08:00
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(['Jack'], [{ name: 'Jack' }], { type: 'single' });
|
2020-12-04 14:26:23 +08:00
|
|
|
});
|
2021-07-02 13:37:55 +08:00
|
|
|
|
|
|
|
it('selectedRows ant selectedKeys should keep sync in initial state', () => {
|
|
|
|
const dataSource = [{ name: 'Jack' }, { name: 'Tom' }, { name: 'Lucy' }, { name: 'John' }];
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
preserveSelectedRowKeys: true,
|
|
|
|
onChange,
|
|
|
|
selectedRowKeys: ['Jack'],
|
|
|
|
};
|
2022-06-13 11:26:16 +08:00
|
|
|
const { container, rerender } = render(
|
2021-07-02 13:37:55 +08:00
|
|
|
<Table
|
|
|
|
dataSource={dataSource.slice(0, 2)}
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
rowKey="name"
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2022-06-13 11:26:16 +08:00
|
|
|
rerender(
|
|
|
|
<Table
|
|
|
|
dataSource={dataSource.slice(2, 4)}
|
|
|
|
rowSelection={rowSelection}
|
|
|
|
rowKey="name"
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
key: 'name',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
fireEvent.click(container.querySelector('tbody input'));
|
2022-05-18 11:59:00 +08:00
|
|
|
expect(onChange).toHaveBeenCalledWith(
|
|
|
|
['Jack', 'Lucy'],
|
|
|
|
[{ name: 'Jack' }, { name: 'Lucy' }],
|
|
|
|
{ type: 'single' },
|
|
|
|
);
|
2021-07-02 13:37:55 +08:00
|
|
|
});
|
2020-05-21 11:02:12 +08:00
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|