QUnit test function select
Hi everyone! Im going to try once again :)
Im working with Qunit for testing javascript functions in a web site. I did a couple of test but im not sure if im doing it right or wrong, I dont get errors but im suspecting that Im not really testing the function it self but just simulating functionalities in the test....
Can someone tell me if im right or wrong? Im so very new in QUnit that I dont know whwere should I start...
But if somene has the time to explain very shortly if Im on the right way would be so great and helpful!
Thank you very much to everyone!!!
here is my function:
- tsrSelect: function () {
return $(this).each(function() {
var $this = $(this),
span = $this.next("span.selectSpan").get(0),
selectedText = $this.children().filter(":selected").text();
if ($this.attr('readonly') || $this.find('option').length < 2) {
if (span) $(span).text(selectedText);
else $this.after($("<span>").text(selectedText).addClass("selectSpan"));
if ($this.hasClass("select2-hidden-accessible")) $this.select2('destroy');
$this.hide();
} else {
if (!$this.hasClass("select2-hidden-accessible")) {
if (span) $(span).remove();
$this.select2({
minimumResultsForSearch: 15,
width: '100%'
});
}
}
});
}
and my test:
- test('tsrSelect', function () {
var fixture = $('#qunit-fixture'),
Element = $('<select class="select2-hidden-accessible" id="Element" readonly><option value="1">One</option><option value="2">Two</option></select>');
ok($(Element).find('option'), "Expected tsrSelect has options");
actual = $(Element).find('option').first().text(), expected = 'One';
ok(actual == expected, 'Expected '+Element+' has a first text ' );
actual = $(Element).find('option').first().val(), expected = "1";
ok(actual == expected, "ok");
actual = $(Element).find('option').last().val(), expected = "2";
ok(actual == expected, "triple ok");
ok($(Element).text('One'), "Expected selectbox has options");
ok($(Element).children().filter(':selected'), 'Expected that the ' +Element+ ' Element has a default filter :selected');
ok($(Element).hasClass('select2-hidden-accessible'), "okay");
});