2018-03-12 20:51:04 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
2017-12-01 19:39:32 +08:00
|
|
|
import Search from '../Search';
|
2018-03-12 21:02:20 +08:00
|
|
|
import Button from '../../button';
|
2017-12-01 19:39:32 +08:00
|
|
|
import focusTest from '../../../tests/shared/focusTest';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2017-12-01 19:39:32 +08:00
|
|
|
|
|
|
|
describe('Input.Search', () => {
|
|
|
|
focusTest(Search);
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Search);
|
2018-03-12 20:51:04 +08:00
|
|
|
|
|
|
|
it('should support custom button', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search enterButton={<button type="button">ok</button>} />);
|
2018-03-12 21:02:20 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-03-12 20:51:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support custom Button', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search enterButton={<Button>ok</Button>} />);
|
2018-03-12 21:02:20 +08:00
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
2018-03-12 20:51:04 +08:00
|
|
|
});
|
2018-04-15 13:14:26 +08:00
|
|
|
|
2018-11-16 20:05:04 +08:00
|
|
|
it('should support ReactNode suffix without error', () => {
|
|
|
|
const fn = () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
mount(<Search suffix={<div>ok</div>} />);
|
2018-11-16 20:05:04 +08:00
|
|
|
};
|
|
|
|
expect(fn).not.toThrow();
|
|
|
|
});
|
|
|
|
|
2018-04-15 13:14:26 +08:00
|
|
|
it('should disable enter button when disabled prop is true', () => {
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search placeholder="input search text" enterButton disabled />);
|
2018-04-15 13:14:26 +08:00
|
|
|
expect(wrapper.find('.ant-btn-primary[disabled]')).toHaveLength(1);
|
|
|
|
});
|
2018-05-26 16:52:33 +08:00
|
|
|
|
2019-09-19 17:19:22 +08:00
|
|
|
it('should disable search icon when disabled prop is true', () => {
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} disabled />);
|
|
|
|
wrapper.find('.anticon-search').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
2018-05-26 16:52:33 +08:00
|
|
|
it('should trigger onSearch when click search icon', () => {
|
|
|
|
const onSearch = jest.fn();
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} />);
|
2018-08-14 11:45:30 +08:00
|
|
|
wrapper.find('.anticon-search').simulate('click');
|
2018-05-26 16:52:33 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'click',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 16:52:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onSearch when click search button', () => {
|
|
|
|
const onSearch = jest.fn();
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search defaultValue="search text" enterButton onSearch={onSearch} />);
|
2018-05-26 16:52:33 +08:00
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'click',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 16:52:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onSearch when click search button with text', () => {
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Search defaultValue="search text" enterButton="button text" onSearch={onSearch} />,
|
2018-05-26 16:52:33 +08:00
|
|
|
);
|
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'click',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 16:52:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onSearch when click search button with customize button', () => {
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Search
|
|
|
|
defaultValue="search text"
|
|
|
|
enterButton={<Button>antd button</Button>}
|
|
|
|
onSearch={onSearch}
|
|
|
|
/>,
|
2018-05-26 16:52:33 +08:00
|
|
|
);
|
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'click',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 16:52:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onSearch when click search button of native', () => {
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
const wrapper = mount(
|
2018-12-07 16:17:45 +08:00
|
|
|
<Search
|
|
|
|
defaultValue="search text"
|
|
|
|
enterButton={<button type="button">antd button</button>}
|
|
|
|
onSearch={onSearch}
|
|
|
|
/>,
|
2018-05-26 16:52:33 +08:00
|
|
|
);
|
|
|
|
wrapper.find('button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'click',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 16:52:33 +08:00
|
|
|
});
|
2018-05-26 17:03:07 +08:00
|
|
|
|
|
|
|
it('should trigger onSearch when press enter', () => {
|
|
|
|
const onSearch = jest.fn();
|
2018-12-07 16:17:45 +08:00
|
|
|
const wrapper = mount(<Search defaultValue="search text" onSearch={onSearch} />);
|
2018-05-26 17:03:07 +08:00
|
|
|
wrapper.find('input').simulate('keydown', { key: 'Enter', keyCode: 13 });
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onSearch).toHaveBeenCalledWith(
|
2018-12-07 16:17:45 +08:00
|
|
|
'search text',
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'keydown',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}),
|
|
|
|
);
|
2018-05-26 17:03:07 +08:00
|
|
|
});
|
2019-02-12 21:39:36 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/14785
|
|
|
|
it('should support addonAfter', () => {
|
|
|
|
const addonAfter = <span>Addon After</span>;
|
|
|
|
const wrapper = mount(<Search addonAfter={addonAfter} />);
|
|
|
|
const wrapperWithEnterButton = mount(<Search enterButton addonAfter={addonAfter} />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
expect(wrapperWithEnterButton.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-09-12 15:47:02 +08:00
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/18729
|
|
|
|
it('should trigger onSearch when click clear icon', () => {
|
|
|
|
const onSearch = jest.fn();
|
2019-09-13 13:04:43 +08:00
|
|
|
const onChange = jest.fn();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Search allowClear defaultValue="value" onSearch={onSearch} onChange={onChange} />,
|
|
|
|
);
|
2019-09-12 15:47:02 +08:00
|
|
|
wrapper
|
|
|
|
.find('.ant-input-clear-icon')
|
|
|
|
.at(0)
|
|
|
|
.simulate('click');
|
2019-09-13 13:04:43 +08:00
|
|
|
expect(onSearch).toHaveBeenLastCalledWith('', expect.anything());
|
|
|
|
expect(onChange).toHaveBeenCalled();
|
2019-09-12 15:47:02 +08:00
|
|
|
});
|
2019-09-14 15:40:33 +08:00
|
|
|
|
2019-09-12 22:32:29 +08:00
|
|
|
it('should support loading', () => {
|
|
|
|
const wrapper = mount(<Search loading />);
|
|
|
|
const wrapperWithEnterButton = mount(<Search loading enterButton />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
expect(wrapperWithEnterButton.render()).toMatchSnapshot();
|
|
|
|
});
|
2019-10-22 21:33:19 +08:00
|
|
|
|
|
|
|
it('should support addonAfter and suffix for loading', () => {
|
|
|
|
const wrapper = mount(<Search loading suffix="suffix" addonAfter="addonAfter" />);
|
|
|
|
const wrapperWithEnterButton = mount(<Search loading enterButton suffix="suffix" addonAfter="addonAfter" />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
expect(wrapperWithEnterButton.render()).toMatchSnapshot();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should support invalid addonAfter and suffix', () => {
|
|
|
|
const wrapper = mount(<Search suffix={[]} addonAfter={[]} />);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
})
|
2017-12-01 19:39:32 +08:00
|
|
|
});
|