-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 renderProp to ShallowWrapper #1863
Conversation
This comment has been minimized.
This comment has been minimized.
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.
Thanks!
Let's add this same API to mount
, for consistency.
* @returns {ShallowWrapper} | ||
*/ | ||
renderProp(propName, ...args) { | ||
const Wrapper = props => props.children; |
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 won't work in React 13.
throw new Error(`no prop called “${propName}“ found`); | ||
} | ||
if (typeof prop !== 'function') { | ||
throw new Error( |
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 should be a TypeError
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to do it in this PR or separately? Separately I assume, just double-checking.
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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 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 :-) |
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.
```jsx | ||
const wrapper = mount(<App />) | ||
.find(Mouse) | ||
.renderProp('render', [10, 20]); |
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a debugging-leftover? This should go away as far as I can tell.
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.
whoops, yes, that's my bad.
- [new] `wrap`: add `wrap` to all adapters (#1863) - [deps] update `enzyme-adapter-utils`, `react-is`
Adds a
renderProp
method toShallowWrapper
s to ease testing render props.This PR basically extracts
renderProp
out of @commercetools/enzyme-extensions. We would deprecate and drop it there ifrenderProp
makes it intoenzyme
itself.I wrote this article a while ago detailing how to use
renderProp
and 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
renderProp
inenzyme-extensions
!Closes #1444. Related to #1856.