Created
October 9, 2018 15:00
-
-
Save Haegin/c82ab53e2858a4020b92128aa76184d0 to your computer and use it in GitHub Desktop.
Testing parts of components
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
const LoginForm = ({email, password, onChange, onSubmit}) => ( | |
<div> | |
<p> | |
<label for="email">Email</label> | |
<input | |
id="email" | |
onChange={val => onChange('email', val)} | |
value={email} | |
/> | |
</p> | |
<p> | |
<label for="password">Password</label> | |
<input | |
id="password" | |
onChange={val => onChange('password', val)} | |
value={password} | |
/> | |
</p> | |
<p> | |
<button onClick={onSubmit}>Submit</button> | |
</p> | |
</div> | |
); | |
const submitHandler = ({email, password}, {submit}) => () => submit(email, password), | |
export default compose( | |
withStateHandlers( | |
{ | |
email: '', | |
password: '', | |
}, | |
{ | |
onChange: () => (name, value) => ({[name]: value}), | |
onSubmit: submitHandler | |
}, | |
), | |
)(LoginForm); | |
export {LoginForm, submitHandler}; | |
/* Tests */ | |
import {submitHandler} from './LoginForm'; | |
describe('submit handler', () => { | |
it('submits the form correctly', () => { | |
const onSubmitSpy = sinon.spy(); | |
submitHandler({email: 'bob@test.com', password: 'secret'}, {submit: onSubmitSpy})() | |
expect(onSubmitSpy).toHaveBeenCalledWith('bob@test.com', 'secret'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment