Enzyme: add support to specify max level to shallow/mount

Created on 20 Jul 2017  路  13Comments  路  Source: enzymejs/enzyme

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})

Most helpful comment

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(

{ shallow() }
);
`````

All 13 comments

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.

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.

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

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

thanks. is there an open issue?

duplicate of #250

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(

{ shallow() }
);
`````

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

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

?

@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 : )

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

@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();
  });

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

Was this page helpful?
0 / 5 - 0 ratings