Skip to content
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 support to specify max level to shallow/mount #1042

Closed
dalimian opened this issue Jul 20, 2017 · 14 comments
Closed

add support to specify max level to shallow/mount #1042

dalimian opened this issue Jul 20, 2017 · 14 comments

Comments

@dalimian
Copy link

shallow is one level deep vs mount is unlimited level deep, I wanted something in between - to be able to specify max level
can we add support to pass options.level to shallow/mount - e.g.
shallow(<MyCompoent />, {level: 2})

@ljharb
Copy link
Member

ljharb commented Jul 20, 2017

That's not something that's possible with shallow or mount.

It will be possible after #1007; but it wouldn't be with a number level (which is brittle), it'd be by defining certain nodes as additional "leaves" that should not be dived into.

@ljharb ljharb closed this as completed Jul 20, 2017
@dalimian
Copy link
Author

thanks, take your word, though curious as to why it wouldn't be possible now with shallow.

Agree taking a black list of nodes is more robust than taking a number.

@dalimian
Copy link
Author

dalimian commented Jul 20, 2017

and is there a plan/open issue to do it, or should we create one and submit a PL

@ljharb
Copy link
Member

ljharb commented Jul 20, 2017

shallow calls into react-test-renderer's shallow method, which doesn't allow for changing leaf nodes.

@dalimian
Copy link
Author

thanks. is there an open issue?

@ljharb
Copy link
Member

ljharb commented Jul 21, 2017

duplicate of #250

@ljharb ljharb marked this as a duplicate of #250 Jul 21, 2017
@lfelipequiros
Copy link

lfelipequiros commented Feb 23, 2018

I know is useless for components inside the wrapper component, but for cases like this one may be useful for someone to shallow inside a shallow.

shallow(
      <WrapperComponent /'>
        { shallow(<Component {...props}/>) }
      </WrapperComponent >);
);

@ljharb
Copy link
Member

ljharb commented Feb 23, 2018

@lfelipequiros i'm relatively sure that wouldn't work; but you might want:

shallow(
      <WrapperComponent >
        <Component {...props} />
      </WrapperComponent>;
);

?

@lfelipequiros
Copy link

@ljharb Why are you relatively sure ? maybe im missing something, but im seeing it working fine . . .
Checking the coverage it is actually doing what i expect, and doesn´t even throw a warning or something like that.

Now that im giving it a closer look maybe is just parsing that shallow object to a unused param inside the wrapper component.

I'll run a couple of test, if i can provide more context on this, i'll come back : )

@ljharb
Copy link
Member

ljharb commented Feb 24, 2018

An enzyme wrapper isn’t a node; React is probably converting it to a string implicitly.

@HamidTanhaei
Copy link

@lfelipequiros @ljharb
using shallow() inside a component totally works fine.

it('should render Redirect on userToken doe`s not exists', () => {
    const wrapper = shallow(<Router>{shallow(<PrivateRoute />)}</Router>);
    expect(shallow(wrapper.find(Route).props().render())).toMatchSnapshot();
  });

@ljharb
Copy link
Member

ljharb commented May 15, 2019

@HamidTanhaei that would fail if the children propType was defined as PropTypes.node.

@udayrajMT
Copy link

Just putting a link to docs for dive() https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/dive.html

@KeitelDOG
Copy link

The same problem still exists, and since member @ljharb talk about an option to put a list of leaves nodes to avoid diving, I realized the best way for me was to:

  • Use mount()
  • Mock the biggest components that will mount too many other components.

For example, let's say I'm testing a Post page, then I mount it, and before doing this I mock components like: Comments, Similar Contents, Analytics, and keep the focus on Post information like the title, contents and interactions, etc. As to mock with jest, I use variable starting with mock so that it can be passed from outside (jest mock external variable constraint) :

import { Text as MockText } from 'react-native';

jest.mock('../../../../src/views/comments', () => {
  return function Comments(props) {
    return <MockText>Comments Here</MockText>;
  };
});

There you escape the tons of components, you still benefit from mount() and useEffect(), and you can dive() in it if you want console.log(wrapper.find('Comments').dive().debug()); and you'll reach the text inside if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants