2016-12-14 14:48:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Search from '../search';
|
2018-10-12 23:47:42 +08:00
|
|
|
import Transfer from '../index';
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
describe('Search', () => {
|
2018-10-12 23:47:42 +08:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2016-12-14 14:48:09 +08:00
|
|
|
it('should show cross icon when input value exists', () => {
|
2017-10-09 13:23:20 +08:00
|
|
|
const wrapper = mount(<Search value="" />);
|
2016-12-14 14:48:09 +08:00
|
|
|
|
2017-04-02 18:09:23 +08:00
|
|
|
expect(wrapper).toMatchSnapshot();
|
2016-12-14 14:48:09 +08:00
|
|
|
|
|
|
|
wrapper.setProps({ value: 'a' });
|
|
|
|
|
2017-04-02 18:09:23 +08:00
|
|
|
expect(wrapper).toMatchSnapshot();
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|
2018-10-12 23:47:42 +08:00
|
|
|
|
|
|
|
it('onSearch', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const dataSource = [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
description: 'a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
description: 'b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'c',
|
|
|
|
title: 'c',
|
|
|
|
description: 'c',
|
|
|
|
},
|
|
|
|
];
|
2018-10-12 23:47:42 +08:00
|
|
|
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Transfer
|
|
|
|
dataSource={dataSource}
|
|
|
|
selectedKeys={[]}
|
|
|
|
targetKeys={[]}
|
|
|
|
render={item => item.title}
|
|
|
|
onSearch={onSearch}
|
|
|
|
showSearch
|
2018-12-07 16:17:45 +08:00
|
|
|
/>,
|
2018-10-12 23:47:42 +08:00
|
|
|
);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change', { target: { value: 'a' } });
|
2018-10-12 23:47:42 +08:00
|
|
|
expect(onSearch).toBeCalledWith('left', 'a');
|
|
|
|
|
|
|
|
onSearch.mockReset();
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-transfer-list-search-action')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2018-10-12 23:47:42 +08:00
|
|
|
expect(onSearch).toBeCalledWith('left', '');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('legacy onSearchChange', () => {
|
|
|
|
const onSearchChange = jest.fn();
|
|
|
|
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Transfer render={item => item.title} onSearchChange={onSearchChange} showSearch />,
|
2018-10-12 23:47:42 +08:00
|
|
|
);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-input')
|
|
|
|
.at(0)
|
|
|
|
.simulate('change', { target: { value: 'a' } });
|
2018-10-12 23:47:42 +08:00
|
|
|
|
|
|
|
expect(errorSpy.mock.calls[0][0]).toMatch(
|
2018-12-07 16:17:45 +08:00
|
|
|
'Warning: `onSearchChange` in Transfer is deprecated. Please use `onSearch` instead.',
|
2018-10-12 23:47:42 +08:00
|
|
|
);
|
|
|
|
expect(onSearchChange.mock.calls[0][0]).toEqual('left');
|
|
|
|
expect(onSearchChange.mock.calls[0][1].target.value).toEqual('a');
|
|
|
|
});
|
2016-12-14 14:48:09 +08:00
|
|
|
});
|