2017-01-13 21:19:23 +08:00
|
|
|
import React from 'react';
|
|
|
|
import AutoComplete from '..';
|
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';
|
2022-06-10 20:35:28 +08:00
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
|
|
import Input from '../../input';
|
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', () => {
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container } = render(
|
2017-01-13 21:19:23 +08:00
|
|
|
<AutoComplete dataSource={['12345', '23456', '34567']}>
|
|
|
|
<textarea />
|
2018-12-07 16:17:45 +08:00
|
|
|
</AutoComplete>,
|
2017-01-13 21:19:23 +08:00
|
|
|
);
|
|
|
|
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelectorAll('textarea').length).toBe(1);
|
|
|
|
fireEvent.change(container.querySelector('textarea'), { target: { value: '123' } });
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2017-05-03 15:33:40 +08:00
|
|
|
// should not filter data source defaultly
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelectorAll('.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', () => {
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container } = render(
|
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>,
|
|
|
|
);
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelectorAll('input').length).toBe(1);
|
|
|
|
fireEvent.change(container.querySelector('input'), { target: { value: 'a' } });
|
2019-08-09 15:25:10 +08:00
|
|
|
|
|
|
|
// should not filter data source defaultly
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelectorAll('.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);
|
|
|
|
|
2022-06-08 09:57:09 +08:00
|
|
|
render(
|
2022-05-10 15:43:29 +08:00
|
|
|
<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', () => {
|
2022-06-08 09:57:09 +08:00
|
|
|
const { asFragment } = render(
|
|
|
|
<AutoComplete open dataSource={[<span key="key">ReactNode</span>]} />,
|
|
|
|
);
|
|
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('legacy AutoComplete.Option should be compatiable', () => {
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container } = render(
|
2020-05-09 19:32:02 +08:00
|
|
|
<AutoComplete>
|
|
|
|
<AutoComplete.Option value="111">111</AutoComplete.Option>
|
|
|
|
<AutoComplete.Option value="222">222</AutoComplete.Option>
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelectorAll('input').length).toBe(1);
|
|
|
|
fireEvent.change(container.querySelector('input'), { target: { value: '1' } });
|
|
|
|
expect(container.querySelectorAll('.ant-select-item-option').length).toBe(2);
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
2020-09-16 11:51:55 +08:00
|
|
|
|
|
|
|
it('should not warning when getInputElement is null', () => {
|
|
|
|
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
|
2022-06-08 09:57:09 +08:00
|
|
|
render(<AutoComplete placeholder="input here" allowClear />);
|
2020-09-16 11:51:55 +08:00
|
|
|
// 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', () => {
|
2022-06-08 09:57:09 +08:00
|
|
|
const { container } = render(
|
2021-03-11 23:11:51 +08:00
|
|
|
<AutoComplete>
|
|
|
|
<Input className="custom" />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
2022-06-08 09:57:09 +08:00
|
|
|
expect(container.querySelector('input').classList.contains('custom')).toBeTruthy();
|
2021-03-11 23:11:51 +08:00
|
|
|
});
|
2017-01-13 21:19:23 +08:00
|
|
|
});
|