0

我正在尝试对以下内容进行单元测试:

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()
 }); 
4

1 回答 1

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');
};
于 2019-01-29T02:58:32.000 回答