question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React nodes passed as props should still be shallow components

See original GitHub issue

When 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

29reactions
ljharbcommented, Aug 15, 2019

@keyboard-clacker not at the moment, but you could do:

const wrapper = shallow(<Component />);
const Footer = () => wrapper.find(Modal).prop('footer');
const footer = shallow(<Footer />);
expect(footer.prop('disabled').toBe(null);
1reaction
aarowmancommented, Jul 27, 2020

+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:

const wrapper = shallow(<MyComponent/>);
const links = wrapper.props().links;

// need to use a new wrapper... and add an extra dummy div here so enzyme doesn't expand it, 
//   then use .find() to get the element itself
const smallerWrapper = shallow(<div>{links[0]}</div>);
const link1forTesting = smallerWrapper.find(Link);

// ... do testing on link1forTesting with enzyme functions

I’d love a way not to need the extra call to shallow and find 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:

const link1forTesting = shallow(links[0], 0); // 0 levels deep
// or
const link1forTesting = superShallow(links[0]);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found