Add renderProp to ShallowWrapper#1863
Add renderProp to ShallowWrapper#1863ljharb merged 3 commits intoenzymejs:masterfrom dferber90:master
Conversation
This comment has been minimized.
This comment has been minimized.
ljharb
left a comment
There was a problem hiding this comment.
Thanks!
Let's add this same API to mount, for consistency.
| * @returns {ShallowWrapper} | ||
| */ | ||
| renderProp(propName, ...args) { | ||
| const Wrapper = props => props.children; |
| throw new Error(`no prop called “${propName}“ found`); | ||
| } | ||
| if (typeof prop !== 'function') { | ||
| throw new Error( |
There was a problem hiding this comment.
I changed it to a TypeError.
I'm curious why you'd consider this one a TypeError, but not the one above as both are problems of the component's props?
| } | ||
|
|
||
| const element = prop(...args); | ||
| const wrapped = getAdapter(this[OPTIONS]).createElement(Wrapper, null, element); |
There was a problem hiding this comment.
we may need to add a method to all the adapters "wrap", so that the adapter itself can handle the wrapper implementation.
There was a problem hiding this comment.
Do you want to do it in this PR or separately? Separately I assume, just double-checking.
There was a problem hiding this comment.
I think it'd need to be in this PR (in a separate commit, ofc).
[enzyme-adapter-utils] [new] add `wrapWithSimpleWrapper`
There was a problem hiding this comment.
I tried to add wrap to all adapters but I'm not familiar enough with the internals of enzyme to get the tests to pass. I still keep bumping into TypeError: inst.render is not a function on React 14 😞 Other versions of React seem to be passing the tests now though!
Let's add this same API to mount, for consistency.
I'm holding off adding renderProp to mount until the tests are passing in shallow. I haven't used mount myself yet either. I thought mount renders the whole tree, in which case the render prop would get executed automatically? I guess I need to play with mount a bit to understand why/how it would be useful there.
| throw new Error(`no prop called “${propName}“ found`); | ||
| } | ||
| if (typeof prop !== 'function') { | ||
| throw new Error( |
There was a problem hiding this comment.
I changed it to a TypeError.
I'm curious why you'd consider this one a TypeError, but not the one above as both are problems of the component's props?
The issue was |
|
I've updated and rebased the branch; I'll give it another review and then we can add the method for |
|
I'm still a bit puzzled by how The only useful case for having I attempted to add It would be nice if you could give me some direction or take over from here. |
|
It's so you can unit-test the render prop without needing to know all the permutations of props that lead to certain input combinations. |
ljharb
left a comment
There was a problem hiding this comment.
I went ahead and added mount support, as well as some unit tests for adapters' wrap method.
This looks good to go, thanks!
|
@ljharb Thanks for driving this home! I visited a friend this weekend so I didn't get to continue working on it. I really appreciate your help! We'll deprecate the |
|
The current API works like this This will call the prop This API is nice to work with but prevents us from ever adding something like an options object or any other param to It might be a better idea to use an API like So far this has not been a problem for us but I wanted to give a heads-up now that it's becoming official. I'm undecided of which API is suited best. |
|
@dferber90 those are really good points. The explicit array is cleaner, until you want no arguments and you want to pass options. The “return a function” approach is cleaner when you have arguments, but perhaps less so when you don’t. I think I’m slightly leaning towards the “returning a function” API. I’ll be releasing the adapters sooner than enzyme itself, so we have a bit of time to make the change, but not much :-) |
| ```jsx | ||
| const wrapper = mount(<App />) | ||
| .find(Mouse) | ||
| .renderProp('render', [10, 20]); |
There was a problem hiding this comment.
This is under "multiple arguments", so the code should be .renderProp('render', 10, 20)
| ```jsx | ||
| const wrapper = shallow(<App />) | ||
| .find(Mouse) | ||
| .renderProp('render', [10, 20]); |
There was a problem hiding this comment.
Same here, this should also be .renderProp('render', 10, 20).
| )(); | ||
|
|
||
| export default function wrap(element) { | ||
| console.log(React.version, Wrapper); |
There was a problem hiding this comment.
Is this a debugging-leftover? This should go away as far as I can tell.
- [new] `wrap`: add `wrap` to all adapters (#1863) - [deps] update `enzyme-adapter-utils`, `react-is`
Adds a
renderPropmethod toShallowWrappers to ease testing render props.This PR basically extracts
renderPropout of @commercetools/enzyme-extensions. We would deprecate and drop it there ifrenderPropmakes it intoenzymeitself.I wrote this article a while ago detailing how to use
renderPropand why.I'm curious for feedback of whether you'd want to accept it or not after some interest was shown in commercetools/enzyme-extensions#8.
Thanks to @tdeekens who helped set up and bounce ideas back&forth with for
renderPropinenzyme-extensions!Closes #1444. Related to #1856.