-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Home-tests): add tests for modules/Home folder
- adds tests for the "modules/Home" files - ensures that all the tests have 100% coverage [Finishes #160928262]
- Loading branch information
Chidiebere Oguejiofor
committed
Oct 12, 2018
1 parent
44e6d87
commit 8fe1019
Showing
7 changed files
with
4,013 additions
and
3,576 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* eslint max-nested-callbacks:off */ | ||
/* global jest:true, */ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import { Cards, mapStateToProps } from '../../../../modules/Home/components/Cards'; | ||
|
||
const sampleProps = { | ||
teams: { teams: [] }, | ||
toggleFavoritesAction: () => {}, | ||
clearTeams: () => {}, | ||
teamsState: [], | ||
favoriteId: [] | ||
}; | ||
const getShallowObj = (props = {}) => shallow(<Cards | ||
{...sampleProps} {...props} />); | ||
|
||
|
||
describe('Testing CardItem Component', () => { | ||
describe('the elements rendered by the component', () => { | ||
it('should match snapshot', () => { | ||
const wrapper = getShallowObj(); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('mapStateToProps', () => { | ||
it('should return an object that contains the value in teams', () => { | ||
const mockTeam = 'mock-team'; | ||
const mockState = { | ||
teams: { teams: mockTeam } | ||
}; | ||
const mappedProp = mapStateToProps(mockState); | ||
expect(mappedProp.teamsState) | ||
.toBe(mockTeam); | ||
}); | ||
}); | ||
|
||
describe('the methods in the component', () => { | ||
describe('componentWillReceiveProps', () => { | ||
it('should be called when props change', () => { | ||
const spy = jest.spyOn(Cards.prototype, 'componentWillReceiveProps'); | ||
const wrapper = getShallowObj(); | ||
const initSpy = jest.fn(); | ||
global.M = { | ||
Autocomplete: { init: initSpy } | ||
}; | ||
wrapper.setProps({ name: 'name' }); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(initSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
|
||
describe('addFavorites method', () => { | ||
it('should be called when props.teamsState.map function', () => { | ||
const props = { | ||
teamsState: [] | ||
}; | ||
const spy = jest.spyOn(props.teamsState, 'map'); | ||
const wrapper = getShallowObj(props); | ||
wrapper.instance().addFavorites(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should be called when props.toggleFavoritesAction when the a props.team.id equals the id passed ', () => { | ||
const id = 3; | ||
|
||
const favActionSpy = jest.fn(); | ||
const props = { | ||
teamsState: [{ id: 1 }, { id: 2 }, { id: 3 }], | ||
toggleFavoritesAction: favActionSpy | ||
}; | ||
const spy = jest.spyOn(props.teamsState, 'map'); | ||
const wrapper = getShallowObj(props); | ||
wrapper.instance().addFavorites(id); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(favActionSpy).toHaveBeenCalled(); | ||
}); | ||
it('should not props.toggleFavoritesAction when the id passed is not in props.teamsState', () => { | ||
const id = 0; | ||
|
||
const favActionSpy = jest.fn(); | ||
const props = { | ||
teamsState: [{ id: 1 }, { id: 2 }, { id: 3 }], | ||
toggleFavoritesAction: favActionSpy | ||
}; | ||
const spy = jest.spyOn(props.teamsState, 'map'); | ||
const wrapper = getShallowObj(props); | ||
wrapper.instance().addFavorites(id); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(favActionSpy).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe('componentWillUnmount', () => { | ||
it('should be called when component unmounts', () => { | ||
const spy = jest.spyOn(Cards.prototype, 'componentWillUnmount'); | ||
const wrapper = getShallowObj(); | ||
wrapper.unmount(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
it('should call props.clearTeams', () => { | ||
const clearTeamsSpy = jest.fn(); | ||
const wrapper = getShallowObj({ clearTeams: clearTeamsSpy }); | ||
wrapper.unmount(); | ||
expect(clearTeamsSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
82 changes: 82 additions & 0 deletions
82
client/src/tests/modules/Home/components/CardItem.test.jsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint max-nested-callbacks:off */ | ||
/* eslint max-len: off */ | ||
/* global jest:true, */ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import CardItem from '../../../../modules/Home/components/CardItem'; | ||
|
||
|
||
const sampleProps = { | ||
item: { | ||
name: 'sample name', | ||
description: 'Sample Description', | ||
id: 0, | ||
progress: '10', | ||
members: 'mock-members' | ||
}, | ||
favorite: '', | ||
lock: '', | ||
toolTip: '', | ||
progressBar: [], | ||
addFavorites: () => {}, | ||
isFavorited: false | ||
}; | ||
const getShallowObj = (props = {}) => shallow(<CardItem | ||
{...sampleProps} {...props} />); | ||
|
||
describe('Testing CardItem Component', () => { | ||
describe('the elements rendered by the component', () => { | ||
it('should match snapshot', () => { | ||
const wrapper = getShallowObj(); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('the props passed to it as argument', () => { | ||
describe('isFavorited prop', () => { | ||
it('should add class "props.favorite" to #add-favorite-anchor when isFavorited === true', () => { | ||
const sampleFav = 'favorite-class'; | ||
const wrapper = getShallowObj({ favorite: sampleFav, isFavorited: true }); | ||
const element = wrapper.find(`#add-favorite-anchor[className*="favorite-icons"]`); | ||
expect(element.length) | ||
.toBe(1); | ||
}); | ||
it('should add class "props.favorite" to #add-favorite-anchor when isFavorited === false', () => { | ||
const sampleFav = 'favorite-class'; | ||
const wrapper = getShallowObj({ favorite: sampleFav, isFavorited: false }); | ||
const element = wrapper.find(`#add-favorite-anchor[className*="favorite-btn"]`); | ||
expect(element.length) | ||
.toBe(1); | ||
}); | ||
}); | ||
describe('item prop', () => { | ||
it('should render a span with className = "card-title" with text equal to props.item.description', () => { | ||
const wrapper = getShallowObj(); | ||
const span = wrapper.find("span.card-title"); | ||
expect(span.text()) | ||
.toBe(sampleProps.item.name); | ||
}); | ||
|
||
it('the div with #progress-bar should contain prop data-tip= props.item.progress', () => { | ||
const wrapper = getShallowObj(); | ||
expect(wrapper.find(`#progress-bar[data-tip="${sampleProps.item.progress}%"]`).length) | ||
.toBe(1); | ||
}); | ||
it('should render an <i> wiith data-tip=item.members', () => { | ||
const wrapper = getShallowObj(); | ||
expect(wrapper.find(`i[data-tip*="${sampleProps.item.members}"]`).length) | ||
.toBe(1); | ||
}); | ||
it('the onClick method of a#add-favorite-anchor should call addFavorites with props.item.id', () => { | ||
const addFavSpy = jest.fn(); | ||
const wrapper = getShallowObj({ addFavorites: addFavSpy }); | ||
wrapper.find("#add-favorite-anchor") | ||
.simulate('click'); | ||
expect(addFavSpy) | ||
.toHaveBeenCalledTimes(1); | ||
expect(addFavSpy) | ||
.toHaveBeenCalledWith(sampleProps.item.id); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
Oops, something went wrong.