Skip to content

Commit

Permalink
Write component tests for ProjectInfo, ProjectSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Feb 19, 2019
1 parent d85b625 commit cb7f17f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/components/ProjectSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Link } from 'react-router-dom'
import '../assets/ProjectSummary.css'

const ProjectSummary = props => {
console.log(props)
let { project } = props
if (project) {
return (
Expand Down Expand Up @@ -43,7 +42,7 @@ const ProjectSummary = props => {
</Grid>
</Fragment>
)
}) || null}
})}
{project.pivotaltracker_url ? (
<Fragment key={project.id}>
<Grid>
Expand All @@ -57,8 +56,8 @@ const ProjectSummary = props => {
</Grid>
</Fragment>
) : null}
{
<Fragment key={project.id}>
{project.slack_channel_name
? <Fragment key={project.slack_channel_name}>
<Grid>
<Grid.Row>
<Icon name='slack hash' size='large' />
Expand All @@ -73,7 +72,7 @@ const ProjectSummary = props => {
</Grid.Row>
</Grid>
</Fragment>
}
: null}
</Card.Description>
</Card.Content>
</Card>
Expand Down
35 changes: 33 additions & 2 deletions src/fixtures/projectInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,42 @@ export default {
user_id: 1,
slug: 'websiteone',
github_url: 'https://github.com/AgileVentures/WebsiteOne',
pivotaltracker_url: null,
pivotaltracker_url: 'https://waffle.io/AgileVentures/WebsiteOne',
pitch: null,
commit_count: 190,
image_url: null,
last_github_update: null,
slack_channel_name: null,
tag_list: []
tag_list: [],
sourceRepositories: [{
id: 77,
url: 'https://github.com/AgileVentures/WebsiteOne',
project_id: 2,
created_at: '2017-09-27T15:01:52.695Z',
updated_at: '2017-09-27T15:01:52.695Z'
}],
members: [{
id: 2,
slug: 'mattwr18'
}],
membersGravatarUrl: [{
'mattwr18': 'https://www.gravatar.com/avatar/9249736dae1898d537770886061c06f9?s=32&d=retro'
}],
videos: [{
id: 6728,
event_id: 2676,
title: 'WebsiteOne Planning - Monday, 12th Mar at 04:30pm (UTC)',
hangout_url: 'https://hangouts.google.com/hangouts/_/ytl/XOYi7nc3GUnbaxQU_J3ToWVE56FIoFztizc5nUfT8sg=',
created_at: '2018-03-12T16:30:55.399Z',
updated_at: '2018-03-12T16:31:05.707Z',
uid: '300-18c2c4db-9aac-428d-8b6e-18c61e8d44e2',
category: 'Scrum',
project_id: 2,
user_id: 3,
yt_video_id: '-a--mSOR4WA',
participants: null,
hoa_status: null,
url_set_directly: true,
youtube_tweet_sent: null
}]
}
78 changes: 78 additions & 0 deletions src/tests/components/ProjectSummary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react'
import { mount } from 'enzyme'
import ProjectSummary from '../../components/ProjectSummary'
import { StaticRouter } from 'react-router'
import project from '../../fixtures/projectInfo'

describe('ProjectSummary', () => {
let wrapper
const context = {}
const props = {
project
}
beforeEach(() => {
wrapper = mount(
<StaticRouter context={context}>
<ProjectSummary {...props} />
</StaticRouter>
)
})

it('displays a link to GitHub repo', () => {
project.sourceRepositories[0].url = 'https://github.com/AgileVentures/WebsiteOne'
let linkToRepo = wrapper.find('a').filterWhere(item => {
return item.prop('href') === project.sourceRepositories[0].url
})

expect(linkToRepo.text()).toEqual('WebsiteOne')
})

it('handles repo urls with ending in forward slashes', () => {
project.sourceRepositories[0].url = 'https://github.com/AgileVentures/WebsiteOne/'
wrapper = mount(
<StaticRouter context={context}>
<ProjectSummary {...props} />
</StaticRouter>
)
let linkToRepo = wrapper.find('a').filterWhere(item => {
return item.prop('href') === project.sourceRepositories[0].url
})
expect(linkToRepo.text()).toEqual('WebsiteOne')
})

it('displays a spinner when no project is in the props', () => {
wrapper = mount(
<StaticRouter context={context}>
<ProjectSummary />
</StaticRouter>
)
expect(wrapper.find('Loader').props().loading).toBe(true)
})

it("displays a project's image if there is one", () => {
project.image_url = 'http://i.imgur.com/23ePvyN.jpg'
wrapper = mount(
<StaticRouter context={context}>
<ProjectSummary {...props} />
</StaticRouter>
)
let projectsImageUrl = wrapper.find('Image').filterWhere(item => {
return item.hasClass('project-info-image')
})
expect(projectsImageUrl.props().src).toEqual(project.image_url)
})

it("displays a member's name if there is one", () => {
project.members[0].first_name = 'Matt'
project.members[0].last_name = 'Rider'
wrapper = mount(
<StaticRouter context={context}>
<ProjectSummary {...props} />
</StaticRouter>
)
let linkToMember = wrapper.find('Link').filterWhere(item => {
return item.prop('to') === '/users/2'
})
expect(linkToMember.text()).toEqual('Matt Rider')
})
})
38 changes: 38 additions & 0 deletions src/tests/containers/ProjectInfo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import { shallow } from 'enzyme'
import { ProjectInfo } from '../../containers/ProjectInfo'
import project from '../../fixtures/projectInfo'

describe('ProjectInfo', () => {
let wrapper
const props = {
match: { params: { slug: 'websiteone' } },
project: {
slug: 'rundfunk-mitbestimmen'
},
fetchProjectInfo: jest.fn(),
setLastLocation: () => {},
location: { pathname: '/projects/websiteone' }
}
beforeEach(() => {
wrapper = shallow(<ProjectInfo {...props} />)
})

it('renders ProjectSummary', () => {
expect(wrapper.find('ProjectSummary')).toBeTruthy()
})

it('calls fetchProjectInfo if the project slug is different from the project slug in the url', () => {
expect(props.fetchProjectInfo).toHaveBeenLastCalledWith(props.match.params.slug)
})

it('sets state if the project props are updated', () => {
wrapper.setProps({ project })
expect(wrapper.state().project).toEqual(project)
})

it('sets state if the project slug is the same as in the url', () => {
wrapper = shallow(<ProjectInfo {...props} project={project} />)
expect(wrapper.state().project).toEqual(project)
})
})

0 comments on commit cb7f17f

Please sign in to comment.