您好我试图使用sinon spy和chai测试表单提交,但是我不确定我是否正在做正确的事情。每当我运行测试时,秒数会以未定义的形式返回。不知道为什么会这样。柴,我是新来的。这是代码
这是测试文件:
describe('CountdownForm', () => {
it('should exist', () => {
expect(CountdownForm).to.exist;
});
it('should call onSetCountdown if valid seconds entered', () => {
const spy = sinon.spy();
const countDownForm = shallow(<CountdownForm onSetCountdown={spy}/>)
countDownForm.refs.seconds.value = '109';
countDownForm.find('form').simulate('submit', { preventDefault(){} })
expect(spy.called).to.be.true;
});
});
这是jsx文件:
class CountdownForm extends Component {
onSubmit = (e) => {
e.preventDefault();
const stringSeconds = this.refs.seconds.value;
if (stringSeconds.match(/^[0-9]*$/)) {
this.refs.seconds.value = '';
this.props.onSetCountdown(parseInt(stringSeconds, 10));
}
}
render() {
return (
<div>
<form ref="form" onSubmit={this.onSubmit} className='countdown-form'>
<input type="text" ref="seconds" placeholder="Enter time in seconds"/>
<button className="button expanded">Start</button>
</form>
</div>
);
}
}