-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(factories): add autoGenerateKey option #2424
feat(factories): add autoGenerateKey option #2424
Conversation
Can be used to disable automatic key generation for shorthand props with string/number values when the factory is called (not when it is created).
Codecov Report
@@ Coverage Diff @@
## master #2424 +/- ##
==========================================
+ Coverage 99.74% 99.74% +<.01%
==========================================
Files 160 160
Lines 2771 2773 +2
==========================================
+ Hits 2764 2766 +2
Misses 7 7
Continue to review full report at Codecov.
|
test/specs/lib/factories-test.js
Outdated
@@ -395,6 +396,18 @@ describe('factories', () => { | |||
value: 'a string', | |||
mapValueToProps: () => ({ undef: undefined, nil: null, zero: 0, empty: '' }), | |||
}) | |||
|
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.
I wasn't completely sure if this was a good place to put the tests, but currently 'key' and 'childKey' (further up) are two separate sections that seem to deal exclusively with explicitly provided object/element props, so it didn't feel right putting it into one of those.
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.
Let's keep all tests for a given option under that describe()
block. Ideally, we only describe(key)
once.
generateKey
option to createShorthand()
generateKey
option to createShorthand()
src/lib/factories.js
Outdated
|
||
if (!_.isNil(childKey)) { | ||
// apply and consume the childKey | ||
props.key = typeof childKey === 'function' ? childKey(props) : childKey | ||
delete props.childKey | ||
} else if (valIsString || valIsNumber) { | ||
} else if (generateKey && (valIsString || valIsNumber)) { |
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.
Currently, setting generateKey: false
can still result in creating a key if childKey
is passed. I think this check can be hoisted up to line 83. If generateKey
is false, I would expect the factory to not perform any operation related to keys. What are your thoughts?
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.
This is how the check was made in the previous implementation (before it was removed). I guess the question is: is there value in being able to disable an explicitly specified childKey
in this way? I believe you can already achieve this by passing in overrideProps: { childKey: null }
. There's also the discrepancy with key
, which bypasses the conditional.
If the logic stays as is, the option could be renamed to autoGenerateKey
or allowAutoKey
(etc.) to signify the intent better. Alternatively the conditional logic could be rejigged so that passing overrideProps: { key: null }
achieves the same thing, giving it better behaviour parity with object/element values (and removing the need for the extra option altogether), but I think it would require us to differentiate between null
and undefined
(potentially brittle?).
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.
I see your point. The shortcoming of using overrideProps
is that it would override the user's key also, even when they explicitly define one. I do like autoGenerateKey
. This way, it can be set to false and users can still choose to pass a key or not and get intuitive results.
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.
The shortcoming of using
overrideProps
is that it would override the user's key also, even when they explicitly define one.
Ah I overlooked that somehow, however there's still defaultProps
which could work:
...
} else if (props.key === undefined && (valIsString || valIsNumber)) {
...
// At call time:
ItemHeader.create(header, { defaultProps: { key: null } })
The obvious downside is that the semantics of that are far less clear to the user compared to a for-purpose boolean flag (and it's a bit verbose).
I'm trying to modify the
I'm not very familiar with enzyme/chai-enzyme, so I'm not sure of the best way to fix this. After looking at the docs the best I can come up with is something like this: const wrapper = shallow(element)
wrapper.should[assertMethod](shorthandElement)
expect(wrapper.find(ShorthandComponent).key()).to.equal(shorthandElement.key) But there's an assumption here that const wrapperChild = shallow(element).find(ShorthandComponent)
expect(wrapperChild.equals(shorthandElement)).to.equal(true)
expect(wrapperChild.key()).to.equal(shorthandElement.key) But that still has the same 1 node assumption, and I don't know how to make it play nice with Help or guidance welcome. |
So I've implemented the flag for all the shorthand props that already have a test that uses
However there are bunch more that aren't specifically tested, at least not using
Edit: the others that can benefit:
I think that's all of them. |
Yes, I think it is best to update all usage of factories where we are creating a single element to use |
Fixed merge conflicts here. |
Thanks. I'm still gradually working on this, it's only taken so long because I'm trying to unify the testing for shorthand patterns that don't fit into the simple case (e.g. nested object keys, collection shorthands). It's mostly done, just need to find the time to do the last little bit. |
I've fixed the remaining components and merged master. I'm OK merging this now and doubling back for the test updates. |
generateKey
option to createShorthand()
Merged master and fixed conflicts again. Ready to merge if tests pass. |
Released in |
See #2418 for more details.
WIP, currently just includes the implementation and a couple of tests.