chai-enzyme icon indicating copy to clipboard operation
chai-enzyme copied to clipboard

The example code for present() is misleading

Open junpos opened this issue 8 years ago • 5 comments

.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

junpos avatar Jun 03 '16 21:06 junpos

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.

ljharb avatar Jun 03 '16 21:06 ljharb

Nice to know the right matcher!

junpos avatar Jun 03 '16 21:06 junpos

We should probably document this behaviour to avoid any further confusion, any chance you can open a PR @junpos?

ayrton avatar Jun 11 '16 07:06 ayrton

@ayrton sorry for my late reply. sure I'll open it soon

junpos avatar Jun 23 '16 22:06 junpos

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.

droarty avatar Jul 21 '16 17:07 droarty