2019-01-12 03:33:27 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import { asyncExpect } from '@/tests/utils';
|
|
|
|
import Search from '../search';
|
|
|
|
import Transfer from '../index';
|
2018-05-23 08:07:02 +00:00
|
|
|
|
|
|
|
describe('Search', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
afterEach(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
errorSpy.mockReset();
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
afterAll(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
errorSpy.mockRestore();
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
2018-05-23 08:07:02 +00:00
|
|
|
it('should show cross icon when input value exists', () => {
|
2018-05-23 08:48:45 +00:00
|
|
|
const props = {
|
|
|
|
propsData: {
|
|
|
|
value: '',
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
const wrapper = mount(Search, props);
|
2018-05-23 08:07:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
2018-05-23 08:07:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper.setProps({ value: 'a' });
|
2018-05-23 08:07:02 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(wrapper.html()).toMatchSnapshot();
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
it('onSearch', async () => {
|
|
|
|
const dataSource = [
|
|
|
|
{
|
|
|
|
key: 'a',
|
|
|
|
title: 'a',
|
|
|
|
description: 'a',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'b',
|
|
|
|
title: 'b',
|
|
|
|
description: 'b',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'c',
|
|
|
|
title: 'c',
|
|
|
|
description: 'c',
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Transfer
|
|
|
|
dataSource={dataSource}
|
|
|
|
selectedKeys={[]}
|
|
|
|
targetKeys={[]}
|
|
|
|
render={item => item.title}
|
|
|
|
onSearch={onSearch}
|
|
|
|
showSearch
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sync: false,
|
2019-01-07 12:58:18 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2019-01-07 12:58:18 +00:00
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const input = wrapper.findAll('.ant-input').at(0);
|
|
|
|
input.element.value = 'a';
|
|
|
|
input.trigger('input');
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
await asyncExpect(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
expect(onSearch).toBeCalledWith('left', 'a');
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
onSearch.mockReset();
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
wrapper
|
|
|
|
.findAll('.ant-transfer-list-search-action')
|
|
|
|
.at(0)
|
2019-01-12 03:33:27 +00:00
|
|
|
.trigger('click');
|
|
|
|
expect(onSearch).toBeCalledWith('left', '');
|
|
|
|
});
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
it('legacy onSearchChange', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const onSearchChange = jest.fn();
|
2019-01-07 12:58:18 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const wrapper = mount(
|
|
|
|
{
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Transfer render={item => item.title} onSearchChange={onSearchChange} showSearch />
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sync: false,
|
2019-01-07 12:58:18 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2019-01-07 12:58:18 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const input = wrapper.findAll('.ant-input').at(0);
|
|
|
|
input.element.value = 'a';
|
|
|
|
input.trigger('input');
|
2019-01-07 12:58:18 +00:00
|
|
|
|
|
|
|
expect(errorSpy.mock.calls[0][0]).toMatch(
|
2020-03-07 11:45:13 +00:00
|
|
|
'Warning: [antdv: Transfer] `searchChange` in Transfer is deprecated. Please use `search` instead.',
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
|
|
|
expect(onSearchChange.mock.calls[0][0]).toEqual('left');
|
|
|
|
expect(onSearchChange.mock.calls[0][1].target.value).toEqual('a');
|
|
|
|
});
|
|
|
|
});
|