Skip to content

Commit 8512956

Browse files
esamattisgaearon
authored andcommitted
Add failing test for #86
1 parent 00b98d5 commit 8512956

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/components/connect.spec.js

+65
Original file line numberDiff line numberDiff line change
@@ -1147,5 +1147,70 @@ describe('React', () => {
11471147
wrapper.setState({ value: 1 });
11481148
expect(target.props.statefulValue).toEqual(1);
11491149
});
1150+
1151+
it('should pass state consistently to mapState', () => {
1152+
const store = createStore(stringBuilder);
1153+
1154+
store.dispatch({ type: 'APPEND', body: 'a'});
1155+
let childMapStateInvokes = 0;
1156+
1157+
@connect(state => ({ state }))
1158+
class Container extends Component {
1159+
1160+
emitChange() {
1161+
store.dispatch({ type: 'APPEND', body: 'b'});
1162+
}
1163+
1164+
render() {
1165+
return (
1166+
<div>
1167+
<button ref="button" onClick={this.emitChange.bind(this)}>change</button>
1168+
<ChildContainer parentState={this.props.state} />
1169+
</div>
1170+
);
1171+
}
1172+
}
1173+
1174+
@connect((state, parentProps) => {
1175+
childMapStateInvokes++;
1176+
// The state from parent props should always be consistent with the current state
1177+
expect(state).toEqual(parentProps.parentState);
1178+
return {};
1179+
})
1180+
class ChildContainer extends Component {
1181+
render() {
1182+
return <Passthrough {...this.props}/>;
1183+
}
1184+
}
1185+
1186+
const tree = TestUtils.renderIntoDocument(
1187+
<ProviderMock store={store}>
1188+
<Container />
1189+
</ProviderMock>
1190+
);
1191+
1192+
expect(childMapStateInvokes).toBe(2);
1193+
1194+
// The store state stays consistent when setState calls are batched
1195+
ReactDOM.unstable_batchedUpdates(() => {
1196+
store.dispatch({ type: 'APPEND', body: 'c'});
1197+
});
1198+
expect(childMapStateInvokes).toBe(3);
1199+
1200+
// setState calls DOM handlers are batched
1201+
const container = TestUtils.findRenderedComponentWithType(tree, Container);
1202+
const node = React.findDOMNode(container.getWrappedInstance().refs.button);
1203+
TestUtils.Simulate.click(node);
1204+
expect(childMapStateInvokes).toBe(4);
1205+
1206+
// In future all setState calls will be batched[1]. Uncomment when it
1207+
// happens. For now redux-batched-updates middleware can be used as
1208+
// workaround this.
1209+
//
1210+
// [1]: https://twitter.com/sebmarkbage/status/642366976824864768
1211+
//
1212+
// store.dispatch({ type: 'APPEND', body: 'd'});
1213+
// expect(childMapStateInvokes).toBe(5);
1214+
});
11501215
});
11511216
});

0 commit comments

Comments
 (0)