Skip to content

Commit 1d6ccc1

Browse files
Ephemmarkerikson
authored andcommitted
Updated outdated SSR-test (dispatch in ancestors) (#1213)
1 parent 9852814 commit 1d6ccc1

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

test/integration/server-rendering.spec.js

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ describe('React', () => {
2828
</div>
2929
)
3030

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+
3153
it('should be able to render connected component with props and state from store', () => {
3254
const store = createStore(greetingReducer)
3355

@@ -71,48 +93,43 @@ describe('React', () => {
7193
expect(store.getState().greeting).toContain('Hi')
7294
})
7395

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', () => {
7597
/*
7698
Dispatching during construct, render or willMount is
7799
almost always a bug with SSR (or otherwise)
78100
79101
This behaviour is undocumented and is likely to change between
80102
implementations, this test only verifies current behaviour
81103
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
83105
tree will see the same state during the render pass.
84-
85106
In all other versions, including v7, the store state may change as actions are dispatched
86107
during lifecycle methods, and components will see that new state immediately as they read it.
87108
*/
88109
const store = createStore(greetingReducer)
89110

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' } }
107114

108115
const markup = renderToString(
109116
<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" />
111126
</Provider>
112127
)
113128

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')
116133
})
117134

118135
it('should render children with changed state if actions are dispatched in ancestor and new Provider wraps children', () => {
@@ -122,35 +139,12 @@ describe('React', () => {
122139
123140
This behaviour is undocumented and is likely to change between
124141
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
125145
*/
126146
const store = createStore(greetingReducer)
127147

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-
154148
const constructAction = { type: 'Update', payload: { greeting: 'Hi' } }
155149
const willMountAction = { type: 'Update', payload: { greeting: 'Hiya' } }
156150
const renderAction = { type: 'Update', payload: { greeting: 'Hey' } }

0 commit comments

Comments
 (0)