Skip to content

Instantly share code, notes, and snippets.

@Haegin
Created October 9, 2018 15:00
Show Gist options
  • Save Haegin/c82ab53e2858a4020b92128aa76184d0 to your computer and use it in GitHub Desktop.
Save Haegin/c82ab53e2858a4020b92128aa76184d0 to your computer and use it in GitHub Desktop.
Testing parts of components
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