2020-09-16 11:43:55 +08:00
|
|
|
/* eslint @typescript-eslint/no-use-before-define: "off" */
|
2016-12-16 17:59:52 +08:00
|
|
|
import React from 'react';
|
2020-12-05 23:51:09 +08:00
|
|
|
import { mount } from 'enzyme';
|
2016-12-16 17:59:52 +08:00
|
|
|
import Transfer from '..';
|
|
|
|
import TransferList from '../list';
|
|
|
|
import TransferOperation from '../operation';
|
|
|
|
import TransferSearch from '../search';
|
2019-05-07 17:10:42 +08:00
|
|
|
import TransferItem from '../ListItem';
|
2016-12-16 17:59:52 +08:00
|
|
|
import Button from '../../button';
|
|
|
|
import Checkbox from '../../checkbox';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2016-12-16 17:59:52 +08:00
|
|
|
|
2018-09-19 15:05:55 +08:00
|
|
|
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
const listCommonProps = {
|
2018-12-07 16:17:45 +08:00
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'c',
|
|
|
|
title: 'c',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
],
|
2016-12-16 17:59:52 +08:00
|
|
|
selectedKeys: ['a'],
|
|
|
|
targetKeys: ['b'],
|
|
|
|
};
|
|
|
|
|
2017-02-24 19:08:02 +08:00
|
|
|
const listDisabledProps = {
|
2018-12-07 16:17:45 +08:00
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
disabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
},
|
|
|
|
],
|
2017-02-24 19:08:02 +08:00
|
|
|
selectedKeys: ['a', 'b'],
|
|
|
|
targetKeys: [],
|
|
|
|
};
|
|
|
|
|
2017-01-27 17:08:12 +08:00
|
|
|
const searchTransferProps = {
|
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: '0',
|
|
|
|
title: 'content1',
|
|
|
|
description: 'description of content1',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
title: 'content2',
|
|
|
|
description: 'description of content2',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
title: 'content3',
|
|
|
|
description: 'description of content3',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
title: 'content4',
|
|
|
|
description: 'description of content4',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
title: 'content5',
|
|
|
|
description: 'description of content5',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: '5',
|
|
|
|
title: 'content6',
|
|
|
|
description: 'description of content6',
|
|
|
|
chosen: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
selectedKeys: [],
|
|
|
|
targetKeys: ['3', '4'],
|
|
|
|
};
|
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
describe('Transfer', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Transfer);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Transfer);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
it('should render correctly', () => {
|
2020-12-05 23:51:09 +08:00
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} />);
|
|
|
|
expect(wrapper).toMatchRenderedSnapshot();
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should move selected keys to corresponding list', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onChange={handleChange} />);
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferOperation).find(Button).at(0).simulate('click'); // move selected keys to right list
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleChange).toHaveBeenCalledWith(['a', 'b'], 'right', ['a']);
|
|
|
|
});
|
|
|
|
|
2019-09-26 11:37:06 +08:00
|
|
|
it('should move selected keys to left list', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
{...listCommonProps}
|
|
|
|
selectedKeys={['a']}
|
|
|
|
targetKeys={['a']}
|
|
|
|
onChange={handleChange}
|
2019-09-26 22:28:08 +08:00
|
|
|
/>,
|
2019-09-26 11:37:06 +08:00
|
|
|
);
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferOperation).find(Button).at(1).simulate('click'); // move selected keys to left list
|
2019-09-26 11:37:06 +08:00
|
|
|
expect(handleChange).toHaveBeenCalledWith([], 'left', ['a']);
|
|
|
|
});
|
|
|
|
|
2017-02-24 19:08:02 +08:00
|
|
|
it('should move selected keys expect disabled to corresponding list', () => {
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listDisabledProps} onChange={handleChange} />);
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferOperation).find(Button).at(0).simulate('click'); // move selected keys to right list
|
2017-02-24 19:08:02 +08:00
|
|
|
expect(handleChange).toHaveBeenCalledWith(['b'], 'right', ['b']);
|
|
|
|
});
|
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
it('should uncheck checkbox when click on checked item', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onSelectChange={handleSelectChange} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferItem)
|
|
|
|
.filterWhere(n => n.prop('item').key === 'a')
|
|
|
|
.simulate('click');
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenLastCalledWith([], []);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check checkbox when click on unchecked item', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onSelectChange={handleSelectChange} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferItem)
|
|
|
|
.filterWhere(n => n.prop('item').key === 'b')
|
|
|
|
.simulate('click');
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenLastCalledWith(['a'], ['b']);
|
|
|
|
});
|
|
|
|
|
2019-09-17 13:31:32 +08:00
|
|
|
it('should not check checkbox when component disabled', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer {...listCommonProps} disabled onSelectChange={handleSelectChange} />,
|
|
|
|
);
|
|
|
|
wrapper
|
|
|
|
.find(TransferItem)
|
|
|
|
.filterWhere(n => n.prop('item').key === 'a')
|
|
|
|
.simulate('click');
|
|
|
|
expect(handleSelectChange).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2016-12-16 17:59:52 +08:00
|
|
|
it('should not check checkbox when click on disabled item', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onSelectChange={handleSelectChange} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferItem)
|
|
|
|
.filterWhere(n => n.prop('item').key === 'c')
|
|
|
|
.simulate('click');
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleSelectChange).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should check all item when click on check all', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onSelectChange={handleSelectChange} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.filterWhere(n => !n.prop('checked'))
|
|
|
|
.simulate('change');
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenCalledWith(['a'], ['b']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should uncheck all item when click on uncheck all', () => {
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onSelectChange={handleSelectChange} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.filterWhere(n => n.prop('checked'))
|
|
|
|
.simulate('change');
|
2016-12-16 17:59:52 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenCalledWith([], []);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call `filterOption` when use input in search box', () => {
|
|
|
|
const filterOption = (inputValue, option) => inputValue === option.title;
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} showSearch filterOption={filterOption} />);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferSearch)
|
|
|
|
.at(0)
|
|
|
|
.find('input')
|
|
|
|
.simulate('change', { target: { value: 'a' } });
|
2020-05-13 19:15:40 +08:00
|
|
|
expect(wrapper.find(TransferList).at(0).find(TransferItem).find(Checkbox)).toHaveLength(1);
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|
2017-01-27 17:08:12 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
const headerText = wrapper =>
|
|
|
|
wrapper
|
|
|
|
.find(TransferList)
|
|
|
|
.at(0)
|
2020-05-13 19:15:40 +08:00
|
|
|
.find('.ant-transfer-list-header-selected')
|
2018-12-07 16:17:45 +08:00
|
|
|
.at(0)
|
|
|
|
.first()
|
|
|
|
.text()
|
|
|
|
.trim();
|
2018-08-28 16:26:32 +08:00
|
|
|
|
2017-01-27 17:08:12 +08:00
|
|
|
it('should display the correct count of items when filter by input', () => {
|
|
|
|
const filterOption = (inputValue, option) => option.description.indexOf(inputValue) > -1;
|
|
|
|
const renderFunc = item => item.title;
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
{...searchTransferProps}
|
|
|
|
showSearch
|
|
|
|
filterOption={filterOption}
|
|
|
|
render={renderFunc}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2017-01-27 17:08:12 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferSearch)
|
|
|
|
.at(0)
|
|
|
|
.find('input')
|
|
|
|
.simulate('change', { target: { value: 'content2' } });
|
2020-01-29 22:22:57 +08:00
|
|
|
expect(headerText(wrapper)).toEqual('1 item');
|
2018-08-28 16:26:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should display the correct locale', () => {
|
|
|
|
const emptyProps = { dataSource: [], selectedKeys: [], targetKeys: [] };
|
|
|
|
const locale = { itemUnit: 'Person', notFoundContent: 'Nothing', searchPlaceholder: 'Search' };
|
2018-11-28 15:00:03 +08:00
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Transfer {...listCommonProps} {...emptyProps} showSearch locale={locale} />,
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2018-08-28 16:26:32 +08:00
|
|
|
|
|
|
|
expect(headerText(wrapper)).toEqual('0 Person');
|
|
|
|
|
|
|
|
expect(
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferList).at(0).find('.ant-transfer-list-search').at(0).prop('placeholder'),
|
2018-08-28 16:26:32 +08:00
|
|
|
).toEqual('Search');
|
|
|
|
|
|
|
|
expect(
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferList).at(0).find('.ant-transfer-list-body-not-found').at(0).text(),
|
2018-08-28 16:26:32 +08:00
|
|
|
).toEqual('Nothing');
|
|
|
|
});
|
|
|
|
|
2019-07-09 11:46:21 +08:00
|
|
|
it('should display the correct locale and ignore old API', () => {
|
2018-08-28 16:26:32 +08:00
|
|
|
const emptyProps = { dataSource: [], selectedKeys: [], targetKeys: [] };
|
|
|
|
const locale = { notFoundContent: 'old1', searchPlaceholder: 'old2' };
|
2019-07-09 11:46:21 +08:00
|
|
|
const newLocalProp = { notFoundContent: 'new1', searchPlaceholder: 'new2' };
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
{...listCommonProps}
|
|
|
|
{...emptyProps}
|
|
|
|
{...locale}
|
|
|
|
locale={newLocalProp}
|
|
|
|
showSearch
|
|
|
|
/>,
|
|
|
|
);
|
2018-08-28 16:26:32 +08:00
|
|
|
|
|
|
|
expect(
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferList).at(0).find('.ant-transfer-list-search').at(0).prop('placeholder'),
|
2019-07-09 11:46:21 +08:00
|
|
|
).toEqual('new2');
|
2018-08-28 16:26:32 +08:00
|
|
|
|
|
|
|
expect(
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferList).at(0).find('.ant-transfer-list-body-not-found').at(0).text(),
|
2019-07-09 11:46:21 +08:00
|
|
|
).toEqual('new1');
|
2018-09-19 15:05:55 +08:00
|
|
|
|
2019-07-09 11:46:21 +08:00
|
|
|
expect(consoleErrorSpy).not.toHaveBeenCalledWith(
|
2019-02-27 15:32:29 +08:00
|
|
|
'Warning: [antd: Transfer] `notFoundContent` and `searchPlaceholder` will be removed, please use `locale` instead.',
|
2018-09-19 15:05:55 +08:00
|
|
|
);
|
|
|
|
consoleErrorSpy.mockRestore();
|
2018-08-28 16:26:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should display the correct items unit', () => {
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} locale={{ itemsUnit: 'People' }} />);
|
|
|
|
|
|
|
|
expect(headerText(wrapper)).toEqual('1/2 People');
|
2017-01-27 17:08:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should just check the filtered item when click on check all after search by input', () => {
|
|
|
|
const filterOption = (inputValue, option) => option.description.indexOf(inputValue) > -1;
|
|
|
|
const renderFunc = item => item.title;
|
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
{...searchTransferProps}
|
|
|
|
showSearch
|
|
|
|
filterOption={filterOption}
|
|
|
|
render={renderFunc}
|
|
|
|
onSelectChange={handleSelectChange}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2017-01-27 17:08:12 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferSearch)
|
|
|
|
.at(0)
|
|
|
|
.find('input')
|
2017-01-27 17:08:12 +08:00
|
|
|
.simulate('change', { target: { value: 'content2' } });
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferList)
|
|
|
|
.at(0)
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.filterWhere(n => !n.prop('checked'))
|
2017-01-27 17:08:12 +08:00
|
|
|
.simulate('change');
|
|
|
|
expect(handleSelectChange).toHaveBeenCalledWith(['1'], []);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should transfer just the filtered item after search by input', () => {
|
|
|
|
const filterOption = (inputValue, option) => option.description.indexOf(inputValue) > -1;
|
|
|
|
const renderFunc = item => item.title;
|
|
|
|
const handleChange = jest.fn();
|
|
|
|
const handleSelectChange = (sourceSelectedKeys, targetSelectedKeys) => {
|
|
|
|
wrapper.setProps({
|
|
|
|
selectedKeys: [...sourceSelectedKeys, ...targetSelectedKeys],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
{...searchTransferProps}
|
|
|
|
showSearch
|
|
|
|
filterOption={filterOption}
|
|
|
|
render={renderFunc}
|
|
|
|
onSelectChange={handleSelectChange}
|
|
|
|
onChange={handleChange}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2017-01-27 17:08:12 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferSearch)
|
|
|
|
.at(0)
|
|
|
|
.find('input')
|
2017-01-27 17:08:12 +08:00
|
|
|
.simulate('change', { target: { value: 'content2' } });
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferList)
|
|
|
|
.at(0)
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.filterWhere(n => !n.prop('checked'))
|
2017-01-27 17:08:12 +08:00
|
|
|
.simulate('change');
|
2020-05-13 19:15:40 +08:00
|
|
|
wrapper.find(TransferOperation).find(Button).at(0).simulate('click');
|
2017-01-27 17:08:12 +08:00
|
|
|
expect(handleChange).toHaveBeenCalledWith(['1', '3', '4'], 'right', ['1']);
|
|
|
|
});
|
2017-05-03 19:05:31 +08:00
|
|
|
|
|
|
|
it('should check correctly when there is a search text', () => {
|
2018-06-22 21:05:13 +08:00
|
|
|
const newProps = { ...listCommonProps };
|
|
|
|
delete newProps.targetKeys;
|
|
|
|
delete newProps.selectedKeys;
|
2017-05-03 19:05:31 +08:00
|
|
|
const handleSelectChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-11-28 15:00:03 +08:00
|
|
|
<Transfer
|
|
|
|
{...newProps}
|
|
|
|
showSearch
|
|
|
|
onSelectChange={handleSelectChange}
|
|
|
|
render={item => item.title}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2017-05-03 19:05:31 +08:00
|
|
|
);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferItem)
|
|
|
|
.filterWhere(n => n.prop('item').key === 'b')
|
|
|
|
.simulate('click');
|
2017-05-03 19:05:31 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenLastCalledWith(['b'], []);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferSearch)
|
|
|
|
.at(0)
|
|
|
|
.find('input')
|
|
|
|
.simulate('change', { target: { value: 'a' } });
|
|
|
|
wrapper
|
|
|
|
.find(TransferList)
|
|
|
|
.at(0)
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.simulate('change');
|
2017-05-03 19:05:31 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenLastCalledWith(['b', 'a'], []);
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find(TransferList)
|
|
|
|
.at(0)
|
|
|
|
.find('.ant-transfer-list-header input[type="checkbox"]')
|
|
|
|
.simulate('change');
|
2017-05-03 19:05:31 +08:00
|
|
|
expect(handleSelectChange).toHaveBeenLastCalledWith(['b'], []);
|
|
|
|
});
|
2017-05-11 10:41:14 +08:00
|
|
|
|
2020-05-13 19:15:40 +08:00
|
|
|
it('should show sorted targetKey', () => {
|
2017-05-11 10:41:14 +08:00
|
|
|
const sortedTargetKeyProps = {
|
2018-12-07 16:17:45 +08:00
|
|
|
dataSource: [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'c',
|
|
|
|
title: 'c',
|
|
|
|
},
|
|
|
|
],
|
2017-05-11 10:41:14 +08:00
|
|
|
targetKeys: ['c', 'b'],
|
|
|
|
lazy: false,
|
|
|
|
};
|
2020-12-05 23:51:09 +08:00
|
|
|
const wrapper = mount(<Transfer {...sortedTargetKeyProps} render={item => item.title} />);
|
|
|
|
expect(wrapper).toMatchRenderedSnapshot();
|
2017-05-11 10:41:14 +08:00
|
|
|
});
|
2018-05-29 15:30:47 +08:00
|
|
|
|
|
|
|
it('should add custom styles when their props are provided', () => {
|
|
|
|
const style = {
|
|
|
|
backgroundColor: 'red',
|
|
|
|
};
|
2019-10-22 11:05:39 +08:00
|
|
|
const leftStyle = {
|
2018-05-29 15:30:47 +08:00
|
|
|
backgroundColor: 'blue',
|
|
|
|
};
|
2019-10-22 11:05:39 +08:00
|
|
|
const rightStyle = {
|
|
|
|
backgroundColor: 'red',
|
|
|
|
};
|
2018-05-29 15:30:47 +08:00
|
|
|
const operationStyle = {
|
|
|
|
backgroundColor: 'yellow',
|
|
|
|
};
|
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
const component = mount(
|
|
|
|
<Transfer
|
|
|
|
{...listCommonProps}
|
|
|
|
style={style}
|
2019-10-22 11:05:39 +08:00
|
|
|
listStyle={({ direction }) => (direction === 'left' ? leftStyle : rightStyle)}
|
2018-11-28 15:00:03 +08:00
|
|
|
operationStyle={operationStyle}
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-11-28 15:00:03 +08:00
|
|
|
);
|
2018-05-29 15:30:47 +08:00
|
|
|
|
2018-05-29 16:37:04 +08:00
|
|
|
const wrapper = component.find('.ant-transfer');
|
|
|
|
const listSource = component.find('.ant-transfer-list').first();
|
|
|
|
const listTarget = component.find('.ant-transfer-list').last();
|
|
|
|
const operation = component.find('.ant-transfer-operation').first();
|
2018-05-29 15:30:47 +08:00
|
|
|
|
2018-05-29 16:37:04 +08:00
|
|
|
expect(wrapper.prop('style')).toHaveProperty('backgroundColor', 'red');
|
|
|
|
expect(listSource.prop('style')).toHaveProperty('backgroundColor', 'blue');
|
2019-10-22 11:05:39 +08:00
|
|
|
expect(listTarget.prop('style')).toHaveProperty('backgroundColor', 'red');
|
2018-05-29 16:37:04 +08:00
|
|
|
expect(operation.prop('style')).toHaveProperty('backgroundColor', 'yellow');
|
2018-05-29 15:30:47 +08:00
|
|
|
});
|
2019-09-26 11:37:06 +08:00
|
|
|
|
|
|
|
it('should support onScroll', () => {
|
|
|
|
const onScroll = jest.fn();
|
2019-09-26 22:28:08 +08:00
|
|
|
const component = mount(<Transfer {...listCommonProps} onScroll={onScroll} />);
|
|
|
|
component
|
|
|
|
.find('.ant-transfer-list')
|
|
|
|
.at(0)
|
|
|
|
.find('.ant-transfer-list-content')
|
|
|
|
.at(0)
|
|
|
|
.simulate('scroll');
|
2019-09-26 11:37:06 +08:00
|
|
|
expect(onScroll).toHaveBeenLastCalledWith('left', expect.anything());
|
2019-09-26 22:28:08 +08:00
|
|
|
component
|
|
|
|
.find('.ant-transfer-list')
|
|
|
|
.at(1)
|
|
|
|
.find('.ant-transfer-list-content')
|
|
|
|
.at(0)
|
|
|
|
.simulate('scroll');
|
2019-09-26 11:37:06 +08:00
|
|
|
expect(onScroll).toHaveBeenLastCalledWith('right', expect.anything());
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support rowKey is function', () => {
|
|
|
|
expect(() => {
|
2019-09-26 22:28:08 +08:00
|
|
|
mount(<Transfer {...listCommonProps} rowKey={record => record.key} />);
|
2019-09-26 11:37:06 +08:00
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should support render value and label in item', () => {
|
|
|
|
const component = mount(
|
|
|
|
<Transfer
|
|
|
|
dataSource={[
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'title',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
render={record => ({ value: `${record.title} value`, label: 'label' })}
|
|
|
|
/>,
|
|
|
|
);
|
2020-12-05 23:51:09 +08:00
|
|
|
expect(component).toMatchRenderedSnapshot();
|
2019-09-26 11:37:06 +08:00
|
|
|
});
|
2020-02-04 10:16:23 +08:00
|
|
|
|
|
|
|
it('should render correct checkbox label when checkboxLabel is defined', () => {
|
|
|
|
const selectAllLabels = ['Checkbox Label'];
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} selectAllLabels={selectAllLabels} />);
|
|
|
|
expect(headerText(wrapper)).toEqual('Checkbox Label');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render correct checkbox label when checkboxLabel is a function', () => {
|
|
|
|
const selectAllLabels = [
|
|
|
|
({ selectedCount, totalCount }) => (
|
|
|
|
<span>
|
|
|
|
{selectedCount} of {totalCount}
|
|
|
|
</span>
|
|
|
|
),
|
|
|
|
];
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} selectAllLabels={selectAllLabels} />);
|
|
|
|
expect(headerText(wrapper)).toEqual('1 of 2');
|
|
|
|
});
|
2020-05-13 19:15:40 +08:00
|
|
|
|
|
|
|
describe('pagination', () => {
|
|
|
|
it('boolean', () => {
|
|
|
|
const wrapper = mount(<Transfer {...listDisabledProps} pagination />);
|
|
|
|
expect(wrapper.find('Pagination').first().props()).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
pageSize: 10,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('object', () => {
|
|
|
|
const wrapper = mount(<Transfer {...listDisabledProps} pagination={{ pageSize: 1 }} />);
|
|
|
|
expect(
|
|
|
|
wrapper.find('.ant-transfer-list').first().find('.ant-transfer-list-content-item'),
|
|
|
|
).toHaveLength(1);
|
|
|
|
expect(wrapper.find('Pagination').first().props()).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
pageSize: 1,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('not exceed max size', () => {
|
|
|
|
const wrapper = mount(<Transfer {...listDisabledProps} pagination={{ pageSize: 1 }} />);
|
|
|
|
wrapper.find('.ant-pagination-next .ant-pagination-item-link').first().simulate('click');
|
|
|
|
expect(wrapper.find('Pagination').first().props()).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
current: 2,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
wrapper.setProps({ targetKeys: ['b', 'c'] });
|
|
|
|
expect(wrapper.find('Pagination').first().props()).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
current: 1,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('remove by click icon', () => {
|
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(<Transfer {...listCommonProps} onChange={onChange} oneWay />);
|
|
|
|
wrapper.find('.ant-transfer-list-content-item-remove').first().simulate('click');
|
|
|
|
expect(onChange).toHaveBeenCalledWith([], 'left', ['b']);
|
|
|
|
});
|
2016-12-16 17:59:52 +08:00
|
|
|
});
|
2021-01-08 10:14:01 +08:00
|
|
|
|
|
|
|
describe('immutable data', () => {
|
|
|
|
// https://github.com/ant-design/ant-design/issues/28662
|
|
|
|
it('dataSource is frozen', () => {
|
|
|
|
const mockData = [
|
|
|
|
Object.freeze({
|
|
|
|
id: 0,
|
|
|
|
title: `title`,
|
|
|
|
description: `description`,
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
const wrapper = mount(<Transfer rowKey={item => item.id} dataSource={mockData} />);
|
|
|
|
expect(wrapper).toMatchRenderedSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2021-02-22 18:41:02 +08:00
|
|
|
|
|
|
|
describe('footer render source and target', () => {
|
|
|
|
// https://github.com/ant-design/ant-design/issues/28082
|
|
|
|
it('currently render footer', () => {
|
|
|
|
const differentFooter = () => ({
|
|
|
|
source: (
|
|
|
|
<Button size="small" className="sourceFooter">
|
|
|
|
reload
|
|
|
|
</Button>
|
|
|
|
),
|
|
|
|
target: (
|
|
|
|
<Button size="small" className="targetFooter">
|
|
|
|
reload
|
|
|
|
</Button>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
const defaultFooter = () => (
|
|
|
|
<Button size="small" className="defaultFooter">
|
|
|
|
reload
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
const wrapper = mount(<Transfer footer={differentFooter} />);
|
|
|
|
const wrapper2 = mount(<Transfer footer={defaultFooter} />);
|
|
|
|
expect(wrapper.exists('.sourceFooter')).toEqual(true);
|
|
|
|
expect(wrapper.exists('.targetFooter')).toEqual(true);
|
|
|
|
expect(wrapper2.exists('.defaultFooter')).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|