ant-design/components/auto-complete/__tests__/index.test.tsx
dependabot[bot] 7e7c47509f
chore(deps-dev): bump eslint-plugin-jest from 26.9.0 to 27.0.1 (#37291)
* chore(deps-dev): bump eslint-plugin-jest from 26.9.0 to 27.0.1

Bumps [eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) from 26.9.0 to 27.0.1.
- [Release notes](https://github.com/jest-community/eslint-plugin-jest/releases)
- [Changelog](https://github.com/jest-community/eslint-plugin-jest/blob/main/CHANGELOG.md)
- [Commits](https://github.com/jest-community/eslint-plugin-jest/compare/v26.9.0...v27.0.1)

---
updated-dependencies:
- dependency-name: eslint-plugin-jest
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: fix eslint errors in test files

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: afc163 <afc163@gmail.com>
2022-08-30 10:57:13 +08:00

102 lines
3.3 KiB
TypeScript

import React from 'react';
import userEvent from '@testing-library/user-event';
import AutoComplete from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { render, screen } from '../../../tests/utils';
import Input from '../../input';
describe('AutoComplete', () => {
mountTest(AutoComplete);
rtlTest(AutoComplete);
it('AutoComplete with custom Input render perfectly', async () => {
render(
<AutoComplete dataSource={['12345', '23456', '34567']}>
<textarea />
</AutoComplete>,
);
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(
<AutoComplete
dataSource={[
{ text: 'text', value: 'value' },
{ text: 'abc', value: 'xxx' },
]}
>
<input />
</AutoComplete>,
);
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(() => undefined);
render(
// @ts-ignore
<AutoComplete dataSource={[() => {}]}>
<textarea />
</AutoComplete>,
);
expect(spy).toHaveBeenCalled();
});
it('legacy dataSource should accept react element option', () => {
render(<AutoComplete open dataSource={[<span key="key">ReactNode</span>]} />);
expect(screen.getByRole('combobox')).toBeInTheDocument();
expect(screen.getByTitle(/reactnode/i)).toBeInTheDocument();
});
it('legacy AutoComplete.Option should be compatiable', async () => {
render(
<AutoComplete>
<AutoComplete.Option value="111">111</AutoComplete.Option>
<AutoComplete.Option value="222">222</AutoComplete.Option>
</AutoComplete>,
);
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', () => {
jest.spyOn(console, 'warn').mockImplementation(() => undefined);
render(<AutoComplete placeholder="input here" allowClear />);
// eslint-disable-next-line no-console
expect(console.warn).not.toHaveBeenCalled();
// @ts-ignore
// eslint-disable-next-line no-console
console.warn.mockRestore();
});
it('should not override custom input className', () => {
render(
<AutoComplete>
<Input className="custom" />
</AutoComplete>,
);
expect(screen.getByRole('combobox')).toHaveClass('custom');
});
});