import userEvent from '@testing-library/user-event';
import React from 'react';
import AutoComplete from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { render, screen } from '../../../tests/utils';
import { resetWarned } from '../../_util/warning';
import Input from '../../input';
describe('AutoComplete', () => {
mountTest(AutoComplete);
rtlTest(AutoComplete);
it('AutoComplete with custom Input render perfectly', async () => {
render(
,
);
expect(screen.getByRole('combobox')).toBeInTheDocument();
// should show options when type in input
await userEvent.type(screen.getByRole('combobox'), '123');
// should not filter data source by default
expect(screen.getByTitle('12345')).toBeInTheDocument();
expect(screen.getByTitle('23456')).toBeInTheDocument();
expect(screen.getByTitle('34567')).toBeInTheDocument();
});
it('AutoComplete should work when dataSource is object array', async () => {
render(
,
);
expect(screen.getByRole('combobox')).toBeInTheDocument();
await userEvent.type(screen.getByRole('combobox'), 'a');
// should not filter data source by default
expect(screen.getByTitle('text')).toBeInTheDocument();
expect(screen.getByTitle('abc')).toBeInTheDocument();
});
it('AutoComplete throws error when contains invalid dataSource', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(
// @ts-ignore
{}]}>
,
);
expect(spy).toHaveBeenCalled();
});
it('legacy dataSource should accept react element option', () => {
render(ReactNode]} />);
expect(screen.getByRole('combobox')).toBeInTheDocument();
expect(screen.getByTitle(/reactnode/i)).toBeInTheDocument();
});
it('legacy AutoComplete.Option should be compatible', async () => {
render(
111
222
,
);
expect(screen.getByRole('combobox')).toBeInTheDocument();
await userEvent.type(screen.getByRole('combobox'), '1');
expect(screen.getByTitle(/111/i)).toBeInTheDocument();
expect(screen.getByTitle(/222/i)).toBeInTheDocument();
});
it('should not warning when getInputElement is null', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
render();
expect(warnSpy).not.toHaveBeenCalled();
warnSpy.mockRestore();
});
it('should not override custom input className', () => {
render(
,
);
expect(screen.getByRole('combobox')).toHaveClass('custom');
});
it('deprecated dropdownClassName', () => {
resetWarned();
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const { container } = render(
,
);
expect(errSpy).toHaveBeenCalledWith(
'Warning: [antd: AutoComplete] `dropdownClassName` is deprecated. Please use `popupClassName` instead.',
);
expect(container.querySelector('.legacy')).toBeTruthy();
errSpy.mockRestore();
});
});