-
-
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
Feature request: wrap JSX without any rendering #1444
Comments
Related to #692 (comment). Here are the use cases as I understand them:
And what is wanted is "pretend this element is what an SFC shallow-renders"? An alternative workaround right now is |
That is a good workaround, I'll have to try it. I'd say the primary request is to be able to use Enzyme wrapper functions without modifying the input - sort of like a shallow render that's so shallow it has zero depth (i.e. doesn't render at all). The implementation details are less of a concern to me, but the current workarounds of adding an artificial level to prevent shallow rendering from going any deeper seem slightly wasteful. |
Just to clarify, just doing
|
@Heedster ah, yes, good point |
@dallonf it does seem like you could do this to cover your use case: const RenderProp = route.prop('render');
const innerWrapper = shallow(<RenderProp match={{ __testMatch: true }} />); Of the three cases above, this seems somewhat straightforward for the second. I'm trying to weigh a few options:
In all cases, I'm not sure what to name it, nor am I sure that any of these are an ideal option. |
The example you gave works in most scenarios, but I've seen some RPCs take a single argument as input instead of a props object, so you couldn't use the functions directly as components. In those cases, Of the options you mentioned:
This is more or less how I treat my workaround function (which can be implemented as either
That could work as well! Although I'm also unsure what you might name it.
My gut feeling is that this would be a bit too narrow of a solution. I've had use cases that wouldn't fit this solution well, like a However, a slight variation on this idea where the children could be rendered in-place within the existing wrapper could be very helpful for the specific RPC scenario... let me doodle up some psuedocode to describe this: const MyPage = ({ title }) => (
<Route>
{({ match }) => (
<div>
<h1>{title}</h1>
<p>{match.url}</p>
</div>
)}
</Route>
);
it('renders', () => {
const wrapper = shallow(<MyComponent title="Hello world" />);
// this is the new function
wrapper.replaceChildren(
// A selector for the element I want to replace
Route,
// a callback function for what to replace it with, first argument is the found element
route => route.prop('children')({ match: { url: 'zombo.com' } })
);
//
expect(wrapper).toMatchSnapshot();
// `Route`'s un-snapshottable children have been replaced with something more representative
// of how a developer thinks about render props
/*
<Route>
<div>
<h1>Hello world<</h1>
<p>zombo.com</p>
</div>
</Route>
*/
}); This idea is very rough but hopefully I communicated the use case ok! |
Sometimes I find that I need to run assertions on raw
ReactElement
s, without needing to shallow render or mount. This is especially common with the hip new "render prop" pattern. Here's an example based on React Router:Note that if I called
shallow(innerJsx)
, it would be treated as a shallow render ofObnoxiousHighlight
, which isn't what I'm interested in testing in this suite.As a workaround, I've been using this dirty function:
But it would be nice to have it as part of Enzyme.
The text was updated successfully, but these errors were encountered: