React nodes passed as props should still be shallow components
See original GitHub issueWhen passing a React Node as a prop, I want to be able to test the node.
ie
<MyComponent
sidePanel={<SidePanel><Child onClick={() => testThis()}/></SidePanel>}
/>
Here I want to be able to test testThis is called.
Describe the solution you’d like
shallow(<Component>).props().sidePanel should still be a shallow component and have the normal api available.
ie this should work.
shallow(<Component>).props().sidePanel.find(Child).props().onClick();
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Shallow Renderer - React
Shallow rendering lets you render a component “one level deep” and assert facts about what its render method returns, without worrying about the...
Read more >Enzyme, How to access a component passed in props
Show activity on this post. I can access the FormControlLabel component like this: const wrapper = shallow(<MyComponent />); wrapper. find('[ ...
Read more >Testing with Jest and Enzyme in React — Part 4 (shallow vs ...
shallow method is used to render the single component that we are testing. · In Enzyme version less than 3, the shallow method...
Read more >Continuous integration for React applications using Jest and ...
fn(); const wrapper = shallow(<App {...props} />); // Call the function on the component instance, passing the mock event wrapper.instance().
Read more >Test React Component Props with Enzyme and Jest
With our component rendered and our assertions testing our nodes, lets test our component's props. We will use the shallow render methods .props...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@keyboard-clacker not at the moment, but you could do:
+1 to something like this. The suggestion from @ljharb above is great: something like
wrapper.wrapProp('sidePanel')
. I agree we shouldn’t just silently change that, because it’s also useful to get the props directly.I’m running into a similar issue where I’m testing the props (which are child components) are set correctly when passed from one component to another.
ComponentA creates a list of child elements and passes it to ComponentB (a reusable template component). We are not doing the normal
children
approach because there is more than one prop, and each one needs to be separately identified.For testing, I couldn’t just use the props directly and had to do a dummy wrapper:
I’d love a way not to need the extra call to
shallow
andfind
and get the specific element from a new wrapper on a dummy element so I can test its properties.I’d also be happy with a way to do a super shallow wrapper that goes 0 levels deep. Basically just turn a normal element into something that has enzyme functions.
Then something like this would work: