chai-enzyme
chai-enzyme copied to clipboard
The example code for present() is misleading
.find()
returns ShallowWrapper
which is alway present no matter what. -- http://airbnb.io/enzyme/docs/api/ShallowWrapper/find.html
expect(wrapper.find('.foo')).to.exist; // passes
expect(wrapper.find('.foo')).to.present(); // passes
Instead existence check should look like
expect(wrapper.find('.foo').length).to.equal(1); // fails
expect(wrapper.find('.foo').html()).to.exist; // fails
Both of those are built-in chai matchers, and since they return objects, that's correct. In other words, those are not the correct matchers to be using. You should use expect(wrapper.find('foo')).to.have.lengthOf(1)
or similar.
Nice to know the right matcher!
We should probably document this behaviour to avoid any further confusion, any chance you can open a PR @junpos?
@ayrton sorry for my late reply. sure I'll open it soon
So I am happy to edit the documentation but I still have a question about this issue. As far as I can tell from the code, .present
is an alias for .exist
which calls wrapper.isPresent()
. But to use the matcher you don't include the parenthesis in either case. So:
expect(wrapper.find(".my-class")).to.exist // Passes
expect(wrapper.find(".my-class")).to.be.present // Passes
expect(wrapper.find(".my-class")).to.exist() // Fails
expect(wrapper.find(".my-class")).to.be.present() // Fails
Calling them like methods gives the error:
undefined is not a constructor (evaluating 'expect(wrapper.find(".my-class")).to.be.present()')
So I assume the documentation should reflect that. If this makes sense, I can go ahead and make the PR.