Enzyme: Error if `hasClass` is called on the wrapper element

Created on 2 Nov 2016  路  6Comments  路  Source: enzymejs/enzyme

I just lost a few minutes because I had:

wrapper = mount(...)
expect(wrapper.hasClass('foo')).toEqual(true)

Where it of course should have been wrapper.find('button').hasClass(...). Would it be possible to catch when someone is calling hasClass on the wrong thing and throw an error? I'm happy to do the PR if you think this is worthwhile. No worries if not!

Potentially even just adding this as a gotcha to the hasClass docs might be enough (which again, I'm happy to do).

Most helpful comment

v2.7.0 just published.

@jackfranklin please reopen if that doesn't solve your issue.

All 6 comments

I've run into the same problem today and noticed that shallow vs mount behaves differently:

````javascript
wrapper = shallow(

bar
)
expect(wrapper.hasClass('foo')).toEqual(true) // ok

wrapper = mount(

bar
)
expect(wrapper.hasClass('foo')).toEqual(true) // not ok
expect(wrapper.find('div').hasClass('foo')).toEqual(true) // ok
````

I'm having the same issue as @simonvizzini

hasClass on a mounted wrapper doesn't always work.

Looks like this commit fixes my issue, since I was trying to check a class on a non DOM element, which was impossible with the current code in 2.6.0.

Any way we can get a new release soon @lelandrichardson @ljharb ?

v2.7.0 just published.

@jackfranklin please reopen if that doesn't solve your issue.

Looks perfect, thank you so much 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdamYahid picture AdamYahid  路  3Comments

ivanbtrujillo picture ivanbtrujillo  路  3Comments

heikkimu picture heikkimu  路  3Comments

amcmillan01 picture amcmillan01  路  3Comments

andrewhl picture andrewhl  路  3Comments