这是使用React/js的Jest/asen检查方法的正确方法吗? [英] Is this the right way to to check a method using Jest / enzyme for React js

查看:83
本文介绍了这是使用React/js的Jest/asen检查方法的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻我的考试通过了.我只想仔细检查这是否是检查方法的正确方法.如果没有,请进行更正

I have my tests passing at the moment. I just want to double check if this is the correct way to check a method. If not, please make the correction if possible

这是我到目前为止所拥有的:file.test.js

This is what I have so far : file.test.js

 it ('handleChange: should call correctly ',() => {
   const wrapper = shallow(<Component {...baseProps } />);
   expect(wrapper).toMatchSnapshot();
   wrapper.setState({e: 'test'});
   expect(wrapper.instance().handleChange({target: {value : 'id'}}))
   expect(wrapper.instance().handleChange({target: {name : 'key'}}))
});

it('deleteAxis : should call correctly',() => {
  const wrapper = shallow(<Component {...baseProps } />);
  expect(wrapper).toMatchSnapshot();
  wrapper.setState({});
  expect(wrapper.instance().deleteAxis({ id:{} }))
})

这是主文件的一部分. File.js

This is part of the main file. File.js

handleChange = (e) => {
  let localState = {}
  let key = e.target.name
  let value = e.target.value
  localState[key] = value
  localState['id'] = this.props.id
  this.props.addNewAxis(localState)
  this.setState(localState)
}

deleteAxis = () => {
  this.props.deleteAxisByID(this.props.id)
}

我希望两种方法都可以通过测试并通过正确的方法.我目前都在工作,但不确定是否正确. 谢谢

I expect both methods to be tested and pass the right way. I have both working at the moment but unsure if its correct. Thanks

推荐答案

这是编写单元测试文件时通常要做的事情

this is what I usually do when I am writing an unit test file

Describe(' FILE NAME', () => {

  let tree;
  let baseProps;
  let mockdefaultDataFromViewXML =  jest.fn().mockReturnValue({});
  let mockaddAxisList = jest.fn().mockReturnValue({});

beforeEach(() => {

baseProps = { 

 defaultDataFromViewXML : mockdefaultDataFromViewXML,
 addAxisList : mockaddAxisList,

这篇关于这是使用React/js的Jest/asen检查方法的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆