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';
|
|
|
|
|
|
|
|
describe('Input.Search', () => {
|
|
|
|
focusTest(Search);
|
2018-03-12 20:51:04 +08:00
|
|
|
|
|
|
|
it('should support custom button', () => {
|
|
|
|
const wrapper = mount(
|
2018-06-22 21:05:13 +08:00
|
|
|
<Search enterButton={<button type="button">ok</button>} />
|
2018-03-12 20:51:04 +08:00
|
|
|
);
|
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', () => {
|
|
|
|
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 = () => {
|
|
|
|
mount(
|
|
|
|
<Search suffix={<div>ok</div>} />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
expect(fn).not.toThrow();
|
|
|
|
});
|
|
|
|
|
2018-04-15 13:14:26 +08:00
|
|
|
it('should disable enter button when disabled prop is true', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Search placeholder="input search text" enterButton disabled />
|
|
|
|
);
|
|
|
|
expect(wrapper.find('.ant-btn-primary[disabled]')).toHaveLength(1);
|
|
|
|
});
|
2018-05-26 16:52:33 +08:00
|
|
|
|
|
|
|
it('should trigger onSearch when click search icon', () => {
|
|
|
|
const onSearch = jest.fn();
|
|
|
|
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);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('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();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Search defaultValue="search text" enterButton onSearch={onSearch} />
|
|
|
|
);
|
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('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(
|
|
|
|
<Search defaultValue="search text" enterButton="button text" onSearch={onSearch} />
|
|
|
|
);
|
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('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(
|
|
|
|
<Search defaultValue="search text" enterButton={<Button>antd button</Button>} onSearch={onSearch} />
|
|
|
|
);
|
|
|
|
wrapper.find('Button').simulate('click');
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('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-06-22 21:05:13 +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);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('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();
|
|
|
|
const wrapper = mount(
|
|
|
|
<Search defaultValue="search text" onSearch={onSearch} />
|
|
|
|
);
|
|
|
|
wrapper.find('input').simulate('keydown', { key: 'Enter', keyCode: 13 });
|
|
|
|
expect(onSearch).toHaveBeenCalledTimes(1);
|
2018-06-25 20:35:05 +08:00
|
|
|
expect(onSearch).toBeCalledWith('search text', expect.objectContaining({
|
|
|
|
type: 'keydown',
|
|
|
|
preventDefault: expect.any(Function),
|
|
|
|
}));
|
2018-05-26 17:03:07 +08:00
|
|
|
});
|
2017-12-01 19:39:32 +08:00
|
|
|
});
|