Skip to content

Instantly share code, notes, and snippets.

@lynndylanhurley
Created November 29, 2018 18:44
Show Gist options
  • Save lynndylanhurley/7fe86c8bea86b917fcc701bc0cba7e08 to your computer and use it in GitHub Desktop.
Save lynndylanhurley/7fe86c8bea86b917fcc701bc0cba7e08 to your computer and use it in GitHub Desktop.
// given this class
class LoginForm extends Component {
static propTypes = {
login: PropTypes.func,
};
render() {
<Formik onSubmit={v => this.props.login(v.username, v.password)}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
{/* ... */}
<button type="submit">submit</button>
</form>
)}
</Formik>
}
}
// in the test suite
import LoginForm from 'components/LoginForm';
import { spy } from 'sinon';
import { mount } from 'enzyme';
it('should call login when form is submitted', function() {
// a spy is a function that tracks how many times its been called
// see https://sinonjs.org/ for more info
const loginSpy = spy();
// instead of the real login function, pass in the spy
const wrapper = mount(<LoginForm login={loginSpy} />);
// simulate the form submission
const submitBtn = wrapper.find('[type="submit"]');
submitBtn.simulate('submit');
// check to see that the login function was called on submit
expect(loginSpy.calledOnce).toBeTruthy();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment