Html code:
<div id="xxx"><div>aaa</div></div>
Js code:
var node = $("#xxx");
var found = node.find("div:visible");
console.log(found.length);
If these code are run on browser, it will output 1
. (live demo: http://jsbin.com/xuqule/2/edit)
But when I run it on jasmine test with phantomjs:
it("should find the visible nodes", function () {
var node = $("<div><div>aaa</div></div>");
var found = node.find("div:visible");
console.log(found);
expect(found.length).toEqual(1);
});
The test is failed, and the found.length
is 0
. Why and how to fix it?
You did not attach anything to the dom, it all lives in memory and that is why you'll get 0.
Try doing something like:
$('body').append(node);
var found = node.find("div:visible");
...
See bin: http://jsbin.com/kuyagijezono/1/edit