Skip to content

Commit

Permalink
Add failing test for reduxjs#118
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Sep 28, 2015
1 parent 607dc96 commit 65256f4
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,65 @@ describe('React', () => {
expect(target.props.statefulValue).toEqual(1);
});

it('calls mapState and mapDispatch for impure components', () => {
const store = createStore(() => ({
foo: 'foo',
bar: 'bar'
}));

const mapStateSpy = expect.createSpy();
const mapDispatchSpy = expect.createSpy().andReturn({});

class ImpureComponent extends Component {
render() {
return <Passthrough statefulValue={this.props.value} />;
}
}

const decorator = connect(
(state, {storeGetter}) => {
mapStateSpy();
return {value: state[storeGetter.storeKey]};
},
mapDispatchSpy,
null,
{ pure: false }
);
const Decorated = decorator(ImpureComponent);

class StatefulWrapper extends Component {
state = {
storeGetter: {storeKey: 'foo'}
};

render() {
return <Decorated storeGetter={this.state.storeGetter} />;
};
}

const tree = TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<StatefulWrapper />
</ProviderMock>
);

const target = TestUtils.findRenderedComponentWithType(tree, Passthrough);
const wrapper = TestUtils.findRenderedComponentWithType(tree, StatefulWrapper);

expect(mapStateSpy.calls.length).toBe(2);
expect(mapDispatchSpy.calls.length).toBe(2);
expect(target.props.statefulValue).toEqual('foo');

// Impure update
const storeGetter = wrapper.state.storeGetter;
storeGetter.storeKey = 'bar';
wrapper.setState({ storeGetter });

expect(mapStateSpy.calls.length).toBe(3);
expect(mapDispatchSpy.calls.length).toBe(3);
expect(target.props.statefulValue).toEqual('bar');
});

it('should pass state consistently to mapState', () => {
const store = createStore(stringBuilder);

Expand Down

0 comments on commit 65256f4

Please sign in to comment.