@@ -34,6 +34,8 @@ describe('React', () => {
34
34
const mapStateSpy1 = jest . fn ( )
35
35
const renderSpy1 = jest . fn ( )
36
36
37
+ let component1StateList
38
+
37
39
const component1Decorator = connect ( state => {
38
40
mapStateSpy1 ( )
39
41
@@ -42,9 +44,11 @@ describe('React', () => {
42
44
}
43
45
} )
44
46
45
- const component2 = props => {
47
+ const component1 = props => {
46
48
const [ state , setState ] = React . useState ( { list : props . list } )
47
49
50
+ component1StateList = state . list
51
+
48
52
React . useEffect ( ( ) => {
49
53
setState ( prevState => ( { ...prevState , list : props . list } ) )
50
54
} , [ props . list ] )
@@ -54,7 +58,7 @@ describe('React', () => {
54
58
return < Component2 list = { state . list } />
55
59
}
56
60
57
- const Component1 = component1Decorator ( component2 )
61
+ const Component1 = component1Decorator ( component1 )
58
62
59
63
const mapStateSpy2 = jest . fn ( )
60
64
const renderSpy2 = jest . fn ( )
@@ -67,33 +71,52 @@ describe('React', () => {
67
71
}
68
72
} )
69
73
70
- const component3 = ( ) => {
74
+ const component2 = props => {
71
75
renderSpy2 ( )
72
76
77
+ expect ( props . list ) . toBe ( component1StateList )
78
+
73
79
return < div > Hello</ div >
74
80
}
75
81
76
- const Component2 = component2Decorator ( component3 )
82
+ const Component2 = component2Decorator ( component2 )
77
83
78
84
rtl . render (
79
85
< ProviderMock store = { store } >
80
86
< Component1 />
81
87
</ ProviderMock >
82
88
)
83
89
90
+ // 1. Initial render
84
91
expect ( mapStateSpy1 ) . toHaveBeenCalledTimes ( 1 )
92
+
93
+ // 1.Initial render
94
+ // 2. C1 useEffect
85
95
expect ( renderSpy1 ) . toHaveBeenCalledTimes ( 2 )
96
+
97
+ // 1. Initial render
86
98
expect ( mapStateSpy2 ) . toHaveBeenCalledTimes ( 1 )
99
+
100
+ // 1. Initial render
87
101
expect ( renderSpy2 ) . toHaveBeenCalledTimes ( 1 )
88
102
89
103
rtl . act ( ( ) => {
90
104
store . dispatch ( { type : 'FOO' } )
91
105
} )
92
106
107
+ // 2. Store dispatch
93
108
expect ( mapStateSpy1 ) . toHaveBeenCalledTimes ( 2 )
109
+
110
+ // 3. Store dispatch
111
+ // 4. C1 useEffect
94
112
expect ( renderSpy1 ) . toHaveBeenCalledTimes ( 4 )
113
+
114
+ // 2. Connect(C2) subscriber
115
+ // 3. Ignored prev child props in re-render and re-runs mapState
95
116
expect ( mapStateSpy2 ) . toHaveBeenCalledTimes ( 3 )
96
- expect ( renderSpy2 ) . toHaveBeenCalledTimes ( 3 )
117
+
118
+ // 2. Batched update from nested subscriber / C1 re-render
119
+ expect ( renderSpy2 ) . toHaveBeenCalledTimes ( 2 )
97
120
} )
98
121
} )
99
122
} )
0 commit comments