admin管理员组文章数量:1292954
I have the following React ponent:
export default class SignUpForm extends React.Component {
...
doSignupForm(event) {
// Some API call...
}
render() {
return (
<div>
<form action="/" onSubmit={this.doSignupForm.bind(this)} id="register-form">
<button type="submit" id="register_button">Sign Up</button>
</form>
</div>
);
}
};
I want to test that the button fires the doSignupForm
function - how do I do this (ideally using Mocha/Chai/Enzyme/Sinon)?
In addition, as you can see the doSignupForm
function fires an API call - should this API call be tested seperately using an integration test (?).
I have the following React ponent:
export default class SignUpForm extends React.Component {
...
doSignupForm(event) {
// Some API call...
}
render() {
return (
<div>
<form action="/" onSubmit={this.doSignupForm.bind(this)} id="register-form">
<button type="submit" id="register_button">Sign Up</button>
</form>
</div>
);
}
};
I want to test that the button fires the doSignupForm
function - how do I do this (ideally using Mocha/Chai/Enzyme/Sinon)?
In addition, as you can see the doSignupForm
function fires an API call - should this API call be tested seperately using an integration test (?).
3 Answers
Reset to default 5You can simulate form submission using React Utils:
var rendered = TestUtils.renderIntoDocument(SignupForm);
var form = TestUtils.findRenderedDOMComponentWithTag(rendered, 'form');
TestUtils.Simulate.submit(form);
Also, testing calls to the actual API is not reliable, you should mock the API call with responses you expect from it, an idea would be to extract the API call in to its own module, and setup an spy to test the behaviour of your ponent with an specific response (example spy with Jasmine):
spyOn(apiModule, "requestProjects").and.callFake(function() {
return { ...someProjects };
});
Reference:
https://facebook.github.io/react/docs/test-utils.html https://volaresystems./blog/post/2014/12/10/Mocking-calls-with-Jasmine
Since your are using Enzyme
and Sinon
import sinon from 'sinon';
import {mount} from 'enzyme';
import expect from 'expect';
it('fires form submit', () => {
const doSignupForm = sinon.stub(SignUpForm.prototype, 'doSignupForm').returns(true);
const wrapper = mount(<SignUpForm />);
wrapper.find('button').simulate('click');
expect(doSignupForm.called).to.be.true;
doSignupForm.restore();
});
As stated here, event bubbling is not supported in Enzyme. Therefore, found the following workaround:
import sinon from 'sinon';
import {mount} from 'enzyme';
import chai from 'chai';
var expect = chai.expect;
it('fires form submit', () => {
const doSignupForm = sinon.stub(SignUpForm.prototype, 'doSignupForm').returns(true);
const wrapper = mount(<SignUpForm />);
wrapper.find('#register_button').get(0).click();
expect(doSignupForm).to.have.been.called;
doSignupForm.restore();
});
本文标签: javascriptHow can I test form submit in ReactStack Overflow
版权声明:本文标题:javascript - How can I test form submit in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741566307a2385744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论