Enzyme: Less strict shallow.contains method

Created on 16 Feb 2017  路  2Comments  路  Source: enzymejs/enzyme

shallow.contains requires complete equality, giving this behaviour:

const wrapper = shallow(
  <div className="foo">
    <span>Hello!</span>
    <div className="bar"></div>
  </div>
);

wrapper.contains(<div className="foo"><span>Hello!</span><div className="bar"></div></div>) // true

wrapper.contains(<div><span>Hello!</span><div></div></div>) // false

wrapper.contains(<div><span></span><div></div></div>) // false

Having a way to perform looser checks would be good in order to check the structure of the elements without needing to know the classes and other attributes.

However, I would expect it to be false if the node we give to looseContains had attributes that the rendered node does not:

const wrapper = shallow(<div class="foo"></div>);

wrapper.looseContains(<div></div>); // true

wrapper.looseContains(<div class="foo bar"></div>); // false

wrapper.looseContains(<div class="bar"></div>); // false
question

Most helpful comment

Are you looking for containsMatchingElement?

All 2 comments

Are you looking for containsMatchingElement?

That's the one @ljharb! Thanks.

Was this page helpful?
0 / 5 - 0 ratings