-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}; | ||
new TestClass(props) | ||
.unhandledProps | ||
.should.eql(props); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 alet
? Is it solely because this variable is meant to be read only? Or another, more specific reason?😃 Just curious
There was a problem hiding this comment.
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:In this case, the var is initialized as
undefined
and then set totrue
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.