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

add getUnhandledProps util #85

Merged
merged 1 commit into from
Oct 23, 2015
Merged
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
16 changes: 16 additions & 0 deletions src/utils/getUnhandledProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import _ from 'lodash';

/**
* Returns an object consisting of props not defined in propTypes nor defaultProps.
* @param {*} instance The `this` keyword in a React Component class.
* @returns {{}} A shallow copy of the prop object
*/
const getUnhandledProps = instance => {
return _.omit(instance.props, (val, key) => {
const inPropTypes = _.has(instance.constructor.propTypes, key);
const inDefaultProps = _.has(instance.constructor.defaultProps, key);
return inPropTypes || inDefaultProps;
});
};

export default getUnhandledProps;
33 changes: 33 additions & 0 deletions test/utils/getUnhandledPops-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import _ from 'lodash';
import getUnhandledProps from 'src/utils/getUnhandledProps';

// Helper class that takes in props and merges defaultProps
class TestClass {
constructor(props) {
this.props = _.assign({}, this.constructor.defaultProps, props);
this.unhandledProps = getUnhandledProps(this);
}
}

describe.only('getUnhandledProps', () => {
it('removes props defined in defaultProps', () => {
TestClass.defaultProps = {imHandled: 'thanks'};
new TestClass()
.unhandledProps
.should.not.have.any.keys(_.keys(TestClass.defaultProps));
});
it('removes props defined in propTypes', () => {
TestClass.propTypes = {imHandled: 'thanks'};
new TestClass()
.unhandledProps
.should.not.have.any.keys(_.keys(TestClass.defaultProps));
});
it('leave props not in defaultProps || propTypes in tact', () => {
TestClass.defaultProps = {imHandled: 'thanks'};
TestClass.propTypes = {alsoHandled: 'got it'};
const props = {thisShould: 'still be here'};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick question here as to why you chose to make this variable a const as opposed to a let? Is it solely because this variable is meant to be read only? Or another, more specific reason?

😃 Just curious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! We've updated the linter. If you make a variable whose value never changes, it will tell you to make it a const instead. The only use case for a let now is for a variable whose value changes. Example:

let hasCertainChild;
React.Children.forEach(child => {
  if (someCondition(child) {
    hasSomeChild = true;
  }
})

In this case, the var is initialized as undefined and then set to true if the condition is met.

Going a bit further, a const wouldn't work here for both of those reasons. It cannot be initialized with an empty value nor can it's value change.

new TestClass(props)
.unhandledProps
.should.eql(props);
});
});