Skip to content

Commit dddb2e8

Browse files
committed
feat(createPubSubConnector): Added to supported options 'ownProps', if false component props will no
Default to true.
1 parent 3541e47 commit dddb2e8

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/components/createPubSubConnector.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function createPubSubConnector(mapSubscriptionsToProps, mapPublis
3232
wrapPublishMethods(mapPublishToProps) :
3333
mapPublishToProps || defaultMapPublishToProps;
3434
const shouldUpdatePublishProps = finalMapPublishToProps.length > 1;
35-
const { withRef = false } = options || {};
35+
const { withRef = false, ownProps = true } = options || {};
3636

3737
let mappedSubscriptions = {};
3838
function registerMappedSubscriptions(pubSub, subscriptionsMap = {}, updateSubscriptionProps, getProps) {
@@ -63,7 +63,9 @@ export default function createPubSubConnector(mapSubscriptionsToProps, mapPublis
6363
if (typeof transformerOrAlias === 'function') {
6464
callback = (...args) => {
6565
// transform values
66-
const newValues = transformerOrAlias(...args, getProps());
66+
const newValues = ownProps ?
67+
transformerOrAlias(...args, getProps())
68+
: transformerOrAlias(...args);
6769

6870
if (!isPlainObject(newValues)) {
6971
throw new Error(

test/components/createPubSubConnector.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,48 @@ test('should return the instance of the wrapped component for use in calling chi
688688

689689
t.end();
690690
});
691+
692+
test(
693+
'should not pass component props to action callback if option `ownProps`'
694+
+ ' is set to false',
695+
t => {
696+
const SIMPLE_UPDATE = 'simpleUpdate';
697+
const pubSubCore = createPubSub();
698+
const register = pubSubCore.register;
699+
let pubSub;
700+
pubSubCore.register = (...args) => {
701+
pubSub = register(...args);
702+
return pubSub;
703+
};
704+
705+
class Container extends Component {
706+
render() {
707+
return (<Passthrough {...this.props} />);
708+
}
709+
}
710+
711+
const mapSubscriptionsToProps = {
712+
[SIMPLE_UPDATE]: (country, ...rest) => {
713+
return { country, other: rest };
714+
},
715+
};
716+
const WrapperContainer = createPubSubConnector(
717+
mapSubscriptionsToProps,
718+
null,
719+
{ ownProps: false }
720+
)(Container);
721+
722+
const tree = TestUtils.renderIntoDocument(
723+
<ProviderMock pubSubCore={pubSubCore}>
724+
<WrapperContainer name="john" color="red" />
725+
</ProviderMock>
726+
);
727+
const stub = TestUtils.findRenderedComponentWithType(tree, Passthrough);
728+
729+
pubSub.publish(SIMPLE_UPDATE, 'Italy');
730+
t.is(stub.props.country, 'Italy');
731+
t.notOk(stub.props.other.length);
732+
733+
t.end();
734+
}
735+
);

0 commit comments

Comments
 (0)