模拟-旨在在1个节点上运行。找到0个代替

时间:2019-03-08 14:58:02

标签: javascript reactjs onchange

无法弄清为什么未调用onChange。我添加了一个ID,但找不到该元素。

File.test.js

it("should call onChange event  on renderToDropdown ", () => {
    baseProps.onChange.mockClear();
    wrapper.setState({
    toStatusList:[{
        StatusID:"",
        Name:"",
    }],
   selectedTo:'password test 1'
    });
   wrapper.update() 
   wrapper.find('select[id="testonchange"]').simulate('change',{target: {value: 'password test 1'}} )
 });

还尝试了以下操作:

wrapper.find('#testonchange').simulate('change',{target: {value: 'password test 1'}} )

File.js

renderToDropdown = () => {
let list = this.state.toStatusList;
let result = list.map((item) => {
  return <option value={item.StatusID}>{item.Name}</option>
})
result.push(<option value='default' disabled>Select...</option>)
return (
  <select value={this.state.selectedTo} id='testonchange' onChange={(e) => {this.handleToChange(e);}}>
    {result}
  </select>
)
}

handleToChange = (e) => {
this.setState({selectedTo: e.target.value});

将Jest和Enzyme用于React JS

1 个答案:

答案 0 :(得分:0)

这里的包装是什么?空实例?浅渲染或挂载?