使用Jest /酶测试this.setState以反应JS-VSC

时间:2019-01-31 16:59:02

标签: reactjs unit-testing jestjs enzyme

我正在尝试运行测试,但是我不确定如何使用正确的属性调用设置状态。我已经设置了道具,但没有确定它的正确路径。任何帮助或提示将非常有帮助。谢谢 这是我目前要测试的内容:

const baseProps = {
 defaultData:{
 url:{},
 method:{},
 name:{},
},
addMetaInformation,
}

//描述并在声明每个变量之前

 it ('componentWillReceiveProps',() => {
  const url = baseProps
  wrapper.instance().setState({
     nextProps: {
     url: 'null',
     method :  'test',
     name : 'test name',
  },
})
 wrapper.instance().componentWillReceiveProps(url)
//expect(wrapper.setState()).toContain(url); // not sure about this one
expect(wrapper.instance().setState).toHaveProperty() // mistake
expect(wrapper).toMatchSnapshot();
});

这是我要测试的代码:

      constructor(props) {
super(props)
this.state = {
  url: null
  }
}
    componentWillReceiveProps(nextProps) {
if (nextProps.url) {
  this.setState({
    url: nextProps.url
  })
}
if (nextProps.defaultData) {
  if (nextProps.defaultData !== this.props.defaultData) {
    this.setState({
      url: nextProps.defaultData.url,
      method: nextProps.defaultData.method,
      name: nextProps.defaultData.name
    })
   }
  }
}

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

}

0 个答案:

没有答案