React-Test-Render调用组件方法

时间:2018-08-23 13:38:50

标签: reactjs react-test-renderer

我具有以下组件:

export const FormikInput: React.SFC<Props> = (props: Props) => {
   const handleChange = (value: string) => {
       // do something
       props.setFieldValue(props.key, value);


    return (
           <TextField
            label={props.label}
            onChangeText={handleChange}
            value={props.values[props.key]}
        />
);

};

我想调用handleChange方法的用户react-test-renderer 有可能吗?

1 个答案:

答案 0 :(得分:0)

classname赋予TextField并像这样模拟您的handleChange

describe('handleChange event success', () => {
it('handleChange invokes without throwing an error', () => {
    const handleChange = jest.fn();
    const wrapper = shallow(<YourComponent
      handleChange ={handleChange}
       />);
    wrapper.find('.myTextField').simulate('change');
    expect(handleChange ).toHaveBeenCalled();
  });
});

您的TS可能需要一些调整,我已经为JS编写了