Background:
I come from a Java background, so I'm not too good at Javascript.
We plan to implement unit testing of JavaScript both on our existing (outdated) code and on future work. We are mainly a java store (Spring, Weblogic, etc.).
We are considering options that give us good integration with the IDE (IntelliJ idea) and sonar, as well as the ability to run them as part of continuous integration.
JsTestDriver marks all fields.
Question:
Most of our existing javascript code: a) is built into JSP and b) uses jQuery to interact directly with page elements.
How should we test a function that is highly dependent on the DOM. Here are some sample code I'm talking about:
function enableOccupationDetailsText (){
$( "#fldOccupation" ).val("Unknown");
$( "#fldOccupation_Details" ).attr("disabled", "");
$( "#fldOccupation_Details" ).val("");
$( "#fldOccupation_Details" ).focus();
}
or
jQuery(document).ready(function(){
var oTable = $('#policies').dataTable( {
"sDom" : 'frplitip',
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "xxxx.do",
"sPaginationType": "full_numbers",
"aaSorting": [[ 1, "asc" ]],
"oLanguage": {
"sProcessing": "Processing...",
"sLengthMenu": "Show _MENU_ policies",
"sZeroRecords": "No matching policies found",
"sInfo": "Showing _START_ to _END_ of _TOTAL_ policies",
"sInfoEmpty": "Showing 0 to 0 of 0 policies",
"sInfoFiltered": "(filtered from _MAX_ total policies)",
"sInfoPostFix": "",
"sSearch": "Search:",
"sUrl": "",
"oPaginate": {
"sFirst": "First",
"sPrevious": "Previous",
"sNext": "Next",
"sLast": "Last"
}
},
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
$('td:eq(0)', nRow).html( "<a href='/ole/policy/general_details.do?policy_id="+aData[0]+"'>"+aData[0]+"</a>" );
return nRow;
},
"fnServerData" : function ( url, data, callback, settings ) {
settings.jqXHR = $.ajax( {
"url": url,
"data": data,
"success": function (json) {
if (json.errorMessage != null) {
var an = settings.aanFeatures.r;
an[0].style.fontSize="18px";
an[0].style.backgroundColor="red";
an[0].style.height="70px";
an[0].innerHTML=json.errorMessage;
setTimeout('window.location="/xxxx"',1000);
} else {
$(settings.oInstance).trigger('xhr', settings);
callback( json );
}
},
"dataType": "json",
"cache": false,
"error": function (xhr, error, thrown) {
if ( error == "parsererror" ) {
alert( "Unexpected error, please contact system administrator. Press OK to be redirected to home page." );
window.location="/xxxx";
}
}
} );
}
} );
$("#policies_filter :text").attr('id', 'fldKeywordSearch');
$("#policies_length :input").attr('id', 'fldNumberOfRows');
$("body").find("span > span").css("border","3px solid red");
oTable.fnSetFilteringDelay(500);
oTable.fnSearchHighlighting();
$("#fldKeywordSearch").focus();
}
);
, (), . DOM, jQuery, datatables, ajax .. , Java-, .
, !