2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
2017-02-23 19:29:47 +08:00
|
|
|
import { mount, render } from 'enzyme';
|
2016-12-14 15:38:26 +08:00
|
|
|
import Table from '..';
|
2018-06-05 20:45:19 +08:00
|
|
|
import Checkbox from '../../checkbox';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Table.rowSelection', () => {
|
|
|
|
const columns = [{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
}];
|
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
|
|
|
];
|
|
|
|
|
|
|
|
function createTable(props = {}) {
|
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{}}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:09:51 +08:00
|
|
|
function renderedNames(wrapper) {
|
|
|
|
return wrapper.find('TableRow').map(row => row.props().record.name);
|
|
|
|
}
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('select by checkbox', () => {
|
|
|
|
const wrapper = mount(createTable());
|
|
|
|
const checkboxes = wrapper.find('input');
|
|
|
|
const checkboxAll = checkboxes.first();
|
|
|
|
|
|
|
|
checkboxAll.simulate('change', { target: { checked: true } });
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
selectionDirty: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
checkboxes.at(1).simulate('change', { target: { checked: false } });
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [1, 2, 3],
|
|
|
|
selectionDirty: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
checkboxes.at(1).simulate('change', { target: { checked: true } });
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [1, 2, 3, 0],
|
|
|
|
selectionDirty: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('select by radio', () => {
|
|
|
|
const wrapper = mount(createTable({ rowSelection: { type: 'radio' } }));
|
|
|
|
const radios = wrapper.find('input');
|
|
|
|
|
|
|
|
expect(radios.length).toBe(4);
|
|
|
|
|
|
|
|
radios.first().simulate('change', { target: { checked: true } });
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [0],
|
|
|
|
selectionDirty: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
radios.last().simulate('change', { target: { checked: true } });
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [3],
|
|
|
|
selectionDirty: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('pass getCheckboxProps to checkbox', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
disabled: record.name === 'Lucy',
|
2018-01-31 15:07:53 +08:00
|
|
|
name: record.name,
|
2016-12-14 14:48:09 +08:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
const checkboxes = wrapper.find('input');
|
|
|
|
|
|
|
|
expect(checkboxes.at(1).props().disabled).toBe(false);
|
2018-01-31 15:07:53 +08:00
|
|
|
expect(checkboxes.at(1).props().name).toEqual(data[0].name);
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(checkboxes.at(2).props().disabled).toBe(true);
|
2018-01-31 15:07:53 +08:00
|
|
|
expect(checkboxes.at(2).props().name).toEqual(data[1].name);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works with pagination', () => {
|
|
|
|
const wrapper = mount(createTable({ pagination: { pageSize: 2 } }));
|
|
|
|
|
|
|
|
const checkboxAll = wrapper.find('SelectionCheckboxAll');
|
|
|
|
const pagers = wrapper.find('Pager');
|
|
|
|
|
|
|
|
checkboxAll.find('input').simulate('change', { target: { checked: true } });
|
2017-09-20 16:26:18 +08:00
|
|
|
expect(checkboxAll.instance().state).toEqual({ checked: true, indeterminate: false });
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
pagers.at(1).simulate('click');
|
2017-09-20 16:26:18 +08:00
|
|
|
expect(checkboxAll.instance().state).toEqual({ checked: false, indeterminate: false });
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
pagers.at(0).simulate('click');
|
2017-09-20 16:26:18 +08:00
|
|
|
expect(checkboxAll.instance().state).toEqual({ checked: true, indeterminate: false });
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4020
|
|
|
|
it('handles defaultChecked', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => ({
|
|
|
|
defaultChecked: record.key === 0,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
2017-09-20 16:26:18 +08:00
|
|
|
let checkboxs = wrapper.find('input');
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(checkboxs.at(1).props().checked).toBe(true);
|
|
|
|
expect(checkboxs.at(2).props().checked).toBe(false);
|
|
|
|
|
|
|
|
checkboxs.at(2).simulate('change', { target: { checked: true } });
|
2017-09-20 16:26:18 +08:00
|
|
|
checkboxs = wrapper.find('input');
|
2016-12-14 14:48:09 +08:00
|
|
|
expect(checkboxs.at(1).props().checked).toBe(true);
|
|
|
|
expect(checkboxs.at(2).props().checked).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be controlled', () => {
|
|
|
|
const wrapper = mount(createTable({ rowSelection: { selectedRowKeys: [0] } }));
|
|
|
|
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [0],
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.setProps({ rowSelection: { selectedRowKeys: [1] } });
|
|
|
|
|
|
|
|
expect(wrapper.instance().store.getState()).toEqual({
|
|
|
|
selectedRowKeys: [1],
|
|
|
|
selectionDirty: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires change & select events', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const handleSelect = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onChange: handleChange,
|
|
|
|
onSelect: handleSelect,
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
|
|
|
wrapper.find('input').last().simulate('change', { target: { checked: true } });
|
|
|
|
|
|
|
|
expect(handleChange).toBeCalledWith([3], [{ key: 3, name: 'Jerry' }]);
|
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' }]);
|
|
|
|
expect(handleSelect.mock.calls[0][3].type).toBe('change');
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:49:23 +08:00
|
|
|
it('fires selectMulti event', () => {
|
|
|
|
const handleSelectMulti = jest.fn();
|
|
|
|
const handleSelect = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onSelect: handleSelect,
|
|
|
|
onSelectMulti: handleSelectMulti,
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
|
|
|
wrapper.find('input').at(1).simulate('change', {
|
|
|
|
target: { checked: true },
|
|
|
|
nativeEvent: { shiftKey: true },
|
|
|
|
});
|
|
|
|
expect(handleSelect).toBeCalled();
|
|
|
|
|
|
|
|
wrapper.find('input').at(3).simulate('change', {
|
|
|
|
target: { checked: true },
|
|
|
|
nativeEvent: { shiftKey: true },
|
|
|
|
});
|
|
|
|
expect(handleSelectMulti).toBeCalledWith(true,
|
|
|
|
[data[0], data[1], data[2]], [data[1], data[2]]);
|
|
|
|
|
|
|
|
wrapper.find('input').at(1).simulate('change', {
|
|
|
|
target: { checked: false },
|
|
|
|
nativeEvent: { shiftKey: true },
|
|
|
|
});
|
|
|
|
expect(handleSelectMulti).toBeCalledWith(false,
|
|
|
|
[], [data[0], data[1], data[2]]);
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('fires selectAll event', () => {
|
|
|
|
const handleSelectAll = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onSelectAll: handleSelectAll,
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
|
|
|
wrapper.find('input').first().simulate('change', { target: { checked: true } });
|
|
|
|
expect(handleSelectAll).toBeCalledWith(true, data, data);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
|
|
|
wrapper.find('input').first().simulate('change', { target: { checked: false } });
|
|
|
|
expect(handleSelectAll).toBeCalledWith(false, [], data);
|
|
|
|
});
|
|
|
|
|
2017-03-24 11:44:36 +08:00
|
|
|
it('render with default selection correctly', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
selections: true,
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = render(wrapper.find('Trigger').instance().getComponent());
|
2017-04-02 18:09:23 +08:00
|
|
|
expect(dropdownWrapper).toMatchSnapshot();
|
2017-02-23 19:29:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('click select all selection', () => {
|
|
|
|
const handleSelectAll = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onSelectAll: handleSelectAll,
|
2017-03-24 11:44:36 +08:00
|
|
|
selections: true,
|
2017-02-23 19:29:47 +08:00
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
|
2017-02-23 19:29:47 +08:00
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').first().simulate('click');
|
|
|
|
|
|
|
|
expect(handleSelectAll).toBeCalledWith(true, data, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires selectInvert event', () => {
|
|
|
|
const handleSelectInvert = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
onSelectInvert: handleSelectInvert,
|
2017-03-24 11:44:36 +08:00
|
|
|
selections: true,
|
2017-02-23 19:29:47 +08:00
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
const checkboxes = wrapper.find('input');
|
|
|
|
|
|
|
|
checkboxes.at(1).simulate('change', { target: { checked: true } });
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
|
2017-02-23 19:29:47 +08:00
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').last().simulate('click');
|
|
|
|
|
|
|
|
expect(handleSelectInvert).toBeCalledWith([1, 2, 3]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires selection event', () => {
|
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
selections: [{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
}, {
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
|
2017-08-27 15:44:22 +08:00
|
|
|
expect(dropdownWrapper.find('.ant-dropdown-menu-item').length).toBe(4);
|
2017-02-23 19:29:47 +08:00
|
|
|
|
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').at(2).simulate('click');
|
|
|
|
expect(handleSelectOdd).toBeCalledWith([0, 1, 2, 3]);
|
|
|
|
|
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').at(3).simulate('click');
|
|
|
|
expect(handleSelectEven).toBeCalledWith([0, 1, 2, 3]);
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2016-12-19 21:10:06 +08:00
|
|
|
|
2017-08-27 15:44:22 +08:00
|
|
|
it('could hide default selection options', () => {
|
|
|
|
const rowSelection = {
|
|
|
|
hideDefaultSelections: true,
|
|
|
|
selections: [{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
}, {
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
|
2017-08-27 15:44:22 +08:00
|
|
|
expect(dropdownWrapper.find('.ant-dropdown-menu-item').length).toBe(2);
|
|
|
|
});
|
|
|
|
|
2017-09-19 09:56:19 +08:00
|
|
|
it('handle custom selection onSelect correctly when hide default selection options', () => {
|
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
|
|
|
const rowSelection = {
|
|
|
|
hideDefaultSelections: true,
|
|
|
|
selections: [{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
}, {
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
|
2017-09-20 16:26:18 +08:00
|
|
|
const dropdownWrapper = mount(wrapper.find('Trigger').instance().getComponent());
|
2017-09-19 09:56:19 +08:00
|
|
|
expect(dropdownWrapper.find('.ant-dropdown-menu-item').length).toBe(2);
|
|
|
|
|
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').at(0).simulate('click');
|
|
|
|
expect(handleSelectOdd).toBeCalledWith([0, 1, 2, 3]);
|
|
|
|
|
|
|
|
dropdownWrapper.find('.ant-dropdown-menu-item > div').at(1).simulate('click');
|
|
|
|
expect(handleSelectEven).toBeCalledWith([0, 1, 2, 3]);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
};
|
|
|
|
const wrapper = mount(createTable({ rowSelection }));
|
|
|
|
const newData = [
|
|
|
|
{ key: 0, name: 'Jack', disabled: true },
|
|
|
|
{ key: 1, name: 'Lucy', disabled: true },
|
|
|
|
];
|
|
|
|
wrapper.setProps({ dataSource: newData });
|
2017-01-08 17:04:09 +08:00
|
|
|
wrapper.find('input').forEach((checkbox) => {
|
2016-12-19 21:10:06 +08:00
|
|
|
expect(checkbox.props().disabled).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
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', () => {
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
|
|
|
disableName: 'Jack',
|
|
|
|
};
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2018-04-22 15:19:04 +08:00
|
|
|
render() {
|
2018-06-22 21:05:13 +08:00
|
|
|
const { disableName } = this.state;
|
2018-04-22 15:19:04 +08:00
|
|
|
return (
|
|
|
|
<Table
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
rowSelection={{
|
2018-06-22 21:05:13 +08:00
|
|
|
getCheckboxProps: record => ({ disabled: record.name === disableName }),
|
2018-04-22 15:19:04 +08:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const wrapper = mount(<App />);
|
|
|
|
let checkboxs = wrapper.find('input');
|
|
|
|
expect(checkboxs.at(1).props().disabled).toBe(true);
|
|
|
|
expect(checkboxs.at(2).props().disabled).toBe(false);
|
|
|
|
wrapper.setState({ disableName: 'Lucy' });
|
|
|
|
checkboxs = wrapper.find('input');
|
|
|
|
expect(checkboxs.at(1).props().disabled).toBe(false);
|
|
|
|
expect(checkboxs.at(2).props().disabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
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(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const wrapper = mount(createTable({
|
|
|
|
rowSelection: {},
|
|
|
|
dataSource: newData,
|
|
|
|
}));
|
|
|
|
wrapper.find('Pager').last().simulate('click'); // switch to second page
|
|
|
|
wrapper.find('input').first().simulate('change', { target: { checked: true } });
|
|
|
|
wrapper.update();
|
|
|
|
expect(renderedNames(wrapper)).toEqual(['10', '11', '12', '13', '14', '15', '16', '17', '18', '19']);
|
|
|
|
});
|
2017-11-18 00:03:30 +08:00
|
|
|
|
|
|
|
it('highlight selected row', () => {
|
|
|
|
const wrapper = mount(createTable());
|
|
|
|
wrapper.find('input').at(1).simulate('change', { target: { checked: true } });
|
|
|
|
expect(wrapper.find('tbody tr').at(0).hasClass('ant-table-row-selected')).toBe(true);
|
|
|
|
});
|
2017-11-18 23:46:24 +08:00
|
|
|
|
|
|
|
it('fix selection column on the left', () => {
|
|
|
|
const wrapper = render(createTable({
|
|
|
|
rowSelection: { fixed: true },
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(wrapper).toMatchSnapshot();
|
|
|
|
});
|
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', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Table
|
|
|
|
rowSelection={{
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
}}
|
|
|
|
columns={columns}
|
|
|
|
dataSource={data}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
expect(wrapper.find(Checkbox).length).toBe(5);
|
|
|
|
wrapper.find(Checkbox).forEach((checkbox) => {
|
|
|
|
expect(checkbox.props().checked).toBe(true);
|
|
|
|
expect(checkbox.props().indeterminate).toBe(false);
|
|
|
|
});
|
|
|
|
wrapper.setProps({
|
|
|
|
dataSource: data.slice(1),
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [1, 2, 3],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(wrapper.find(Checkbox).length).toBe(4);
|
|
|
|
wrapper.find(Checkbox).forEach((checkbox) => {
|
|
|
|
expect(checkbox.props().checked).toBe(true);
|
|
|
|
expect(checkbox.props().indeterminate).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|