Add test case for AutoComplete

This commit is contained in:
afc163 2019-03-26 14:47:50 +08:00
parent f4a92d0997
commit 94db815765
No known key found for this signature in database
GPG Key ID: 738F973FCE5C6B48
3 changed files with 53 additions and 3 deletions

View File

@ -13,9 +13,11 @@ export default class InputElement extends React.Component<InputElementProps, any
? this.ele.focus()
: (ReactDOM.findDOMNode(this.ele) as HTMLInputElement).focus();
};
blur = () => {
this.ele.blur ? this.ele.blur() : (ReactDOM.findDOMNode(this.ele) as HTMLInputElement).blur();
};
saveRef = (ele: HTMLInputElement) => {
this.ele = ele;
const { ref: childRef } = this.props.children as any;
@ -23,6 +25,7 @@ export default class InputElement extends React.Component<InputElementProps, any
childRef(ele);
}
};
render() {
return React.cloneElement(
this.props.children,

View File

@ -0,0 +1,50 @@
import React from 'react';
import { mount } from 'enzyme';
import AutoComplete from '..';
import focusTest from '../../../tests/shared/focusTest';
describe('AutoComplete could be focus', () => {
focusTest(AutoComplete);
});
describe('AutoComplete children could be focus', () => {
beforeAll(() => {
jest.useFakeTimers();
});
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterAll(() => {
jest.useRealTimers();
});
afterEach(() => {
document.body.removeChild(container);
});
it('focus() and onFocus', () => {
const handleFocus = jest.fn();
const wrapper = mount(
<AutoComplete onFocus={handleFocus} />
, { attachTo: container });
wrapper.find('input').instance().focus();
jest.runAllTimers();
expect(handleFocus).toBeCalled();
});
it('blur() and onBlur', () => {
const handleBlur = jest.fn();
const wrapper = mount(
<AutoComplete onBlur={handleBlur} />
, { attachTo: container });
wrapper.find('input').instance().focus();
jest.runAllTimers();
wrapper.find('input').instance().blur();
jest.runAllTimers();
expect(handleBlur).toBeCalled();
});
});

View File

@ -1,11 +1,8 @@
import React from 'react';
import { mount } from 'enzyme';
import AutoComplete from '..';
import focusTest from '../../../tests/shared/focusTest';
describe('AutoComplete with Custom Input Element Render', () => {
focusTest(AutoComplete);
it('AutoComplete with custom Input render perfectly', () => {
const wrapper = mount(
<AutoComplete dataSource={['12345', '23456', '34567']}>