Skip to content
This repository was archived by the owner on Apr 18, 2022. It is now read-only.

Commit cf03554

Browse files
committed
Add tests for connector component.
1 parent 780dad1 commit cf03554

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"cross-env": "^5.0.5",
4040
"enzyme": "^2.9.1",
4141
"jest": "^21.1.0",
42+
"lodash": "^4.17.4",
4243
"react": "^15.6.1",
4344
"react-dom": "^15.6.1",
4445
"react-test-renderer": "^15.6.1",

src/__tests__/redux.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from 'react'
2+
import { createStore } from 'redux'
3+
import { mount } from 'enzyme'
4+
import { memoize } from 'lodash'
5+
6+
import Redux from '../redux'
7+
8+
const reducer = (state = { test: 0 }, action) => {
9+
switch (action.type) {
10+
case 'INCREMENT':
11+
return { test: state.test + 1 }
12+
default:
13+
return state
14+
}
15+
}
16+
17+
const store = createStore(reducer)
18+
19+
describe('Redux', function() {
20+
const wrapper = mount(
21+
<Redux selector={state => state.test}>
22+
{(test, dispatch) => (
23+
<div onClick={() => dispatch({ type: 'INCREMENT' })}>{test}</div>
24+
)}
25+
</Redux>,
26+
{ context: { reduxRenderStore: store } }
27+
)
28+
29+
it('should render without throwing an error', () => {
30+
expect(wrapper.exists()).toBe(true)
31+
})
32+
33+
it('should contain the text from the store', () => {
34+
expect(wrapper.html()).toBe('<div>0</div>')
35+
})
36+
37+
it('should change when dispatch is called', () => {
38+
wrapper.find('div').simulate('click')
39+
expect(wrapper.html()).toBe('<div>1</div>')
40+
})
41+
42+
it('should not re-render when state is same', () => {
43+
let renderCount = 0
44+
45+
const memoizedSelector = memoize(state => state.test)
46+
const wrapper = mount(
47+
<Redux selector={memoizedSelector}>
48+
{(test, dispatch) => {
49+
// Increment the render count to show that a side effect occurred
50+
renderCount += 1
51+
return (
52+
<div onClick={() => dispatch({ type: 'NOT_INCREMENT' })}>
53+
{test}
54+
</div>
55+
)
56+
}}
57+
</Redux>,
58+
{ context: { reduxRenderStore: store } }
59+
)
60+
61+
expect(renderCount).toBe(1)
62+
63+
wrapper.find('div').simulate('click')
64+
expect(renderCount).toBe(1)
65+
})
66+
})

0 commit comments

Comments
 (0)