Created
November 29, 2018 18:44
-
-
Save lynndylanhurley/7fe86c8bea86b917fcc701bc0cba7e08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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