Javascript onBlur不会在开玩笑的测试用例中被调用,React应用程序
我有一个组件,其中我在Javascript onBlur不会在开玩笑的测试用例中被调用,React应用程序,javascript,reactjs,jasmine,jestjs,enzyme,Javascript,Reactjs,Jasmine,Jestjs,Enzyme,我有一个组件,其中我在input type=text上编写了onBlur和onChange事件处理程序 <input type="text" onBlur={ this.fetchProspectIdDetails } onChange={ this.handleProspectIdChange } name="prospectId" value={this.state.prospectId}></input> 但是未调用onBlur函数,即fetchProspectI
input type=text
上编写了onBlur
和onChange
事件处理程序
<input type="text" onBlur={ this.fetchProspectIdDetails } onChange={ this.handleProspectIdChange } name="prospectId" value={this.state.prospectId}></input>
但是未调用onBlur
函数,即fetchProspectIdDetails
it('Testing the ProspectId State', () => {
wrapper.find('input[name="prospectId"]')
.simulate('blur', {target: {name: 'prospectId', value: '001G000000mIQIY'}});
expect(wrapper.state('prospectId')).toEqual('001G000000mIQIY');
});
我做错了什么?它工作正常。以下是一个工作示例:
index.jsx
:
import React,{Component}来自'React';
导出类示例扩展组件{
建造师(道具){
超级(道具);
此.state={
prospectId:1,
validSalesForceId:是的,
};
this.fetchProspectIdDetails=this.fetchProspectIdDetails.bind(this);
this.handleProspectIdChange=this.handleProspectIdChange.bind(this);
}
FetchProspectId详细信息(e){
console.log('onblur');
this.setState({prospectId:e.target.value});
}
手动透视图更改(e){
console.log('onchange');
this.setState({validSalesForceId:!!e.target.value});
}
render(){
返回(
);
}
}
index.test.jsx
:
import{shall}来自“酶”;
从“React”导入React;
从“”导入{Example};
描述('53407041',()=>{
让包装纸;
在每个之前(()=>{
包装器=浅();
});
它('测试空Salesforce Id检查',()=>{
find('input[name=“prospectId”]”)。simulate('change',{target:{name:'prospectId',value:''}});
expect(wrapper.state('validSalesForceId')).toEqual(false);
});
它('正在测试ProspectId状态',()=>{
包装纸
.find('input[name=“prospectId”]”)
.simulate('blur',{target:{name:'prospectId',value:'001G000000mIQIY'});
expect(wrapper.state('prospectId')).toEqual('001G000000mIQIY');
});
});
单元测试结果:
PASS examples/53407041/index.test.jsx
53407041
✓ Testing empty Salesforce Id Check (59 ms)
✓ Testing the ProspectId State (2 ms)
console.log
onchange
at Example.handleProspectIdChange (examples/53407041/index.jsx:19:13)
console.log
onblur
at Example.fetchProspectIdDetails (examples/53407041/index.jsx:15:13)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.jsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 5.286 s
源代码:可能是模拟更改而不是模糊
包装。查找('input[name=“prospectId”]')。模拟('blur',{target:{name:'prospectId',value:'001G000000mIQIY'})代码>我在模糊测试用例中错误地编写了更改。在实际中,我编写了blur,但它没有被调用。我将编辑这个问题,以纠正这个错误。@Atulkumarsingh您能分享fetchProspectIdDetails
的代码吗?
PASS examples/53407041/index.test.jsx
53407041
✓ Testing empty Salesforce Id Check (59 ms)
✓ Testing the ProspectId State (2 ms)
console.log
onchange
at Example.handleProspectIdChange (examples/53407041/index.jsx:19:13)
console.log
onblur
at Example.fetchProspectIdDetails (examples/53407041/index.jsx:15:13)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.jsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 5.286 s