Skip to content

Commit 1f2afe9

Browse files
saboyamarkerikson
authored andcommitted
adding a react hooks test that fails with v7 alpha
1 parent 9543efe commit 1f2afe9

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

test/components/hooks.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*eslint-disable react/prop-types*/
2+
3+
import React from 'react'
4+
import { createStore } from 'redux'
5+
import { Provider as ProviderMock, connect } from '../../src/index.js'
6+
import * as rtl from 'react-testing-library'
7+
import 'jest-dom/extend-expect'
8+
9+
describe('React', () => {
10+
describe('connect', () => {
11+
afterEach(() => rtl.cleanup())
12+
13+
it('should render on useEffect hook state update', () => {
14+
const store = createStore((state, action) => {
15+
let newState =
16+
state !== undefined
17+
? state
18+
: {
19+
byId: {},
20+
list: []
21+
}
22+
switch (action.type) {
23+
case 'FOO':
24+
newState = {
25+
...newState,
26+
list: [1],
27+
byId: { 1: 'foo' }
28+
}
29+
break
30+
}
31+
return newState
32+
})
33+
34+
const mapStateSpy1 = jest.fn()
35+
const renderSpy1 = jest.fn()
36+
37+
const component1Decorator = connect(state => {
38+
mapStateSpy1()
39+
40+
return {
41+
list: state.list
42+
}
43+
})
44+
45+
const component1 = props => {
46+
renderSpy1()
47+
48+
return <Component2 list={props.list} />
49+
}
50+
51+
const Component1 = component1Decorator(component1)
52+
53+
const renderSpy2 = jest.fn()
54+
55+
const Component2 = props => {
56+
const [state, setState] = React.useState({ list: props.list })
57+
58+
React.useEffect(() => {
59+
setState({ list: props.list })
60+
}, [props.list])
61+
62+
renderSpy2()
63+
64+
return <Component3 list={state.list} />
65+
}
66+
67+
const mapStateSpy3 = jest.fn()
68+
const renderSpy3 = jest.fn()
69+
70+
const component3Decorator = connect((state, ownProps) => {
71+
mapStateSpy3()
72+
73+
return {
74+
mappedProp: ownProps.list.map(id => state.byId[id])
75+
}
76+
})
77+
78+
const component3 = () => {
79+
renderSpy3()
80+
81+
return <div>Hello</div>
82+
}
83+
84+
const Component3 = component3Decorator(component3)
85+
86+
rtl.render(
87+
<ProviderMock store={store}>
88+
<Component1 />
89+
</ProviderMock>
90+
)
91+
92+
expect(mapStateSpy1).toHaveBeenCalledTimes(1)
93+
expect(renderSpy1).toHaveBeenCalledTimes(1)
94+
expect(renderSpy2).toHaveBeenCalledTimes(2)
95+
expect(mapStateSpy3).toHaveBeenCalledTimes(1)
96+
expect(renderSpy3).toHaveBeenCalledTimes(1)
97+
98+
store.dispatch({ type: 'FOO' })
99+
100+
expect(mapStateSpy1).toHaveBeenCalledTimes(2)
101+
expect(renderSpy1).toHaveBeenCalledTimes(2)
102+
expect(renderSpy2).toHaveBeenCalledTimes(3)
103+
expect(mapStateSpy3).toHaveBeenCalledTimes(2)
104+
expect(renderSpy3).toHaveBeenCalledTimes(2)
105+
})
106+
})
107+
})

0 commit comments

Comments
 (0)