2017-01-13 21:19:23 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import AutoComplete from '..';
|
2021-03-11 23:11:51 +08:00
|
|
|
import Input from '../../input';
|
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';
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2020-05-09 19:32:02 +08:00
|
|
|
describe('AutoComplete', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(AutoComplete);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(AutoComplete);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2017-01-13 21:19:23 +08:00
|
|
|
it('AutoComplete with custom Input render perfectly', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<AutoComplete dataSource={['12345', '23456', '34567']}>
|
|
|
|
<textarea />
|
2018-12-07 16:17:45 +08:00
|
|
|
</AutoComplete>,
|
2017-01-13 21:19:23 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(wrapper.find('textarea').length).toBe(1);
|
|
|
|
wrapper.find('textarea').simulate('change', { target: { value: '123' } });
|
|
|
|
|
2017-05-03 15:33:40 +08:00
|
|
|
// should not filter data source defaultly
|
2019-09-12 20:15:17 +08:00
|
|
|
expect(wrapper.find('.ant-select-item-option').length).toBe(3);
|
2017-01-13 21:19:23 +08:00
|
|
|
});
|
2019-08-09 15:25:10 +08:00
|
|
|
|
|
|
|
it('AutoComplete should work when dataSource is object array', () => {
|
|
|
|
const wrapper = mount(
|
2020-01-06 11:13:39 +08:00
|
|
|
<AutoComplete
|
|
|
|
dataSource={[
|
|
|
|
{ text: 'text', value: 'value' },
|
|
|
|
{ text: 'abc', value: 'xxx' },
|
|
|
|
]}
|
|
|
|
>
|
2019-08-09 15:25:10 +08:00
|
|
|
<input />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('input').length).toBe(1);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: 'a' } });
|
|
|
|
|
|
|
|
// should not filter data source defaultly
|
2019-09-12 20:15:17 +08:00
|
|
|
expect(wrapper.find('.ant-select-item-option').length).toBe(2);
|
2019-08-09 15:25:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('AutoComplete throws error when contains invalid dataSource', () => {
|
2022-05-10 15:43:29 +08:00
|
|
|
const spy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
|
|
|
|
|
|
mount(
|
|
|
|
<AutoComplete dataSource={[() => {}]}>
|
|
|
|
<textarea />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
2019-08-09 15:25:10 +08:00
|
|
|
});
|
2020-05-09 19:32:02 +08:00
|
|
|
|
|
|
|
it('legacy dataSource should accept react element option', () => {
|
|
|
|
const wrapper = mount(<AutoComplete open dataSource={[<span key="key">ReactNode</span>]} />);
|
2021-12-04 17:06:36 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('legacy AutoComplete.Option should be compatiable', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<AutoComplete>
|
|
|
|
<AutoComplete.Option value="111">111</AutoComplete.Option>
|
|
|
|
<AutoComplete.Option value="222">222</AutoComplete.Option>
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('input').length).toBe(1);
|
|
|
|
wrapper.find('input').simulate('change', { target: { value: '1' } });
|
|
|
|
expect(wrapper.find('.ant-select-item-option').length).toBe(2);
|
|
|
|
});
|
2020-09-16 11:51:55 +08:00
|
|
|
|
|
|
|
it('should not warning when getInputElement is null', () => {
|
|
|
|
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
|
|
|
|
mount(<AutoComplete placeholder="input here" allowClear />);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
expect(console.warn).not.toBeCalled();
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn.mockRestore();
|
|
|
|
});
|
2021-03-11 23:11:51 +08:00
|
|
|
|
|
|
|
it('should not override custom input className', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<AutoComplete>
|
|
|
|
<Input className="custom" />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
|
|
|
expect(wrapper.find('input').hasClass('custom')).toBe(true);
|
|
|
|
});
|
2017-01-13 21:19:23 +08:00
|
|
|
});
|