Skip to content

Instantly share code, notes, and snippets.

@camjackson
Last active August 31, 2016 12:27
Show Gist options
  • Save camjackson/e20c450c5cd08750b948324719d43da0 to your computer and use it in GitHub Desktop.
Save camjackson/e20c450c5cd08750b948324719d43da0 to your computer and use it in GitHub Desktop.
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);
});
it('shows the family name and given initial when not loading', () => {
const userProfile = shallow(<UserProfile isLoading={false} familyName="Wayne" givenName="Bruce" />);
expect(userProfile.contains('Wayne, B.')).toBe(true);
});
});
import React from 'react';
import LoadingSpinner from './LoadingSpinner';
const UserProfile = ({ familyName, givenName, isLoading }) => {
if (isLoading) {
return (
<div className="container">
<LoadingSpinner />
</div>
);
}
return (
<div className="container">
<div className="row">My Profile</div>
<div className="row">
<span>Name: {`${familyName}, ${givenName[0]}.`}</span>
</div>
</div>
);
};
export default UserProfile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment