Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support subscribe predicates with all/watched props available #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function compose(dataLoader, options = {}) {

_shouldSubscribe(props) {
const firstRun = !this._cachedWatchingProps;
const nextProps = pick(props, propsToWatch);
const nextProps = propsToWatch ? pick(props, propsToWatch) : props;
const currentProps = this._cachedWatchingProps || {};
this._cachedWatchingProps = nextProps;

Expand Down
101 changes: 99 additions & 2 deletions src/tests/compose.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint react/prefer-stateless-function: 0, react/prop-types: 0 */
/* eslint no-unused-expressions: 0, react/prefer-stateless-function: 0, react/prop-types: 0 */

import React from 'react';
import { shallow, mount } from 'enzyme';
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('compose', () => {
expect(callCount).to.be.equal(1);
});

it('should not run if the watching props changed', () => {
it('should run if the watching props changed', () => {
const options = {
propsToWatch: ['name'],
};
Expand Down Expand Up @@ -241,6 +241,25 @@ describe('compose', () => {
el.instance()._subscribe({ name: 'arunoda', age: 30 });
expect(callCount).to.be.equal(2);
});

it('should watch all props', () => {
const options = {};

let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);

const el = mount(<Container name="arunoda" age={20} />);

// first run with same props
el.instance()._subscribe({ name: 'arunoda', age: 20 });
expect(callCount).to.be.equal(2);

// second run with changed props
el.instance()._subscribe({ name: 'arunoda', age: 20 });
expect(callCount).to.be.equal(3);
});
});
});

Expand All @@ -257,6 +276,84 @@ describe('compose', () => {
expect(el.html()).to.match(/arunoda/);
});

it('should run for all props', () => {
const options = {
shouldSubscribe: (props, nextProps) => {
const propKeys = Object.keys(props);
expect(propKeys).to.have.length(2).and.include.members(['name', 'age']);
const nextPropKeys = Object.keys(nextProps);
expect(nextPropKeys).to.have.length(2).and.include.members(['name', 'age']);

return props.name !== nextProps.name;
},
};

let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);

const el = mount(<Container name="arunoda" age="20" />);

el.instance()._subscribe({ name: 'arunoda', age: 20 });
expect(callCount).to.be.equal(1);

el.instance()._subscribe({ name: 'nacho', age: 28 });
expect(callCount).to.be.equal(2);
});

it('should run for watched props', () => {
const options = {
propsToWatch: ['name'],
shouldSubscribe: (props, nextProps) => {
const propKeys = Object.keys(props);
expect(propKeys).to.have.length(1).and.include('name');
const nextPropKeys = Object.keys(nextProps);
expect(nextPropKeys).to.have.length(1).and.include('name');

return props.name !== nextProps.name;
},
};

let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);

const el = mount(<Container name="arunoda" />);

el.instance()._subscribe({ name: 'arunoda' });
expect(callCount).to.be.equal(1);

el.instance()._subscribe({ name: 'nacho' });
expect(callCount).to.be.equal(2);
});

it('should not run for unwatched props', () => {
const options = {
propsToWatch: ['age'],
shouldSubscribe: (props, nextProps) => {
expect(props).to.be.empty;
expect(nextProps).to.be.empty;

return props.name !== nextProps.name;
},
};

let callCount = 0;
const Container = compose(() => {
callCount += 1;
}, options)(Comp);

const el = mount(<Container name="arunoda" />);

el.instance()._subscribe({ name: 'arunoda' });
expect(callCount).to.be.equal(1);

el.instance()._subscribe({ name: 'nacho' });
expect(callCount).to.be.equal(1);
});

it('should ignore propsToWatch', () => {
const options = {
shouldSubscribe: () => true,
Expand Down