开发者

How to write unit test to assert the value of a jQuery element

开发者 https://www.devze.com 2022-12-19 23:54 出处:网络
I am using jQuery 1.4.1 and here is a simple code. buildCol1: function() { var col = $(\'<select />\', {className: \'col1\' }).append($(\'<option />\'));

I am using jQuery 1.4.1 and here is a simple code.

buildCol1: function() {
  var col = $('<select />', {className: 'col1' }).append($('<option />'));
  return $('<td />').append(col);
},

I am using qunit . In this case the method call is returning a jquery element. Since I am writing a test I should have something like

s = "<td><select class='col1'><option></option></select></td>"

I am not sure how do I get the fully expanded text value from a j开发者_如何学JAVAquery element. I am sure I can get that by fully treaversing all the children. But I am hoping that there is an easier way to unit test this method.

Update:

After I posted the question I got an idea that on the returned element I can check for class name and for the right count of children. Basically this will be like inspecting the returned td to have right values.


So, you want to get the plain HTML source of a jQuery selection? The solution is to put it into a containing element and get that element's inner HTML:

var $myEl = $('<select />', {className: 'col1' }).append($('<option />'));
var src = $("<div></div>").append($myEl).html();
0

精彩评论

暂无评论...
验证码 换一张
取 消