Add test case for AutoComplete

pull/18184/head
afc163 6 years ago
parent ca3f4a18ca
commit 94ab1ea2b1

@ -1,31 +1,11 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export interface InputElementProps {
children: React.ReactElement<any>;
}
export default class InputElement extends React.Component<InputElementProps, any> {
private ele: HTMLInputElement;
focus = () => {
if (this.ele.focus) {
this.ele.focus();
} else {
(ReactDOM.findDOMNode(this.ele) as HTMLInputElement).focus();
}
};
blur = () => {
if (this.ele.blur) {
this.ele.blur();
} else {
(ReactDOM.findDOMNode(this.ele) as HTMLInputElement).blur();
}
};
saveRef = (ele: HTMLInputElement) => {
this.ele = ele;
const { ref: childRef } = this.props.children as any;
if (typeof childRef === 'function') {
childRef(ele);

@ -52,4 +52,25 @@ describe('AutoComplete children could be focus', () => {
jest.runAllTimers();
expect(handleBlur).toHaveBeenCalled();
});
it('child.ref should work', () => {
const mockRef = jest.fn();
mount(
<AutoComplete dataSource={[]}>
<input ref={mockRef} />
</AutoComplete>,
);
expect(mockRef).toHaveBeenCalled();
});
it('child.ref instance should support be focused and blured', () => {
let inputRef;
mount(
<AutoComplete dataSource={[]}>
<input ref={node => { inputRef = node; }} />
</AutoComplete>,
);
expect(typeof inputRef.focus).toBe('function');
expect(typeof inputRef.blur).toBe('function');
});
});

@ -22,14 +22,4 @@ describe('AutoComplete with Custom Input Element Render', () => {
// should not filter data source defaultly
expect(dropdownWrapper.find('MenuItem').length).toBe(3);
});
it('child.ref should work', () => {
const mockRef = jest.fn();
mount(
<AutoComplete dataSource={[]}>
<input ref={mockRef} />
</AutoComplete>,
);
expect(mockRef).toHaveBeenCalled();
});
});

Loading…
Cancel
Save