我想要我的笑话+酶单元测试,saveSignUpFirstCreds
被称为:
this.setState({ requestInProgress: true, errorMessage: null }, () => {
return Api.registerPhone(values.phone)
.then(() => {
this.props.saveSignUpFirstCreds(values);
this.handleSubmitSuccess();
})
});
我有一个开玩笑的 api 手册模拟,我的代码到目前为止看起来像这样:
wrapper.setState({ requestInProgress: true, errorMessage: null }, () => {
// DO i need to do something here ???
// I need the promise to be fullfilled and then check the prop and then call done
});
简而言之"如何在setState回调中开玩笑测试承诺"
为此,您必须存根组件的 setState 函数,并测试是否触发了回调。
//component.js
onSubmit() {
this.setState({count: this.state.count + 1},
this.props.onSubmitCallback()
)}
//test.js
const setState = sinon.stub(SomeComponent.prototype, ‘setState’)
.callsFake( (newState, callback) => { callback() });
const onSubmitCallback = sinon.spy();
const component =
shallow(<SomeComponent onSubmitCallback={onSubmitCallback}/>);
component.instance().onSubmit();
expect(setState).to.be.calledOnce; //PASS!
expect(onSubmitCallback).to.be.calledOnce; //PASS!
如本文所述 - https://medium.com/@tjhubert/react-js-testing-setstates-callback-907df1fe720d
return expect(saveSignUpFirstCreds(value)).resolves.toEqual('Something');
应该做这个伎俩。更多信息请点击此处 https://facebook.github.io/jest/docs/en/tutorial-async.html,以及此处:https://facebook.github.io/jest/docs/en/asynchronous.html