toExist doesn't work as I'd expect #64

Closed
philipwalton opened this Issue May 18, 2012 · 8 comments

Comments

Projects
None yet
5 participants

The toExist matcher simply checks whether or not the jQuery element has a size greater than 0.

toExist: function() {
  return this.actual.size() > 0;
}

This is fine for most cases, but if you already have a reference to a jQuery object and you want to test that some function properly removed that object from the DOM, toExist won't work.

Here's an example:

var el = $('#some-element');
el.remove()
expect(el).not.toExist() // incorrectly fails

Perhaps my expectation isn't the intended purpose of toExist, but it seems to me toExist should test whether or not an element exists in the DOM. (Or, I suppose, a new matcher could be created to test existence in the DOM.)

It could look something like this:

toExistInDOM: function() {
  return $(document).find(this.actual).size() > 0;
}

Cool, thanks!

wnodom commented Sep 9, 2013

How would I use the updated toExist matcher to test for elements that aren't in the DOM? In its original form, it was possible to do the following (common in unit testing of AngularJS directives, for example):

expect( elem.find("div[ng-show=readonly]") ).toExist();

This no longer works, and I'm not sure how to correct the problem without reverting to the old version of the matcher.

@wnodom same for me - I opened a new issue here: #148

Collaborator

travisjeffery commented Sep 26, 2013

@philipwalton i'm undoing this. if you want to check something was removed from the dom, then do something like:

expect($(document)).not.toContain($el)

wnodom commented Sep 28, 2013

@travisjeffery Thanks! Now I can remove the old version of the matcher from my own code. :-)

@wnodom wnodom referenced this issue in bradphelan/jasminerice Sep 30, 2013

Merged

Fix jasmine-jquery toExist() #74

I just came across this problem as well and I agree with @philipwalton that .toExist() should check if the element exists in the DOM and @wnodom should use expect(elem).toContain('div[ng-show=readonly]') for elements that that don't exist in the DOM.

Collaborator

travisjeffery commented Jan 9, 2014

use toBeInDom(). api's not changing.

@travisjeffery OK great, thanks! Hadn't noticed that method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment