Skip to content

Instantly share code, notes, and snippets.

@camjackson
camjackson / UserProfile-test.js
Last active August 31, 2016 12:27
React component unit testing
import React from 'react';
import { shallow } from 'enzyme';
import UserProfile from '../UserProfile';
import LoadingSpinner from '../LoadingSpinner';
describe('UserProfile', () => {
it('shows the spinner when loading', () => {
const userProfile = shallow(<UserProfile isLoading />);
expect(userProfile.containsMatchingElement(<LoadingSpinner />)).toBe(true);
});
@camjackson
camjackson / input-test.js
Last active February 4, 2016 13:22
Better example of testing interaction in a React.js component
/*
I think this is a better example of interaction testing, because the component has its own
internal function, which a) does more than one thing, and b) has a small amount of logic
where it plucks the new value out of the DOM event. Those we can test in isolation.
*/
const MyInput = props => {
const handleChange = event => {
props.updateTheValue(event.target.value);
props.fetchSomeDataThatDependsUponTheValue();
};
@camjackson
camjackson / login-form.js
Last active November 17, 2019 09:18
Basic stateless React.js form
/*
This is obviously a very basic example. It takes props to render the inputs with their values, and
to render any error state. It also takes props to update the state, or submit the form.
*/
const LoginForm = props => (
<form onSubmit={props.onSubmit}>
<input type="text" name="username" onChange={props.onChangeUsername} value={props.username}/>
{ props.userNameError && <span class="error">{ props.usernameError }</span> }
<input type="password" name="password" onChange={props.onChangePassword} value={props.password}/>