ant-design/components/input/__tests__/Search.test.tsx
Dunqing 6759887c44
chore: migrate to vitest (#42506)
* chore: migrate to vitest

* chore: update ci

* fix: test correctly

* test: support puppeteer

* chore: update coverage

* chore: update include/exclude

* chore: update config

* test: update incorrect tests

* chore: update script

* chore: update

* fix: should close browser at the ended

* chore: improve

* fix: test cause tsc error

* fix: eslint error

* chore: exclude correctly

* test: update snap and fix some tests

* chore: update test config

* fix: countup.js

* fix: incorrect test

* chore: update reference

* test: update

* fix: countup.js

* fix: timeout

* chore: update site test

* fix: fixed countup version

* chore: remove unsed code

* test: update

* test: update demo timeout

* test: update timeout

* chore: update image test

* chore: update threads

* fix: image/svg+xml test failed

* chore: limits threads

* test: update test coverage include

* chore: remove jest files

* chore: rename jest to vi

* chore: update document

* chore: fix missing @types/jsdom

* chore: update coverage

* chore: update snap

* fix:watermark test cases are incorrect

* feat: update ignore comment

* test: fix test case

* test: reset body scrollTop

* test: clean up

* test: use vi

* test: update snapshot

* test: update snapshot

* test: fix dropdown test failed

* fix: toHaveStyle cause test fail

* test: improve test case

* test: fix

* fix: color failed, refer to https://github.com/jsdom/jsdom/pull/3560

* test: fix

* test: fix

* test: fix circular import

* test: revert

* ci: coverage failed

* test: fix c8 ignore comment

* chore: incorrect config

* chore: fix ignore ci

* test: revert svg+xml

* test: fix realTimers

* feat: rc-trigger should be remove

* test: fix some failed test

* chore: remove unused deps and configure eslint-plugin-vitest

* test: update snap

* chore: remove jest

* test: fix lint error

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
Co-authored-by: afc163 <afc163@gmail.com>
2023-06-07 11:54:50 +08:00

220 lines
8.7 KiB
TypeScript

import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import focusTest from '../../../tests/shared/focusTest';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import Button from '../../button';
import type { InputRef } from '../Input';
import Search from '../Search';
describe('Input.Search', () => {
focusTest(Search, { refFocus: true });
mountTest(Search);
rtlTest(Search);
it('should support custom button', () => {
const { asFragment } = render(<Search enterButton={<button type="button">ok</button>} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('should support custom Button', () => {
const { asFragment } = render(<Search enterButton={<Button>ok</Button>} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('should support enterButton null', () => {
expect(() => {
render(<Search enterButton={null} />);
}).not.toThrow();
});
it('should support ReactNode suffix without error', () => {
const { asFragment } = render(<Search suffix={<div>ok</div>} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('should disable enter button when disabled prop is true', () => {
const { container } = render(<Search placeholder="input search text" enterButton disabled />);
expect(container.querySelectorAll('.ant-btn-primary[disabled]')).toHaveLength(1);
});
it('should disable search icon when disabled prop is true', () => {
const onSearch = vi.fn();
const { container } = render(
<Search defaultValue="search text" onSearch={onSearch} disabled />,
);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(0);
});
it('should trigger onSearch when click search icon', () => {
const onSearch = vi.fn();
const { container } = render(<Search defaultValue="search text" onSearch={onSearch} />);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
it('should trigger onSearch when click search button', () => {
const onSearch = vi.fn();
const { container } = render(
<Search defaultValue="search text" enterButton onSearch={onSearch} />,
);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
it('should trigger onSearch when click search button with text', () => {
const onSearch = vi.fn();
const { container } = render(
<Search defaultValue="search text" enterButton="button text" onSearch={onSearch} />,
);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
it('should trigger onSearch when click search button with customize button', () => {
const onSearch = vi.fn();
const { container } = render(
<Search
defaultValue="search text"
enterButton={<Button>antd button</Button>}
onSearch={onSearch}
/>,
);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
it('should trigger onSearch when click search button of native', () => {
const onSearch = vi.fn();
const onButtonClick = vi.fn();
const { container } = render(
<Search
defaultValue="search text"
enterButton={
<button type="button" onClick={onButtonClick}>
antd button
</button>
}
onSearch={onSearch}
/>,
);
fireEvent.click(container.querySelector('button')!);
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
expect(onButtonClick).toHaveBeenCalledTimes(1);
});
it('should trigger onSearch when press enter', () => {
const onSearch = vi.fn();
const { container } = render(<Search defaultValue="search text" onSearch={onSearch} />);
fireEvent.keyDown(container.querySelector('input')!, { key: 'Enter', keyCode: 13 });
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
// https://github.com/ant-design/ant-design/issues/34844
it('should not trigger onSearch when press enter using chinese inputting method', () => {
const onSearch = vi.fn();
const { container } = render(<Search defaultValue="search text" onSearch={onSearch} />);
fireEvent.compositionStart(container.querySelector('input')!);
fireEvent.keyDown(container.querySelector('input')!, { key: 'Enter', keyCode: 13 });
expect(onSearch).not.toHaveBeenCalled();
fireEvent.compositionEnd(container.querySelector('input')!);
fireEvent.keyDown(container.querySelector('input')!, { key: 'Enter', keyCode: 13 });
expect(onSearch).toHaveBeenCalledTimes(1);
expect(onSearch).toHaveBeenCalledWith('search text', expect.anything());
});
// https://github.com/ant-design/ant-design/issues/14785
it('should support addonAfter', () => {
const addonAfter = <span>Addon After</span>;
const { asFragment } = render(<Search addonAfter={addonAfter} />);
const { asFragment: asFragmentWithEnterButton } = render(
<Search enterButton addonAfter={addonAfter} />,
);
expect(asFragment().firstChild).toMatchSnapshot();
expect(asFragmentWithEnterButton().firstChild).toMatchSnapshot();
});
// https://github.com/ant-design/ant-design/issues/18729
it('should trigger onSearch when click clear icon', () => {
const onSearch = vi.fn();
const onChange = vi.fn();
const { container } = render(
<Search allowClear defaultValue="value" onSearch={onSearch} onChange={onChange} />,
);
fireEvent.click(container.querySelector('.ant-input-clear-icon')!);
expect(onSearch).toHaveBeenLastCalledWith('', expect.anything());
expect(onChange).toHaveBeenCalled();
});
it('should support loading', () => {
const { asFragment } = render(<Search loading />);
const { asFragment: asFragmentWithEnterButton } = render(<Search loading enterButton />);
expect(asFragment().firstChild).toMatchSnapshot();
expect(asFragmentWithEnterButton().firstChild).toMatchSnapshot();
});
it('should not trigger onSearch when press enter while loading', () => {
const onSearch = vi.fn();
const { container } = render(<Search loading onSearch={onSearch} />);
fireEvent.keyDown(container.querySelector('input')!, { key: 'Enter', keyCode: 13 });
expect(onSearch).not.toHaveBeenCalled();
});
it('should support addonAfter and suffix for loading', () => {
const { asFragment } = render(<Search loading suffix="suffix" addonAfter="addonAfter" />);
const { asFragment: asFragmentWithEnterButton } = render(
<Search loading enterButton suffix="suffix" addonAfter="addonAfter" />,
);
expect(asFragment().firstChild).toMatchSnapshot();
expect(asFragmentWithEnterButton().firstChild).toMatchSnapshot();
});
it('should support invalid suffix', () => {
const { asFragment } = render(<Search suffix={[]} />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('should support invalid addonAfter', () => {
const { asFragment } = render(<Search addonAfter={[]} enterButton />);
expect(asFragment().firstChild).toMatchSnapshot();
});
it('should prevent search button mousedown event', () => {
const ref = React.createRef<InputRef>();
const { container } = render(<Search ref={ref} enterButton="button text" />, {
container: document.body,
});
ref.current?.focus();
expect(document.activeElement).toBe(container.querySelector('input'));
fireEvent.mouseDown(container.querySelector('button')!);
expect(document.activeElement).toBe(container.querySelector('input'));
});
it('not crash when use function ref', () => {
const ref = vi.fn();
const { container } = render(<Search ref={ref} enterButton />);
expect(() => {
fireEvent.mouseDown(container.querySelector('button')!);
}).not.toThrow();
});
// https://github.com/ant-design/ant-design/issues/27258
it('Search with allowClear should have one className only', () => {
const { container } = render(<Search allowClear className="className" />);
expect(
container.querySelector('.ant-input-group-wrapper')?.classList.contains('className'),
).toBe(true);
expect(
container.querySelector('.ant-input-affix-wrapper')?.classList.contains('className'),
).toBe(false);
});
});