Last active
August 31, 2016 12:27
-
-
Save camjackson/e20c450c5cd08750b948324719d43da0 to your computer and use it in GitHub Desktop.
React component unit testing
This file contains hidden or 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
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); | |
}); | |
}); |
This file contains hidden or 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
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