2017-01-13 21:19:23 +08:00
|
|
|
import React from 'react';
|
2022-08-08 22:42:57 +08:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2017-01-13 21:19:23 +08:00
|
|
|
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-08-08 22:42:57 +08:00
|
|
|
import { render, screen } from '../../../tests/utils';
|
2022-06-10 20:35:28 +08:00
|
|
|
import Input from '../../input';
|
2022-09-08 14:33:11 +08:00
|
|
|
import { resetWarned } from '../../_util/warning';
|
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
|
|
|
|
2022-08-08 22:42:57 +08:00
|
|
|
it('AutoComplete with custom Input render perfectly', async () => {
|
|
|
|
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-08-08 22:42:57 +08:00
|
|
|
expect(screen.getByRole('combobox')).toBeInTheDocument();
|
|
|
|
|
|
|
|
// should show options when type in input
|
|
|
|
await userEvent.type(screen.getByRole('combobox'), '123');
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2022-08-08 22:42:57 +08:00
|
|
|
// should not filter data source by default
|
|
|
|
expect(screen.getByTitle('12345')).toBeInTheDocument();
|
|
|
|
expect(screen.getByTitle('23456')).toBeInTheDocument();
|
|
|
|
expect(screen.getByTitle('34567')).toBeInTheDocument();
|
2017-01-13 21:19:23 +08:00
|
|
|
});
|
2019-08-09 15:25:10 +08:00
|
|
|
|
2022-08-08 22:42:57 +08:00
|
|
|
it('AutoComplete should work when dataSource is object array', async () => {
|
|
|
|
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-08-08 22:42:57 +08:00
|
|
|
expect(screen.getByRole('combobox')).toBeInTheDocument();
|
|
|
|
await userEvent.type(screen.getByRole('combobox'), 'a');
|
2019-08-09 15:25:10 +08:00
|
|
|
|
2022-08-08 22:42:57 +08:00
|
|
|
// should not filter data source by default
|
|
|
|
expect(screen.getByTitle('text')).toBeInTheDocument();
|
|
|
|
expect(screen.getByTitle('abc')).toBeInTheDocument();
|
2019-08-09 15:25:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('AutoComplete throws error when contains invalid dataSource', () => {
|
2022-11-28 14:19:25 +08:00
|
|
|
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
2022-05-10 15:43:29 +08:00
|
|
|
|
2022-06-08 09:57:09 +08:00
|
|
|
render(
|
2022-08-08 22:42:57 +08:00
|
|
|
// @ts-ignore
|
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-08-08 22:42:57 +08:00
|
|
|
render(<AutoComplete open dataSource={[<span key="key">ReactNode</span>]} />);
|
|
|
|
|
|
|
|
expect(screen.getByRole('combobox')).toBeInTheDocument();
|
|
|
|
expect(screen.getByTitle(/reactnode/i)).toBeInTheDocument();
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
|
|
|
|
2022-08-08 22:42:57 +08:00
|
|
|
it('legacy AutoComplete.Option should be compatiable', async () => {
|
|
|
|
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-08-08 22:42:57 +08:00
|
|
|
expect(screen.getByRole('combobox')).toBeInTheDocument();
|
|
|
|
await userEvent.type(screen.getByRole('combobox'), '1');
|
|
|
|
expect(screen.getByTitle(/111/i)).toBeInTheDocument();
|
|
|
|
expect(screen.getByTitle(/222/i)).toBeInTheDocument();
|
2020-05-09 19:32:02 +08:00
|
|
|
});
|
2020-09-16 11:51:55 +08:00
|
|
|
|
|
|
|
it('should not warning when getInputElement is null', () => {
|
2022-11-28 14:19:25 +08:00
|
|
|
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
2022-06-08 09:57:09 +08:00
|
|
|
render(<AutoComplete placeholder="input here" allowClear />);
|
2022-11-28 14:19:25 +08:00
|
|
|
expect(warnSpy).not.toHaveBeenCalled();
|
|
|
|
warnSpy.mockRestore();
|
2020-09-16 11:51:55 +08:00
|
|
|
});
|
2021-03-11 23:11:51 +08:00
|
|
|
|
|
|
|
it('should not override custom input className', () => {
|
2022-08-08 22:42:57 +08:00
|
|
|
render(
|
2021-03-11 23:11:51 +08:00
|
|
|
<AutoComplete>
|
|
|
|
<Input className="custom" />
|
|
|
|
</AutoComplete>,
|
|
|
|
);
|
2022-08-08 22:42:57 +08:00
|
|
|
expect(screen.getByRole('combobox')).toHaveClass('custom');
|
2021-03-11 23:11:51 +08:00
|
|
|
});
|
2022-09-08 14:33:11 +08:00
|
|
|
|
|
|
|
it('deprecated dropdownClassName', () => {
|
|
|
|
resetWarned();
|
|
|
|
|
|
|
|
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
const { container } = render(
|
|
|
|
<AutoComplete
|
|
|
|
dropdownClassName="legacy"
|
|
|
|
open
|
|
|
|
options={[{ label: 'little', value: 'little' }]}
|
|
|
|
searchValue="l"
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(errSpy).toHaveBeenCalledWith(
|
|
|
|
'Warning: [antd: AutoComplete] `dropdownClassName` is deprecated, please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
expect(container.querySelector('.legacy')).toBeTruthy();
|
|
|
|
|
|
|
|
errSpy.mockRestore();
|
|
|
|
});
|
2017-01-13 21:19:23 +08:00
|
|
|
});
|