@@ -28,6 +28,28 @@ describe('React', () => {
28
28
</ div >
29
29
)
30
30
31
+ class Dispatcher extends React . Component {
32
+ constructor ( props ) {
33
+ super ( props )
34
+ if ( props . constructAction ) {
35
+ props . dispatch ( props . constructAction )
36
+ }
37
+ }
38
+ UNSAFE_componentWillMount ( ) {
39
+ if ( this . props . willMountAction ) {
40
+ this . props . dispatch ( this . props . willMountAction )
41
+ }
42
+ }
43
+ render ( ) {
44
+ if ( this . props . renderAction ) {
45
+ this . props . dispatch ( this . props . renderAction )
46
+ }
47
+
48
+ return < Greeter greeted = { this . props . greeted } />
49
+ }
50
+ }
51
+ const ConnectedDispatcher = connect ( ) ( Dispatcher )
52
+
31
53
it ( 'should be able to render connected component with props and state from store' , ( ) => {
32
54
const store = createStore ( greetingReducer )
33
55
@@ -71,48 +93,43 @@ describe('React', () => {
71
93
expect ( store . getState ( ) . greeting ) . toContain ( 'Hi' )
72
94
} )
73
95
74
- it . skip ( 'should render children with original state even if actions are dispatched in ancestor' , ( ) => {
96
+ it ( 'should render children with updated state if actions are dispatched in ancestor' , ( ) => {
75
97
/*
76
98
Dispatching during construct, render or willMount is
77
99
almost always a bug with SSR (or otherwise)
78
100
79
101
This behaviour is undocumented and is likely to change between
80
102
implementations, this test only verifies current behaviour
81
103
82
- Note: this test passes in v6, because we use context to propagate the store state, and the entire
104
+ Note: this test fails in v6, because we use context to propagate the store state, and the entire
83
105
tree will see the same state during the render pass.
84
-
85
106
In all other versions, including v7, the store state may change as actions are dispatched
86
107
during lifecycle methods, and components will see that new state immediately as they read it.
87
108
*/
88
109
const store = createStore ( greetingReducer )
89
110
90
- class Dispatcher extends React . Component {
91
- constructor ( props ) {
92
- super ( props )
93
- props . dispatch ( props . action )
94
- }
95
- UNSAFE_componentWillMount ( ) {
96
- this . props . dispatch ( this . props . action )
97
- }
98
- render ( ) {
99
- this . props . dispatch ( this . props . action )
100
-
101
- return < Greeter greeted = { this . props . greeted } />
102
- }
103
- }
104
- const ConnectedDispatcher = connect ( ) ( Dispatcher )
105
-
106
- const action = { type : 'Update' , payload : { greeting : 'Hi' } }
111
+ const constructAction = { type : 'Update' , payload : { greeting : 'Hi' } }
112
+ const willMountAction = { type : 'Update' , payload : { greeting : 'Hiya' } }
113
+ const renderAction = { type : 'Update' , payload : { greeting : 'Hey' } }
107
114
108
115
const markup = renderToString (
109
116
< Provider store = { store } >
110
- < ConnectedDispatcher action = { action } greeted = "world" />
117
+ < ConnectedDispatcher
118
+ constructAction = { constructAction }
119
+ greeted = "world"
120
+ />
121
+ < ConnectedDispatcher
122
+ willMountAction = { willMountAction }
123
+ greeted = "world"
124
+ />
125
+ < ConnectedDispatcher renderAction = { renderAction } greeted = "world" />
111
126
</ Provider >
112
127
)
113
128
114
- expect ( markup ) . toContain ( 'Hello world' )
115
- expect ( store . getState ( ) . greeting ) . toContain ( 'Hi' )
129
+ expect ( markup ) . toContain ( 'Hi world' )
130
+ expect ( markup ) . toContain ( 'Hiya world' )
131
+ expect ( markup ) . toContain ( 'Hey world' )
132
+ expect ( store . getState ( ) . greeting ) . toContain ( 'Hey' )
116
133
} )
117
134
118
135
it ( 'should render children with changed state if actions are dispatched in ancestor and new Provider wraps children' , ( ) => {
@@ -122,35 +139,12 @@ describe('React', () => {
122
139
123
140
This behaviour is undocumented and is likely to change between
124
141
implementations, this test only verifies current behaviour
142
+
143
+ This test works both when state is fetched directly in connected
144
+ components and when it is fetched in a Provider and placed on context
125
145
*/
126
146
const store = createStore ( greetingReducer )
127
147
128
- class Dispatcher extends React . Component {
129
- constructor ( props ) {
130
- super ( props )
131
- if ( props . constructAction ) {
132
- props . dispatch ( props . constructAction )
133
- }
134
- }
135
- UNSAFE_componentWillMount ( ) {
136
- if ( this . props . willMountAction ) {
137
- this . props . dispatch ( this . props . willMountAction )
138
- }
139
- }
140
- render ( ) {
141
- if ( this . props . renderAction ) {
142
- this . props . dispatch ( this . props . renderAction )
143
- }
144
-
145
- return (
146
- < Provider store = { store } >
147
- < Greeter greeted = { this . props . greeted } />
148
- </ Provider >
149
- )
150
- }
151
- }
152
- const ConnectedDispatcher = connect ( ) ( Dispatcher )
153
-
154
148
const constructAction = { type : 'Update' , payload : { greeting : 'Hi' } }
155
149
const willMountAction = { type : 'Update' , payload : { greeting : 'Hiya' } }
156
150
const renderAction = { type : 'Update' , payload : { greeting : 'Hey' } }
0 commit comments