2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { asyncExpect, sleep } from '../../../tests/utils';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Table from '..';
|
2021-10-03 05:30:50 +00:00
|
|
|
jest.mock('../../_util/Portal');
|
2018-06-06 14:33:09 +00:00
|
|
|
describe('Table.rowSelection', () => {
|
2021-09-25 08:51:32 +00:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
2021-10-03 05:30:50 +00:00
|
|
|
document.body.innerHTML = '';
|
2021-09-25 08:51:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2019-01-12 03:33:27 +00:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
},
|
|
|
|
];
|
2018-06-06 14:33:09 +00:00
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ key: 0, name: 'Jack' },
|
|
|
|
{ key: 1, name: 'Lucy' },
|
|
|
|
{ key: 2, name: 'Tom' },
|
|
|
|
{ key: 3, name: 'Jerry' },
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
2020-08-12 08:30:13 +00:00
|
|
|
function getTableOptions(props = {}) {
|
2018-06-06 14:33:09 +00:00
|
|
|
return {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-06-06 14:33:09 +00:00
|
|
|
columns,
|
|
|
|
dataSource: data,
|
|
|
|
rowSelection: {},
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
sync: false,
|
|
|
|
attachedToDocument: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-06 14:33:09 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
function renderedNames(wrapper) {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findAllComponents({ name: 'BodyRow' }).map(row => {
|
2019-01-12 03:33:27 +00:00
|
|
|
return row.props().record.name;
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
function getSelections(wrapper) {
|
|
|
|
return [...wrapper.vm.table.selectedKeySet].sort();
|
2018-06-06 14:33:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
xit('select by checkbox', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions());
|
|
|
|
const checkboxes = wrapper.findAll('input');
|
2020-08-12 08:30:13 +00:00
|
|
|
const checkboxAll = checkboxes[0];
|
2019-01-12 03:33:27 +00:00
|
|
|
checkboxAll.element.checked = true;
|
|
|
|
checkboxAll.trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([0, 1, 2, 3]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxes[1].element.checked = false;
|
|
|
|
checkboxes[1].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([1, 2, 3]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxes[1].element.checked = true;
|
|
|
|
checkboxes[1].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([0, 1, 2, 3]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
xit('select by radio', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection: { type: 'radio' } }));
|
|
|
|
const radios = wrapper.findAll('input');
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(radios.length).toBe(4);
|
2020-08-12 08:30:13 +00:00
|
|
|
radios[0].element.checked = true;
|
|
|
|
radios[0].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([0]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
radios[radios.length - 1].element.checked = true;
|
|
|
|
radios[radios.length - 1].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([3]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('pass getCheckboxProps to checkbox', async () => {
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
2019-01-12 03:33:27 +00:00
|
|
|
getCheckboxProps: record => ({
|
2021-09-25 08:51:32 +00:00
|
|
|
disabled: record.name === 'Lucy',
|
|
|
|
name: record.name,
|
2019-01-12 03:33:27 +00:00
|
|
|
}),
|
|
|
|
};
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
2020-08-12 08:30:13 +00:00
|
|
|
const checkboxes = wrapper.findAll('input');
|
|
|
|
await sleep();
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(checkboxes[1].wrapperElement.disabled).toBe(false);
|
|
|
|
expect(checkboxes[1].wrapperElement.name).toEqual(data[0].name);
|
|
|
|
expect(checkboxes[2].wrapperElement.disabled).toBe(true);
|
|
|
|
expect(checkboxes[2].wrapperElement.name).toEqual(data[1].name);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('works with pagination', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions({ pagination: { pageSize: 2 } }));
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
|
|
|
const checkboxAll = wrapper.find('input');
|
|
|
|
checkboxAll.wrapperElement.checked = true;
|
|
|
|
checkboxAll.trigger('change');
|
2020-08-12 08:30:13 +00:00
|
|
|
const pagers = wrapper.findAllComponents({ name: 'Pager' });
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'ACheckbox' }).props()).toEqual(
|
|
|
|
expect.objectContaining({ checked: true, indeterminate: false }),
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
pagers[1].trigger('click');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'ACheckbox' }).props()).toEqual(
|
|
|
|
expect.objectContaining({ checked: false, indeterminate: false }),
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
pagers[0].trigger('click');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'ACheckbox' }).props()).toEqual(
|
|
|
|
expect.objectContaining({ checked: true, indeterminate: false }),
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4020
|
2021-09-25 08:51:32 +00:00
|
|
|
it('handles defaultChecked', async () => {
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
2018-06-08 14:38:25 +00:00
|
|
|
getCheckboxProps: record => {
|
|
|
|
return {
|
2021-09-25 08:51:32 +00:00
|
|
|
defaultChecked: record.key === 0,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-08 14:38:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
mount(Table, getTableOptions({ rowSelection }));
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(errorSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [ant-design-vue: Table] Do not set `checked` or `defaultChecked` in `getCheckboxProps`. Please use `selectedRowKeys` instead.',
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
xit('can be controlled', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection: { selectedRowKeys: [0] } }));
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([0]);
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.setProps({ rowSelection: { selectedRowKeys: [1] } });
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(getSelections(wrapper)).toEqual([1]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
|
|
|
it('fires change & select events', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleChange = jest.fn();
|
|
|
|
const handleSelect = jest.fn();
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
|
|
|
onChange: handleChange,
|
|
|
|
onSelect: handleSelect,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
|
|
|
const checkboxs = wrapper.findAll('input');
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxs[checkboxs.length - 1].element.checked = true;
|
|
|
|
checkboxs[checkboxs.length - 1].trigger('change');
|
2019-01-12 03:33:27 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(handleChange).toBeCalledWith([3], [{ key: 3, name: 'Jerry' }]);
|
|
|
|
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' }]);
|
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
|
|
|
it('fires selectAll event', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleSelectAll = jest.fn();
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
|
|
|
onSelectAll: handleSelectAll,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
|
|
|
const checkboxs = wrapper.findAll('input');
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxs[0].element.checked = true;
|
|
|
|
checkboxs[0].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectAll).toBeCalledWith(true, data, data);
|
|
|
|
});
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxs[0].element.checked = false;
|
|
|
|
checkboxs[0].trigger('change');
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectAll).toBeCalledWith(false, [], data);
|
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('render with default selection correctly', async () => {
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
|
|
|
selections: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-06 14:33:09 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2018-06-06 14:33:09 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(dropdownWrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('click select all selection', () => {
|
|
|
|
const handleChange = jest.fn();
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
2021-09-25 08:51:32 +00:00
|
|
|
onChange: handleChange,
|
2018-06-06 14:33:09 +00:00
|
|
|
selections: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-06 14:33:09 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
dropdownWrapper.findAll('.ant-dropdown-menu-item')[0].trigger('click');
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(handleChange.mock.calls[0][0]).toEqual([0, 1, 2, 3]);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('fires selectInvert event', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleSelectInvert = jest.fn();
|
2018-06-06 14:33:09 +00:00
|
|
|
const rowSelection = {
|
|
|
|
onSelectInvert: handleSelectInvert,
|
|
|
|
selections: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
|
|
|
const checkboxes = wrapper.findAll('input');
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxes[1].element.checked = true;
|
|
|
|
checkboxes[1].trigger('change');
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
2019-01-12 03:33:27 +00:00
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-06 14:33:09 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
const div = dropdownWrapper.findAll('li.ant-dropdown-menu-item');
|
|
|
|
div[1].trigger('click');
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectInvert).toBeCalledWith([1, 2, 3]);
|
|
|
|
});
|
2018-06-06 14:33:09 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('fires selection event', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
2018-06-08 14:38:25 +00:00
|
|
|
const rowSelection = {
|
2019-01-12 03:33:27 +00:00
|
|
|
selections: [
|
2021-09-25 08:51:32 +00:00
|
|
|
Table.SELECTION_ALL,
|
|
|
|
Table.SELECTION_INVERT,
|
2019-01-12 03:33:27 +00:00
|
|
|
{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-08 14:38:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(dropdownWrapper.findAll('.ant-dropdown-menu-item').length).toBe(4);
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
dropdownWrapper.findAll('.ant-dropdown-menu-item')[2].trigger('click');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectOdd).toBeCalledWith([0, 1, 2, 3]);
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
dropdownWrapper.findAll('.ant-dropdown-menu-item')[3].trigger('click');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectEven).toBeCalledWith([0, 1, 2, 3]);
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('could hide default selection options', () => {
|
2018-06-08 14:38:25 +00:00
|
|
|
const rowSelection = {
|
|
|
|
hideDefaultSelections: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
selections: [
|
|
|
|
{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-08 14:38:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
|
|
|
expect(dropdownWrapper.findAll('.ant-dropdown-menu-item').length).toBe(2);
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('handle custom selection onSelect correctly when hide default selection options', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const handleSelectOdd = jest.fn();
|
|
|
|
const handleSelectEven = jest.fn();
|
2018-06-08 14:38:25 +00:00
|
|
|
const rowSelection = {
|
|
|
|
hideDefaultSelections: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
selections: [
|
|
|
|
{
|
|
|
|
key: 'odd',
|
|
|
|
text: '奇数项',
|
|
|
|
onSelect: handleSelectOdd,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'even',
|
|
|
|
text: '偶数项',
|
|
|
|
onSelect: handleSelectEven,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2018-06-08 14:38:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
|
|
|
expect(dropdownWrapper.findAll('.ant-dropdown-menu-item').length).toBe(2);
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
dropdownWrapper.findAll('.ant-dropdown-menu-item')[0].trigger('click');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectOdd).toBeCalledWith([0, 1, 2, 3]);
|
2018-06-08 14:38:25 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
dropdownWrapper.findAll('.ant-dropdown-menu-item')[1].trigger('click');
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(handleSelectEven).toBeCalledWith([0, 1, 2, 3]);
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
|
|
|
// https:// github.com/ant-design/ant-design/issues/4245
|
2021-09-25 08:51:32 +00:00
|
|
|
it('handles disabled checkbox correctly when dataSource changes', async () => {
|
2018-06-08 14:38:25 +00:00
|
|
|
const rowSelection = {
|
|
|
|
getCheckboxProps: record => {
|
2021-09-25 08:51:32 +00:00
|
|
|
return { disabled: record.disabled };
|
2018-06-08 14:38:25 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Table, getTableOptions({ rowSelection }));
|
2018-06-08 14:38:25 +00:00
|
|
|
const newData = [
|
|
|
|
{ key: 7, name: 'Jack', disabled: true },
|
|
|
|
{ key: 8, name: 'Lucy', disabled: true },
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
2018-06-08 14:38:25 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.setProps({ dataSource: newData });
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
wrapper.findAll('input').forEach(checkbox => {
|
|
|
|
expect(checkbox.wrapperElement.disabled).toBe(true);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/4779
|
2021-09-25 08:51:32 +00:00
|
|
|
it('should not switch pagination when select record', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const newData = [];
|
2018-06-08 14:38:25 +00:00
|
|
|
for (let i = 0; i < 20; i += 1) {
|
|
|
|
newData.push({
|
|
|
|
key: i.toString(),
|
|
|
|
name: i.toString(),
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
Table,
|
|
|
|
getTableOptions({
|
|
|
|
rowSelection: {},
|
|
|
|
dataSource: newData,
|
|
|
|
}),
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
|
|
|
const pager = wrapper.findAllComponents({ name: 'Pager' });
|
|
|
|
pager[pager.length - 1].trigger('click'); // switch to second page
|
2020-08-12 08:30:13 +00:00
|
|
|
wrapper.findAll('input')[0].element.checked = true;
|
|
|
|
wrapper.findAll('input')[0].trigger('change');
|
2019-01-12 03:33:27 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(renderedNames(wrapper)).toEqual([
|
|
|
|
'10',
|
|
|
|
'11',
|
|
|
|
'12',
|
|
|
|
'13',
|
|
|
|
'14',
|
|
|
|
'15',
|
|
|
|
'16',
|
|
|
|
'17',
|
|
|
|
'18',
|
|
|
|
'19',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
|
|
|
it('highlight selected row', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(Table, getTableOptions());
|
2020-08-12 08:30:13 +00:00
|
|
|
wrapper.findAll('input')[1].element.checked = true;
|
|
|
|
wrapper.findAll('input')[1].trigger('change');
|
|
|
|
await asyncExpect(() => {
|
|
|
|
expect(wrapper.findAll('tbody tr')[0].classes()).toContain('ant-table-row-selected');
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-06-08 14:38:25 +00:00
|
|
|
|
|
|
|
it('fix selection column on the left', async () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
Table,
|
|
|
|
getTableOptions({
|
|
|
|
rowSelection: { fixed: true },
|
|
|
|
}),
|
|
|
|
);
|
2018-06-08 14:38:25 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/10629
|
2021-09-25 08:51:32 +00:00
|
|
|
it('should keep all checked state when remove item from dataSource', async () => {
|
2018-12-12 02:15:00 +00:00
|
|
|
const wrapper = mount(Table, {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-12-12 02:15:00 +00:00
|
|
|
columns,
|
|
|
|
dataSource: data,
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [0, 1, 2, 3],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-12 08:30:13 +00:00
|
|
|
expect(wrapper.findAllComponents({ name: 'ACheckbox' }).length).toBe(5);
|
|
|
|
const allCheckbox = wrapper.findAllComponents({ name: 'ACheckbox' });
|
2018-12-12 02:15:00 +00:00
|
|
|
Array(allCheckbox.length).forEach((_, index) => {
|
2021-09-25 08:51:32 +00:00
|
|
|
const checkbox = allCheckbox[index];
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(checkbox.vm.checked).toBe(true);
|
|
|
|
expect(checkbox.vm.indeterminate).toBe(false);
|
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
wrapper.setProps({
|
|
|
|
dataSource: data.slice(1),
|
|
|
|
rowSelection: {
|
|
|
|
selectedRowKeys: [1, 2, 3],
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
await asyncExpect(() => {
|
2020-08-12 08:30:13 +00:00
|
|
|
expect(wrapper.findAllComponents({ name: 'ACheckbox' }).length).toBe(4);
|
|
|
|
const allCheckbox = wrapper.findAllComponents({ name: 'ACheckbox' });
|
2018-12-12 02:15:00 +00:00
|
|
|
Array(allCheckbox.length).forEach((_, index) => {
|
2021-09-25 08:51:32 +00:00
|
|
|
const checkbox = allCheckbox[index];
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(checkbox.vm.checked).toBe(true);
|
|
|
|
expect(checkbox.vm.indeterminate).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11042
|
|
|
|
it('add columnTitle for rowSelection', async () => {
|
|
|
|
const wrapper = mount(Table, {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2018-12-12 02:15:00 +00:00
|
|
|
columns,
|
|
|
|
dataSource: data,
|
|
|
|
rowSelection: {
|
|
|
|
columnTitle: '多选',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findAll('thead tr th')[0].text()).toBe('多选');
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-12-12 02:15:00 +00:00
|
|
|
await asyncExpect(() => {
|
|
|
|
wrapper.setProps({
|
|
|
|
rowSelection: {
|
|
|
|
type: 'radio',
|
|
|
|
columnTitle: '单选',
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findAll('thead tr th')[0].text()).toBe('单选');
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/11384
|
2021-09-25 08:51:32 +00:00
|
|
|
it('should keep item even if in filter', async () => {
|
2019-01-06 10:11:50 +00: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,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const onChange = jest.fn();
|
2019-01-06 10:11:50 +00:00
|
|
|
const rowSelection = {
|
|
|
|
onChange,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2019-01-06 10:11:50 +00:00
|
|
|
|
|
|
|
const wrapper = mount(Table, {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2019-01-12 03:33:27 +00:00
|
|
|
columns: filterColumns,
|
|
|
|
dataSource: data,
|
2020-03-18 06:52:02 +00:00
|
|
|
rowSelection,
|
2019-01-06 10:11:50 +00:00
|
|
|
},
|
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const dropdownWrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
2021-09-25 08:51:32 +00:00
|
|
|
return wrapper.findComponent({ name: 'Trigger' }).vm.getComponent();
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
2019-01-06 10:11:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
{ sync: false },
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
|
|
|
function clickFilter(indexList) {
|
|
|
|
indexList.forEach(index => {
|
|
|
|
dropdownWrapper
|
|
|
|
.findAll('.ant-dropdown-menu-item .ant-checkbox-wrapper')
|
|
|
|
[index].trigger('click');
|
|
|
|
});
|
|
|
|
dropdownWrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').trigger('click');
|
|
|
|
}
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function clickItem() {
|
2020-08-12 08:30:13 +00:00
|
|
|
wrapper.findAll(
|
|
|
|
'tbody .ant-table-selection-column .ant-checkbox-input',
|
|
|
|
)[0].element.checked = true;
|
|
|
|
wrapper.findAll('tbody .ant-table-selection-column .ant-checkbox-input')[0].trigger('change');
|
2019-01-06 10:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check Jack
|
2021-09-25 08:51:32 +00:00
|
|
|
clickFilter([0]);
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.findAll('tbody tr').length).toBe(1);
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
clickItem();
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onChange.mock.calls[0][0].length).toBe(1);
|
|
|
|
expect(onChange.mock.calls[0][1].length).toBe(1);
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
// Check Lucy
|
2021-10-03 05:30:50 +00:00
|
|
|
clickFilter([0]);
|
|
|
|
await sleep();
|
|
|
|
clickFilter([1]);
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.findAll('tbody tr').length).toBe(1);
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
clickItem();
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onChange.mock.calls[1][0].length).toBe(2);
|
|
|
|
expect(onChange.mock.calls[1][1].length).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
it('render correctly when set childrenColumnName', async () => {
|
2019-01-06 10:11:50 +00:00
|
|
|
const newDatas = [
|
|
|
|
{
|
|
|
|
key: 1,
|
|
|
|
name: 'Jack',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 11,
|
|
|
|
name: 'John Brown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 2,
|
|
|
|
name: 'Lucy',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
key: 21,
|
|
|
|
name: 'Lucy Brown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
2019-01-06 10:11:50 +00:00
|
|
|
|
|
|
|
const wrapper = mount(Table, {
|
2020-07-25 07:46:49 +00:00
|
|
|
props: {
|
2020-03-18 06:52:02 +00:00
|
|
|
columns,
|
2019-01-12 03:33:27 +00:00
|
|
|
dataSource: newDatas,
|
|
|
|
rowSelection: {},
|
|
|
|
childrenColumnName: 'test',
|
2019-01-06 10:11:50 +00:00
|
|
|
},
|
|
|
|
sync: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2019-01-06 10:11:50 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const checkboxes = wrapper.findAll('input');
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxes[1].element.checked = true;
|
|
|
|
checkboxes[1].trigger('change');
|
2021-09-25 08:51:32 +00:00
|
|
|
await sleep();
|
|
|
|
expect(wrapper.findComponent({ name: 'ACheckbox' }).props()).toEqual(
|
|
|
|
expect.objectContaining({ checked: false, indeterminate: true }),
|
|
|
|
);
|
2020-08-12 08:30:13 +00:00
|
|
|
checkboxes[2].element.checked = true;
|
|
|
|
checkboxes[2].trigger('change');
|
2019-01-06 10:11:50 +00:00
|
|
|
await asyncExpect(() => {
|
2021-09-25 08:51:32 +00:00
|
|
|
expect(wrapper.findComponent({ name: 'ACheckbox' }).props()).toEqual(
|
|
|
|
expect.objectContaining({ checked: true, indeterminate: false }),
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|