Skip to content

Commit 4580302

Browse files
committed
Add more unit tests
1 parent a45f63c commit 4580302

File tree

23 files changed

+1597
-13
lines changed

23 files changed

+1597
-13
lines changed

src/components/HOCs/WithErrors/WithErrors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { connect } from 'react-redux'
22
import { buildError, addError, removeError, clearErrors } from '../../../services/Error/Error'
33

4-
const mapStateToProps = (state, ownProps) => {
4+
export const mapStateToProps = (state, ownProps) => {
55
return ({errors: state.currentErrors})
66
}
77

8-
const mapDispatchToProps = dispatch => {
8+
export const mapDispatchToProps = dispatch => {
99
return {
1010
buildError,
1111
addError: error => dispatch(addError(error)),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React, { Component } from 'react'
2+
import { mapStateToProps, mapDispatchToProps } from './WithErrors'
3+
import { buildError,
4+
addError,
5+
removeError,
6+
clearErrors } from '../../../services/Error/Error'
7+
8+
jest.mock('../../../services/Error/Error')
9+
10+
let basicState = null
11+
12+
beforeEach(() => {
13+
basicState = {
14+
currentErrors: "some error"
15+
}
16+
})
17+
18+
test("mapStateToProps maps currentErrors to errors", () => {
19+
const mappedProps = mapStateToProps(basicState)
20+
expect(mappedProps.errors).toEqual(basicState.currentErrors)
21+
22+
expect(mappedProps).toMatchSnapshot()
23+
})
24+
25+
test("mapDispatchToProps maps function buildError", () => {
26+
const dispatch = jest.fn(() => Promise.resolve())
27+
const mappedProps = mapDispatchToProps(dispatch, {})
28+
29+
expect(mappedProps.buildError).toBe(buildError)
30+
})
31+
32+
test("mapDispatchToProps maps function addError", () => {
33+
const dispatch = jest.fn(() => Promise.resolve())
34+
const mappedProps = mapDispatchToProps(dispatch, {})
35+
36+
mappedProps.addError("thisError")
37+
expect(dispatch).toBeCalled()
38+
expect(addError).toBeCalledWith("thisError")
39+
})
40+
41+
test("mapDispatchToProps maps function addError", () => {
42+
const dispatch = jest.fn(() => Promise.resolve())
43+
const mappedProps = mapDispatchToProps(dispatch, {})
44+
45+
mappedProps.addError("thisError")
46+
expect(dispatch).toBeCalled()
47+
expect(addError).toBeCalledWith("thisError")
48+
})
49+
50+
test("mapDispatchToProps maps function clearErrors", () => {
51+
const dispatch = jest.fn(() => Promise.resolve())
52+
const mappedProps = mapDispatchToProps(dispatch, {})
53+
54+
mappedProps.clearErrors()
55+
expect(dispatch).toBeCalled()
56+
expect(clearErrors).toBeCalled()
57+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`mapStateToProps maps currentErrors to errors 1`] = `
4+
Object {
5+
"errors": "some error",
6+
}
7+
`;

src/components/HOCs/WithProjects/WithProjects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import _get from 'lodash/get'
66
import { projectSchema } from '../../../services/Project/Project'
77
import { fetchProjectChallenges } from '../../../services/Challenge/Challenge'
88

9-
const mapStateToProps = (state, ownProps) => ({
9+
export const mapStateToProps = (state, ownProps) => ({
1010
projects: _compact(_map(_get(state, 'entities.projects', {}),
1111
project => project.id ?
1212
denormalize(project, projectSchema(), state.entities) :
1313
null))
1414
})
1515

16-
const mapDispatchToProps = dispatch => ({
16+
export const mapDispatchToProps = dispatch => ({
1717
fetchProjectChallenges: (projectId) => dispatch(fetchProjectChallenges(projectId)),
1818
})
1919

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { Component } from 'react'
2+
import { denormalize } from 'normalizr'
3+
import { mapStateToProps, mapDispatchToProps } from './WithProjects'
4+
import { fetchProjectChallenges }
5+
from '../../../services/Challenge/Challenge'
6+
7+
jest.mock('normalizr')
8+
jest.mock('../../../services/Challenge/Challenge')
9+
10+
denormalize.mockImplementation((project, schema, entities) => project)
11+
12+
let basicState = null
13+
14+
beforeEach(() => {
15+
basicState = {
16+
entities: {
17+
projects: [
18+
{
19+
id: "123",
20+
name: "first",
21+
},
22+
{
23+
id: "456",
24+
name: "second",
25+
}
26+
],
27+
}
28+
}
29+
})
30+
31+
test("mapStateToProps maps projects", () => {
32+
const mappedProps = mapStateToProps(basicState)
33+
expect(mappedProps.projects).toEqual(basicState.entities.projects)
34+
35+
expect(mappedProps).toMatchSnapshot()
36+
})
37+
38+
test("mapDispatchToProps maps function fetchProjectChallenges", () => {
39+
const dispatch = jest.fn(() => Promise.resolve())
40+
const mappedProps = mapDispatchToProps(dispatch, {})
41+
42+
mappedProps.fetchProjectChallenges("someProjectId")
43+
expect(dispatch).toBeCalled()
44+
expect(fetchProjectChallenges).toBeCalledWith("someProjectId")
45+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`mapStateToProps maps projects 1`] = `
4+
Object {
5+
"projects": Array [
6+
Object {
7+
"id": "123",
8+
"name": "first",
9+
},
10+
Object {
11+
"id": "456",
12+
"name": "second",
13+
},
14+
],
15+
}
16+
`;

src/components/HOCs/WithSearchExecution/WithSearchExecution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const WithSearchExecution = (WrappedComponent, searchName, searchFunction) =>
3333
)
3434
)
3535

36-
const _WithSearchExecution = function(WrappedComponent, searchName, searchFunction) {
36+
export const _WithSearchExecution = function(WrappedComponent, searchName, searchFunction) {
3737
return class extends Component {
3838
fetchResults = _debounce(query => {
3939
this.props.performSearch(searchName, query, searchFunction)
@@ -48,7 +48,7 @@ const _WithSearchExecution = function(WrappedComponent, searchName, searchFuncti
4848
}
4949
}
5050

51-
const mapDispatchToProps = dispatch => ({
51+
export const mapDispatchToProps = dispatch => ({
5252
performSearch: (searchName, query, searchProjects) =>
5353
dispatch(performSearch(searchName, query, searchProjects)),
5454
})
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { Component } from 'react'
2+
import _isFunction from 'lodash/isFunction'
3+
import { _WithSearchExecution, mapDispatchToProps }
4+
from './WithSearchExecution'
5+
import { performSearch } from '../../../services/Search/Search'
6+
7+
jest.mock('../../../services/Search/Search')
8+
9+
let basicState = null
10+
let WrappedComponent = null
11+
12+
beforeEach(() => {
13+
basicState = {
14+
searchQueries: {
15+
searchName: "my search"
16+
}
17+
}
18+
19+
WrappedComponent = _WithSearchExecution(
20+
() => <div className="child" />,
21+
"searchName",
22+
() => "my function"
23+
)
24+
})
25+
26+
test("searchQueries.searchName is passed through to the wrapped component", () => {
27+
const wrapper = shallow(
28+
<WrappedComponent {...basicState} />
29+
)
30+
31+
expect(
32+
wrapper.props().searchQueries.searchName
33+
).toBe(basicState.searchQueries.searchName)
34+
35+
expect(wrapper).toMatchSnapshot()
36+
})
37+
38+
test("fetchResults function is passed through to the wrapped component", () => {
39+
const myPerformSearch = jest.fn()
40+
const wrapper = shallow(
41+
<WrappedComponent {...basicState} performSearch={myPerformSearch} />
42+
)
43+
44+
expect(_isFunction(wrapper.props().fetchResults)).toBe(true)
45+
})
46+
47+
test("mapDispatchToProps maps function performSearch", () => {
48+
const dispatch = jest.fn(() => Promise.resolve())
49+
const mappedProps = mapDispatchToProps(dispatch, {})
50+
51+
mappedProps.performSearch("searchName", "query", "searchProjects")
52+
expect(dispatch).toBeCalled()
53+
expect(performSearch).toBeCalledWith("searchName", "query", "searchProjects")
54+
expect(mappedProps).toMatchSnapshot()
55+
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`mapDispatchToProps maps function performSearch 1`] = `
4+
Object {
5+
"performSearch": [Function],
6+
}
7+
`;
8+
9+
exports[`searchQueries.searchName is passed through to the wrapped component 1`] = `
10+
ShallowWrapper {
11+
"length": 1,
12+
Symbol(enzyme.__root__): [Circular],
13+
Symbol(enzyme.__unrendered__): <Component
14+
searchQueries={
15+
Object {
16+
"searchName": "my search",
17+
}
18+
}
19+
/>,
20+
Symbol(enzyme.__renderer__): Object {
21+
"batchedUpdates": [Function],
22+
"getNode": [Function],
23+
"render": [Function],
24+
"simulateEvent": [Function],
25+
"unmount": [Function],
26+
},
27+
Symbol(enzyme.__node__): Object {
28+
"instance": null,
29+
"key": undefined,
30+
"nodeType": "function",
31+
"props": Object {
32+
"0": "m",
33+
"1": "y",
34+
"2": " ",
35+
"3": "s",
36+
"4": "e",
37+
"5": "a",
38+
"6": "r",
39+
"7": "c",
40+
"8": "h",
41+
"fetchResults": [Function],
42+
"searchQueries": Object {
43+
"searchName": "my search",
44+
},
45+
},
46+
"ref": null,
47+
"rendered": null,
48+
"type": [Function],
49+
},
50+
Symbol(enzyme.__nodes__): Array [
51+
Object {
52+
"instance": null,
53+
"key": undefined,
54+
"nodeType": "function",
55+
"props": Object {
56+
"0": "m",
57+
"1": "y",
58+
"2": " ",
59+
"3": "s",
60+
"4": "e",
61+
"5": "a",
62+
"6": "r",
63+
"7": "c",
64+
"8": "h",
65+
"fetchResults": [Function],
66+
"searchQueries": Object {
67+
"searchName": "my search",
68+
},
69+
},
70+
"ref": null,
71+
"rendered": null,
72+
"type": [Function],
73+
},
74+
],
75+
Symbol(enzyme.__options__): Object {
76+
"adapter": ReactSixteenAdapter {
77+
"options": Object {
78+
"enableComponentDidUpdateOnSetState": true,
79+
},
80+
},
81+
},
82+
}
83+
`;

src/components/HOCs/WithSearchQuery/WithSearchQuery.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const WithSearchQuery = (WrappedComponent, searchName) =>
2222
_WithSearchQuery(WrappedComponent, searchName)
2323
)
2424

25-
const _WithSearchQuery = function(WrappedComponent, searchName) {
25+
export const _WithSearchQuery = function(WrappedComponent, searchName) {
2626
return class extends Component {
2727
setSearch = (query) => this.props.setSearch(query, searchName)
2828
clearSearch = () => this.props.clearSearch(searchName)
@@ -48,11 +48,11 @@ const _WithSearchQuery = function(WrappedComponent, searchName) {
4848
}
4949
}
5050

51-
const mapStateToProps = state => ({
51+
export const mapStateToProps = state => ({
5252
currentSearch: _get(state, 'currentSearch')
5353
})
5454

55-
const mapDispatchToProps = dispatch => ({
55+
export const mapDispatchToProps = dispatch => ({
5656
setSearch: (query, searchName) => dispatch(setSearch(searchName, query)),
5757
clearSearch: (searchName) => dispatch(clearSearch(searchName)),
5858
})

0 commit comments

Comments
 (0)