Skip to content

Commit 83756ec

Browse files
committed
Merge pull request #51 from istarkov/master
Add failing test
2 parents 61a3df7 + 4dc59bc commit 83756ec

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/components/createConnect.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const defaultMergeProps = (stateProps, dispatchProps, parentProps) => ({
1212
...dispatchProps
1313
});
1414

15-
const resetValue = (_, key) => ({ [key]: undefined });
16-
1715
function getDisplayName(Component) {
1816
return Component.displayName || Component.name || 'Component';
1917
}
@@ -82,7 +80,7 @@ export default function createConnect(React) {
8280
};
8381

8482
shouldComponentUpdate(nextProps, nextState) {
85-
return !shallowEqual(this.state, nextState);
83+
return !shallowEqual(this.state.props, nextState.props);
8684
}
8785

8886
constructor(props, context) {
@@ -99,7 +97,9 @@ export default function createConnect(React) {
9997

10098
this.stateProps = computeStateProps(this.store);
10199
this.dispatchProps = computeDispatchProps(this.store);
102-
this.state = this.computeNextState();
100+
this.state = {
101+
props: this.computeNextState()
102+
};
103103
}
104104

105105
recomputeStateProps() {
@@ -132,10 +132,9 @@ export default function createConnect(React) {
132132

133133
recomputeState(props = this.props) {
134134
const nextState = this.computeNextState(props);
135-
if (!shallowEqual(nextState, this.state)) {
135+
if (!shallowEqual(nextState, this.state.props)) {
136136
this.setState({
137-
...Object.keys(this.state).reduce(resetValue, {}),
138-
...nextState
137+
props: nextState
139138
});
140139
}
141140
}
@@ -185,7 +184,7 @@ export default function createConnect(React) {
185184
render() {
186185
return (
187186
<WrappedComponent ref='wrappedInstance'
188-
{...this.state} />
187+
{...this.state.props} />
189188
);
190189
}
191190
}

test/components/connect.spec.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,52 @@ describe('React', () => {
222222
};
223223

224224
expect(propsBefore.x).toEqual(true);
225-
expect(propsAfter.x).toNotEqual(true);
225+
expect('x' in propsAfter).toEqual(false, 'x prop must be removed');
226+
});
227+
228+
it('should remove undefined props without mapDispatchToProps', () => {
229+
const store = createStore(() => ({}));
230+
let props = { x: true };
231+
let container;
232+
233+
@connect(() => ({}))
234+
class ConnectContainer extends Component {
235+
render() {
236+
return (
237+
<div {...this.props} />
238+
);
239+
}
240+
}
241+
242+
class HolderContainer extends Component {
243+
render() {
244+
return (
245+
<ConnectContainer {...props} />
246+
);
247+
}
248+
}
249+
250+
TestUtils.renderIntoDocument(
251+
<Provider store={store}>
252+
{() => (
253+
<HolderContainer ref={instance => container = instance} />
254+
)}
255+
</Provider>
256+
);
257+
258+
const propsBefore = {
259+
...TestUtils.findRenderedDOMComponentWithTag(container, 'div').props
260+
};
261+
262+
props = {};
263+
container.forceUpdate();
264+
265+
const propsAfter = {
266+
...TestUtils.findRenderedDOMComponentWithTag(container, 'div').props
267+
};
268+
269+
expect(propsBefore.x).toEqual(true);
270+
expect('x' in propsAfter).toEqual(false, 'x prop must be removed');
226271
});
227272

228273
it('should ignore deep mutations in props', () => {

0 commit comments

Comments
 (0)