reactjs - onChange 事件没有被调用
问题描述
我正在尝试测试以下 onChange 事件-测试通过,但根据 jest--coverage 未正确调用
使用 JEST - 酶反应 JS
总共有 4 个 onChange 事件,我能够测试前 2 个。
这是组件:前 2 个 onChange 事件正在传递:
renderBottomPanel = (type) => {
if(type !== 'none'){
return(
<div className="bottom-panel">
<SplitPane split="vertical" minSize={50} maxSize={50} defaultSize={50}>
<div>
<a onClick={this.saveChartTemplateToView}>
<FontAwesome title="Save Chart Template" name='savetemplate' className={'fa-floppy-o no-border fa-floppy-o-override-color'}/>
</a>
<a onClick={this.prettyChartTemplate}>
<FontAwesome title="Format JSON" name='formathtml' className={'fa-list-ul no-border'}/>
</a>
</div>
<SplitPane split="vertical" minSize={300} defaultSize={window.screen.availWidth/2 - 75}>
<div className="liquid-content-panel">
<textarea id='chartcontent' name='chartcontent' className='liquid-content-input' type='text' onChange={e => this.setState({viewChartScript:e.target.value})} value={this.state.viewChartScript}/>
</div>
<div className="shaped-JSON-panel">
<textarea id = 'shapedJSON' name='shapedJSON' className='shaped-JSON-input' type='text' onChange={e => this.setState({chartPrototype:e.target.value})} value={JSON.stringify(this.state.chartPrototype, null, 2)} disabled={true}/>
</div>
</SplitPane>
</SplitPane>
</div>
)
}
else{
return(
<div className="bottom-panel">
<SplitPane split="vertical" minSize={50} maxSize={50} defaultSize={50}>
<div>
<a onClick={this.saveTemplateToView}>
<FontAwesome title="Save Template" name='savetemplate' className={'fa-floppy-o no-border fa-floppy-o-override-color'}/>
</a>
<a onClick={this.prettyTemplate}>
<FontAwesome title="Format HTML" name='formathtml' className={'fa-list-ul no-border'}/>
</a>
</div>
<SplitPane split="vertical" minSize={300} defaultSize={window.screen.availWidth/2 - 75}>
<div className="liquid-content-panel">
<textarea id='liquidcontent' name='liquidcontent' className='liquid-content-input' type='text' onChange={e => this.setState({viewScript:e.target.value})} value={this.state.viewScript}/>
</div>
<div className="shaped-JSON-panel">
// <textarea name='shapedJSON' className='shaped-JSON-input' type='text' onChange={e => this.setState({cleanJSON:e.target.value})} value={this.state.cleanJSON} disabled={true}/>
</div>
</SplitPane>
</SplitPane>
</div>
)
}
}
这就是我的测试。我为前两个编写了设置,它们正在工作。
在职的
it(" RenderBottomPanel method : should test onChange event on chart content :", () => {
baseProps.onChange.mockClear();
wrapper.setState({
viewChartScript:'test-id'
});
wrapper.update()
wrapper.find('#chartcontent').simulate('change',{target: {value: 'testing'}} )
expect(wrapper.state().viewChartScript).toEqual('testing');
});
失败
it(" RenderBottomPanel method : should test onChange event on liquidcontent :", () => {
baseProps.onChange.mockClear();
wrapper.setState({
viewScript:'test-id'
});
wrapper.update()
wrapper.find('#liquidcontent').simulate('change',{target: {value: 'testing'}} )
expect(wrapper.state().viewScript).toEqual('testing');
});
谢谢:目标是通过此测试并正确模拟更改
解决方案
推荐阅读
- php - PHP 在 Visual Studio Code 中无法正常运行
- r - 使用 Caret 查找重要的特征
- python-3.x - 无法连接到套接字/opt/local/var/run/dbus/system_bus_socket:没有这样的文件或目录
- ios - HStack 中的两个按钮互相作用
- javascript - Footable 分页根本不显示
- python-3.x - While循环电话号码
- r - 为什么 dplyr group_by 和 summarise 不起作用?
- c# - Paytm 集成 - SendOTP API - 434 错误请求错误
- r - 如何删除公式对象中的引号?
- android - (Kotlin)从 json 数组解析 GSON 不正确的响应