如何测试以下内容:使用React JS-开玩笑/酶

时间:2019-01-29 02:52:29

标签: javascript reactjs unit-testing

我正在尝试对以下各项进行单元测试:

handleChange = (e) => {
let localState = Object.assign({}, this.state)
localState[e.target.name] = e.target.value
this.setState(localState)
this.props.addMetaInformation(localState)
}
 }

我对大多数文件进行了单元测试,但不确定如何为上述代码运行。我如何基于下面的代码测试上面的方法或函数 谢谢

   describe('Component', () => {

   let tree;
   let baseProps; 
 // let this.props = let mockprops
  beforeEach(() => {
  // props : mockprops;
          }
  })
it ('should render without a  props ',() => {
 baseProps = {
 ...baseProps,
 //props: {},

};
   tree = renderer.create(<Component {...baseProps } />)
  let treeJson = tree.toJSON();
  expect(treeJson).toMatchSnapshot();
  tree.unmount()
 }); 

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作。

it ('should return meta-input-correct',() => {
 const wrapper = shallow(<Component {...baseProps } />);
 wrapper.setState({test: 'test'});
 expect(wrapper.instance().getElementId('test')).toEqual('meta-input-correct');
};

it ('should return meta-input-incorrect',() => {
 const wrapper = shallow(<Component {...baseProps } />);
 wrapper.setState({nameString: 'test'});
 expect(wrapper.instance().getElementId('testing')).toEqual('meta-input-incorrect');
};